<?php

session_start();


require_once ("connection_sql.php");

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

date_default_timezone_set('Asia/Colombo');

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


    $ResponseXML = "";
    $ResponseXML .= "<new>";
         
    if ($_SESSION['company'] == "") { 
        exit("Please Login");
    }
    if ($_SESSION['CURRENT_USER'] == "") { 
        exit("Please Login");
    }

    $sql = "SELECT EXPENSES FROM invpara where flag='".$_SESSION['company']."' ";
    $result = $conn->query($sql);
    $row = $result->fetch();
    $no = $row['EXPENSES'];
    $tmpinvno = "00000" . $no;
    $lenth = strlen($tmpinvno);
     
    $refno = trim("".$_SESSION['company']."/E/ ") . substr($tmpinvno, $lenth - 6);
    $uniq = uniqid();   
     

    $ResponseXML .= "<id><![CDATA[$refno]]></id>";
    $ResponseXML .= "<uniq><![CDATA[$uniq]]></uniq>";
    $ResponseXML .= "<sdate><![CDATA[".date("Y-m-d")."]]></sdate>"; 
    $ResponseXML .= "</new>";

    echo $ResponseXML;
}

if ($_GET["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 s_expenses where  REFNO='".$_GET["refno"]."' and CANCELL='0'"; 
        $result = $conn->query($sql);
        if ($row = $result->fetch()) {
            exit("Already Saved Item..!!!");
        } 

        $sql = "SELECT * from s_expenses where  REFNO='".$_GET["refno"]."' and CANCELL='1'"; 
        $result = $conn->query($sql);
        if ($row = $result->fetch()) {
            exit("Already Canceled Item..!!!");
        }
        
        $sql = "SELECT EXPENSES FROM invpara where flag='".$_SESSION['company']."' ";
        $result = $conn->query($sql);
        $row = $result->fetch();
        $no = $row['EXPENSES'];
        $tmpinvno = "00000" . $no;
        $lenth = strlen($tmpinvno);
        $refno = trim("".$_SESSION['company']."/E/ ") . substr($tmpinvno, $lenth - 6);

        $sql = "Insert into s_expenses (REFNO,TMP_NO,SDATE, TYPE,DEPARTMENT,EMPLOYEE, AMOUNT, REMARK, USER, USERDTIME, COMPANY) values 
            ('" . $refno . "','" . $_GET["uniq"] . "','" . date("Y-m-d") . "', '" . $_GET["type"] . "', '" . $_GET["department"] . "', '" . $_GET["employee"] . "', '" . $_GET["amount"] . "', '" . $_GET["remark"] . "', '" . $_SESSION["CURRENT_USER"] . "', '" . date("Y-m-d H:i:s") . "', '".$_SESSION['company']."')";
        $result = $conn->query($sql);

        $sql2 = "update invpara set EXPENSES=EXPENSES+1 where flag='".$_SESSION['company']."'";
        $result2 = $conn->query($sql2);

        $sql = "insert into entry_log(refno, username, docname, trnType, stime, sdate,company) values 
        ('" . $refno . "', '" . $_SESSION["CURRENT_USER"] . "', 'Expeses', 'Save' , '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "','".$_SESSION['company']."')";
        $result = $conn->query($sql);
        
        echo "Saved";
        
        $conn->commit();

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

}

if ($_GET["Command"] == "pass_inv") {
    
    $ResponseXML = "";
    $ResponseXML .= "<salesdetails>";

    $refno = $_GET["code"];

    $sql = "Select * from s_expenses where REFNO='" . $refno . "'"; 
    $result = $conn->query($sql);
    if ($rowM = $result->fetch()) {
        $ResponseXML .= "<id><![CDATA[" . json_encode($rowM) .  "]]></id>";
    }

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

if ($_GET["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");
        }
        
        $refno =$_GET["refno"];
        $sql = "select * from s_expenses where REFNO='".$refno."' ";
        $result = $conn->query($sql);
        if (!$row = $result->fetch()) { 
            exit("No Result Found...."); 
        }

        $sql = "select * from s_expenses where REFNO='".$refno."' and CANCELL='1'";
        $result = $conn->query($sql);
        if ($row = $result->fetch()) { 
            exit("Already Canceled...."); 
        }

        $sql1 = "update s_expenses set CANCELL='1' where REFNO='" . $refno . "'";
        $result1 = $conn->query($sql1);

        $sql2 = "insert into entry_log(refno, username, docname, trnType, stime, sdate,company) values 
        ('" . trim($refno) . "', '" . $_SESSION["CURRENT_USER"] . "', 'Expeses', 'CANCEL', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "','".$_SESSION['company']."')";
        $result2 = $conn->query($sql2);

        echo "Canceled";

        $conn->commit();

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

}