<?php

session_start();

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



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

date_default_timezone_set('Asia/Colombo');

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



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


    $_SESSION["insert"] = 1;

    $_SESSION["brand"] = $_GET["brand"]; 

    $sql = "Select ORD_NO from invpara where flag='".$_SESSION['company']."'";   
    $result = mysqli_query($GLOBALS['dbinv'], $sql);
    $row = mysqli_fetch_array($result);
    $tmpinvno = "000000" . $row["ORD_NO"];
    $lenth = strlen($tmpinvno);
    $invno = trim("".$_SESSION['company']."/PO/ ") . substr($tmpinvno, $lenth - 6);
    $_SESSION["invno"] = $invno;

    $sql = "Select ORD_NO from tmpinvpara where flag='".$_SESSION['company']."'";
    $result = mysqli_query($GLOBALS['dbinv'], $sql);
    $row = mysqli_fetch_array($result);
    $_SESSION["tmp_no_purord"] = "".$_SESSION['company']."/PO/" . $row["ORD_NO"];

     

    $sql1 = "update tmpinvpara set ORD_NO=ORD_NO+1 where flag='".$_SESSION['company']."'";
    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
    
    
     $sql1 = "delete from tmp_purord_data where tmp_no='" . $_SESSION["tmp_no_purord"] . "'";
    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
    
    
     $sql1 = "delete from tmp_purord_data where user='" . $_SESSION["CURRENT_USER"] . "'";
    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);


    echo $invno;
}


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

    $department = $_GET["department"];

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


    $sql = "delete from tmp_purord_data where str_code='" . $_GET['itemcode'] . "' and tmp_no='" . $_SESSION['tmp_no_purord'] . "' ";
    //$ResponseXML .= $sql;
    $result = mysqli_query($GLOBALS['dbinv'], $sql);

    //echo $_GET['rate'];
    //echo $_GET['qty'];

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


    $sql = "select * from s_mas where stk_no = '" . $_GET['itemcode'] . "'";
    $result = mysqli_query($GLOBALS['dbinv'], $sql);
    $row = mysqli_fetch_array($result);

    if (($row['active_t'] == "1") and ( $row['active_t1'] == "1")) {
        $msg = "Item Locked";
    } else {
        $sql = "Insert into tmp_purord_data (tmp_no, str_invno, str_code, str_description, partno, qty,rate,user)values 
				('" . $_SESSION["tmp_no_purord"] . "', '" . $_GET['invno'] . "', '" . $_GET['itemcode'] . "', '" . $_GET['item'] . "', '" . $_GET["partno"] . "', " . $qty . ", '" . $_GET["rate"] . "','" . $_SESSION["CURRENT_USER"] . "') ";
        $result = mysqli_query($GLOBALS['dbinv'], $sql);
    }
    $ResponseXML .= "<msg><![CDATA[" . $msg . "]]></msg>";
    $ResponseXML .= "<sales_table><![CDATA[ <table><tr>
                              <td width=\"140\"  background=\"\" ><font color=\"#FFFFFF\">Item Code</font></td>
                              <td width=\"680\"  background=\"\"><font color=\"#FFFFFF\">Description</font></td>
                              <td width=\"150\"  background=\"\"><font color=\"#FFFFFF\">Pattern</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Quantity</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Rate</font></td>
                             <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\"> </font></td>
                            </tr>";

    $qty=0;
    $sql = "Select * from tmp_purord_data where tmp_no='" . $_SESSION['tmp_no_purord'] . "'";
    $result = mysqli_query($GLOBALS['dbinv'], $sql);
    while ($row = mysqli_fetch_array($result)) {
        $ResponseXML .= "<tr>                              
                             <td align=\"center\">" . $row['str_code'] . "</a></td>
							 <td align=\"left\">" . $row['str_description'] . "</a></td>
							 <td align=\"center\">" . $row['partno'] . "</a></td>
							 <td align=\"right\">" . number_format($row['qty'], 2, ".", ",") . "</a></td>
							 <td align=\"right\">" . number_format($row['rate'], 2, ".", ",") . "</a></td>
							 <td align=\"center\"><img src=\"images/delete_01.png\" width=\"20\" height=\"20\" id=" . $row['str_code'] . "  name=" . $row['str_code'] . " onClick=\"del_item('" . $row['str_code'] . "');\"></td></tr>";
    $qty=$qty+$row['qty'];
    }

    $ResponseXML .= "   </table>]]></sales_table>";
    $ResponseXML .= "<qtytot><![CDATA[" . $qty . "]]></qtytot>";

    $ResponseXML .= " </salesdetails>";

    //	}	


    echo $ResponseXML;
}


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

    //$department=$_GET["department"];
    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";

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

    $_SESSION["update"] = 1;

    $sql = "select * from s_ordmas where REFNO='" . $_GET['invno'] . "' ";

    $result = mysqli_query($GLOBALS['dbinv'], $sql);
    if ($row = mysqli_fetch_array($result)) {
        $sql2 = "Select * from vendor where CODE='".$row['SUP_CODE']."'";
        $result2 = mysqli_query($GLOBALS['dbinv'], $sql2);
        $row2 = mysqli_fetch_array($result2);
        
        $ResponseXML .= "<REFNO><![CDATA[" . $row['REFNO'] . "]]></REFNO>";
        $ResponseXML .= "<SDATE><![CDATA[" . $row['SDATE'] . "]]></SDATE>";
        $ResponseXML .= "<SUP_CODE><![CDATA[" . $row['SUP_CODE'] . "]]></SUP_CODE>";
        $ResponseXML .= "<SUP_NAME><![CDATA[" . $row['SUP_NAME'] . "]]></SUP_NAME>";
        $ResponseXML .= "<add><![CDATA[" . $row2['ADD1'] . "]]></add>";
        $ResponseXML .= "<REMARK><![CDATA[" . $row['REMARK'] . "]]></REMARK>";
        $ResponseXML .= "<DEP_CODE><![CDATA[" . $row['DEP_CODE'] . "]]></DEP_CODE>";
        $ResponseXML .= "<DEP_NAME><![CDATA[" . $row['DEP_NAME'] . "]]></DEP_NAME>";
        $ResponseXML .= "<REP_CODE><![CDATA[" . $row['REP_CODE'] . "]]></REP_CODE>";
        $ResponseXML .= "<REP_NAME><![CDATA[" . $row['REP_NAME'] . "]]></REP_NAME>";
        $ResponseXML .= "<S_date><![CDATA[" . $row['S_date'] . "]]></S_date>";
        $ResponseXML .= "<LC_No><![CDATA[" . $row['LC_No'] . "]]></LC_No>";
        $ResponseXML .= "<pi_no><![CDATA[" . $row['pi_no'] . "]]></pi_no>";
        $ResponseXML .= "<Brand><![CDATA[" . $row['Brand'] . "]]></Brand>";
        $ResponseXML .= "<invno><![CDATA[" . $row['invno'] . "]]></invno>";
        $ResponseXML .= "<blno><![CDATA[" . $row['blno'] . "]]></blno>";
         $ResponseXML .= "<country><![CDATA[" . $row['COUNTRY'] . "]]></country>";
        
        $sql23 = "Select sum(ORD_QTY) as qty from s_ordtrn where REFNO='".$row['REFNO'] ."'";
        $result23 = mysqli_query($GLOBALS['dbinv'], $sql23);
        $row23= mysqli_fetch_array($result23);
        
        $ResponseXML .= "<qtytot><![CDATA[" . $row23['qty'] . "]]></qtytot>";
        
        
        
         $_SESSION["brand"] = $row['Brand']; 
        $_SESSION['tmp_no_purord'] = $row["tmp_no"];
    }

    $sql = "delete from tmp_purord_data where tmp_no='" . $_SESSION['tmp_no_purord'] . "' ";

    $result = mysqli_query($GLOBALS['dbinv'], $sql);

    $sql = "select * from s_ordtrn where REFNO='" . $_GET['invno'] . "' ";

    $result = mysqli_query($GLOBALS['dbinv'], $sql);
    while ($row = mysqli_fetch_array($result)) {
        $sql1 = "Insert into tmp_purord_data (tmp_no, str_invno, str_code, str_description, partno, qty,rate,user)values 
				('" . $_SESSION['tmp_no_purord'] . "', '" . $row['REFNO'] . "', '" . $row['STK_NO'] . "', '" . $row['DESCRIPT'] . "', '" . $row["partno"] . "', " . $row["ORD_QTY"] . ", '" . $row["RATE"] . "','".$_SESSION["CURRENT_USER"]."') ";

        $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
    }






    $ResponseXML .= "<sales_table><![CDATA[ <table><tr>
                              <td width=\"140\"  background=\"\" ><font color=\"#FFFFFF\">Item Code</font></td>
                              <td width=\"680\"  background=\"\"><font color=\"#FFFFFF\">Description</font></td>
                              <td width=\"100\"  background=\"\">Pattern</td>
                              <td width=\"100\"  background=\"\">Quantity</td> 
                              <td width=\"100\"  background=\"\">Rate</td> 
                            </tr>";


    $sql = "Select * from tmp_purord_data where tmp_no='" . $_SESSION['tmp_no_purord'] . "'";
    $result = mysqli_query($GLOBALS['dbinv'], $sql);
    while ($row = mysqli_fetch_array($result)) {
        $ResponseXML .= "<tr>                              
                             <td align=\"center\" >" . $row['str_code'] . "</a></td>
							 <td>" . $row['str_description'] . "</a></td>
							 <td >" . $row['partno'] . "</a></td>
							 <td align=\"right\">" . number_format($row['qty'], 0, ".", ",") . "</a></td>
							 <td align=\"right\">" . number_format($row['rate'], 2, ".", ",") . "</a></td>
							 <td align=\"center\"><img src=\"images/delete_01.png\" width=\"20\" height=\"20\" id=" . $row['str_code'] . "  name=" . $row['str_code'] . " onClick=\"del_item('" . $row['str_code'] . "');\"></td>
							 </tr>";
    }

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

    $sql = "select * from entry_log where refno ='" . $_GET['invno'] . "' order by id";
    $result = mysqli_query($GLOBALS['dbinv'], $sql);
    while ($row = mysqli_fetch_array($result)) {
        $log .= $row['username'] . "/" . $row['stime'] . "<br>";
    }

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

    $ResponseXML .= " </salesdetails>";

    //	}	


    echo $ResponseXML;
}


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


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


    $sql = "delete from tmp_purord_data where str_code='" . $_GET['code'] . "' and tmp_no='" . $_SESSION["tmp_no_purord"] . "' ";

    $result = mysqli_query($GLOBALS['dbinv'], $sql);

    $ResponseXML .= "<sales_table><![CDATA[ <table><tr>
                              <td width=\"140\"  background=\"\" ><font color=\"#FFFFFF\">Item Code</font></td>
                              <td width=\"680\"  background=\"\"><font color=\"#FFFFFF\">Description</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Pattern</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Quantity</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Rate</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\"> </font></td>
                            </tr>";

$qty=0;
    $sql = "Select * from tmp_purord_data where tmp_no='" . $_SESSION['tmp_no_purord'] . "'";
    $result = mysqli_query($GLOBALS['dbinv'], $sql);
    while ($row = mysqli_fetch_array($result)) {
        $ResponseXML .= "<tr>
                              
                             <td align=\"center\">" . $row['str_code'] . "</a></td>
							 <td align=\"left\">" . $row['str_description'] . "</a></td>
							 <td align=\"center\">" . $row['partno'] . "</a></td>
							 <td align=\"right\">" . $row['qty'] . "</a></td>
							 <td align=\"right\">" . $row['rate'] . "</a></td>
							 <td align=\"center\"><img src=\"images/delete_01.png\" width=\"20\" height=\"20\" id=" . $row['str_code'] . "  name=" . $row['str_code'] . " onClick=\"del_item('" . $row['str_code'] . "');\"></td></tr>";
   $qty=$qty+$row['qty'];
   }

    $ResponseXML .= "   </table>]]></sales_table>";
$ResponseXML .= "<qtytot><![CDATA[" . $qty . "]]></qtytot>";
    $ResponseXML .= " </salesdetails>";


    //	}	


    echo $ResponseXML;
}



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

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

    $sql = "select * from s_ordmas where REFNO='" . $_GET["invno"] . "'";
    $result = mysqli_query($GLOBALS['dbinv'], $sql);
    //echo $sql;

    if ($row = mysqli_fetch_array($result)) {
        // $sql1 = "delete from s_ordmas where REFNO='" . $_GET["invno"] . "'";
        // $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);

        // $sql1 = "delete from s_ordtrn where REFNO='" . $_GET["invno"] . "'";
        // $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
        
        exit('Already Saved No');
    } else {
        $refno=$_GET["invno"];
        
        $sql = "Select ORD_NO from invpara where flag='".$_SESSION['company']."'";
        $result = mysqli_query($GLOBALS['dbinv'], $sql);
        $row = mysqli_fetch_array($result);
        $tmpinvno = "000000" . $row["ORD_NO"];
        $lenth = strlen($tmpinvno);
        $invno = trim("".$_SESSION['company']."/PO/ ") . substr($tmpinvno, $lenth - 6);
        $_SESSION["invno"] = $invno;
        $refno=$invno;

        $sql1 = "update invpara set ORD_NO=ORD_NO+1 where flag='".$_SESSION['company']."'";
        $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
    }
    
    

    $sql1 = "select * from s_stomas where CODE='" . $_GET["department"] . "'";
    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
    $row1 = mysqli_fetch_array($result1);
    $dep_name = $row1["DESCRIPTION"];

    $sql1 = "select * from s_salrep where REPCODE='" . $_GET["salesrep"] . "'";
    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
    $row1 = mysqli_fetch_array($result1);
    $rep_name = $row1["Name"];
    
    

    $sql1 = "insert into s_ordmas(REFNO,COUNTRY, SDATE, SUP_CODE, SUP_NAME, REMARK, DEP_CODE, DEP_NAME, REP_CODE, REP_NAME, cancel, S_date, LC_No, pi_no, Brand, tmp_no,invno,blno,company) values ('" . $refno . "', '" . $_GET["country"] . "', '" . $_GET["invdate"] . "', '" . $_GET["supplier_code"] . "','" . $_GET["supplier_name"] . "','" . $_GET["remarks"] . "','" . $_GET["department"] . "','" . $dep_name . "','" . $_GET["salesrep"] . "','" . $rep_name . "', '0','" . $_GET["dte_shedule"] . "','" . $_GET["lc_no"] . "', '" . $_GET["pi_no"] . "', '" . $_GET["brand"] . "',  '" . $_SESSION['tmp_no_purord'] . "', '" . $_GET["invno1"] . "', '" . $_GET["blno"] . "','".$_SESSION['company']."')";
    //echo $sql1;
    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);


    $sql = "select * from tmp_purord_data where tmp_no='" . $_SESSION['tmp_no_purord'] . "'";
    $result = mysqli_query($GLOBALS['dbinv'], $sql);
    while ($row = mysqli_fetch_array($result)) {
        $sql1 = "insert into s_ordtrn(REFNO, SDATE, STK_NO, DESCRIPT, ORD_QTY,RATE, partno, CANCEL, tmp_no) values
        ('" . $refno . "', '" . $_GET["invdate"] . "', '" . $row["str_code"] . "','" . $row["str_description"] . "','" . $row["qty"] . "','" . $row["rate"] . "','" . $row["partno"] . "','0', '" . $_SESSION['tmp_no_purord'] . "')";
        //echo $sql1;
        $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);

        $sql = "select * from s_mas where stk_no = '" . $row['str_code'] . "'";
        $result_i = mysqli_query($GLOBALS['dbinv'], $sql);
        $row_i = mysqli_fetch_array($result_i);

        if ($row_i['active_t'] == "1") {
            $sql = "update s_mas set active_t1 = '1' where stk_no = '" . trim($row['str_code']) . "'";
            $result1 = mysqli_query($GLOBALS['dbinv'], $sql);
        }
    }
    //$_SESSION["print"]=1;
    
    
    $sql1 = "delete from tmp_purord_data where tmp_no='" . $_SESSION['tmp_no_purord'] . "'";
    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
    
     $sql1 = "delete from tmp_purord_data where user='" . $_SESSION["CURRENT_USER"] . "'";
    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);


    $sql2 = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values ('" . $refno . "', '" . $_SESSION["CURRENT_USER"] . "', 'Order', 'SAVE', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";
    $result = mysqli_query($GLOBALS['dbinv'], $sql2);

    echo "Saved";
}


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

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

    $sql = "select * from s_ordmas where REFNO='" . $_GET["invno"] . "'";
    $result = mysqli_query($GLOBALS['dbinv'], $sql);
    //echo $sql;

    if ($row = mysqli_fetch_array($result)) {
        
         
        
        if($row['cancel'] !='0'){
            exit('Already Entered or Cancel Arn Found...');
        }
       
        $sql1 = "delete from s_ordmas where REFNO='" . $_GET["invno"] . "'";
        $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);

        $sql1 = "delete from s_ordtrn where REFNO='" . $_GET["invno"] . "'";
        $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
    } 
     
    $invno = $_GET["invno"];
    $_SESSION["invno"] = $_GET["invno"];

    
   

    $sql1 = "select * from s_stomas where CODE='" . $_GET["department"] . "'";
    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
    $row1 = mysqli_fetch_array($result1);
    $dep_name = $row1["DESCRIPTION"];

    $sql1 = "select * from s_salrep where REPCODE='" . $_GET["salesrep"] . "'";
    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
    $row1 = mysqli_fetch_array($result1);
    $rep_name = $row1["Name"];

    

    $sql1 = "insert into s_ordmas(REFNO,COUNTRY, SDATE, SUP_CODE, SUP_NAME, REMARK, DEP_CODE, DEP_NAME, REP_CODE, REP_NAME, cancel, S_date, LC_No, pi_no, Brand, tmp_no,invno,blno,company) values 
    ('" .  $_GET["invno"] . "', '" . $_GET["country"] . "', '" . $_GET["invdate"] . "', '" . $_GET["supplier_code"] . "','" . $_GET["supplier_name"] . "','" . $_GET["remarks"] . "','" . $_GET["department"] . "','" . $dep_name . "','" . $_GET["salesrep"] . "','" . $rep_name . "', '0','" . $_GET["dte_shedule"] . "','" . $_GET["lc_no"] . "', '" . $_GET["pi_no"] . "', '" . $_GET["brand"] . "',  '" . $_SESSION['tmp_no_purord'] . "', '" . $_GET["invno1"] . "', '" . $_GET["blno"] . "','".$_SESSION['company']."')";
    //echo $sql1;
    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);


    $sql = "select * from tmp_purord_data where tmp_no='" . $_SESSION['tmp_no_purord'] . "'";
    $result = mysqli_query($GLOBALS['dbinv'], $sql);
    while ($row = mysqli_fetch_array($result)) {
        $sql1 = "insert into s_ordtrn(REFNO, SDATE, STK_NO, DESCRIPT, ORD_QTY,RATE, partno, CANCEL, tmp_no) values
        ('" . $_GET["invno"] . "', '" . $_GET["invdate"] . "', '" . $row["str_code"] . "','" . $row["str_description"] . "','" . $row["qty"] . "','" . $row["rate"] . "','" . $row["partno"] . "','0', '" . $_SESSION['tmp_no_purord'] . "')";
        //echo $sql1;
        $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);

        $sql = "select * from s_mas where stk_no = '" . $row['str_code'] . "'";
        $result_i = mysqli_query($GLOBALS['dbinv'], $sql);
        $row_i = mysqli_fetch_array($result_i);

        if ($row_i['active_t'] == "1") {
            $sql = "update s_mas set active_t1 = '1' where stk_no = '" . trim($row['str_code']) . "'";
            $result1 = mysqli_query($GLOBALS['dbinv'], $sql);
        }
    }
    //$_SESSION["print"]=1;
    
    
     $sql1 = "delete from tmp_purord_data where tmp_no='" . $_SESSION['tmp_no_purord'] . "'";
    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
    
     $sql1 = "delete from tmp_purord_data where user='" . $_SESSION["CURRENT_USER"] . "'";
    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);


    $sql2 = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values ('" . $_GET["invno"] . "', '" . $_SESSION["CURRENT_USER"] . "', 'Po Order', 'Updated', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";
    $result = mysqli_query($GLOBALS['dbinv'], $sql2);

    echo "Updated";
}


if ($_GET["Command"] == "cancel_inv") {
    $sql1 = "update s_ordmas set cancel='1',REMARK='" . $_GET["remarks"] . "' where REFNO='" . $_GET["invno"] . "'";
    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);

    $sql1 = "update s_ordtrn set CANCEL='1' where REFNO='" . $_GET["invno"] . "'";
    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
    
     $sql2 = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values ('" . $_GET["invno"] . "', '" . $_SESSION["CURRENT_USER"] . "', 'Po Order', 'Canceled', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";
    $result = mysqli_query($GLOBALS['dbinv'], $sql2);
    
    
    echo "Canceled!";
}

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

    echo $_SESSION["print"];
}


if ($_GET["Command"] == "tmp_crelimit") {
    //echo "abc";
    $crLmt = 0;
    $cat = "";

    $rep = trim(substr($_GET["Com_rep1"], 0, 5));

    $sql = "select * from br_trn where rep='" . $rep . "' and cus_code='" . trim($_GET["txt_cuscode"]) . "' and brand='" . trim($_GET["cmbbrand1"]) . "'";
    echo $sql;
    $result = mysqli_query($GLOBALS['dbinv'], $sql);
    if ($row = mysqli_fetch_array($result)) {
        $crLmt = $row["credit_lim"];
        If (is_null($row["cat"]) == false) {
            $cat = trim($row["cat"]);
        } else {
            $crLmt = 0;
        }
    }
}
?>