<?php

session_start();


require_once ("connection_sql.php");

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

date_default_timezone_set('Asia/Colombo');

if ($_GET["Command"] == "new_inv") {
    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";

    $ResponseXML = "";
    $ResponseXML .= "<new>";
    
    if ($_SESSION['company'] == "") { 
        exit("Please Login");
    }
    if ($_SESSION['CURRENT_USER'] == "") { 
        exit("Please Login");
    }
    
    $sql = "select HANDOVER from invpara where flag='".$_SESSION['company']."'"; 
    $result = $conn->query($sql);
    $row = $result->fetch();
    $tmpinvno = "000000" . $row["HANDOVER"] ;
    $lenth = strlen($tmpinvno);
    $refno = trim("".$_SESSION['company']."/CHO/") . substr($tmpinvno, $lenth - 6);
    
    $uniq = uniqid();
    $ResponseXML .= "<refno><![CDATA[$refno]]></refno>";
    $ResponseXML .= "<uniq><![CDATA[$uniq]]></uniq>";
    $ResponseXML .= "<sdate><![CDATA[" . date('Y-m-d') . "]]></sdate>";

    $ResponseXML .= "</new>";

    echo $ResponseXML;
}

if ($_POST["Command"] == "save_inv") {

    try {
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $conn->beginTransaction();
        
        if ($_SESSION['company'] == "") { 
            exit("Please Login");
        }
        if ($_SESSION['CURRENT_USER'] == "") { 
            exit("Please Login");
        }
        
        
        $sql = "SELECT * from cash_book_summery where  trn_date='".$_POST['sdate']."'  ";
        $result = $conn->query($sql);
        if ($row = $result->fetch()) {
            exit("Cash Book Updated.Can't Save...!!!");
        }
        
        $sql = "SELECT * from s_cash_handover where  REFNO='".$_POST['refno']."'  and CANCELL='0' and  COMPANY='".$_SESSION['company']."' ";
        $result = $conn->query($sql);
        if ($row = $result->fetch()) {
            exit("Already Saved Ref No...!!!");
        }
        
        $sql = "SELECT * from s_cash_handover where  REFNO='".$_POST['refno']."' and CANCELL='1' and  COMPANY='".$_SESSION['company']."' ";
        $result = $conn->query($sql);
        if ($row = $result->fetch()) {
            exit("Already Cancel Ref No...!!!");
        }
        
        $sql = "select HANDOVER from invpara where flag='".$_SESSION['company']."'"; 
        $result = $conn->query($sql);
        $row = $result->fetch();
        $tmpinvno = "000000" . $row["HANDOVER"] ;
        $lenth = strlen($tmpinvno);
        $refno = trim("".$_SESSION['company']."/CHO/") . substr($tmpinvno, $lenth - 6);

        $sql1 = "insert into s_cash_handover(REFNO,SDATE,TMP_NO,AMOUNT,COMPANY,DATETIME,USER,REMARK) values
        ('" .$refno . "', '" . $_POST['sdate'] . "', '" . $_POST['uniq'] . "', '" . $_POST['amount'] . "','".$_SESSION['company']."', '" . date("Y-m-d H:i:s") . "', '" . $_SESSION["CURRENT_USER"] . "', '" . $_POST['remark'] . "')"; 
        $result1 = $conn->query($sql1);
        
        $sql1 = "update invpara set HANDOVER=HANDOVER+1 where flag='".$_SESSION['company']."'"; 
        $result1 = $conn->query($sql1);
        
        $sqllog = "insert into entry_log(refno, username, docname, trnType, stime, sdate,company) values 
        ('" . $refno . "', '" . $_SESSION["CURRENT_USER"] . "', 'HAND OVER', 'Save', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "','".$_POST['department']."')";
        $resultlog = $conn->query($sqllog);
        
        
       echo "Saved";
       
       $conn->commit();
    } catch (Exception $e) {
        $conn->rollBack();
        echo $e;
    }
}

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

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

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

    $cuscode = $_GET["custno"];

    $sql = "Select * from s_cash_handover where  REFNO ='" . $cuscode . "' and  COMPANY='".$_SESSION['company']."' ";
    $result = $conn->query($sql);

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

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

if ($_POST["Command"] == "cancel_inv") {


    try {
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $conn->beginTransaction();
        
        if ($_SESSION['company'] == "") { 
            exit("Please Login");
        }
        if ($_SESSION['CURRENT_USER'] == "") { 
            exit("Please Login");
        }
        
        $sql = "SELECT * from s_cash_handover where  REFNO='".$_POST['refno']."' and CANCELL='1' and  COMPANY='".$_SESSION['company']."' ";
        $result = $conn->query($sql);
        if ($row = $result->fetch()) {
            exit("Already Cancel Ref No...!!!");
        }

        $sql = "update s_cash_handover set CANCELL = '1' where  REFNO='".$_POST['refno']."' and  COMPANY='".$_SESSION['company']."'";
        $result = $conn->query($sql);
        
        $sqllog = "insert into entry_log(refno, username, docname, trnType, stime, sdate,company) values
        ('" . trim($_POST['refno']) . "', '" . $_SESSION["CURRENT_USER"] . "', 'HAND OVER', 'CANCEL', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "','".$row['department']."')";
        $resultlog = $conn->query($sqllog);
        
        echo "Cancel";

        $conn->commit();

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

