<?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") {
    if ($_SESSION["CURRENT_USER"] == "") {
    echo "Please Loging Again !!!";
    exit();
}

if ($_SESSION["company"] == "") {
    echo "Invalid User Session";
    exit();
}
    header('Content-Type: text/xml');
    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";

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

    $sql = "SELECT CATEGORY  FROM invpara";
    $result = $conn->query($sql);
    $row = $result->fetch();
    $no = $row['CATEGORY'];
    $tmpinvno = "0" . $no;
    $lenth = strlen($tmpinvno);
    
    $refno =   substr($tmpinvno, $lenth - 2);
    
    $uniq = uniqid();
    $ResponseXML .= "<id><![CDATA[$refno]]></id>";
    $ResponseXML .= "<uniq><![CDATA[$uniq]]></uniq>";
    $ResponseXML .= "</new>";

    echo $ResponseXML;
}


if ($_GET["Command"] == "save_inv") {
    if ($_SESSION["CURRENT_USER"] == "") {
    echo "Please Loging Again !!!";
    exit();
}

if ($_SESSION["company"] == "") {
    echo "Invalid User Session";
    exit();
}

    try {
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $conn->beginTransaction();
        
         $sql = "select * from s_mas_category where REFNO='".$_GET["refno"]."' ";
        $result = $conn->query($sql);
        if ($row = $result->fetch()) { 
            exit("Already Saved...."); 
        }
        
         $sql = "select * from s_mas_category where name='".$_GET["category"]."' ";
        $result = $conn->query($sql);
        if ($row = $result->fetch()) { 
            exit("Already Saved...."); 
        }
      
        $sql = "SELECT CATEGORY  FROM invpara";
        $result = $conn->query($sql);
        $row = $result->fetch();
        $no = $row['CATEGORY'];
        $tmpinvno = "0" . $no;
        $lenth = strlen($tmpinvno);
        
        $refno =   substr($tmpinvno, $lenth - 2);

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

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

        $sql_log = "insert into entry_log(refno, username, docname, trnType, stime, sdate,company) values 
        ('" . $_GET["category"] . "', '" . $_SESSION["CURRENT_USER"] . "', 'CATEGORY 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") {
    if ($_SESSION["CURRENT_USER"] == "") {
    echo "Please Loging Again !!!";
    exit();
}

if ($_SESSION["company"] == "") {
    echo "Invalid User Session";
    exit();
}

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

        $sql_data = "SELECT * from s_mas_category where REFNO='".$_GET["refno"]."'";
        $result_data = $conn->query($sql_data);
        if (!$row = $result_data->fetch()) {
            exit("No Result Found...!!!");
        }
        
          $sql = "select * from s_mas_category where name='".$_GET["category"]."' ";
        $result = $conn->query($sql);
        if ($row = $result->fetch()) { 
            exit("Already Saved...."); 
        }

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

        $sql = "select * from s_mas_category where REFNO='".$_GET["refno"]."'";
        $result = $conn->query($sql);
        if ($row = $result->fetch()) { 
            $sql = "update s_mas_category set name='" . $_GET["category"] . "' where REFNO='".$_GET["refno"]."'";
            $result = $conn->query($sql); 
            // echo $sql; 
        }

        $sql_log = "insert into entry_log(refno, username, docname, trnType, stime, sdate,company) values 
        ('" . $_GET["category"] . "', '" . $_SESSION["CURRENT_USER"] . "', 'CATEGORY Master', '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"){
    if ($_SESSION["CURRENT_USER"] == "") {
    echo "Please Loging Again !!!";
    exit();
}

if ($_SESSION["company"] == "") {
    echo "Invalid User Session";
    exit();
}

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

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


        $sql = "update s_mas_category set CANCELL='1' where REFNO='".$_GET["refno"]."'";
        $result = $conn->query($sql);

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

        echo "Canceled";

        $conn->commit();

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

}

?>
