<?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") {
    
        if ($_SESSION['company'] == "") { 
            exit("Please Login");
        }
        if ($_SESSION['CURRENT_USER'] == "") { 
            exit("Please Login");
        }

    // $sql = "select GIN from invpara where flag='".$_SESSION['company']."'"; 
    // $result = $conn->query($sql);
    // $row = $result->fetch();
    // $tmpinvno = "000000" . $row["GIN"];
    // $lenth = strlen($tmpinvno);
    // $invno = trim("".$_SESSION['company']."/ST/") . substr($tmpinvno, $lenth - 12);
    
    $sql = "SELECT DAG_TRN FROM invpara where flag='".$_SESSION['company']."'"; 
    $result = $conn->query($sql); 
    $row = $result->fetch();
    $no = $row['DAG_TRN']; 
    $tmpinvno = "000000" . $no;
    $lenth = strlen($tmpinvno); 
    $refno =trim("".$_SESSION['company']."/DGIN/") . substr($tmpinvno, $lenth - 7); 

    // $sql = "Select QTNNO from tmpinvpara where flag='".$_SESSION['company']."'"; 
    // $result = $conn->query($sql);
    // $row = $result->fetch();

    $tono = uniqid();

    $sql = "delete from tmp_gin_data_multi where user_id='" . $_SESSION["CURRENT_USER"] . "'";
    $result = $conn->query($sql);
    
    $sql = "delete from tmp_gin_data_multi where tmp_no='" . $tono . "'";
    $result = $conn->query($sql);

    // $sql = "update tmpinvpara set QTNNO=QTNNO+1 where flag='".$_SESSION['company']."'"; 
    // $result = $conn->query($sql);


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


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

   $ResponseXML = "";
    $ResponseXML .= "<salesdetails>";
    
     $ResponseXML .= "<sales_table><![CDATA[<table class=\"table table-bordered\"  style=\"width:100%\"> ";

     $sql = "select * from dag_item where (flag ='2' OR flag ='3'  or flag ='5' or flag ='6' ) and cancel='0'  and invno IS NULL ";  
    
     
    if ($_POST['s_refno'] != "") {
        $sql .= " and jobno like '%" . $_POST['s_refno'] . "%'";
    }
    if ($_POST['s_make'] != "") {
        $sql .= " and marker like '%" . $_POST['s_make'] . "%'";
    }
    if ($_POST['s_size'] != "") {
        $sql .= " and size like '%" . $_POST['s_size'] . "%'";
    }
    if ($_POST['s_warrenty'] != "") {
        $sql .= " and warrenty like '%" . $_POST['s_warrenty'] . "%'";
    }
    if ($_POST['s_serial'] != "") {
        $sql .= " and serialno like '%" . $_POST['s_serial'] . "%'";
    }
    if ($_POST['s_cost'] != "") {
        $sql .= " and cost like '%" . $_POST['s_cost'] . "%'";
    }
    if ($_POST['s_cash'] != "") {
        $sql .= " and total like '%" . $_POST['s_cash'] . "%'";
    }
    
    $sql .=" order by id desc limit 50";
   
    foreach ($conn->query($sql) as $row) {
       
        $cuscode = $row['jobno']; 
       
        
        $ResponseXML .= "<tr >               
        <td onclick=\"pass_item_inv('$cuscode');\" style='width: 15%; text-align:left;'> " . $row['jobno'] . "</a></td>
        <td onclick=\"pass_item_inv('$cuscode');\" style='width: 10%; text-align:left;'>" . $row['marker'] . "</a></td>
        <td onclick=\"pass_item_inv('$cuscode');\" style='width: 10%; text-align:left;'>" . $row['size'] . "</a></td>
        <td onclick=\"pass_item_inv('$cuscode');\" style='width: 10%; text-align:left;'>" . $row['warrenty'] . "</a></td>
        <td onclick=\"pass_item_inv('$cuscode');\" style='width: 15%; text-align:left;'>" . $row['serialno'] . "</a></td>
        <td onclick=\"pass_item_inv('$cuscode');\" style='width: 10%; text-align:right;'>" . $row['cost'] . "</a></td>
        <td onclick=\"pass_item_inv('$cuscode');\" style='width: 10%; text-align:right;'>" . $row['total'] . "</a></td> 
        </tr>";
                 
    }

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

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


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

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

    
    $sql =  "Select * from dag_item where jobno='".$_GET['itno']."' ";
    $result = $conn->query($sql); 
    if ($row = $result->fetch()) { 
        
        $ResponseXML .= "<str_code><![CDATA[".$row['STK_NO']."]]></str_code>";
        $ResponseXML .= "<jobno><![CDATA[".$row['jobno']."]]></jobno>";
        $ResponseXML .= "<serialno><![CDATA[".$row['serialno']."]]></serialno>";
        $ResponseXML .= "<make><![CDATA[".$row['marker']."]]></make>";
        $ResponseXML .= "<size><![CDATA[".$row['size']."]]></size>";
        $ResponseXML .= "<str_selpri><![CDATA[".$row['total']."]]></str_selpri>";
        
        $department=trim(substr($_GET["department"], 0, 2));
        
        $sql1 = "Select count(*) as count from dag_item  where cancel='0'  and  serialno='".$row['serialno']."' and company='".$_SESSION['company']."' group by  serialno asc";
        $result_1 = $conn->query($sql1);
        if ($row_1= $result_1->fetch()) { 
            
            if (is_null($row_1["count"])==false){
                $ResponseXML .= "<qtyinhand><![CDATA[".$row_1["count"]."]]></qtyinhand>";
            }  else {
                $ResponseXML .= "<qtyinhand><![CDATA[0]]></qtyinhand>";
            }
        }  else {
            $ResponseXML .= "<qtyinhand><![CDATA[0]]></qtyinhand>";
        }
    }               

    // $ResponseXML .= " <itemlist><![CDATA[ <table class=\"table table-bordered\" border=\"1\">
    // <tr>
    // <td width=\"122\" align=\"center\"><b>Department</b></td>
    // <td width=\"51\" align=\"center\"><b>Quantity</b></td>
    // <td width=\"51\" align=\"center\"><b>Pending Order</b></td>

    // </tr>";

    // $sql2 =  "Select * from s_stomas" ;
    
    // foreach ($conn->query($sql2) as $row2) {
    //             // while ($row2 = mysqli_fetch_array($sql2)) {

    //     $sql3 = "Select QTYINHAND from s_submas where STO_CODE='" . $row2["CODE"] . "' and STK_NO='" . $_GET["itno"] . "' and qtyinhand >0";
    //     $result3 = $conn->query($sql3);
    //     if ($row3 = $result3->fetch()) { 
          
    //         $mord = 0;
            
    //         $sql4 =  "Select sum(qty) as qty from view_cusordmas_trn where department='" . $row2["CODE"] . "' and STK_NO='" . $_GET["itno"] . "' and cancell='0' and invno ='0'";
    //         $result4 = $conn->query($sql4);
    //         if ($row4 = $result4->fetch()) { 
    //             $mord = $row4['qty'];
    //         }
            
    //         $ResponseXML .= "<tr>
    //         <td width=\"122\" align=\"center\"><b>" . $row2["DESCRIPTION"] . "</b></font></td>
    //         <td width=\"51\"  align=center><b>" . number_format($row3["QTYINHAND"], 0, ".", ",") . "</b></font></td> 
    //         <td width=\"51\"  align=center><b>" . $mord . "</b></font></td>";



    //         $ResponseXML .= "</tr>";
    //     } else {

    //     }
    // }

    

    // $ResponseXML .= "</table>]]></itemlist>";

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

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

    if ($_GET["tmpno"] == "") {
        exit("Error");
    }

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

    $sql = "delete from tmp_gin_data_multi where dep_from ='" . $_GET['from_dep'] . "' and jobno='" . $_GET['jobno'] . "' and tmp_no='" . $_GET['tmpno'] . "' ";
    $result = $conn->query($sql);

    $qty = str_replace(",", "", $_GET["qty"]);



    $sql = "Insert into tmp_gin_data_multi (dep_from,str_invno, str_code, cur_qty, tmp_no, user_id,rate,jobno,partno,serialno,type)values 
    ('" . $_GET['from_dep'] . "','" . $_GET['invno'] . "', '". $_GET['itemCode'] ."', " . $qty . ", '" . $_GET["tmpno"] . "', '" . $_SESSION["CURRENT_USER"] . "', '" . $_GET["rate"] . "', '" . $_GET['jobno'] . "','" . $_GET["size"] . "','" . $_GET["serialno"] . "','DAG GIN TRANSFER') ";
    $result = $conn->query($sql);
    

    $ResponseXML .= "<sales_table><![CDATA[<table class=\"table table-bordered \">
    <tr class=\"info\">
    <td style=\"width: 15%;\" align='center'><b>From Department</b></td>
    <td style=\"width: 15%;\" align='center'><b>Job No</b></td>
    <td style=\"width: 15%;\" align='center'><b>Serial No</b></td>
    <td style=\"width: 15%;\" align='right'><b>Rate</b></td>
    <td style=\"width: 15%;\" align='center'><b>Quantity</b></td>
    <td style=\"width: 15%;\" align='right'><b>Unit Total</b></td>
    <td style=\"width: 5%;\"></td>
    </tr>";

    $i = 1;
    $totamo =0;
    $sql = "Select * from view_tmp_gin where tmp_no='" . $_GET['tmpno'] . "' and type='DAG GIN TRANSFER' ";
    foreach ($conn->query($sql) as $row) {

        $ResponseXML .= "<tr>                           
        <td align='center'>" . $row['DESCRIPTION'] . "</td>
        <td align='left'>" . $row['jobno'] . "</td>
        <td align='left'>" . $row['serialno'] . "</td>
        <td align='right'>" . $row['rate'] . "</td>
        <td align='center'>" . number_format($row['cur_qty'], 0, ".", ",") . "</td>
        <td align='right'>" . number_format(($row['rate']*$row['cur_qty']), 2, ".", ",") . "</td>
        <td><a class=\"btn btn-danger btn-sm\" onClick=\"del_item('" . $row['id'] . "')\"> <span class='fa fa-remove'></span></a></td>
        </tr>";
        $i = $i + 1;
        $totamo = $totamo + ($row['rate']*$row['cur_qty']);
    }

    $ResponseXML .= "</table>]]></sales_table>";
    $ResponseXML .= "<item_count><![CDATA[" . $i . "]]></item_count>";
    $ResponseXML .= "<totamo><![CDATA[" . $totamo . "]]></totamo>";
    $ResponseXML .= "</salesdetails>";

    echo $ResponseXML;
}

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

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

    $sql = "delete from tmp_gin_data_multi where id='" . $_GET['code'] . "' and tmp_no='" . $_GET['tmpno'] . "' and type='DAG GIN TRANSFER'";
    $result = $conn->query($sql);

    $ResponseXML .= "<sales_table><![CDATA[<table class=\"table table-bordered\">
    <tr class=\"info\">
    <td style=\"width: 15%;\" align='center'><b>From Department</b></td>
    <td style=\"width: 15%;\" align='center'><b>Job No</b></td>
    <td style=\"width: 15%;\" align='center'><b>Serial No</b></td>
    <td style=\"width: 15%;\" align='right'><b>Rate</b></td>
    <td style=\"width: 15%;\" align='center'><b>Quantity</b></td>
    <td style=\"width: 15%;\" align='right'><b>Unit Total</b></td>
    <td style=\"width: 5%;\"></td>
    </tr>";

    $i = 1;
    $totamo =0;
    $sql = "Select * from view_tmp_gin where tmp_no='" . $_GET['tmpno'] . "' and type='DAG GIN TRANSFER' ";
    $result = $conn->query($sql);
    foreach ($conn->query($sql) as $row) {

        $ResponseXML .= "<tr>                           
        <td align='center'>" . $row['DESCRIPTION'] . "</td>
        <td align='left'>" . $row['jobno'] . "</td>
        <td align='left'>" . $row['serialno'] . "</td>
        <td align='right'>" . $row['rate'] . "</td>
        <td align='center'>" . number_format($row['cur_qty'], 0, ".", ",") . "</td>
        <td align='right'>" . number_format(($row['rate']*$row['cur_qty']), 2, ".", ",") . "</td>
        <td><a class=\"btn btn-danger btn-sm\" onClick=\"del_item('" . $row['id'] . "')\"> <span class='fa fa-remove'></span></a></td>
        </tr>";
        $i = $i + 1;
        $totamo = $totamo + ($row['rate']*$row['cur_qty']);
    }

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

    $ResponseXML .= "<item_count><![CDATA[" . $i . "]]></item_count>";
    $ResponseXML .= "<totamo><![CDATA[" . $totamo . "]]></totamo>";


    $ResponseXML .= "</salesdetails>";

    echo $ResponseXML;
}


if ($_GET["Command"] == "save_inv") {
    
    if ($_SESSION['company'] == "") { 
        exit("Please Login");
    }
    if ($_SESSION['CURRENT_USER'] == "") { 
        exit("Please Login");
    }

    $sql = "select * from s_ginmas where tmp_no = '" . $_GET["tmpno"] . "'";
    $result = $conn->query($sql);
    if ($row = $result->fetch()) {
        exit("Error");
    }

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

        $idate = $_GET["sdate"];

        $sql2 = "select * from s_stomas where CODE='" . $_GET["to_dep"] . "' ";
        $result1 = $conn->query($sql2);
        $row1 = $result1->fetch();
        $DESCRIPTION_to = $row1["DESCRIPTION"];


        $sql = "select dep_from,str_code,cur_qty,rate,type from tmp_gin_data_multi where tmp_no='" . $_GET["tmpno"] . "' and type='DAG GIN TRANSFER' group by dep_from";
        // echo $sql;
        foreach ($conn->query($sql) as $row) {

            // $sqlsub1 = "Select * from s_submas where STK_NO='" . $row["str_code"] . "' and QTYINHAND<'".$row['cur_qty']."' and STO_CODE='" . $_GET["from_dep"] . "'"; 
            // $resultsub1 = $conn->query($sqlsub1);
            // if ($rowsub1 = $resultsub1->fetch()) {
            //     exit('Under Stock');
            // }

            $sql_para = "SELECT DAG_TRN FROM invpara where flag='".$_SESSION['company']."'"; 
            $result_para = $conn->query($sql_para); 
            $row_para = $result_para->fetch();
            $no = $row_para['DAG_TRN']; 
            $tmpinvno = "000000" . $no;
            $lenth = strlen($tmpinvno); 
            $invno =trim("".$_SESSION['company']."/DGIN/") . substr($tmpinvno, $lenth - 7);
    
    
            $sql2 = "select * from s_stomas where CODE='" . $row['dep_from'] . "' ";
            $result2 = $conn->query($sql2);
            $row2 = $result2->fetch();
            $DESCRIPTION_from = $row2["DESCRIPTION"];


            $sql1 = "insert into s_ginmas(SDATE, REF_NO, DEP_FROM, DEP_F_NAME, DEP_TO, DEP_T_NAME, tmp_no,del_to,company,TYPE) values
            ('" . $idate . "', '" . $invno . "', '" . $row['dep_from'] . "', '" . $DESCRIPTION_from . "', '" . $_GET["to_dep"] . "', '" . $DESCRIPTION_to . "','" . $_GET["tmpno"] . "','M','".$_SESSION['company']."','DAG GIN TRANSFER')";
            // echo $sql1;
            $result2 = $conn->query($sql1);

            $sql = "select * from tmp_gin_data_multi where tmp_no='" . $_GET["tmpno"] . "' and dep_from = '" . $row['dep_from'] . "' and type='DAG GIN TRANSFER' "; 
            // echo $sql;
            foreach ($conn->query($sql) as $row1) {

                $cur_qty = str_replace(",", "", $row1["cur_qty"]);
                
                $sql_dag = "Select * from dag_item where STK_NO='" . $row1["str_code"] . "' and jobno='" . $row1["jobno"] . "' and serialno='" . $row1["serialno"] . "'  ";
                // echo $sql_dag;
                $result_dag = $conn->query($sql_dag);
                $row_dag = $result_dag->fetch();

                $sqlsub = "Select * from s_submas where STK_NO='" . $row1["str_code"] . "' and STO_CODE='" . $_GET["to_dep"] . "'";
                $resultsub = $conn->query($sqlsub);
                if ($rowsub = $resultsub->fetch()) {
                    $sql1 = "update s_submas set QTYINHAND=QTYINHAND+" . $cur_qty . " where STK_NO='" . $row1["str_code"] . "' and STO_CODE='" . $_GET["to_dep"] . "'";
                    // echo $sql1;
                    $result1 = $conn->query($sql1);
                } else {
                    $sql1 = "insert into s_submas(STO_CODE, STK_NO, DESCRIPt, OPEN_STK, QTYINHAND) values 
                    ('" . $_GET["to_dep"] . "',  '" . $row1["str_code"] . "', '" . $row1["str_description"] . "', 0, '" . $cur_qty . "',  '" . $row1["str_code"] . "')";
                    // echo $sql1;
                    $result1 = $conn->query($sql1);
                }

                $sql1 = "update s_submas set QTYINHAND=QTYINHAND-" . $cur_qty . " where STK_NO='" . $row1["str_code"] . "' and STO_CODE='" . $row['dep_from'] . "'";
                // echo $sql1;
                $result1 = $conn->query($sql1);
                
                $sql1="insert into s_gintrn(REFNO, SDATE, STK_NO, DESCRIPT, ORD_QTY, partno, CANCEL, tmp_no, RATE,TYPE,JOBNO,SERIALNO,OLD_FLAG,company) values 
                ('".$invno."', '".$idate."', '".$row1["str_code"]."','".$row1["str_description"]."','".$cur_qty."','".$row1["partno"]."','0', '".$row1['tmp_no']."','".$row1["rate"]."','".$row1["type"]."','" . $row1["jobno"] . "','" . $row1["serialno"] . "','" . $row_dag["flag"] . "','".$_SESSION['company']."')";
                $result1 = $conn->query($sql1);

                $sql1 = "update dag_item set department='" . $_GET["to_dep"] . "',flag='9' where STK_NO='" . $row1["str_code"] . "' and jobno='" . $row1["jobno"] . "' and serialno='" . $row1["serialno"] . "' ";
                // echo $sql1;
                $result1 = $conn->query($sql1);

                $sql1 = "insert into s_trn(STK_NO, SDATE, QTY, LEDINDI, REFNO, DEPARTMENT, seri_no, Dev, SAL_EX, ACTIVE, DONO,cost,JOBNO,TYPE,SERIALNO,company,sdate1) values 
                ('" . $row1["str_code"] . "', '" . $idate . "', '" . $cur_qty . "', 'GININ', '" . $invno . "', '" . $row['dep_from'] . "', '', '" . $_SESSION['dev'] . "', '', '1', '','" . $row1["rate"] . "','" . $row1["jobno"] . "','" . $row1["type"] . "','" . $row1["serialno"] . "','".$_SESSION['company']."', '" . $idate . "')";
                // echo $sql1;
                $result1 = $conn->query($sql1);

                $sql1 = "insert into s_trn(STK_NO, SDATE, QTY, LEDINDI, REFNO, DEPARTMENT, seri_no, Dev, SAL_EX, ACTIVE, DONO,cost,JOBNO,TYPE,SERIALNO,company,sdate1) values
                ('" . $row1["str_code"] . "', '" . $idate . "', '" . $cur_qty . "', 'GINRN', '" . $invno . "', '" . $_GET["to_dep"] . "', '', '" . $_SESSION['dev'] . "', '', '1', '','" . $row1["rate"] . "','" . $row1["jobno"] . "','" . $row1["type"] . "','" . $row1["serialno"] . "','".$_SESSION['company']."', '" . $idate . "')";
                // echo $sql1;
                $result1 = $conn->query($sql1);
            }

            $sql1 = "update invpara set DAG_TRN=DAG_TRN+1 where flag='".$_SESSION['company']."'"; 
            $result1 = $conn->query($sql1);

            $sql2 = "insert into entry_log(refno, username, docname, trnType, stime, sdate,company) values 
            ('" . $invno . "', '" . $_SESSION["CURRENT_USER"] . "', 'DAG GIN TRANSFER', 'SAVE', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "','".$_SESSION['company']."')";
            $result = $conn->query($sql2);
        }

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


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

    //$department=$_GET["department"];

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


    $sql1 = "delete from tmp_gin_data_multi where tmp_no='" . $_GET["invno"] . "'";
    $result1 = $conn->query($sql1);


    $sql = "select * from s_ginmas where tmp_no='" . $_GET['invno'] . "' and type='DAG GIN TRANSFER' ";

    foreach ($conn->query($sql) as $row) {
        
        $REF_NO = $row['REF_NO'];
        $dep_to = $row['DEP_TO'];
        $sdate = $row['SDATE'];

        $sql1 = "select * from s_trn where REFNO='" . $row['REF_NO'] . "' and LEDINDI='GININ' and TYPE='DAG GIN TRANSFER' ";
        // echo $sql1;
        foreach ($conn->query($sql1) as $row1) {


            $sql2 = "Insert into tmp_gin_data_multi (dep_from,str_invno, str_code, str_description, cur_qty, tmp_no, user_id,rate,jobno,serialno,type)values 
            ('" . $row1['DEPARTMENT'] . "','" . $row['REF_NO'] . "', '" . $row1['STK_NO'] . "', '', " . $row1["QTY"] . ", '" . $_GET["invno"] . "', '" . $_SESSION["CURRENT_USER"] . "','" . $row1['cost'] . "','" . $row1['JOBNO'] . "','" . $row1['SERIALNO'] . "','DAG GIN TRANSFER') ";
            // echo $sql2;
            $result = $conn->query($sql2);
            
        }
    }


    $ResponseXML .= "<sales_table><![CDATA[<table class=\"table\">
    <tr>
    <td style=\"width: 15%;\" align='center'><b>From Department</b></td>
    <td style=\"width: 15%;\" align='center'><b>Job No</b></td>
    <td style=\"width: 15%;\" align='center'><b>Serial No</b></td>
    <td style=\"width: 15%;\" align='right'><b>Rate</b></td>
    <td style=\"width: 15%;\" align='center'><b>Quantity</b></td>
    <td style=\"width: 15%;\" align='right'><b>Unit Total</b></td>
    </tr>";

    $i = 1;
    $totrate=0;
    $sql = "Select * from view_tmp_gin where tmp_no='" . $_GET['invno'] . "' and type='DAG GIN TRANSFER'";
    foreach ($conn->query($sql) as $row) {
        $rate=0;
        $ResponseXML .= "<tr>                              
        <td align='center'>" . $row['DESCRIPTION'] . "</td>
        <td align='left'>" . $row['jobno'] . "</td>
        <td align='left'>" . $row['serialno'] . "</td>
        <td align='right'>" . $row['rate'] . "</td>
        <td align='center'>" . number_format($row['cur_qty'], 0, ".", ",") . "</td>
        <td align='right'>" . number_format(($row['rate']*$row['cur_qty']), 2, ".", ",") . "</td>
        </tr>";
        $i = $i + 1;
        $rate=$row['rate']*$row['cur_qty'];
        $totrate=$totrate+$rate;
    }
    
    $ResponseXML .= "   </table>]]></sales_table>";
    
    $ResponseXML .= "<totamount><![CDATA[" . $totrate . "]]></totamount>";
    
    $ResponseXML .= "<invno><![CDATA[" . $REF_NO . "]]></invno>";
    $ResponseXML .= "<tmp_no><![CDATA[" . $_GET['invno'] . "]]></tmp_no>";
    $ResponseXML .= "<to_dep><![CDATA[" . $dep_to . "]]></to_dep>";
    $ResponseXML .= "<sdate><![CDATA[" . $sdate . "]]></sdate>";
    $ResponseXML .= " </salesdetails>";
    echo $ResponseXML;
}

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

    $ResponseXML = "";
    $ResponseXML .= "<salesdetails>";
    try {
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $conn->beginTransaction();
        
        if ($_SESSION['company'] == "") { 
            exit("Please Login");
        }
        if ($_SESSION['CURRENT_USER'] == "") { 
            exit("Please Login");
        }


        $sql = "select REF_NO,cancel from s_ginmas where tmp_no ='" . $_GET['tmpno'] . "' and cancel='1'";
        $result = $conn->query($sql);
        if ($row = $result->fetch()) {
            exit("Already Cancel Record...!!!");
        }
        
        $sql = "select REF_NO,cancel from s_ginmas where tmp_no ='" . $_GET['tmpno'] . "' and TYPE='DAG GIN TRANSFER'";
        $result = $conn->query($sql);
        if ($row = $result->fetch()) {

            $sql = "select REF_NO , dep_to,tmp_no from s_ginmas where tmp_no ='" . $_GET['tmpno'] . "'";
            foreach ($conn->query($sql) as $row1) {
                
                $sql_tmp = "select dep_from,str_code,jobno,serialno,type,tmp_no from tmp_gin_data_multi where tmp_no='" . $row1["tmp_no"] . "' and type='DAG GIN TRANSFER' group by dep_from";
                // echo $sql_tmp;
                $result_tmp = $conn->query($sql_tmp);
                $row_tmp = $result_tmp->fetch();
                
                $sql_gtrn = "select * from s_gintrn where REFNO='" . $row1["REF_NO"] . "' and TYPE='DAG GIN TRANSFER'";
                $result_gtrn = $conn->query($sql_gtrn);
                $row_gtrn = $result_gtrn->fetch();

                $sql = "select * from s_trn where REFNO='" . $row1["REF_NO"] . "' and LEDINDI='GINRN' and TYPE='DAG GIN TRANSFER'";
                foreach ($conn->query($sql) as $row) {
                    $sql1 = "update s_submas set QTYINHAND=QTYINHAND-" . $row["QTY"] . " where STK_NO='" . $row["STK_NO"] . "' and STO_CODE='" . $row["DEPARTMENT"] . "'";
                    $result1 = $conn->query($sql1);
                }

                $sql = "select * from s_trn where REFNO='" . $row1["REF_NO"] . "' and LEDINDI='GININ' and TYPE='DAG GIN TRANSFER'";
                foreach ($conn->query($sql) as $row) {
                    $sql1 = "update s_submas set QTYINHAND=QTYINHAND+" . $row["QTY"] . " where STK_NO='" . $row["STK_NO"] . "' and STO_CODE='" . $row["DEPARTMENT"] . "'";
                    $result1 = $conn->query($sql1);
                }
                
                $sql1 = "update dag_item set department='" . $row_tmp["dep_from"] . "',flag='" . $row_gtrn['OLD_FLAG'] . "' where STK_NO='" . $row_tmp["str_code"] . "' and jobno='" . $row_tmp["jobno"] . "' and serialno='" . $row_tmp["serialno"] . "' ";
                // echo $sql1;
                $result1 = $conn->query($sql1);
                
                $sql1 = "update  s_gintrn set CANCEL='1' where tmp_no ='" . $_GET['tmpno'] . "' and TYPE='DAG GIN TRANSFER'";
                $result1 = $conn->query($sql1);

                $sql1 = "delete from s_trn where REFNO='" . $row1["REF_NO"] . "' and TYPE='DAG GIN TRANSFER'";
                $result1 = $conn->query($sql1);

                $sql1 = "update  s_ginmas set cancel='1' where tmp_no ='" . $_GET['tmpno'] . "' and TYPE='DAG GIN TRANSFER'";
                $result1 = $conn->query($sql1);
            }
            
            $sqllog = "insert into entry_log(refno, username, docname, trnType, stime, sdate,company) values
            ('" . trim($_POST['refno']) . "', '" . $_SESSION["CURRENT_USER"] . "', 'DAG GIN TRANSFER', 'CANCEL', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "','".$row['department']."')";
            $resultlog = $conn->query($sqllog);
            echo "Cancelled";

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








