<?php

session_start();


require_once ("connection_sql.php");

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

date_default_timezone_set('Asia/Colombo');

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

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

    $sql = "SELECT expense FROM invpara";
    $result = $conn->query($sql);

    $row = $result->fetch();
    $no = $row['expense'];
    $uniq = uniqid();
    $ResponseXML .= "<id><![CDATA[$no]]></id>";
    $ResponseXML .= "<uniq><![CDATA[$uniq]]></uniq>";
    $ResponseXML .= "<date><![CDATA[".date('Y-m-d')."]]></date>";

    $ResponseXML .= "</new>";

    echo $ResponseXML;
}


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


    try {
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $conn->beginTransaction();
        
         $sql = "select * from expense where refno ='" . $_POST['invno'] . "'"; 

        $result = $conn->query($sql);

        if ($row = $result->fetch()) {
            
             exit('Already Enterd');
        }
         
        $sql1 = "SELECT * FROM s_stomas where CODE='". $_POST['dep'] ."'";
        $resul1 = $conn->query($sql1);
        $row1 = $resul1->fetch();
        $no = $row['expense'];
        
        $sql2 = "insert into expense(refno,name, cat,amount,sdate,depcode,dep) values ('" . $_POST['invno'] . "', '" . $_POST['name'] . "','" . $_POST['cat'] . "','" . $_POST['amount'] . "','" . $_POST['sdate'] . "','" . $_POST['dep'] . "','".$row1['DESCRIPTION']."')"; 
        $result2 = $conn->query($sql2);

        $sql = "SELECT expense FROM invpara";
        $resul = $conn->query($sql);
        $row = $resul->fetch();
        $no = $row['expense'];
        $no2 = $no + 1;
        $sql = "update invpara set expense = $no2 where expense = $no";
        $result = $conn->query($sql);
        

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

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

    header('Content-Type: text/xml');
    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";

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

    $cuscode = $_POST["custno"];

    $sql = "Select * from expense where   refno ='" . $cuscode . "'";
    $result = $conn->query($sql);

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

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

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

    $ResponseXML = "";
    $ResponseXML .= "<salesdetails>";
    try {
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $conn->beginTransaction();

        $sql = "select * from expense where refno ='" . $_POST['invno'] . "'";
        $result = $conn->query($sql);
        if ($row = $result->fetch()) {
            
            $sql5 = "select * from expense  where refno ='" . $_POST['invno'] . "' and cancel='1'";
            $result5 = $conn->query($sql5);
            if ($rows5 = $result5->fetch()) { 
                exit("Advance Already Canceled !!!"); 
            } 
  
            $sql = "update expense set cancel='1' where refno = '" . $_POST['invno'] . "'";
            $conn->exec($sql);
              
            
            $sqllog = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values ('" . $_POST['invno'] . "', '" . $_SESSION["CURRENT_USER"] . "', 'Expense', 'CANCEL', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";
            $resultlog = $conn->query($sqllog);
            
            echo "Cancel";
            $conn->commit();
        } else {
            echo "Entry Not Found";
        }
    } catch (Exception $e) {
        $conn->rollBack();
        echo $e;
    }
}
?>