<?php

session_start();

////////////////////////////////////////////// Database Connector /////////////////////////////////////////////////////////////
require_once ("connection_sql.php");

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

date_default_timezone_set('Asia/Colombo');

/////////////////////////////////////// GetValue //////////////////////////////////////////////////////////////////////////
///////////////////////////////////// Registration /////////////////////////////////////////////////////////////////////////

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

    $invno = getno();

    $sql = "Select QTNNO from tmpinvpara_acc";
    $result = $conn->query($sql);
    $row = $result->fetch();

    $tono = $row['QTNNO'];

    $sql = "delete from tmp_po_data where tmp_no='" . $tono . "'";
    $result = $conn->query($sql);

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


    $ResponseXML = "";
    $ResponseXML .= "<salesdetails>";
    $ResponseXML .= "<invno><![CDATA[" . $invno . "]]></invno>";
    $ResponseXML .= "<tmpno><![CDATA[" . $tono . "]]></tmpno>";
    $ResponseXML .= "<dt><![CDATA[" . date('Y-m-d') . "]]></dt>";

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

function getno() {

    include './connection_sql.php';
    $sql = "select QTNNO from invpara";
    $result = $conn->query($sql);
    $row = $result->fetch();
    $tmpinvno = "000000" . $row["QTNNO"];
    $lenth = strlen($tmpinvno);
    return $invno = trim("O/") . substr($tmpinvno, $lenth - 7);
}

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

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


    $sql = "delete from tmp_po_data where stk_no='" . $_GET['itemCode'] . "' and tmp_no='" . $_GET['tmpno'] . "' ";
    $result = $conn->query($sql);
    if ($_GET["Command1"] == "add_tmp") {
       
        $sql = "Insert into tmp_po_data(stk_no, descript,rate, tmp_no,serial_no,brand)values 
			('" . $_GET['itemCode'] . "', '" . $_GET['itemDesc'] . "','" . $_GET['itemPrice'] . "','" . $_GET['tmpno'] . "','" . $_GET['serial_no'] . "','" . $_GET['brand'] . "')";
        $result = $conn->query($sql);
    }

    $ResponseXML .= "<sales_table><![CDATA[<table class=\"table\">
					<tr>
						<td style=\"width: 90px;\">Item</td>
						<td>Description</td>
						<td style=\"width: 100px;\">Brand</td>
						<td style=\"width: 100px;\">Serial No</td>
						<td style=\"width: 100px;\">Rate</td>
						<td style=\"width: 10px;\"></td>
					</tr>";

    $i = 1;
    $mtot = 0;
    $sql = "Select * from tmp_po_data where tmp_no='" . $_GET['tmpno'] . "'";
    foreach ($conn->query($sql) as $row) {

        $ResponseXML .= "<tr>                              
                             <td ondblclick=\"setitem('" . $row['stk_no'] . "','" . $row['descript'] . "','" . $row['rate'] . "');\">" . $row['stk_no'] . "</td>
                             <td ondblclick=\"setitem('" . $row['stk_no'] . "','" . $row['descript'] . "','" . $row['rate'] . "');\">" . $row['descript'] . "</td>
                             <td ondblclick=\"setitem('" . $row['stk_no'] . "','" . $row['descript'] . "','" . $row['rate'] . "');\">" . $row['brand'] . "</td>
                             <td ondblclick=\"setitem('" . $row['stk_no'] . "','" . $row['descript'] . "','" . $row['rate'] . "');\">" . $row['serial_no'] . "</td>
                             <td ondblclick=\"setitem('" . $row['stk_no'] . "','" . $row['descript'] . "','" . $row['rate'] . "');\">" . number_format($row['rate'], 2, ".", ",") . "</td>
                             <td><a class=\"btn btn-danger btn-xs\" onClick=\"del_item('" . $row['stk_no'] . "')\"> <span class='fa fa-remove'></span></a></td>
                             </tr>";

        $mtot = $mtot + $row['rate'];
        $i = $i + 1;
    }

    $ResponseXML .= "   </table>]]></sales_table>";
    $ResponseXML .= "<subtot><![CDATA[" . number_format($mtot, 2, ".", ",") . "]]></subtot>";
    $ResponseXML .= "</salesdetails>";

    echo $ResponseXML;
}

if ($_GET["Command"] == "save_item") {
    try {
        
        
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $conn->beginTransaction();

        $sql = "select * from tmp_po_data where tmp_no='" . $_GET["tmpno"] . "'";
        foreach ($conn->query($sql) as $row) {

            $sql1 = "Insert into s_ordtrn_dag (REFNO, STK_NO, DESCRIPT, RATE ,serial_no,brand,lvl) values 
		('" . $_GET['txt_entno'] . "','" . $row["stk_no"] . "','" . $row["descript"] . "', '" . $row["rate"] . "', '" . $row["serial_no"] . "', '" . $row["brand"] . "','0')";
            $result = $conn->query($sql1);
        }

        $sql = "insert into s_ordmas_dag(REFNO, SDATE, SUP_CODE, SUP_NAME,COUNTRY, REMARK, AMOUNT) values 
                                        ('" . $_GET['txt_entno'] . "', '" . $_GET['invdate'] . "', '" . $_GET["customercode"] . "', '" . $_GET["customername"] . "' ,'" . $_GET['cont'] . "','" . $_GET['txt_remarks'] . "','" . $_GET['subtot'] . "')";

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

        $sql = "delete from tmp_po_data where tmp_no='" . $_GET['tmpno'] . "' ";
        $result = $conn->query($sql);

        
        
        $sql = "SELECT coll_no FROM invpara";
        $resul = $conn->query($sql);
        $row = $resul->fetch();
        $no = $row['coll_no'];
        $no2 = $no + 1;
        $sql = "update invpara set coll_no = $no2 where coll_no = $no";
        $result = $conn->query($sql);
        
        
        
        
        $conn->commit();
        echo "Saved";
    } catch (Exception $e) {
        $conn->rollBack();
        echo $e;
    }
}

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

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

    $sql = "SELECT coll_no FROM invpara";
    $result = $conn->query($sql);
    $row = $result->fetch();
    $no = $row['coll_no'];
    $ResponseXML .= "<id><![CDATA[$no]]></id>";
    $ResponseXML .= "</new>";

    echo $ResponseXML;
        
       
}

?>