<?php

session_start();


require_once ("connection_sql.php");

header('Content-Type: text/xml');

date_default_timezone_set('Asia/Colombo');

if ($_GET["Command"] == "new_inv") {
    if ($_SESSION["CURRENT_USER"] == "") {
    echo "Please Loging Again !!!";
    exit();
}

if ($_SESSION["company"] == "") {
    echo "Invalid User Session";
    exit();
}

    $sql = "select LABOUR from invpara";
    $result = $conn->query($sql);
    $row = $result->fetch();
    $sql = "SELECT ID FROM s_labour ORDER BY ID DESC LIMIT 1";
    $result = $conn->query($sql);

    $lastID = 0; // default if no records found
    if ($row = $result->fetch()) {
        $lastID = $row["ID"];
    }

     $tmpinvno = "0000" . $lastID + 1; ;
        $lenth = strlen($tmpinvno);
        //  . "/" . $_GET["salesrep"]
        $invno = trim("L/") . substr($tmpinvno, $lenth - 3);


    $ResponseXML = "";
    $ResponseXML .= "<salesdetails>";
    $ResponseXML .= "<invno><![CDATA[" . $invno . "]]></invno>";
    $ResponseXML .= "<sdate><![CDATA[" . date("Y-m-d") . "]]></sdate>";
    $ResponseXML .= "</salesdetails>";
    echo $ResponseXML;
}

if ($_GET["Command"] == "pass_quot") {

    $_SESSION["custno"] = $_GET['custno'];

    header('Content-Type: text/xml');
    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";

    $ResponseXML = "";
    $ResponseXML .= "<salesdetails>";

    $cuscode = $_GET["custno"];

    $sql = "Select * from s_labour where   CODE ='" . $cuscode . "'";
    $result = $conn->query($sql);

    if ($rowM = $result->fetch()) {
        $ResponseXML .= "<id><![CDATA[" . json_encode($rowM) .  "]]></id>";
    }



   $ResponseXML .= "</salesdetails>";
   echo $ResponseXML;
}

if ($_GET["Command"] == "save_inv") {
    if ($_SESSION["CURRENT_USER"] == "") {
    echo "Please Loging Again !!!";
    exit();
}

if ($_SESSION["company"] == "") {
    echo "Invalid User Session";
    exit();
}

    try {
         $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $conn->beginTransaction();

        $sql = "Select * from s_labour where   CODE ='" . $_GET["refno"] . "'";
        $result= $conn->query($sql);
        if ($row = $result->fetch()) {
            $conn->rollBack();
            exit(" No Already Exist !!!");
        }

        $sqlisalma_q = "Select * from s_labour where   DESCRIPTION ='" . $_GET["refno"] . "'";
        $resultsalma_q = $conn->query($sqlisalma_q);
        if ($rowsalma_q = $resultsalma_q->fetch()) {
            $conn->rollBack();
            exit(" Name Already Exist !!!");
        }

        $sql = "select LABOUR from invpara";
    $result = $conn->query($sql);
    $row = $result->fetch();
//
//        $tmpinvno = "000" . $row["LABOUR"] ;
//        $lenth = strlen($tmpinvno);
//        //  . "/" . $_GET["salesrep"]
//        $refno = trim("L/") . substr($tmpinvno, $lenth - 3);


        $sql = "INSERT INTO s_labour (CODE, DESCRIPTION, SDATE, SCODE, ACT, type) VALUES 
        ('" . $_GET['refno'] . "', 
         '" . $_GET['name'] . "', 
         '" . $_GET['sdate'] . "', 
         '" . $_GET['company'] . "', 
         '1', 
         '" . $_GET['type'] . "')";

        $result = $conn->query($sql);



        $sql = "update invpara set LABOUR = LABOUR +1";
        $res_inv = $conn->query($sql);

        $sql2s = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values 
         ('" . $_GET["refno"] . "', '" . $_SESSION["CURRENT_USER"] . "', 'Labour', 'Save', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";
        $results2s = $conn->query($sql2s);

        echo "Saved";
        $conn->commit();
    } catch (Exception $e) {
        $conn->rollBack();
        echo $e;
    }
}



if ($_GET['Command']=="update_inv")
{
    if ($_SESSION["CURRENT_USER"] == "") {
    echo "Please Loging Again !!!";
    exit();
}

if ($_SESSION["company"] == "") {
    echo "Invalid User Session";
    exit();
}
  try {
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $conn->beginTransaction();
    $sql = "select * from s_labour where CODE ='" . $_GET['refno'] . "' ";
    $result = $conn->query($sql);
      if ($row = $result->fetch()) {

        $sql = "update s_labour set DESCRIPTION ='" . $_GET['name'] . "',type ='" . $_GET['type'] . "',SCODE ='" . $_GET['company'] . "'  where   CODE ='" . $_GET['refno'] . "' ";
        $result = $conn->query($sql);

         $sql2s = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values 
         ('" . $_GET["refno"] . "', '" . $_SESSION["CURRENT_USER"] . "', 'Labour', 'Update', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";
        $results2s = $conn->query($sql2s);

        echo "Updated";
    } else{
        echo "No Result Found...";
    }

     $conn->commit();

  } catch (Exception $e) {
      $conn->rollBack();
      echo $e;
  }
}


if ($_GET['Command']=="cancel_inv")
{
    if ($_SESSION["CURRENT_USER"] == "") {
    echo "Please Loging Again !!!";
    exit();
}

if ($_SESSION["company"] == "") {
    echo "Invalid User Session";
    exit();
}
  try {
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $conn->beginTransaction();
    $sql = "select * from s_labour where CODE ='" . $_GET['refno'] . "' ";
    $result = $conn->query($sql);
      if ($row = $result->fetch()) {

        $sql = "update s_labour set cancel ='1'  where   CODE ='" . $_GET['refno'] . "' ";
        $result = $conn->query($sql);

         $sql2s = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values 
         ('" . $_GET["refno"] . "', '" . $_SESSION["CURRENT_USER"] . "', 'Labour', 'Cancel', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";
        $results2s = $conn->query($sql2s);

        echo "Canceled";
    } else{
        echo "No Result Found...";
    }

     $conn->commit();

  } catch (Exception $e) {
      $conn->rollBack();
      echo $e;
  }
}
?>