<?php

session_start();


require_once ("connection_sql.php");

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

date_default_timezone_set('Asia/Colombo');
 
if ($_GET["Command"] == "new_inv") {

    $sql = "select LABOUR from invpara";
    $result = $conn->query($sql);
    $row = $result->fetch();
   
     $tmpinvno = "000" . $row["LABOUR"] ;
        $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") {

    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)values 
             ('" . $refno . "','" . $_GET['name'] . "', '" . $_GET['sdate'] . "', '" . $_GET['company'] . "','1') ";
            $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")
{
  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'] . "',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")
{
  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;
  }
}
?>