<?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 EMPLOYEE FROM invpara where flag='".$_SESSION['company']."' ";
    $result = $conn->query($sql);
    $row = $result->fetch();
    $no = $row['EMPLOYEE'];
    $tmpinvno = "00000" . $no;
    $lenth = strlen($tmpinvno);
     
    $refno = trim("".$_SESSION['company']."/EM/ ") . substr($tmpinvno, $lenth - 6);
    $uniq = uniqid();   
     

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

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

        $sql = "Insert into s_employee (REFNO,TMP_NO, NAME,ADDRESS,TEL,COMPANY) values 
            ('" . $refno . "','" . $_GET["uniq"] . "', '" . $_GET["name"] . "', '" . $_GET["add"] . "', '" . $_GET["tel"] . "', '" . $_GET["company"] . "')";
        $result = $conn->query($sql);

        $sql2 = "update invpara set EMPLOYEE=EMPLOYEE+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_employee 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"] == "update_inv") {

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

        $sql_data = "SELECT * from s_employee where REFNO='".$_GET["refno"]."'";
        $result_data = $conn->query($sql_data);
        if (!$row = $result_data->fetch()) {
            exit("No Result Found...!!!");
        }

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

        $sql = "select * from s_employee where REFNO='".$_GET["refno"]."'";
        $result = $conn->query($sql);
        if ($row = $result->fetch()) { 
            $sql = "update s_employee set NAME='" . $_GET["name"] . "',TMP_NO='" . $_GET["uniq"] . "',ADDRESS='" . $_GET["add"] . "',TEL='" . $_GET["tel"] . "' ,COMPANY='" . $_GET["company"] . "' where REFNO='".$_GET["refno"]."'";
            $result = $conn->query($sql); 
        }

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

        echo "Updated";

        $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();

        if ($_SESSION['company'] == "") { 
            exit("Please Login");
        }
        if ($_SESSION['CURRENT_USER'] == "") { 
            exit("Please Login");
        }
        
        $refno =$_GET["refno"];
        $sql = "select * from s_employee where REFNO='".$refno."' ";
        $result = $conn->query($sql);
        if (!$row = $result->fetch()) { 
            exit("No Result Found...."); 
        }

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

        $sql1 = "update s_employee 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;
    }

}