<?php

session_start();

////////////////////////////////////////////// Database Connector ///////////////////////////////////////////////////////////


require_once ("connection_sql.php");
////////////////////////////////////////////// Write XML ////////////////////////////////////////////////////////////////////
header('Content-Type: text/xml');

date_default_timezone_set('Asia/Colombo');

if ($_GET["Command"] == "new_inv") {
    header('Content-Type: text/xml');
    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";

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

    $sql = "SELECT EXPENSES_TYPE FROM invpara where flag='".$_SESSION['company']."' ";
    $result = $conn->query($sql);
    $row = $result->fetch();
    $no = $row['EXPENSES_TYPE'];
    $tmpinvno = "000" . $no;
    $lenth = strlen($tmpinvno);
    $refno = trim("EX/ ") . substr($tmpinvno, $lenth - 4);

    $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();
        
        $sql = "SELECT * from s_expense_type where  REFNO='".$_GET["refno"]."' and CANCELL='0'"; 
        $result = $conn->query($sql);
        if ($row = $result->fetch()) { 
            exit("Already Saved...."); 
        }
        
        $sql = "SELECT * from s_expense_type where  REFNO='".$_GET["refno"]."' and CANCELL='1'"; 
        $result = $conn->query($sql);
        if ($row = $result->fetch()) { 
            exit("Already Saved...."); 
        }
      
        $sql = "SELECT EXPENSES_TYPE FROM invpara where flag='".$_SESSION['company']."' ";
        $result = $conn->query($sql);
        $row = $result->fetch();
        $no = $row['EXPENSES_TYPE'];
        $tmpinvno = "000" . $no;
        $lenth = strlen($tmpinvno);
        
        $refno = trim("EX/ ") . substr($tmpinvno, $lenth - 4);

        $sql ="insert into s_expense_type(REFNO, NAME) values 
        ('" . $_GET["refno"] . "', '" . $_GET["name"] . "')";
        $result = $conn->query($sql);

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

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

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

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

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

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

        $sql1 = "update s_expense_type 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 Type', '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;
    }

}