<?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"] == "brandsession") {
 
    $_SESSION["brand"]=$_GET["brand"];
    $_SESSION["brand"]="";
 
} 

if ($_GET["Command"] == "new_inv") {
    $_SESSION["department"]="";
    
    $sql = "select SPINV2,VATTYPE,PAYTYPE from invpara where flag='".$_SESSION['company']."'"; 
    $result = $conn->query($sql);
    $row = $result->fetch();
    
    $sql1 = "select * from s_stomas where CODE='".$_GET['department']."'"; 
    $result1 = $conn->query($sql1);
    $row1 = $result1->fetch();
    
    $vattype=$row['VATTYPE'];
    $paytype=$row['PAYTYPE'];
    
    
    if($row['PAYTYPE']=='CA'){ 
        $sql = "select SPINV2 from invpara where flag='".$_SESSION['company']."'"; 
        $result = $conn->query($sql);
        $row = $result->fetch();
        
        $sql1 = "select * from s_stomas where CODE='".$_GET['department']."'"; 
        $result1 = $conn->query($sql1);
        $row1 = $result1->fetch();
        
        
        $tmpinvno = "000000" . $row["SPINV2"] ;
        $lenth = strlen($tmpinvno);
        //  . "/" . $_GET["salesrep"]
        $invno = trim("".$row1['SCODE']."/INV/CB/") . substr($tmpinvno, $lenth - 6);
     
        $sql = "Select SPINV2 from tmpinvpara where flag='".$_SESSION['company']."'"; 
        $result = $conn->query($sql);
        $row = $result->fetch();
    
        $tono = $row['SPINV2'];
    
        $sql = "delete from tmp_inv_data where tmp_no='" . $tono . "'";
        $result = $conn->query($sql);
    
        $sql = "update tmpinvpara set SPINV2=SPINV2+1 where flag='".$_SESSION['company']."'"; 
        $result = $conn->query($sql);
        $_SESSION["brand"]=$_GET["brand"];
         
    }else{
        $sql = "select SPINV from invpara where flag='".$_SESSION['company']."'"; 
        $result = $conn->query($sql);
        $row = $result->fetch();
        
        $sql1 = "select * from s_stomas where CODE='".$_GET['department']."'"; 
        $result1 = $conn->query($sql1);
        $row1 = $result1->fetch();
        
        
        $tmpinvno = "000000" . $row["SPINV"];
        $lenth = strlen($tmpinvno);
        $invno = trim("".$row1['SCODE']."/INV/CRM/") . substr($tmpinvno, $lenth - 6);
        
        $sql = "Select SPINV from tmpinvpara where flag='".$_SESSION['company']."'"; 
        $result = $conn->query($sql);
        $row = $result->fetch();
    
        $tono = $row['SPINV'];
    
        $sql = "delete from tmp_inv_data where tmp_no='" . $tono . "'";
        $result = $conn->query($sql);
    
        $sql = "update tmpinvpara set SPINV=SPINV+1 where flag='".$_SESSION['company']."'"; 
        $result = $conn->query($sql);
        $_SESSION["brand"]=$_GET["brand"];
        
    }
    
    $_SESSION["brand"]="";
    $_SESSION["department"]=$_GET['department'];
    

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

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

    if ($_GET['paymethod'] == "CA") {
        $sql = "select SPINV2 from invpara where flag='".$_SESSION['company']."'"; 
        $result = $conn->query($sql);
        $row = $result->fetch();
        
         $sql1 = "select * from s_stomas where CODE='".$_GET['department']."'"; 
        $result1 = $conn->query($sql1);
        $row1 = $result1->fetch();
        
        $tmpinvno = "000000" .$row["SPINV2"];
        $lenth = strlen($tmpinvno);
        //  . "/" . $_GET["salesrep"]
        $invno = trim("".$row1['SCODE']."/INV/CB/") . substr($tmpinvno, $lenth - 6);
      
    }else{
        $sql = "select SPINV from invpara where flag='".$_SESSION['company']."'"; 
        $result = $conn->query($sql);
        $row = $result->fetch();
        
         $sql1 = "select * from s_stomas where CODE='".$_GET['department']."'"; 
        $result1 = $conn->query($sql1);
        $row1 = $result1->fetch();
        
        $tmpinvno = "000000" . $row["SPINV"];
        $lenth = strlen($tmpinvno);
        $invno = trim("".$row1['SCODE']."/INV/CRM/") . substr($tmpinvno, $lenth - 6);
        
        
    }
    

    $ResponseXML = "";
    $ResponseXML .= "<salesdetails>";
    $ResponseXML .= "<invno><![CDATA[" . $invno . "]]></invno>"; 
    $ResponseXML .= "</salesdetails>";
    echo $ResponseXML;
}

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


    $sql = "select last_update from invpara where flag='".$_SESSION['company']."'"; 
    $result = $conn->query($sql);
    $row = mysqli_fetch_array($result);

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

        $sqlinv = "select * from s_salma where REF_NO='" . $_GET["invno"] . "' ";
        $resultinv = $conn->query($sqlinv);
        if ($rowinv = $resultinv->fetch()) {
            if ($rowinv['CANCELL'] == "1") {
                exit("Already Canceled..");
            }
            // if (date("Y-m") != date("Y-m", strtotime($rowinv["sdate1"]))) {
            //     //if (date("Y-m") == date("Y-m", strtotime($rowinv["SDATE"]))) {
            //     echo 'Not in Current Month Can not Cancel';
            //     exit();
            // }
            if (($rowinv["TOTPAY"] == 0) and ( $rowinv["SDATE"] > $row["last_update"])) {

                $sql2 = "update s_salma set CANCELL='1',note='".$_GET["remarks"]."' where REF_NO='" . $_GET["invno"] . "'";
                $result2 = $conn->query($sql2);

                $sql2 = "update vendor set CUR_BAL=CUR_BAL-" . $rowinv["GRAND_TOT"] . " where CODE='" . $_GET["customer_code"] . "'";
                $result2 = $conn->query($sql2);


                $sql2 = "update br_trn set credit=credit-" . $rowinv["GRAND_TOT"] . " where cus_code='" . $_GET["customer_code"] . "' and Rep='" . $_GET["salesrep"] . "'";
                $result2 = $conn->query($sql2);
                
                $sql2="delete from ledger WHERE l_refno='" . $_GET["invno"] . "'";
                $resul2 = $conn1->query($sql2);


                $sqltmp = "select * from s_invo where REF_NO='" . $_GET['invno'] . "' ";
                $resulttmp = $conn->query($sqltmp);
                while ($rowtmp = $resulttmp->fetch()) {
                    $sql2 = "update s_invo set CANCELL='1' where REF_NO='" . $_GET['invno'] . "'";
                    $resul2 = $conn->query($sql2);

                    $sql2 = "update s_mas set QTYINHAND=QTYINHAND+" . $rowtmp["QTY"] . " where STK_NO='" . $rowtmp['STK_NO'] . "'";
                    $resul2 = $conn->query($sql2);

                    $sql2 = "update s_submas set QTYINHAND=QTYINHAND+" . $rowtmp["QTY"] . " where STO_CODE='" . $rowinv["DEPARTMENT"] . "' and STK_NO='" . $rowtmp['STK_NO'] . "'";
                    $resul2 = $conn->query($sql2);

                    $sql2 = "delete from s_trn where REFNO='" . $_GET['invno'] . "'";
                    $resul2 = $conn->query($sql2);

                    $sql2 = "delete from s_trn_all where REFNO='" . $_GET['invno'] . "'";
                    $resul2 = $conn->query($sql2);

                    $sql2 = "delete from s_trn_stores where REFNO='" . $_GET['invno'] . "'";
                    $resul2 = $conn->query($sql2);


                    $sql2 = "delete from s_led where REF_NO='" . $_GET['invno'] . "'";
                    $resul2 = $conn->query($sql2);
                }


                $sql2 = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values ('" . $_GET['invno'] . "', '" . $_SESSION["CURRENT_USER"] . "', 'Invoice', 'Cancel', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";
                $result2 = $conn->query($sql2);
                echo "Cancelled";
                $conn->commit();
            } else {
                echo "Already Paid";
                exit();
            }
        } else {
            echo "Can't Cancel";
            exit();
        }
    } catch (Exception $e) {
        $conn->rollBack();
        echo $e;
    }
}


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

    if ($_GET['Command1'] == "add") {

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

        $actual_selling = str_replace(",", "", $_GET["rate"]);
        $qty = str_replace(",", "", $_GET["qty"]);
        $subtotal = str_replace(",", "", $_GET["subtotal"]);
        $subtotal1 = str_replace(",", "", $_GET["subtotal"]);


        $subtotal2 = $rate - ($rate * $_GET["discountper"] / 100);

        if ($subtotal1 = !$subtotal2) {
            if (($subtotal1 != 1) && ($subtotal2 != 0)) {
                //echo $subtotal1; 
                exit('Error');
            }
        }
 
        
             $sql = "select QTYINHAND from s_submas where STK_NO='" . $_GET['itemcode']  . "' AND STO_CODE='" . $_GET["department"] . "'";
            $result = $conn->query($sql);
            if ($row = $result->fetch()) {
                if ($row["QTYINHAND"] > $qty) {
                     $sql = "Insert into tmp_inv_data (str_invno, str_code, str_description, cur_rate, cur_qty, dis_per,  cur_subtot,  brand, tmp_no,actual_selling)values 
                    ('" . $_GET['invno'] . "', '" . $_GET['itemcode'] . "', '" . $_GET['item'] . "', " . $rate . ", " . $qty . ", " . $_GET["discountper"] . ", " . $subtotal . ", '" . $_GET['brand'] . "', '" . $_GET['tmpno'] . "'," . $rate . ") ";
            
                    $result = $conn->query($sql);
                }else{
                    // $sql_mas = "Select * from s_mas where STK_NO='" .$_GET['itemcode'] . "' ";
                    // $resultmas = $conn->query($sql_mas);
                    // $rowmas = $resultmas->fetch();
                    // if($rowmas['CAT']=='None Stock Item'){
                         $sql = "Insert into tmp_inv_data (str_invno, str_code, str_description, cur_rate, cur_qty, dis_per,  cur_subtot,  brand, tmp_no,actual_selling)values 
                        ('" . $_GET['invno'] . "', '" . $_GET['itemcode'] . "', '" . $_GET['item'] . "', " . $rate . ", " . $qty . ", " . $_GET["discountper"] . ", " . $subtotal . ", '" . $_GET['brand'] . "', '" . $_GET['tmpno'] . "'," . $rate . ") ";
                
                        $result = $conn->query($sql);
                    // }else{
                    //     exit('Under  Stock In Your Department!!!!');
                    // }
                     
                }
            }
    }

    if ($_GET['Command1'] == "del") {

        $sql = "delete from tmp_inv_data where id='" . $_GET['id'] . "'";
        $result = $conn->query($sql);
    }
    
    if ($_GET['Command1'] == "editrate") {
        $actual_selling = str_replace(",", "", $_GET["rate"]);
        $qty = str_replace(",", "", $_GET["qty"]);
        $subtotal = str_replace(",", "", $_GET["subtotal"]);
        $subtotal1 = str_replace(",", "", $_GET["subtotal"]);


        $subtotal2 = $rate - ($rate * $_GET["discountper"] / 100);
        if ($subtotal1 = !$subtotal2) {
            if (($subtotal1 != 1) && ($subtotal2 != 0)) { 
                exit('Error');
            }
        }

        $sql = "update tmp_inv_data set cur_rate='".$actual_selling."',actual_selling='".$actual_selling."',cur_qty='".$qty."',dis_per='".$_GET["discountper"]."',cur_subtot='".$subtotal."' where id='" . $_GET['id'] . "'" ;  
        $result = $conn->query($sql);
    }


 

    $ResponseXML = "";
    $ResponseXML .= "<salesdetails>";
    $ResponseXML .= "<sql><![CDATA[" . $sql . "]]></sql>";
    $ResponseXML .= "<sales_table><![CDATA[ <table class=\"table\">     
    <tr>
    <th  class='col-md-1'>Item Code</th>
    <th  size='15'>&nbsp;</th>
    <th  class='col-md-5'>Description</th>
    <th  class='col-md-2'>Price</th>
    <th  class='col-md-1'>Qty</th>
    <th class='col-md-1'>Dis (%)</th>
    <th class='col-md-2'>Unit Total</th>
    <th  size='15'>#</th>
    <th  size='15'></th>
                
                
    </tr>";

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

        $id = "id" . $i;
        $code = "code" . $i;
        $itemd = "itemd" . $i;
        $rate = "rate" . $i;

        $qty = "qty" . $i;
        $discountper = "discountper" . $i;
        $subtotal = "subtotal" . $i;
        $discount = "discount" . $i;

        $sql_mas = "Select * from s_mas where STK_NO='" . $row['str_code'] . "' and BRAND_NAME='" . $_GET['brand'] . "'";
        $resultmas = $conn->query($sql_mas);
        $rowmas = $resultmas->fetch();

        $dt = date('Y-m-d', strtotime(date('Y-m-d') . ' - 90 days'));

        $sql1 = "Select sum(REC_QTY) as stk from s_purtrn where STK_NO='" . $row['str_code'] . "' and CANCEL='0' and SDATE>'" . $dt . "' ";
        $result1 = $conn->query($sql1);
        $row1 = $result1->fetch();

        $mnewstk = 0;
        $unsold = 0;
        if (is_null($row1["stk"]) == false) {
            $mnewstk = $row1["stk"];
        }

        if ($rowmas["QTYINHAND"] > $row1["stk"]) {
            $unsold = $rowmas["QTYINHAND"] - $mnewstk;
        }

        $bgcolour = "";

        if ($unsold > 0) {
            $bgcolour = "green";
        }

        $ResponseXML .= "<tr>                              
        <td onClick=\"disp_qty('" . $row['str_code'] . "');\" class='hidden' ><div id='" . $id . "'  >" . $row['id'] . "</div></td>
        <td onClick=\"disp_qty('" . $row['str_code'] . "');\"><div id='" . $code . "'>" . $row['str_code'] . "</div></td>
        <td size='15'>&nbsp;</td>";
        if ($bgcolour == "") {
            $ResponseXML .= "<td onClick=\"disp_qty('" . $row['str_code'] . "');\"><div id='" . $itemd . "'>" . $row['str_description'] . "</div></td>";
        } else {
            $ResponseXML .= "<td bgcolor=\" . $bgcolour . \" onClick=\"disp_qty('" . $row['str_code'] . "');\"><div id='" . $itemd . "'>" . $row['str_description'] . "</div></td>";
        }
        $ResponseXML .= "<td align=right onClick=\"disp_qty('" . $row['str_code'] . "');\">
        <input type='number' placeholder='Rate'  id='" . $rate . "' value=" . $row['cur_rate'] . " disabled onblur=\"calc1_table('".$i."');\"  class='form-control'> </td>
        <td align=right onClick=\"disp_qty('" . $row['str_code'] . "');\">
        <input type='number' placeholder='Qty'  id='" . $qty . "' value=" . $row['cur_qty'] . " disabled onblur=\"calc1_table('".$i."');\"   class='form-control'> </td>
        <td align=right onClick=\"disp_qty('" . $row['str_code'] . "');\">
        <input type='number' placeholder='Qty'  id='" . $discountper . "' value=" . number_format($row['dis_per'], 2, ".", ",")   . " onblur=\"calc1_table('".$i."');\"   class='form-control'> <input type=\"hidden\"  name=\"" . $discount . "\" id=\"" . $discount . "\" size=\"15\"  value=\"" . number_format($row['cur_rate'], 2, ".", ",") . "\"    /></td>
        <td align=right onClick=\"disp_qty('" . $row['str_code'] . "');\"><div id='" . $subtotal . "'>" . number_format($row['cur_subtot'], 2, ".", ",") . "</div></td>
        <td><a class=\"btn btn-danger btn-xs\"  onClick=\"del_item('" . $row['id'] . "');\"><span class='fa fa-remove'></a></td>";

        $sql = "select QTYINHAND from s_submas where STK_NO='" . $row['str_code'] . "' AND STO_CODE='" . $_GET["department"] . "'";
        $result_q = $conn->query($sql);
        if ($row_q = $result_q->fetch()) {
            $qty = number_format($row_q['QTYINHAND'], 0, ".", ",");
        } else {
            $qty = 0;
        }


        $mtot = $mtot + $row['cur_subtot'];
        $mtot1 = $mtot1 + $row['cur_subtot'];




        $mtot2 = $mtot2 + ($row['cur_rate'] * $row['cur_qty']);


        $ResponseXML .= "<td>" . $qty . "</td>



        </tr>";
        $i = $i + 1;
    }
    $ResponseXML .= "   </table>]]></sales_table>";

    $ResponseXML .= "<item_count><![CDATA[" . $i . "]]></item_count>";
    $ResponseXML .= "<Command1><![CDATA[" . $_GET['Command1'] . "]]></Command1>";

    $sql1 = "select vatrate from invpara  where flag='".$_SESSION['company']."'"; 
    $result1 = $conn->query($sql1);
    $row1 = $result1->fetch();

    if ($_GET['vatmethod'] != "non") {
        $mtot1 = $mtot1 / ( 1 + ($row1['vatrate'] / 100));
        //  $mtot1 = $mtot;
        //  $mtax = $mtot1*$row1['vatrate'] / 100;
    } else {
        $mtot1 = $mtot;
        $mtax = 0;
    }
    
    

    $mtax = $mtot - $mtot1;
    
    
    
    $mdis = $mtot2 - $mtot;
    $ResponseXML .= "<subtot><![CDATA[" . number_format($mtot1, 2, ".", "") . "]]></subtot>";
 

    $ResponseXML .= "<totdiscount><![CDATA[" . number_format($mdis, 2, ".", "") . "]]></totdiscount>";
    $ResponseXML .= "<tax><![CDATA[" . number_format($mtax, 2, ".", "") . "]]></tax>";
    $ResponseXML .= "<invtot><![CDATA[" . number_format($mtot, 2, ".", "") . "]]></invtot>";
    
    
    
    if ($_GET['paymethod'] == "CA") {
        $sql = "select SPINV2,vatrate from invpara where flag='".$_SESSION['company']."'"; 
        $result = $conn->query($sql);
        $row = $result->fetch();
        
        $sql1 = "select * from s_stomas where CODE='".$_GET['department']."'"; 
        $result1 = $conn->query($sql1);
        $row1 = $result1->fetch();
        
        
        $tmpinvno = "000000" . $row["SPINV2"];
        $lenth = strlen($tmpinvno);
        $invno = trim("".$row1['SCODE']."/INV/CB/") . substr($tmpinvno, $lenth - 6);
         
     
    }else{
        $sql = "select SPINV,vatrate from invpara where flag='".$_SESSION['company']."'"; 
        $result = $conn->query($sql);
        $row = $result->fetch();
        
        $sql1 = "select * from s_stomas where CODE='".$_GET['department']."'"; 
        $result1 = $conn->query($sql1);
        $row1 = $result1->fetch();
        
        $tmpinvno = "000000" . $row["SPINV"] ;
        $lenth = strlen($tmpinvno);
        $invno = trim("".$row1['SCODE']."/INV/CRM/") . substr($tmpinvno, $lenth - 6);
         
      
    }
    
    
    
    
    $ResponseXML .= "<invno><![CDATA[" . $invno . "]]></invno>";



    $ResponseXML .= " </salesdetails>";


    echo $ResponseXML;
}


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



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

    if (trim($_GET["brand"]) != "") {
        $sql = "Select * from s_mas where STK_NO='" . $_GET['itno'] . "' and BRAND_NAME='" . $_GET["brand"] . "'";
    } else {
        $sql = "Select * from s_mas where STK_NO='" . $_GET['itno'] . "'";
    }
    $result_q = $conn->query($sql);
    if ($row = $result_q->fetch()) {

        $ResponseXML .= "<str_code><![CDATA[" . $row['STK_NO'] . "]]></str_code>";
        $ResponseXML .= "<str_description><![CDATA[" . $row['DESCRIPT'] . "]]></str_description>";
        $ResponseXML .= "<actual_selling><![CDATA[" . $row['SELLING'] . "]]></actual_selling>";


        $SELLING = $row['SELLING'];

        $ResponseXML .= "<str_selpri><![CDATA[" . number_format($SELLING, 2, ".", "") . "]]></str_selpri>";

        $ResponseXML .= "<str_partno><![CDATA[" . $row["PART_NO"] . "]]></str_partno>";
        $ResponseXML .= "<costcenter><![CDATA[" . $row["costcenter"] . "]]></costcenter>";

        $department = trim(substr($_GET["department"], 0, 2));


        $sql = "select QTYINHAND from s_submas where STK_NO='" . $_GET['itno'] . "' AND STO_CODE='" . $department . "'";
        $result_q = $conn->query($sql);
        if ($row_q = $result_q->fetch()) {
            $qty = number_format($row_q['QTYINHAND'], 0, ".", "");
        } else {
            $qty = 0;
        }

        $ResponseXML .= "<qtyinhand><![CDATA[" . $qty . "]]></qtyinhand>";

        $ResponseXML .= "<str_status><![CDATA[yes]]></str_status>";
    } else {
        $ResponseXML .= "<str_status><![CDATA[no]]></str_status>";
    }




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

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

    try {


        $insuf_qty = "";
        $sql = "select * from tmp_inv_data where tmp_no='" . $_GET["tmpno"] . "'";
        $i = 0;
        foreach ($conn->query($sql) as $rowtmp1) {
            $sql = "select QTYINHAND from s_submas where STK_NO='" . $rowtmp1['str_code'] . "' AND STO_CODE='" . $_GET["department"] . "'";
            $result = $conn->query($sql);
            if ($row = $result->fetch()) {
                 $sql_mas = "Select * from s_mas where STK_NO='" .$rowtmp1['str_code'] . "' ";
                    $resultmas = $conn->query($sql_mas);
                    $rowmas = $resultmas->fetch();
                    if($rowmas['CAT'] !='None Stock Item'){
                        if ($row["QTYINHAND"] < $rowtmp1["cur_qty"]) {
                            $insuf_qty = $rowtmp1['str_code'];
                        }
                    }
            }
            $i = $i + 1;
        }

        if ($_SESSION['UserName'] == "") {
            exit("Invalid Session");
        }

        // if ($insuf_qty != "") {
        //     exit("Under Stock");
        // }

        if ($i == 0) {
            exit("Items Not Found");
        }

        $i = 0;
        $sqltmp1 = "select dis_per from tmp_inv_data where tmp_no='" . $_GET["tmpno"] . "' group by dis_per";
        foreach ($conn->query($sql) as $rowtmp1) {
            $i = $i + 1;
        }


        if ($i > 1) {
            exit("Error");
        }

        $sqlcumas = "select * from s_cusordmas where REF_NO='" . trim($_GET["ordno"]) . "' and INVNO !='0'";
        $resultcumas = $conn->query($sqlcumas);
        if ($rowsumas = $resultcumas->fetch()) { 
         exit("Already Invoiced Order No...."); 
     }

     $totdiscount = str_replace(",", "", $_GET["totdiscount"]);
     $subtot = str_replace(",", "", $_GET["subtot"]);
     $invtot = str_replace(",", "", $_GET["invtot"]);
     $tax = str_replace(",", "", $_GET["tax"]);
     $vat = "";


     if (($_GET["vatmethod"] == "vat") or ( $_GET["vatmethod"] == "svat") or ( $_GET["vatmethod"] == "evat")) {
        $vat = "1";
    } else {
        $vat = "0";
    }
    $svat = 0;
    if ($_GET["vatmethod"] == "svat") {
        $svat = str_replace(",", "", $_GET["tax"]);
    }



    $sql = "select SPINV,vatrate from invpara where flag='".$_SESSION['company']."'"; 
    $result = $conn->query($sql);
    $row = $result->fetch();


    if ($vat != "1") {
        if ($tax > 0) {
            exit('Error In Tax');
        }
    }


    $sqltmp1 = "select sum(cur_subtot) as cur_subtot from tmp_inv_data where tmp_no='" . $_GET["tmpno"] . "'";
    $result = $conn->query($sqltmp1);
    if ($rowclass = $result->fetch()) {
        // if (number_format($rowclass['cur_subtot'], 2, ".", "") != ($invtot-$tax)) {
        //     exit('Error In Total');
        // }

        if ($vat == "1") {
            $mtot = number_format($rowclass['cur_subtot'] / (1 + $row['vatrate'] / 100), 2, ".", "");
            if ($mtot != $subtot) {
//                    exit("Error In Sub Total");
            }
            $mtot = $rowclass['cur_subtot'] - $mtot;
            if ($mtot = !$tax) {
                exit("Error in Tax");
            }
        }
    } else {
        exit("Error");
    }


    $sql = "select * from vendor where CODE = '" . trim($_GET["customercode"]) . "'";
    $result = $conn->query($sql);
    if ($row = $result->fetch()) {
        if ($row["blacklist"] == "1") {
//                $sqlapp = "select * from s_cusordmas where ref_no='" . trim($_GET["salesord1"]) . "'";
//                $resultapp = $conn->query($sqlapp);
//                if ($rowapp = $resultapp->fetch()) {
//                    if ($rowapp["approveby"] == "0") {
//                        exit("Invoice Facilitey Stoped for This Customer,Please Invoice For Approved Sales Order");
//                    } else {
//                        exit("Invoice Facilitey Stoped for This Customer,Please Invoice For Approved Sales Order");
//                    }
//                    $_SESSION["print"] = 0;
//                }
            exit("Invoice Facilitey Stoped for This Customer..!!!");
        }
    }






    $InvClass = "";
    $sqlclass = "select class from brand_mas where barnd_name='" . trim($_GET["brand"]) . "'";
    $result = $conn->query($sqlclass);
    if ($rowclass = $result->fetch()) {
        if (is_null($rowclass["class"]) == false) {
            $InvClass = $rowclass["class"];
        }
    }

    $sql_brtrn = "select * from br_trn where Rep='" . trim($_GET["salesrep"]) . "' and brand='" . trim($InvClass) . "' and cus_code='" . trim($_GET["customercode"]) . "'";
    $result_brtrn = $conn->query($sql_brtrn);
    $row_brtrn = $result_brtrn->fetch();
    $cuscat = trim($row_brtrn["CAT"]);

    $d1 = str_replace(",", "", $_GET["discount_org1"]);
    $d2 = str_replace(",", "", $_GET["discount_org2"]);
    $d3 = str_replace(",", "", $_GET["discount_org3"]);

    if ($d1 == "") {
        $d1 = 0;
    }
    if ($d2 == "") {
        $d2 = 0;
    }
    if ($d3 == "") {
        $d3 = 0;
    }


    $d = 100 - (100 - $d3) * (100 - $d2) * (100 - $d1) / 100;
        //===============================================

    $cre_balance = str_replace(",", "", $_GET["balance"]);



    $customername = str_replace("~", "&", $_GET["customername"]);
    $cus_address = str_replace("~", "&", $_GET["cus_address"]);

    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $conn->beginTransaction();
    
    if ($_GET['paymethod'] == "CA") {
        $sql = "select SPINV2,vatrate from invpara where flag='".$_SESSION['company']."'"; 
        $result = $conn->query($sql);
        $row = $result->fetch();
        
        $sql1 = "select * from s_stomas where CODE='".$_GET['department']."'"; 
        $result1 = $conn->query($sql1);
        $row1 = $result1->fetch();
        
        $tmpinvno = "000000" . $row["SPINV2"];
        $lenth = strlen($tmpinvno);
        $invno = trim("".$row1['SCODE']."/INV/CB/") . substr($tmpinvno, $lenth - 6);
        
         $sql = "update invpara set SPINV2 = SPINV2 +1 where flag='".$_SESSION['company']."'"; 
        $res_inv = $conn->query($sql);
     
    }else{
        $sql = "select SPINV,vatrate from invpara where flag='".$_SESSION['company']."'"; 
        $result = $conn->query($sql);
        $row = $result->fetch();
        
        $sql1 = "select * from s_stomas where CODE='".$_GET['department']."'"; 
        $result1 = $conn->query($sql1);
        $row1 = $result1->fetch();
        
        $tmpinvno = "000000" . $row["SPINV"];
        $lenth = strlen($tmpinvno);
        $invno = trim("".$row1['SCODE']."/INV/CRM/") . substr($tmpinvno, $lenth - 6);
        
         $sql = "update invpara set SPINV = SPINV +1 where flag='".$_SESSION['company']."'"; 
        $res_inv = $conn->query($sql);
      
    }
    
    

    $sqlisalma_q = "select REF_NO from s_salma where REF_NO='" . $invno . "'";
    $resultsalma_q = $conn->query($sqlisalma_q);
    if ($rowsalma_q = $resultsalma_q->fetch()) {
        $conn->rollBack();
        exit("Invoice No Already Exist !!!");
    }
    // $sqlisalma_q = "select REF_NO from s_salma where tmp_no='" . $_GET["tmpno"] . "'";
    // $resultsalma_q = $conn->query($sqlisalma_q);
    // if ($rowsalma_q = $resultsalma_q->fetch()) {
    //     $conn->rollBack();
    //     exit("Invoice No Already Exist !!!");
    // }
 
        $mvatrate = $row["vatrate"];
 
    $totpay1 = 1;

    $sql = "select * from tmp_inv_data where tmp_no='" . $_GET["tmpno"] . "'";
    foreach ($conn->query($sql) as $rowtmp) {
        $dis_per = $rowtmp["cur_rate"] * $rowtmp["cur_qty"] * $rowtmp["dis_per"] * 0.01;

        $sqlmas = "select * from s_mas where STK_NO='" . trim($rowtmp["str_code"]) . "'";
        $resultmas = $conn->query($sqlmas);
        $rowmas = $resultmas->fetch();

        if (trim($rowmas['type']) == "TBB") {
            $totpay1 = "0";
        }
        if (trim($rowmas['type']) == "TBR") {
            $totpay1 = "0";
        }
        if (trim($rowmas['type']) == "OTR") {
            $totpay1 = "0";
        }
        $sql_invo = "Insert into s_invo  (REF_NO, SDATE, STK_NO, DESCRIPT, PART_NO, COST, PRICE, QTY, DEPARTMENT, DIS_per, DIS_rs, REP, TAX_PER, BRAND, vatrate, Print_dis1, Print_dis2, Print_dis3, subtot, ret_qty, DEV, CANCELL, c_code, seri_no, ad,sdate1,type1) values "
        . "('" . $invno . "', '" . $_GET["invdate"] . "', '" . trim($rowtmp["str_code"]) . "', '" . trim($rowtmp["str_description"]) . "', '" . $rowmas["PART_NO"] . "', " . $rowmas["COST"] . ", " . $rowtmp["actual_selling"] . ", " . $rowtmp["cur_qty"] . ", '" . trim($_GET["department"]) . "', '" . $rowtmp["dis_per"] . "', " . $dis_per . ", '" . $_GET["salesrep"] . "', '" . $mvatrate . "', '" . $rowmas["BRAND_NAME"] . "', " . $mvatrate . ", " . $d1 . ", " . $d2 . ", " . $d3 . ", " . $rowtmp["cur_subtot"] . ", '0', '', '0', '" . $_GET["customercode"] . "', '', '" . $rowtmp["ad"] . "','" . $_GET["invdate"] . "','" . $_GET["type1"] . "' )";
        $result_invo = $conn->query($sql_invo);

        $act_selling=$rowtmp["actual_selling"]-($rowtmp["actual_selling"]*$rowtmp["dis_per"]/100);
        
        $sqls_trn = "Insert into s_trn (STK_NO, SDATE, REFNO, QTY, LEDINDI, DEPARTMENT, Dev, seri_no, SAL_EX, ACTIVE, DONO,sdate1,COST,SELLING) values
        ('" . trim($rowtmp["str_code"]) . "','" . $_GET["invdate"] . "','" . trim($invno) . "', " . $rowtmp["cur_qty"] . ", 'INV', '" . trim($_GET["department"]) . "', '" . $_SESSION['dev'] . "', '', '', '1', '','" . $_GET["invdate"] . "','" . $rowmas["COST"] . "','" . $act_selling . "')";
        $results_trn = $conn->query($sqls_trn);

        $sqls_mas = "update s_mas set QTYINHAND= QTYINHAND-" . $rowtmp["cur_qty"] . " where STK_NO='" . trim($rowtmp["str_code"]) . "'";
        $results_mas = $conn->query($sqls_mas);

        $sqls_submas = "update s_submas set QTYINHAND=QTYINHAND- " . $rowtmp["cur_qty"] . " where STK_NO= '" . trim($rowtmp["str_code"]) . "' and STO_CODE= '" . trim($_GET["department"]) . "'";
        $results_submas = $conn->query($sqls_submas);
    }

    $sqlrep = "select * from s_salrep where REPCODE = '" . trim($_GET["salesrep"]) . "'";
    $resultrep = $conn->query($sqlrep);
    if ($rowrep = $resultrep->fetch()) {
        $maindepart = $rowrep['RGROUP1'];
    } else {
        $maindepart = "";
    }

    $sql = "Insert into s_salma  (REF_NO, TRN_TYPE, SDATE, C_CODE, TEL,Brand, CUS_NAME, VAT, VAT_VAL, TYPE, DISCOU, AMOUNT, GRAND_TOT,  TOTPAY, ORD_NO, ORD_DA,  DEPARTMENT, SAL_EX, BTT, cre_pe, GST, DIS, DIS1, DIS2, SVAT, Account, TOTPAY1, REMARK, REQ_DATE, CANCELL, DEV, tmp_no, DIS_RUP, CASH, SETTLED, DES_CAT, Accname, Costcenter, RET_AMO, Comm, red, seri_no, points, LOCK1, deliin, vat_no, s_vat_no, C_ADD1,c_code1,transp,deli_name,deli_add,maindepartment,sdate1,company,veheno,jobno,millage,note,deli_date,type1,labour) values "
    . "('" . $invno . "', 'INV', '" . $_GET["invdate"] . "', '" . trim($_GET["customercode"]) . "', '" . $_GET["tel"] . "', '" . trim($_GET["brand"]) . "', '" . trim($customername) . "','" . $vat . "', " . $tax . ", '".$_GET["paymethod"]."'," . $totdiscount . ", " . $subtot . " , " . $invtot . ", 0, '" . trim($_GET["salesord1"]) . "', '" . $_GET["invdate"] . "', '" . trim($_GET["department"]) . "', '" . trim($_GET["salesrep"]) . "', " . $tax . ", " . $_GET["credper"] . " , " . $mvatrate . ", " . $d1 . ", " . $d2 . ", " . $d3 . ", '" . $svat . "', 'NOTAUTH', '" . $totpay1 . "',  '" . $invno . "', '" . $_GET["invdate"] . "', '0', '" . $_SESSION['dev'] . "', '" . $_GET["tmpno"] . "', 0, 0, '0', 'N', 'OFFICE', '" . $_GET['promotion'] . "', 0, 'N', '0', '', '0', '0', '', '" . trim($_GET["vat1"]) . "', '" . trim($_GET["vat2"]) . "', '" . $cus_address . "','" . $_GET['c_subcode'] . "','" . $_GET['delitype'] . "','" . $_GET['deli_name'] . "','" . $_GET['deli_address'] . "','" . $maindepart . "','" . $_GET["invdate"] . "','".$_SESSION['company']."','" . $_GET["vehino"] . "','" . $_GET["jobno"] . "','" . $_GET["millage"] . "', '" . $_GET["remarks"] . "','" . $_GET["invdate"] . "','" . $_GET["type1"] . "','" . $_GET["labour"] . "')";
    $resultsalma = $conn->query($sql);

    $sqlpara = "update vendor set temp_limit= '0'  where CODE='" . trim($_GET["customercode"]) . "'";
    $resultpara = $conn->query($sqlpara);

    $sqls_submas = "Insert into s_led(REF_NO, SDATE, C_CODE, AMOUNT, FLAG, DEPARTMENT,company) values
    ('" . trim($invno) . "', '" . $_GET["invdate"] . "', '" . trim($_GET["customercode"]) . "', " . $invtot . ", 'INV', '" . $_GET["department"] . "','".$_SESSION['company']."')";
    $results_submas = $conn->query($sqls_submas);

    $sqlbr_trn = "update br_trn set tmplmt= '0'   where cus_code='" . trim($_GET["customercode"]) . "' and brand='" . trim($InvClass) . "' and Rep='" . trim($_GET["salesrep"]) . "'";
    $resultbr_trn = $conn->query($sqlbr_trn);

    $sqlbr_trn = "update s_cusordmas set INVNO= '" . $invno . "' where  REF_NO='" . $_GET["ordno"] . "'";
    $resultbr_trn = $conn->query($sqlbr_trn);
    
    
    /////////// if cash invoice ////////////////////////
			
			if ($_GET["paymethod"]=="CA"){
				
				$sql = "select INV_RECNO from invpara where flag='".$_SESSION['company']."'"; 
        		 $result = $conn->query($sql);
                $row = $result->fetch();

        		$tmprecno = "00000" . $row["INV_RECNO"];
        		$lenth = strlen($tmprecno); 
        		$recno = trim("".$_SESSION['company']."/PR/ ") . substr($tmprecno, $lenth - 5);
        		
        		 
    
    
				$sql = "insert into s_crec(CA_REFNO, CA_DATE,INVNO, CA_CODE, CA_CASSH, CA_AMOUNT, overpay, FLAG, pay_type, CA_SALESEX, CANCELL, tmp_no, DEPARTMENT, cus_ref, AC_REFNO, TTDATE, DEV,company) values
	            ('" . $recno . "', '" . $_GET["invdate"] . "', '" . $invno . "', '" . trim($_GET["customercode"]) . "', " . $invtot . ", " . $invtot . ", 0, 'REC', 'Cash', '" . $_GET["salesrep"] . "', '0', '" . $_GET["tmpno"] . "', 'O', '0', '', '',  '" . $_SESSION['dev'] . "','".$_SESSION['company']."' )";
              	$result = $conn->query($sql); 
				
				$sql1 = "insert into s_sttr(ST_REFNO, ST_DATE, ST_INVONO, ST_PAID, ST_CHNO, st_chdate, ST_FLAG, st_days, ap_days, st_chbank, cus_code, DEV, del_days, deliin_days, deliin_amo, deliin_lock, department,company) values
	  ('" . $recno . "', '" . $_GET["invdate"] . "', '" . $invno . "', " . $invtot . ", '', '" . $_GET["invdate"] . "', 'CAS', '', '', '', '" . trim($_GET["customercode"]) . "', '" . $_SESSION['dev'] . "', 0, 0, 0, '0', 'O','".$_SESSION['company']."')";
                $result = $conn->query($sql1);
				
				$sql2 = "update s_salma set TOTPAY=TOTPAY + " . $invtot . " where REF_NO = '" . $invno . "'";
                $result = $conn->query($sql2);
				
				$sql2 = "update s_salma set CASH=CASH + " . $invtot . " where REF_NO = '" . $invno . "'";
                $result = $conn->query($sql2);
				
				$sql2 = "update invpara set INV_RECNO=INV_RECNO + 1  where flag='".$_SESSION['company']."'"; 
            	$result = $conn->query($sql2);
                                
                //-------------cash-gl                
                $fulltot = $invtot;
                
                

                $l_lmem = "Cash recieved from " . trim($customername).'-'.$invno;
                $ldate = $_GET["invdate"]; 
                
                 
                 $sql = "insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, l_flag2, l_flag3, l_yearfl, comcode, chno, recdate, l_year) values 
                    ('" . trim($recno) . "', '" . $ldate . "', '120100', " . $fulltot . ", 'REC', 'CRE', '" . $l_lmem . "', '0', 'R', 0, '" . $_SESSION['company'] . "', '', '0', " . date("Y") . ")";
                $result = $conn1->query($sql);    
                
                $sql = "insert into ledger(l_refno, l_date, l_code, l_amount, l_flag, l_flag1, l_lmem, l_flag2, l_flag3, l_yearfl, comcode, chno, recdate, l_year) values 
                    ('" . trim($recno) . "', '" . $ldate . "', '220504', " . $fulltot . ", 'REC', 'DEB', '" . $l_lmem . "', '0', 'R', 0, '" . $_SESSION['company'] . "', '', '0', " . date("Y") . ")";
                $result = $conn1->query($sql); 
                
                 
                
		//-------------cash-gl                                
				
			}
			

    $sqlbrand = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values ('" . trim($invno) . "', '" . $_SESSION["CURRENT_USER"] . "', 'Invoice', 'Save', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";
    $resultbrand = $conn->query($sqlbrand);

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

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

    try {
        $sql = "update s_salma set ORD_NO= '" . $_GET["salesord1"] . "',deli_name= '" . $_GET["deli_name"] . "',veheno= '" . $_GET["vehino"] . "',jobno= '" . $_GET["jobno"] . "',millage= '" . $_GET["millage"] . "',note= '" . $_GET["remarks"] . "', deli_add='" . $_GET["deli_address"] . "' where  REF_NO='" . $_GET["invno"] . "'";
        $result = $conn->query($sql);

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

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

    try {
        $sql = "update s_salma set cre_pe= '" . $_GET["credper"] . "' where  REF_NO='" . $_GET["invno"] . "'";
        $result = $conn->query($sql);

        $sqlentry = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values ('" . $_GET["invno"] . "', '" . $_SESSION["CURRENT_USER"] . "', 'Invoice', 'CredDays Update', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";
        $resultenrty = $conn->query($sqlentry);

        echo "C.P Updated";
    } catch (Exception $e) {
        $conn->rollBack();
        echo $e;
    }
}  

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


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

    try {
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $conn->beginTransaction();
  
        $sqlinv = "select * from vendor where NAME='" . $_GET["customer_name"] . "' and TELE1='" . $_GET["customer_tel"] . "' ";
        $resultinv = $conn->query($sqlinv);
        if ($rowinv = $resultinv->fetch()) {
            $msg= "Already Found Customer..!!!!";   
            $ResponseXML .= "<msg><![CDATA[" . $msg . "]]></msg>";
            $ResponseXML .= "</salesdetails>";
            echo $ResponseXML;
            exit(); 
        }
        
       
        
        $sql = "SELECT cus,VATTYPE FROM invpara where flag='".$_SESSION['company']."'";  
        $result = $conn->query($sql);
        $row = $result->fetch();
        
        $vattype=$row['VATTYPE'];
        $no = $row['cus'];
        $tmpinvno = "0000" . $no;
        $lenth = strlen($tmpinvno);
        $refno = trim("V/ ") . substr($tmpinvno, $lenth - 5);
        
        $sql_1 = "select * from vendor where CODE='" . $refno . "'  ";
        $result_1= $conn->query($sql_1);
        if ($row_1 = $result_1->fetch()) {
            $msg= "Error Result..";   
            $ResponseXML .= "<msg><![CDATA[" . $msg . "]]></msg>";
            $ResponseXML .= "</salesdetails>";
            echo $ResponseXML;
            exit(); 
        }
        
        if($vattype =="1"){
            if(($_GET['customer_vat_no'] == "")){
                $msg= "Please Enter Vat No";   
                $ResponseXML .= "<msg><![CDATA[" . $msg . "]]></msg>";
                $ResponseXML .= "</salesdetails>";
                echo $ResponseXML;
                exit(); 
            }
            
        }else if($vattype =="0"){
            if(($_GET['customer_vat_no'] != "") && ($_GET['customer_vat_no'] !="")){
                $msg= "Can't Add Vat Customer.Please Clear Vat No & Svat No";   
                $ResponseXML .= "<msg><![CDATA[" . $msg . "]]></msg>";
                $ResponseXML .= "</salesdetails>";
                echo $ResponseXML;
                exit(); 
            }
        }
        
        
        
 
       
        $txtCNAME = str_replace("~", "&", $_GET['customer_name']);
    
        
        $sql_data = "insert into vendor  (CODE, NAME, ADD1,TELE1,vatno, CAT, svatno,OPDATE) values 
          ('" . $refno . "', '" . $txtCNAME . "', '" . $_GET['customer_address'] . "', '" . $_GET['customer_tel'] . "', '" . $_GET['customer_vat_no'] . "', 'C', '" . $_GET['customer_svat_no'] . "','" . date("Y-m-d") . "')";
        $result_data = $conn->query($sql_data);
        
        $sql = "delete from vender_sub where c_code = '" . $refno . "' and c_main = '" . $refno . "'";
        $result = $conn->query($sql);


        $c_add = $_GET['customer_address'];
        $sql = "insert into vender_sub (c_code,c_name,c_add,c_tele,c_vatno,c_svatno,c_main,type) values
        ('" . $refno . "','" . $txtCNAME . "','" . $c_add . "','" . $_GET['customer_tel'] . "','" . $_GET['customer_vat_no'] . "','" . $_GET['customer_svat_no'] . "','" . $refno . "','M') ";
        $result = $conn->query($sql);
        
         $sql = "update invpara set cus = cus + 1"; 
         $result = $conn->query($sql);
         
         
        $sql2 = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values 
        ('" . $refno . "', '" . $_SESSION["CURRENT_USER"] . "', 'Customer', 'Save', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";
        $result2 = $conn->query($sql2);
        
        $msg ="Saved";
                
        $conn->commit();
            
         
    } catch (Exception $e) {
        $conn->rollBack();
        echo $e;
    }
    
    $sql_VEN= "select * from vendor where CODE='" . $refno . "' ";
    $result_VEN = $conn->query($sql_VEN);
    if ($row_VEN = $result_VEN->fetch()) {
        
        $ResponseXML .= "<customer_code><![CDATA[" . $row_VEN['CODE'] . "]]></customer_code>";
        $ResponseXML .= "<customer_name><![CDATA[" . $row_VEN['NAME'] . "]]></customer_name>";
        $ResponseXML .= "<customer_tel><![CDATA[" . $row_VEN['TELE1'] . "]]></customer_tel>";
        $ResponseXML .= "<customer_address><![CDATA[" . $row_VEN['ADD1'] . "]]></customer_address>";
        $ResponseXML .= "<customer_vat_no><![CDATA[" . $row_VEN['vatno'] . "]]></customer_vat_no>";
        $ResponseXML .= "<customer_svat_no><![CDATA[" . $row_VEN['svatno'] . "]]></customer_svat_no>";
    }else{
        $msg="Error Result.."; 
    }
         
    $ResponseXML .= "<msg><![CDATA[" . $msg . "]]></msg>";
    
 
    $ResponseXML .= "</salesdetails>";
    echo $ResponseXML;
}

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

 

    try {
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $conn->beginTransaction();
  
        $sqlinv = "select * from vendor where TELE1='" . $_GET["customer_tel"] . "' "; 
        $resultinv = $conn->query($sqlinv);
        if ($rowinv = $resultinv->fetch()) {
            exit('Telephone No Result Customer Found.Please Check...!!!');
        }else{
            
        }
        
       
       
            
         
    } catch (Exception $e) {
        $conn->rollBack();
        echo $e;
    }
    
    
}
