<?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"] == "setitem") {

    $ResponseXML = "";
    $ResponseXML .= "<salesdetails>";
    $sql = "delete from tmp_po_data_dag where stk_no='" . $_GET['itemCode'] . "' and tmp_no='" . $_GET['tmpno'] . "' ";
    $result = $conn->query($sql);

    if ($_GET["Command1"] == "add_tmp") {
        $rate = str_replace(",", "", $_GET["itemPrice"]);
        $qty = str_replace(",", "", $_GET["qty"]);
        if ($_GET['vat'] != "non") {
            $sql = "select vatrate,nbt from invpara";
            $result_vat = $conn->query($sql);
            if ($row_vat = $result_vat->fetch()) {
            $rate = $rate / (1 + $row_vat['vatrate'] / 100);
            $rate = $rate / (1 + $row_vat['nbt'] / 100);
            }
        }
        $discount = 0;
        $subtotal = $rate * $qty;
        $sql = "Insert into tmp_po_data_dag (stk_no, descript, qty, rate,subtot, tmp_no, descript1, srlNo, reqDate)values 
		('" . $_GET['itemCode'] . "', '" . $_GET['itemDesc'] . "', " . $rate . ", " . $_GET['qty'] . ",'" . $subtotal . "','" . $_GET['tmpno'] . "','" . $_GET['itemDesc1'] . "','" . $_GET['srlNo'] . "','" . $_GET['reqDate'] . "') ";
        $result = $conn->query($sql);
    }

    $ResponseXML .= "<sales_table><![CDATA[<table class=\"table\">
					<tr>
						<td style=\"width: 90px;\">Item</td>
						<td>Size</td>
                                            
						<td>Make</td>
					
                                                <td>Serial Number</td>
                                        
						<td>Required Date</td>
						<td style=\"width: 60px; visibility:hidden;\">Qty</td>
						<td style=\"width: 100px; visibility:hidden;\">Rate</td>
						<td style=\"width: 100px; visibility:hidden;\">Sub Total</td>
						<td style=\"width: 10px;\"></td>
					</tr>";
    $i = 1;
    $mtot = 0;
    $sql = "Select * from tmp_po_data_dag where tmp_no='" . $_GET['tmpno'] . "'";
    foreach ($conn->query($sql) as $row) {
        $ResponseXML .= "<tr>                              
                             <td>" . $row['stk_no'] . "</td>
							 <td>" . $row['descript'] . "</td>
							 <td>" . $row['descript1'] . "</td>
							 <td>" . $row['srlNo'] . "</td>
							 <td>" . $row['reqDate'] . "</td>
							 <td style=\"visibility:hidden;\">" . number_format($row['rate'], 2, ".", ",") . "</td>
							 <td style=\"visibility:hidden;\">" . number_format($row['qty'], 2, ".", ",") . "</td>
							 <td style=\"visibility:hidden;\">" . number_format($row['subtot'], 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['subtot'];
        $i = $i + 1;
    }

    $mtot1 = 0;
    if ($_GET['vat'] != "non") {
        $sql = "select vatrate from invpara";
        $result = $conn->query($sql);
        if ($row = $result->fetch()) {
            $mtot1 = $mtot * ($row['vatrate'] / 100);
        }
    }
    $sql = "select vatrate,nbt from invpara";
    $result = $conn->query($sql);
    $row = $result->fetch();
    $nbt = ($mtot * ($row['nbt'] / 100));
    if ($_GET['vat'] != "non") {
        $mtot1 = ($mtot + $nbt) * ($row['vatrate'] / 100);
    }
    $ResponseXML .= "   </table>]]></sales_table>";
    $ResponseXML .= "<item_count><![CDATA[" . $i . "]]></item_count>";
    $ResponseXML .= "<vattot><![CDATA[" . number_format($mtot1, 2, ".", ",") . "]]></vattot>";
    $ResponseXML .= "<nbt><![CDATA[" . number_format($nbt, 2, ".", ",") . "]]></nbt>";
    $ResponseXML .= "<gtot><![CDATA[" . number_format($mtot1 + $mtot + $nbt, 2, ".", ",") . "]]></gtot>";
    $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();
                $stkNo = $_GET['txt_entno'];
        $sql = "select REF_NO,CANCELL,itm_cancel,C_CODE,BRAND,seri_no,status from view_salma_sinvo_dcl where  STK_NO='$stkNo'";
      
        $result = $conn->query($sql);
        if ($row = $result->fetch()) {

            if ($row["CANCELL"] == "1") {
                echo "Collection Cancelled";
                exit();
            }

            if (($row["itm_cancel"] == "2")||($row["itm_cancel"] == "3")) {
                echo "Cannot change!";
                exit();
            }
             else {
                $invno = $row['REF_NO'];
            }
        } else {
            exit("Entry Not Entered!");
        }
       
        $phrase = "";
        $LEDINDI = "";
        if($row['status']=="3"){ 
            exit("Completed...");
        }else if($_GET["status"]==$row['status']){ 
            exit("Please set the status!");
        }else if ($_GET["status"]=="1"){
            if($row["status"]=="3"){
                 exit("Can't Reverse  Process!");
            }  
            $sql = "update s_mas set status='".$_GET["status"]."',QTYINHAND='0' where STK_NO = '$stkNo'";
            $result = $conn->query($sql);
            
            $sql = "update s_submas set  QTYINHAND='0' where STK_NO = '$stkNo' and STO_CODE='01'";
            $result = $conn->query($sql);
            
            $sql = "update s_invo_dcl set status='".$_GET["status"]."', invDate='".$_GET["invdate"]."',s_code='".$_GET["c_code"]."', s_name='".$_GET["c_name"]."' where STK_NO = '$stkNo'";
            $result = $conn->query($sql);
            
        }else if ($_GET["status"]=="2"){
            if($row["status"]=="3"){
                 exit("Can't Reverse  Process!");
            }
            $sql = "update s_mas set status='".$_GET["status"]."',statusc='".$_GET["status"]."',QTYINHAND='1' where STK_NO = '$stkNo'";
            $result = $conn->query($sql);
             
            $sql = "update s_submas set  QTYINHAND='1' where STK_NO = '$stkNo' and STO_CODE='01'";
            $result = $conn->query($sql);
            
            $sql = "update s_invo_dcl set status='".$_GET["status"]."',rjctDate='".$_GET["invdate"]."', statusc='".$_GET["status"]."' where STK_NO = '$stkNo'";
            $result = $conn->query($sql);
            
        }else if ($_GET["status"]=="3"){
            if($_GET["dcharge"]==""){
                 exit("Please Enter Dag Amount!");
            }
            if($_GET["ccharge"]==""){
                 exit("Please Enter Company Charge!");
            } 
             $sql = "update s_mas set status='".$_GET["status"]."',QTYINHAND='1',SELLING='".$_GET["dcharge"]."'+'".$_GET["ccharge"]."',COST='".$_GET["dcharge"]."' where STK_NO = '$stkNo'";
            $result = $conn->query($sql);
             
            $sql = "update s_submas set  QTYINHAND='1' where STK_NO = '$stkNo' and STO_CODE='01'";
            $result = $conn->query($sql);          
            
            $sql = "update s_invo_dcl set status='".$_GET["status"]."',cmpDate='".$_GET["invdate"]."',dcharge='".$_GET["dcharge"]."',ccharge='".$_GET["ccharge"]."' where STK_NO = '$stkNo'";
            $result = $conn->query($sql);
            
              
            $sql = "Insert into c_bal (REFNO, SDATE, CUSCODE, AMOUNT,DEP, SAL_EX, trn_type, vatrate)values 
    		('" . $stkNo . "', '" . $_GET['invdate'] . "', " . $row['C_CODE'] . ", " . $_GET["dcharge"] . ",'01','01','DCL','8') ";
            $result = $conn->query($sql);
        } 
         
        
        $conn->commit();

        echo "Saved";
    } catch (Exception $e) {

        $conn->rollBack();

        echo $e;
    }
}



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



    $ResponseXML = "";

    $ResponseXML .= "<salesdetails>";

    $sql = "Select * from view_salma_sinvo_dcl where STK_NO = '" . $_GET['refno'] . "'";
    $result = $conn->query($sql);
    if ($row = $result->fetch()) {
        $ResponseXML .= "<C_REFNO><![CDATA[" . $_GET['refno'] . "]]></C_REFNO>";
        $ResponseXML .= "<C_DATE><![CDATA[" . $row["SDATE"] . "]]></C_DATE>"; 
        $ResponseXML .= "<C_CODE><![CDATA[" . $row["C_CODE"] . "]]></C_CODE>";
        $ResponseXML .= "<name><![CDATA[" . $row["CUS_NAME"] . "]]></name>";
        $ResponseXML .= "<s_code><![CDATA[" . $row["s_code"] . "]]></s_code>";
        $ResponseXML .= "<s_name><![CDATA[" . $row["s_name"] . "]]></s_name>";
        $ResponseXML .= "<txt_remarks><![CDATA[" . $row["itm_rmk"] . "]]></txt_remarks>";
        $ResponseXML .= "<Attn><![CDATA[" . $row['C_ADD1'] . "]]></Attn>";
        $ResponseXML .= "<tmp_no><![CDATA[" . $row["tmp_no"] . "]]></tmp_no>"; 
        $ResponseXML .= "<status><![CDATA[" . $row["status"] . "]]></status>";
        $ResponseXML .= "<dcharge><![CDATA[" . $row["dcharge"] . "]]></dcharge>";
        $ResponseXML .= "<ccharge><![CDATA[" . $row["ccharge"] . "]]></ccharge>";
        $msg = "";
        if ($row['CANCELL'] == "1") {
            $msg = "Cancelled";
        }
        $ResponseXML .= "<msg><![CDATA[" . $msg . "]]></msg>";
        
        $sql = "Select * from s_invo_dcl where REF_NO='" . $row["REF_NO"] . "' and STK_NO = '" . $_GET['refno'] . "'";
         $ResponseXML .= "<sales_table><![CDATA[<table class=\"table\">
				<tr class=\"info\">
				<td>Item No</td> 
				<td>Size</td> 
				<td></td> 
                <td>Brand</td>
                <td>Serial Number</td> 
		    	<td>Required</td>              
        		<td>Completed</td>
				<td>Rejected</td> 
 	</tr>";

        $i = 1;
        $mtot = 0;

        foreach ($conn->query($sql) as $row1) {

            $ResponseXML .= "<tr>                              
                            <td>" . $row1['STK_NO'] . "</td>
							 <td>" . $row1['DESCRIPT'] . "</td>
							 <td></td>
							 <td>" . $row1['BRAND'] . "</td>
							 <td>" . $row1['srlNo'] . "</td>
							 <td>" . $row1['reqDate'] . "</td>
							 <td>" . $row1['cmpDate'] . "</td>
							 <td>" . $row1['rjctDate'] . "</td>
							 
							 
							 </tr>";

            $mtot = $mtot + $subtotal;
            $i = $i + 1;
        }

        $mtot1 = 0;

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


if ($_GET["Command"] == "update_list") {
    $ResponseXML = "";
    $ResponseXML .= "<table class=\"table\">
	            <tr>
                        <th width=\"121\">Reference No</th>
                        <th width=\"121\"></th>
                        <th width=\"121\">Date</th>
                        <th width=\"100\">Code</th> 
                        <th width=\"200\">Name</th> 
                        <th width=\"121\"></th>  
                    </tr>";

    $sql = "select REF_NO,STK_NO, SDATE,C_CODE,CUS_NAME from view_salma_sinvo_dcl where STK_NO != '' ";

    if ($_GET['refno'] != "") {
        $sql .= " and STK_NO like '%" . $_GET['refno'] . "%'";
    }
        if ($_GET['refno1'] != "") {
        $sql .= " and C_CODE like '%" . $_GET['refno1'] . "%'";
    }
    if ($_GET['cusname'] != "") {
        $sql .= " and CUS_NAME like '%" . $_GET['cusname'] . "%'";
    }
    $stname = $_GET['stname'];

    $sql .= " ORDER BY SDATE desc limit 50";


//    echo $sql;
    foreach ($conn->query($sql) as $row) {

        $cuscode = $row["STK_NO"];
        $ResponseXML .= "<tr>               
                              <td onclick=\"crnview('$cuscode', '$stname');\">" . $row['STK_NO'] . "</a></td>
                              <td onclick=\"crnview('$cuscode', '$stname');\"></a></td>
                              <td onclick=\"crnview('$cuscode', '$stname');\">" . $row['SDATE'] . "</a></td>
                              <td onclick=\"crnview('$cuscode', '$stname');\">" . $row['C_CODE'] . "</a></td>
                               <td onclick=\"crnview('$cuscode', '$stname');\">" . $row['CUS_NAME'] . "</a></td>
                               <td onclick=\"crnview('$cuscode', '$stname');\"></a></td>
                            </tr>";
    }

    $ResponseXML .= "</table>";

    echo $ResponseXML;
}



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

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

        $sql = "select REF_NO,CANCELL,TOTPAY,DEPARTMENT,dele_no from s_salma_dcl where tmp_no ='" . $_GET['tmpno'] . "'";
        $result = $conn->query($sql);
        if ($row = $result->fetch()) {
            if ($row['CANCELL'] != "0") {
            echo "Already Cancelled";
                exit();
            }
            if ($row['TOTPAY'] > 0) {
                echo "Already Paid";
                exit();
            }

            $invno = $row['REF_NO'];

            $sql = "update s_salma_dcl set CANCELL='1' where REF_NO = '" . $row['REF_NO'] . "'";
            $conn->exec($sql);
 
            $sql = "delete from s_trn where REFNO='" . $row['REF_NO'] . "'";
            $conn->exec($sql);
            
              $sql = "update s_invo_dcl set CANCELL='1' where ref_no = '" . $row['REF_NO'] . "'";
            $conn->exec($sql);
            echo "ok";

            $conn->commit();
        } else {
            echo "Entry Not Found";
        }
    } catch (Exception $e) {
        $conn->rollBack();
        echo $e;
    }
}
?>