<?php

session_start();

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

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

date_default_timezone_set('Asia/Colombo');

if (isset($_GET["Command"])) {
    if ($_GET["Command"] == "get_bank") {
        $sql = "select * from bankmas where bcode='" . $_GET["bankcode"] . "'";
        $result = mysqli_query($GLOBALS['dbinv'], $sql);
        if ($row = mysqli_fetch_array($result)) {
            echo $row["bname"];
        }
    }

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

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

        $sql = mysqli_query($GLOBALS['dbinv'], "Select * from vendor where CODE='" . $_GET['custno'] . "'") or die(mysqli_error());
        if ($row = mysqli_fetch_array($sql)) {
            $ResponseXML .= "<code><![CDATA[" . $row['CODE'] . "]]></code>";
            $ResponseXML .= "<name><![CDATA[" . $row['NAME'] . "]]></name>";
            $ResponseXML .= "<address><![CDATA[" . $row['ADD1'] . " " . $row['ADD2'] . "]]></address>";
        }

        $ResponseXML .= "<sales_table_acc><![CDATA[ <table  border=1  cellspacing=0>
								<tr><td width=\"80\"  background=\"\" ><font color=\"#FFFFFF\"></font></td>
								<td width=\"80\"  background=\"\" ><font color=\"#FFFFFF\">Invoice Date</font></td>
								<td width=\"200\"  background=\"\" ><font color=\"#FFFFFF\">Invoice No</font></td>
								<td width=\"80\"  background=\"\" ><font color=\"#FFFFFF\">Value</font></td>
								<td width=\"80\"  background=\"\" ><font color=\"#FFFFFF\">Paid</font></td>
								<td width=\"80\"  background=\"\" ><font color=\"#FFFFFF\">Overdue</font></td>
								<td width=\"80\"  background=\"\" ><font color=\"#FFFFFF\">Chq Pay</font></td>
								<td width=\"80\"  background=\"\" ><font color=\"#FFFFFF\">Chq Balance</font></td>
								<td width=\"80\"  background=\"\" ><font color=\"#FFFFFF\">Cash Pay</font></td>
								<td width=\"80\"  background=\"\" ><font color=\"#FFFFFF\">Inv Balance</font></td> 
								<td width=\"80\"  background=\"\" ><font color=\"#FFFFFF\">Select</font></td></tr>";

        //$sql = mysqli_query($GLOBALS['dbinv'],"Select C_CODE, REF_NO, deli_date, GRAND_TOT, TOTPAY, SDATE from s_salma where C_CODE='".$_GET['custno']."' and GRAND_TOT > TOTPAY and DEV='" & rsusercon!dev & "' and CANCELL='0' and SAL_EX='" & Trim(Left(Com_rep, 5)) & "' and Accname <> 'NON STOCK' ORDER BY SDATE") or die(mysqli_error());
        //	echo "Select C_CODE, REF_NO, deli_date, GRAND_TOT, TOTPAY, SDATE from s_salma where C_CODE='".$_GET['custno']."' and GRAND_TOT > TOTPAY and CANCELL='0' and SAL_EX='".$_GET["refno"]."' and Accname <> 'NON STOCK' ORDER BY SDATE";

        $i = 1;
        //	echo "Select C_CODE, REF_NO, deli_date, GRAND_TOT, TOTPAY, SDATE from s_salma where C_CODE='".$_GET['custno']."' and GRAND_TOT > TOTPAY and CANCELL='0'  ORDER BY SDATE";

        if ($_GET['refno'] == "") {
            $sql = mysqli_query($GLOBALS['dbinv'], "Select C_CODE, REF_NO, deli_date, GRAND_TOT, TOTPAY, SDATE from s_salma where C_CODE='" . $_GET['custno'] . "' and LOCK1='1' and GRAND_TOT > TOTPAY and (GRAND_TOT-TOTPAY)>0.5 and CANCELL='0' and DEV='" . $_SESSION["dev"] . "' ORDER BY SDATE") or die(mysqli_error());
        } else {

            $sql = mysqli_query($GLOBALS['dbinv'], "Select C_CODE, REF_NO, deli_date, GRAND_TOT, TOTPAY, SDATE from s_salma where C_CODE='" . $_GET['custno'] . "' and LOCK1='1' and GRAND_TOT > TOTPAY and (GRAND_TOT-TOTPAY)>0.5 and CANCELL='0' and SAL_EX='" . $_GET['refno'] . "' and DEV='" . $_SESSION["dev"] . "'  ORDER BY SDATE") or die(mysqli_error());
        }
        while ($row = mysqli_fetch_array($sql)) {

            $sdate = "sdate" . $i;
            $delidate = "delidate" . $i;

            $invval = "invval" . $i;

            if (is_null($row["deli_date"] == false)) {
                $ResponseXML .= "<tr><td><div id=" . $delidate . ">" . $row["deli_date"] . "</div></td>";
            } else {
                $ResponseXML .= "<tr><td><div id=" . $delidate . ">" . $row["SDATE"] . "</div></td>";
            }

            $ResponseXML .= "<td><div id=" . $sdate . ">" . $row["SDATE"] . "</div></td>";

            $j = $i + 1;

            $overdue = "overdue" . $i;

            $chq_pay = "chq_pay" . $i;
            $chq_pay_next = "chq_pay" . $j;

            $chq_balance = "chq_balance" . $i;
            $chq_balance_next = "chq_balance" . $j;

            $cash_pay = "cash_pay" . $i;
            $cash_pay_next = "cash_pay" . $j;

            $inv_balance = "inv_balance" . $i;
            $overdueamt = $row["GRAND_TOT"] - $row["TOTPAY"];
            //number_format($row["GRAND_TOT"]-$row["TOTPAY"], 2, ".")

            $invno = "invno" . $i;
            $select= "select" . $i;
            $ref = $row["REF_NO"];
            
            
            $ResponseXML .= "<td><div id=" . $invno . ">" . $row["REF_NO"] . "</div></td>
									 <td><div id=" . $invval . " align=\"right\">" . number_format($row["GRAND_TOT"], 2, ".", ",") . "</div></td>
									 <td ><div align=\"right\">" . number_format($row["TOTPAY"], 2, ".", ",") . "</div></td>
									 <td><input type=\"text\"  class=\"text_purchase3_right\" name=" . $overdue . " id=" . $overdue . " value=" . $overdueamt . " size=\"10\" disabled align=\"right\" /></td>
									 <td><input type=\"text\"  class=\"text_purchase3_right\" name=" . $chq_pay . " id=" . $chq_pay . " onBlur=\"calc_bal('$overdue', '$chq_pay', '$inv_balance', '$chq_balance', '$chq_balance_next', '$cash_pay', '$i', event);\"  onKeyPress=\"keyset('$chq_pay_next', event);\"   size=\"10\" align=\"right\"/></td>									
									 <td><input type=\"text\"  class=\"text_purchase3_right\" name=" . $chq_balance . " disabled id=" . $chq_balance . " onKeyPress=\"keysetvalue('$chq_balance','$chq_balance_next', '$chq_pay', event);\" size=\"10\" align=\"right\"  /></td>
									 <td><input type=\"text\"  class=\"text_purchase3_right\" name=" . $cash_pay . " id=" . $cash_pay . " onBlur=\"calc_bal_cash('$overdue', '$cash_pay_next', '$chq_pay', '$inv_balance', '$cash_pay', event);\" onKeyPress=\"keyset('$cash_pay_next', event);\" size=\"10\" align=\"right\"/></td>
									 <td><input type=\"text\"  class=\"text_purchase3_right\" name=" . $inv_balance . " id=" . $inv_balance . " disabled size=\"10\" align=\"right\"/></td> 
									 <td><input type=\"checkbox\"  class=\"text_purchase3_right\" name=" . $select . " id=" . $select . " onclick=\"selectupdate('$ref', event);\" size=\"10\" align=\"right\"/></td></tr>";
            $i = $i + 1;
        }

        $overdue = "overdue" . $i;

        $chq_pay = "chq_pay" . $i;
        $chq_pay_next = "chq_pay" . $j;

        $chq_balance = "chq_balance" . $i;
        $chq_balance_next = "chq_balance" . $j;

        $cash_pay = "cash_pay" . $i;
        $cash_pay_next = "cash_pay" . $j;

        $inv_balance = "inv_balance" . $i;
        $overdueamt = $row["GRAND_TOT"] - $row["TOTPAY"];
        $invno = "invno" . $i;
        $ResponseXML .= "<tr><td><td></td>
									 <td></td><div id=" . $invno . "></div></td>
									 <td></td>
									 <td></td>
									 <td><input type=\"text\"  class=\"text_purchase3_right\" name=" . $overdue . " id=" . $overdue . " value=" . $overdueamt . " size=\"10\"/></td>
									 <td><input type=\"text\"  class=\"text_purchase3_right\" name=" . $chq_pay . " id=" . $chq_pay . " onBlur=\"calc_bal('$overdue', '$chq_pay', '$inv_balance', '$chq_balance', '$chq_balance_next', '$cash_pay', '$i', event);\"  onKeyPress=\"keyset('$chq_pay_next', event);\"   size=\"10\"/></td>									
									 <td><input type=\"text\"  class=\"text_purchase3_right\" name=" . $chq_balance . " id=" . $chq_balance . " onKeyPress=\"keysetvalue('$chq_balance','$chq_balance_next', '$chq_pay', event);\" size=\"10\"/></td>
									 <td><input type=\"text\"  class=\"text_purchase3_right\" name=" . $cash_pay . " id=" . $cash_pay . " onKeyPress=\"calc_bal_cash('$overdue', '$cash_pay_next', '$chq_pay', '$inv_balance', '$cash_pay', event);\" size=\"10\"/></td>
									 <td><input type=\"text\"  class=\"text_purchase3_right\" name=" . $inv_balance . " id=" . $inv_balance . " disabled size=\"10\"/></td></tr>";

        $_SESSION["count"] = $i;
        $ResponseXML .= "   </table>]]></sales_table_acc>";
        $ResponseXML .= "<mcount><![CDATA[" . $_SESSION["count"] . "]]></mcount>";
        $ResponseXML .= "</salesdetails>";
        echo $ResponseXML;
    }
    
if ($_GET["Command"] == "selectupinvoice") {
    
     
    $sql = "update s_salma set LOCK1='1' where REF_NO='" . $_GET["inv"]."' ";
    // echo $sql;
    $result = mysqli_query($GLOBALS['dbinv'], $sql);
      
}

    if ($_GET["Command"] == "chng_chqno") {
        $sql = "update tmp_cash_chq set chqno='" . $_GET["chqno"] . "' where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "' and tmp_count=" . $_GET["i"];
        $result = mysqli_query($GLOBALS['dbinv'], $sql);
    }

    if ($_GET["Command"] == "chng_chqdate") {
        $sql = "update tmp_cash_chq set chqdate='" . $_GET["chqdate"] . "' where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "' and tmp_count=" . $_GET["i"];
        $result = mysqli_query($GLOBALS['dbinv'], $sql);
    }

    if ($_GET["Command"] == "chng_bank") {
        $sql = "update tmp_cash_chq set chqbank='" . $_GET["bank"] . "' where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "' and tmp_count=" . $_GET["i"];
        //echo $sql;
        $result = mysqli_query($GLOBALS['dbinv'], $sql);
    }

    if ($_GET["Command"] == "chng_chqamt") {
        $chqamt = str_replace(",", "", $_GET["chqamt"]);

        $sql = "update tmp_cash_chq set chqamt='" . $chqamt . "' where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "' and tmp_count=" . $_GET["i"];
        $result = mysqli_query($GLOBALS['dbinv'], $sql);

        $sql = "select sum(chqamt) as totamt from tmp_cash_chq where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "'";
        $result = mysqli_query($GLOBALS['dbinv'], $sql);
        $row = mysqli_fetch_array($result);
        echo $row["totamt"];
    }

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

        //echo "Regt=".$Regt."RegimentNo=".RegimentNo."Command=".$Command;addchq_cash_rec

        if ($_SESSION["tmp_no_cashrec"] != "") {
            if (date("Y-m-d", strtotime($_GET["chqdate"])) <= date("Y-m-d", strtotime($_GET["invdate"]))) {
                //$chqdate = DateAdd("d", 1, Date)
                $date = $_GET["invdate"];
                $date1 = date('Y-m-d', strtotime($date . ' + 1 days'));
            } else {
                $date1 = $_GET["chqdate"];
            }
            $date1 = $_GET["chqdate"];
            
            $sql = "delete from tmp_cash_chq where chqno='" . $_GET["chqno"] . "' and tmp_no='" . $_SESSION["tmp_no_cashrec"] . "'";
            $result = mysqli_query($GLOBALS['dbinv'], $sql);

            $sql = "select max(tmp_count) as max_num from tmp_cash_chq where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "'";
            //echo $sql;
            $result = mysqli_query($GLOBALS['dbinv'], $sql);
            if ($row = mysqli_fetch_array($result)) {
                if ((is_null($row["max_num"]) == false) and ($row["max_num"] != "")) {
                    $i = $row["max_num"] + 1;
                } else {
                    $i = 1;
                }
            }
            $chqamtval = str_replace(",", "", $_GET["chqamt"]);

            $sql = "insert into tmp_cash_chq(recno, chqno, chqdate, chqbank, chqamt, tmp_no, tmp_count) values ('" . $_GET["invno"] . "', '" . $_GET["chqno"] . "', '" . $date1 . "', '" . $_GET["bank"] . "', '" . $chqamtval . "', '" . $_SESSION["tmp_no_cashrec"] . "', " . $i . ")";
            $result = mysqli_query($GLOBALS['dbinv'], $sql);

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

            $ResponseXML .= "<chq_table><![CDATA[ <table border=1 cellspacing=\"0\" cellpadding=\"0\" class=\"bcgl1\"><tr height=\"25\">
					<td width=\"200\"  background=\"\">Cheque No</td>
					<td width=\"100\"  background=\"\">Cheque Date</td>
					<td width=\"230\"  background=\"\">Bank</td>
					<td width=\"140\"  background=\"\">Amount</td>
					</tr>";

            $sql = "select * from tmp_cash_chq where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "' order by tmp_count";
            $result = mysqli_query($GLOBALS['dbinv'], $sql);
            while ($row = mysqli_fetch_array($result)) {
                $chqno = "chqno" . $row["tmp_count"];
                $chqdate = "chqdate" . $row["tmp_count"];
                $bank = "bank" . $row["tmp_count"];
                $chqamt = "chqamt" . $row["tmp_count"];

                $ResponseXML .= "<tr>
					<td><input type=\"text\"  class=\"text_purchase3\" name=\"" . $chqno . "\" id=\"" . $chqno . "\" size=\"10\" onblur=\"chng_chqno('" . $row["tmp_count"] . "');\" value=\"" . $row["chqno"] . "\"     /></td>
					<td><input type=\"text\"  class=\"text_purchase3\" size=\"10\" id=\"" . $chqdate . "\" name=\"" . $chqdate . "\" onblur=\"chng_chqdate('" . $row["tmp_count"] . "');\" value=\"" . $row["chqdate"] . "\" /></td>
					<td><input type=\"text\" size=\"15\" name=\"" . $bank . "\" id=\"" . $bank . "\" value=\"" . $row["chqbank"] . "\" class=\"text_purchase3\" onblur=\"chng_bank('" . $row["tmp_count"] . "');\" /></td>
					<td align=right><input type=\"text\" size=\"15\" name=\"" . $chqamt . "\" id=\"" . $chqamt . "\" value=\"" . number_format($row["chqamt"], 2, ".", ",") . "\" onblur=\"chng_chqamt('" . $row["tmp_count"] . "');\" class=\"text_purchase3_right\" /></td>
					<td ><img src=\"images/delete_01.png\" width=\"20\" height=\"20\" id=" . $row['chqno'] . "  name=" . $row['chqno'] . " onClick=\"del_item('" . $row['chqno'] . "');\"></td></tr>";

                $totchq = $totchq + $row["chqamt"];
            }

            $ResponseXML .= "   </table>]]></chq_table>";
            $ResponseXML .= "<chqtot><![CDATA[" . $totchq . "]]></chqtot>";
            $ResponseXML .= "<chqbal><![CDATA[" . $totchq . "]]></chqbal>";
            $ResponseXML .= "<chq_validity><![CDATA[yes]]></chq_validity>";

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

            $ResponseXML = " <salesdetails>";
            $ResponseXML .= "<chq_validity><![CDATA[no]]></chq_validity>";

            $ResponseXML .= " </salesdetails>";
            echo $ResponseXML;
        }
    }
    
     if ($_GET["Command"] == "pass_cus_cash_rec_all") {
    include_once("connectioni.php");

    header('Content-Type: text/xml');
    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";

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



    $sql = mysqli_query($GLOBALS['dbinv'],"Select * from vendor where CODE='" . $_GET['custno'] . "'") or die(mysqli_error($GLOBALS['dbinv']));
    if ($row = mysqli_fetch_array($sql)) {
        $ResponseXML .= "<code><![CDATA[" . $row['CODE'] . "]]></code>";
        $ResponseXML .= "<name><![CDATA[" . $row['NAME'] . "]]></name>";
        $ResponseXML .= "<address><![CDATA[" . $row['ADD1'] . " " . $row['ADD2'] . "]]></address>";
    }

    $ResponseXML .= "<sales_table_acc><![CDATA[ <table  border=1  cellspacing=0>
								<tr><td width=\"80\"  background=\"\" ><font color=\"#FFFFFF\"></font></td>
								<td width=\"80\"  background=\"\" ><font color=\"#FFFFFF\">Date</font></td>
								<td width=\"200\"  background=\"\" ><font color=\"#FFFFFF\">Invoice No</font></td>
								<td width=\"80\"  background=\"\" ><font color=\"#FFFFFF\">Value</font></td>
								<td width=\"80\"  background=\"\" ><font color=\"#FFFFFF\">Paid</font></td>
								<td width=\"80\"  background=\"\" ><font color=\"#FFFFFF\">Overdue</font></td>
								<td width=\"80\"  background=\"\" ><font color=\"#FFFFFF\">Chq Pay</font></td>
								<td width=\"80\"  background=\"\" ><font color=\"#FFFFFF\">Chq Balance</font></td>
								<td width=\"80\"  background=\"\" ><font color=\"#FFFFFF\">Cash Pay</font></td>
								<td width=\"80\"  background=\"\" ><font color=\"#FFFFFF\">Inv Balance</font></td>
								<td width=\"80\"  background=\"\" ><font color=\"#FFFFFF\">Select</font></td></tr>";

    //$sql = mysqli_query($GLOBALS['dbinv'],"Select C_CODE, REF_NO, deli_date, GRAND_TOT, TOTPAY, SDATE from s_salma where C_CODE='".$_GET['custno']."' and GRAND_TOT > TOTPAY and DEV='" & rsusercon!dev & "' and CANCELL='0' and SAL_EX='" & Trim(Left(Com_rep, 5)) & "' and Accname <> 'NON STOCK' ORDER BY SDATE") or die(mysqli_error($GLOBALS['dbinv']));
    //	echo "Select C_CODE, REF_NO, deli_date, GRAND_TOT, TOTPAY, SDATE from s_salma where C_CODE='".$_GET['custno']."' and GRAND_TOT > TOTPAY and CANCELL='0' and SAL_EX='".$_GET["refno"]."' and Accname <> 'NON STOCK' ORDER BY SDATE";

    $i = 1;
    //	echo "Select C_CODE, REF_NO, deli_date, GRAND_TOT, TOTPAY, SDATE from s_salma where C_CODE='".$_GET['custno']."' and GRAND_TOT > TOTPAY and CANCELL='0'  ORDER BY SDATE";

    if ($_GET['refno'] == "") {
        $sql = mysqli_query($GLOBALS['dbinv'],"Select C_CODE, REF_NO, deli_date, GRAND_TOT, TOTPAY, SDATE from s_salma where C_CODE='" . $_GET['custno'] . "' and GRAND_TOT > TOTPAY and (GRAND_TOT-TOTPAY)>0 and CANCELL='0'   and DEV='" . $_SESSION["dev"] . "' and LOCK1='1' ORDER BY SDATE") or die(mysqli_error($GLOBALS['dbinv']));
    } else {

        $sql = mysqli_query($GLOBALS['dbinv'],"Select C_CODE, REF_NO, deli_date, GRAND_TOT, TOTPAY, SDATE from s_salma where C_CODE='" . $_GET['custno'] . "' and GRAND_TOT > TOTPAY and (GRAND_TOT-TOTPAY)>0 and CANCELL='0'   and DEV='" . $_SESSION["dev"] . "' and LOCK1='1' ORDER BY SDATE") or die(mysqli_error($GLOBALS['dbinv']));
    }
    while ($row = mysqli_fetch_array($sql)) {

        $sdate = "sdate" . $i;
        $delidate = "delidate" . $i;

        $invval = "invval" . $i;


        if (is_null($row["deli_date"] == false)) {
            $ResponseXML .= "<tr><td><div id=" . $delidate . ">" . $row["REQ_DATE"] . "</div></td>";
        } else {
            $ResponseXML .= "<tr><td><div id=" . $delidate . ">" . $row["SDATE"] . "</div></td>";
        }

        $ResponseXML .= "<td><div id=" . $sdate . ">" . $row["SDATE"] . "</div></td>";

        $j = $i + 1;

        $overdue = "overdue" . $i;

        $chq_pay = "chq_pay" . $i;
        $chq_pay_next = "chq_pay" . $j;

        $chq_balance = "chq_balance" . $i;
        $chq_balance_next = "chq_balance" . $j;

        $cash_pay = "cash_pay" . $i;
        $cash_pay_next = "cash_pay" . $j;

        $inv_balance = "inv_balance" . $i;
        $overdueamt = $row["GRAND_TOT"] - $row["TOTPAY"];
        //number_format($row["GRAND_TOT"]-$row["TOTPAY"], 2, ".")

        $invno = "invno" . $i;
    $select= "select" . $i;
    $ref = $row["REF_NO"];
        $ResponseXML .= "<td><div id=" . $invno . ">" . $row["REF_NO"] . "</div></td>
									 <td><div id=" . $invval . " align=\"right\">" . number_format($row["GRAND_TOT"], 2, ".", ",") . "</div></td>
									 <td ><div align=\"right\">" . number_format($row["TOTPAY"], 2, ".", ",") . "</div></td>
									 <td><input type=\"text\"  class=\"text_purchase3_right\" name=" . $overdue . " id=" . $overdue . " value=" . $overdueamt . " size=\"10\" disabled align=\"right\" /></td>
									 <td><input type=\"text\"  class=\"text_purchase3_right\" name=" . $chq_pay . " id=" . $chq_pay . " onBlur=\"calc_bal('$overdue', '$chq_pay', '$inv_balance', '$chq_balance', '$chq_balance_next', '$cash_pay', '$i', event);\"  onKeyPress=\"keyset('$chq_pay_next', event);\"   size=\"10\" align=\"right\"/></td>									
									 <td><input type=\"text\"  class=\"text_purchase3_right\" name=" . $chq_balance . " disabled id=" . $chq_balance . " onKeyPress=\"keysetvalue('$chq_balance','$chq_balance_next', '$chq_pay', event);\" size=\"10\" align=\"right\"  /></td>
									 <td><input type=\"text\"  class=\"text_purchase3_right\" name=" . $cash_pay . " id=" . $cash_pay . " onBlur=\"calc_bal_cash('$overdue', '$cash_pay_next', '$chq_pay', '$inv_balance', '$cash_pay', event);\" onKeyPress=\"keyset('$cash_pay_next', event);\" size=\"10\" align=\"right\"/></td>
									 <td><input type=\"text\"  class=\"text_purchase3_right\" name=" . $inv_balance . " id=" . $inv_balance . " disabled size=\"10\" align=\"right\"/></td> 
                                   <td><input type=\"checkbox\"  class=\"text_purchase3_right\" name=" . $select . " id=" . $select . " onclick=\"selectupdate($ref, event);\" size=\"10\" align=\"right\"/></td>
                                   </tr>";
        $i = $i + 1;
    }
    //  $sql = "update s_salma set LOCK1='0' where C_CODE='" . $_GET['custno'] . "'"; 
    // $result = $db->RunQuery($sql);

    $overdue = "overdue" . $i;

    $chq_pay = "chq_pay" . $i;
    $chq_pay_next = "chq_pay" . $j;

    $chq_balance = "chq_balance" . $i;
    $chq_balance_next = "chq_balance" . $j;

    $cash_pay = "cash_pay" . $i;
    $cash_pay_next = "cash_pay" . $j;

    $inv_balance = "inv_balance" . $i;
    $overdueamt = $row["GRAND_TOT"] - $row["TOTPAY"];
    $invno = "invno" . $i;
    $ResponseXML .= "<tr><td><td></td>
									 <td></td><div id=" . $invno . "></div></td>
									 <td></td>
									 <td></td>
									 <td><input type=\"text\"  class=\"text_purchase3_right\" name=" . $overdue . " id=" . $overdue . " value=" . $overdueamt . " size=\"10\"/></td>
									 <td><input type=\"text\"  class=\"text_purchase3_right\" name=" . $chq_pay . " id=" . $chq_pay . " onBlur=\"calc_bal('$overdue', '$chq_pay', '$inv_balance', '$chq_balance', '$chq_balance_next', '$cash_pay', '$i', event);\"  onKeyPress=\"keyset('$chq_pay_next', event);\"   size=\"10\"/></td>									
									 <td><input type=\"text\"  class=\"text_purchase3_right\" name=" . $chq_balance . " id=" . $chq_balance . " onKeyPress=\"keysetvalue('$chq_balance','$chq_balance_next', '$chq_pay', event);\" size=\"10\"/></td>
									 <td><input type=\"text\"  class=\"text_purchase3_right\" name=" . $cash_pay . " id=" . $cash_pay . " onKeyPress=\"calc_bal_cash('$overdue', '$cash_pay_next', '$chq_pay', '$inv_balance', '$cash_pay', event);\" size=\"10\"/></td>
									 <td><input type=\"text\"  class=\"text_purchase3_right\" name=" . $inv_balance . " id=" . $inv_balance . " disabled size=\"10\"/></td></tr>";

    $_SESSION["count"] = $i;
    $ResponseXML .= "   </table>]]></sales_table_acc>";
    $ResponseXML .= "<mcount><![CDATA[" . $_SESSION["count"] . "]]></mcount>";
    $ResponseXML .= "</salesdetails>";
    echo $ResponseXML;
}

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

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

        $sql = "delete from tmp_cash_chq where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "' and chqno='" . $_GET["chqno"] . "'";
        //echo $sql;
        $result = mysqli_query($GLOBALS['dbinv'], $sql);

        $ResponseXML .= "<chq_table><![CDATA[ <table border=1 cellspacing=\"0\" cellpadding=\"0\" class=\"bcgl1\"><tr height=\"25\">
					<td width=\"200\"  background=\"\">Cheque No</td>
					<td width=\"100\"  background=\"\">Cheque Date</td>
					<td width=\"230\"  background=\"\">Bank</td>
					<td width=\"140\"  background=\"\">Amount</td>
					</tr>";

        $sql = "select * from tmp_cash_chq where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "' ";
        $result = mysqli_query($GLOBALS['dbinv'], $sql);
        while ($row = mysqli_fetch_array($result)) {
            $ResponseXML .= "<tr>
					<td>" . $row["chqno"] . "</td>
					<td>" . $row["chqdate"] . "</td>
					<td>" . $row["chqbank"] . "</td>
					<td align=right>" . number_format($row["chqamt"], 2, ".", ",") . "</td>
					<td ><img src=\"images/delete_01.png\" width=\"20\" height=\"20\" id=" . $row['chqno'] . "  name=" . $row['chqno'] . " onClick=\"del_item('" . $row['chqno'] . "');\"></td></tr>";

            $totchq = $totchq + $row["chqamt"];
        }

        $ResponseXML .= "   </table>]]></chq_table>";
        $ResponseXML .= "<chqtot><![CDATA[" . $totchq . "]]></chqtot>";
        $ResponseXML .= "<chqbal><![CDATA[" . $totchq . "]]></chqbal>";

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

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

        /* 	$sql="Select CAS_INV_NO_m from invpara";
          $result =mysqli_query($GLOBALS['dbinv'],$sql) ;
          $row = mysqli_fetch_array($result);
          $tmpinvno="000000".$row["CAS_INV_NO_m"];
          $lenth=strlen($tmpinvno);
          $invno="INV".substr($tmpinvno, $lenth-7);
          echo $invno; */

        if ($_SESSION["dev"] != "") {
 

                if ($_SESSION['dev'] == "0") {

                    $sql = "select INV_RECNO from invpara where flag='".$_SESSION['company']."'"; 
                    $result = mysqli_query($GLOBALS['dbinv'], $sql);
                    $row = mysqli_fetch_array($result);

                    $tmprecno = "000000" . $row["INV_RECNO"];
                    $lenth = strlen($tmprecno);

                    $recno = trim("".$_SESSION['company']."/PR/ ") . substr($tmprecno, $lenth - 6);
                    $_SESSION["recno"] = $recno;

                    $sql = "Select INV_RECNO from tmpinvpara where flag='".$_SESSION['company']."'"; 
                    $result = mysqli_query($GLOBALS['dbinv'], $sql);
                    $row = mysqli_fetch_array($result);
                    $_SESSION["tmp_no_cashrec"] = "".$_SESSION['company']."/PR/ " . $row["INV_RECNO"];
 
                    $sql = "update tmpinvpara set INV_RECNO=INV_RECNO+1 where flag='".$_SESSION['company']."'"; 
                    $result = mysqli_query($GLOBALS['dbinv'], $sql);
                }

                if ($_SESSION['dev'] == "1") {

                    $sql = "select INV_RECNO_M from invpara  where flag='".$_SESSION['company']."'"; 
                    $result = mysqli_query($GLOBALS['dbinv'], $sql);
                    $row = mysqli_fetch_array($result);

                    $tmprecno = "000000" . $row["INV_RECNO_M"];
                    $lenth = strlen($tmprecno);
                    $recno = trim("".$_SESSION['company']." ") . substr($tmprecno, $lenth - 6);
                    $_SESSION["recno"] = $recno;

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

                    $sql = "update tmpinvpara set INV_RECNO_M=INV_RECNO_M+1 where flag='".$_SESSION['company']."'"; 
                    $result = mysqli_query($GLOBALS['dbinv'], $sql);
                }

                $sql = "delete from tmp_inv_data where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "'";
                $result = mysqli_query($GLOBALS['dbinv'], $sql);

                $sql = "delete from tmp_cash_chq where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "'";
                $result = mysqli_query($GLOBALS['dbinv'], $sql);

                $sql = "delete from tmp_cash_chq where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "'";
                $result = mysqli_query($GLOBALS['dbinv'], $sql);

                $sql = "delete from tmp_utilization where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "'";
                $result = mysqli_query($GLOBALS['dbinv'], $sql);
            
            $ResponseXML = "";
            $ResponseXML .= "<salesdetails>";

            $ResponseXML .= "<recno><![CDATA[" . $recno . "]]></recno>";
            $ResponseXML .= "<cur_date><![CDATA[" . date("Y-m-d") . "]]></cur_date>";
            $ResponseXML .= "<chq_validity><![CDATA[yes]]></chq_validity>";

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

            $ResponseXML = " <salesdetails>";
            $ResponseXML .= "<chq_validity><![CDATA[no]]></chq_validity>";

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

    if ($_GET["Command"] == "check_print") {
        $sql = "delete from tmprct where recno='" . $_GET["recno"] . "'";
        //echo $sql;
        $result = mysqli_query($GLOBALS['dbinv'], $sql);

        $i = 1;
        //echo $_GET["mcount"];
        while ($_GET["mcount"] >= $i) {
            $sdate = "sdate" . $i;
            $invno = "invno" . $i;
            $invval = "invval" . $i;
            $overdue = "overdue" . $i;
            $chq_pay = "chq_pay" . $i;
            $cash_pay = "cash_pay" . $i;
            $inv_balance = "inv_balance" . $i;

            $invval_val = str_replace(",", "", $_GET[$invval]);
            $overdue_val = str_replace(",", "", $_GET[$overdue]);
            $chq_pay_val = str_replace(",", "", $_GET[$chq_pay]);
            $cash_pay_val = str_replace(",", "", $_GET[$cash_pay]);
            $inv_balance_val = str_replace(",", "", $_GET[$inv_balance]);

            if ((($_GET[$chq_pay_val] != "") and ($_GET[$chq_pay_val] != "0")) or (($_GET[$cash_pay_val] != "") and ($_GET[$cash_pay_val] != "0"))) {

                $paid = $_GET[$chq_pay_val] + $_GET[$cash_pay_val];

                $sql = "insert into  tmprct (SDATE, REFNO, AMOUNT, balance, paid, OVERDUE, flag) value ('" . $_GET[$sdate] . "', '" . $_GET[$invno] . "', '" . $_GET[$invval_val] . "', '" . $_GET[$overdue_val] . "', '" . $paid . "', 'INV')";
                //echo $sql;
                $result = mysqli_query($GLOBALS['dbinv'], $sql);
            }

            $i = $i + 1;
        }

        $sql1 = "select * from tmp_cash_chq where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "'";
        $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
        while ($row1 = mysqli_fetch_array($result1)) {
            $sql = "insert into  tmprct (SDATE, REFNO, AMOUNT, balance, CH_VAL, flag) value ('" . $row1["chqdate"] . "', '" . $row1["chqno"] . "', '" . $row1["chqbank"] . "', '" . $row1["chqamt"] . "', '" . $row1["chqamt"] . "', 'CHK')";
            $result = mysqli_query($GLOBALS['dbinv'], $sql);
        }
    }

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

        include_once ("connectioni.php");

        $ResponseXML .= "";
        //$ResponseXML .= "<invdetails>";

        $ResponseXML .= "<table width=\"735\" border=\"0\" class=\"form-matrix-table\">
                            <tr>
                              <td width=\"121\"  background=\"\" ><font color=\"#FFFFFF\">Ref No</font></td>
                              <td width=\"424\"  background=\"\"><font color=\"#FFFFFF\">Date</font></td>
							   <td width=\"424\"  background=\"\"><font color=\"#FFFFFF\">Amount</font></td>
							    <td width=\"424\"  background=\"\"><font color=\"#FFFFFF\">Customer</font></td>
                             
   							</tr>";

        if ($_GET["mfield"] == "recno") {
            $letters = $_GET['recno'];
            //$letters = preg_replace("/[^a-z0-9 ]/si","",$letters);
            //$letters="/".$letters;
            //$a="SELECT * from s_salma where REF_NO like  '$letters%'";
            //echo $a;
            //echo "SELECT * from s_crec where  CA_REFNO and DEV='".$_SESSION["dev"]."' like  '$letters%' limit 50";
            $sql = mysqli_query($GLOBALS['dbinv'], "SELECT * from s_crec where   DEV='" . $_SESSION["dev"] . "' and CA_REFNO like  '$letters%' limit 50") or die(mysqli_error());
        } else if ($_GET["mstatus"] == "recdate") {
            $letters = $_GET['recdate'];
            //$letters = preg_replace("/[^a-z0-9 ]/si","",$letters);
            $sql = mysqli_query($GLOBALS['dbinv'], "SELECT * from s_crec where DEV='" . $_SESSION["dev"] . "' and CA_DATE like  '$letters%' limit 50") or die(mysqli_error());
        } else if ($_GET["mstatus"] == "recamt") {
            $letters = $_GET['recamt'];
            //$letters = preg_replace("/[^a-z0-9 ]/si","",$letters);
            $sql = mysqli_query($GLOBALS['dbinv'], "SELECT * from s_crec where DEV='" . $_SESSION["dev"] . "' and CA_AMOUNT like  '$letters%' limit 50") or die(mysqli_error());
        } else {
            $letters = $_GET['recno'];
            //$letters = preg_replace("/[^a-z0-9 ]/si","",$letters);
            $sql = mysqli_query($GLOBALS['dbinv'], "SELECT * from s_crec where DEV='" . $_SESSION["dev"] . "' and CA_REFNO like  '$letters%' limit 50") or die(mysqli_error());
        }

        while ($row = mysqli_fetch_array($sql)) {
            $REF_NO = $row['CA_REFNO'];
            $stname = $_GET["mstatus"];
            $ResponseXML .= "<tr>
                           	  <td onclick=\"recno('$REF_NO');\">" . $row['CA_REFNO'] . "</a></td>
                              <td onclick=\"recno('$REF_NO');\">" . $row['CA_DATE'] . "</a></td>
                              <td onclick=\"recno('$REF_NO');\">" . $row['CA_AMOUNT'] . "</a></td>";

            $sql1 = "SELECT * FROM vendor where CODE = '" . $row["CA_CODE"] . "'";
            $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
            $row1 = mysqli_fetch_array($result1);
            $ResponseXML .= "<td onclick=\"recno('$REF_NO');\">" . $row1['NAME'] . "</a></td>                          	
                            </tr>";
        }

        $ResponseXML .= "   </table>";

        echo $ResponseXML;
    }

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

        include_once ("connectioni.php");

        $ResponseXML .= "";
        //$ResponseXML .= "<invdetails>";

        $ResponseXML .= "<table width=\"735\" border=\"0\" class=\"form-matrix-table\">
                            <tr>
                              <td width=\"121\"  background=\"\" ><font color=\"#FFFFFF\">Bank Code</font></td>
                              <td width=\"424\"  background=\"\"><font color=\"#FFFFFF\">Banl Name</font></td>
							  
                             
   							</tr>";

        if ($_GET["mstatus"] == "cash_rec") {
            if ($_GET["mfield"] == "bcode") {
                $letters = $_GET['bcode'];
                $sql = mysqli_query($GLOBALS['dbinv'], "SELECT * from bankmas where  bcode like  '$letters%' order by bname limit 50") or die(mysqli_error());
            } else if ($_GET["mfield"] == "bank") {
                $letters = $_GET['bank'];
                $sql = mysqli_query($GLOBALS['dbinv'], "SELECT * from bankmas where  bname like  '$letters%' order by bname limit 50") or die(mysqli_error());
            }
        }

        while ($row = mysqli_fetch_array($sql)) {

            $ResponseXML .= "<tr>
                           	    <td onclick=\"selbank('" . $row["bcode"] . "', '" . $stname . "');\">" . $row["bcode"] . "</a></td>
                              <td onclick=\"selbank('" . $row["bcode"] . "', '" . $stname . "');\">" . $row["bname"] . "</a></td>
				
							                           	
                            </tr>";
        }

        $ResponseXML .= "   </table>";

        echo $ResponseXML;
    }

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

        $ResponseXML = "";
        $ResponseXML .= "<salesdetails>";
        $sql = "select * from bankmas where bcode='" . $_GET["bcode"] . "' order by bname limit 50";
        $result = mysqli_query($GLOBALS['dbinv'], $sql);
        if ($row = mysqli_fetch_array($result)) {
            $ResponseXML .= "<bname><![CDATA[" . $row["bname"] . "]]></bname>";
        }

        $ResponseXML .= "</salesdetails>";

        echo $ResponseXML;
    }

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

        $ResponseXML = "";
        $ResponseXML .= "<salesdetails>";
        $sql = "select * from s_crec where CA_REFNO='" . $_GET["recno"] . "'";
        //echo $sql;
        $result = mysqli_query($GLOBALS['dbinv'], $sql);
        if ($row = mysqli_fetch_array($result)) {
            $ResponseXML .= "<CA_REFNO><![CDATA[" . $row["CA_REFNO"] . "]]></CA_REFNO>";
            $ResponseXML .= "<CA_DATE><![CDATA[" . $row["CA_DATE"] . "]]></CA_DATE>";
            $ResponseXML .= "<CA_CODE><![CDATA[" . $row["CA_CODE"] . "]]></CA_CODE>";
            $ResponseXML .= "<cancel><![CDATA[" . $row["CANCELL"] . "]]></cancel>";
            $_SESSION["tmp_no_cashrec"] = $row["tmp_no"];

            $sql1 = "select * from vendor where CODE='" . $row["CA_CODE"] . "'";
            $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
            if ($row1 = mysqli_fetch_array($result1)) {
                $ResponseXML .= "<cusname><![CDATA[" . $row1["NAME"] . "]]></cusname>";
                 $ResponseXML .= "<cus_address><![CDATA[" . $row1["ADD1"] . "]]></cus_address>";
            }
            $ResponseXML .= "<CA_CASSH><![CDATA[" . $row["CA_CASSH"] . "]]></CA_CASSH>";
            $ResponseXML .= "<CA_AMOUNT><![CDATA[" . $row["CA_AMOUNT"] . "]]></CA_AMOUNT>";
            $ResponseXML .= "<CA_SALESEX><![CDATA[" . $row["CA_SALESEX"] . "]]></CA_SALESEX>";
            $ResponseXML .= "<pay_type><![CDATA[" . $row["pay_type"] . "]]></pay_type>";
            $ResponseXML .= "<TTDATE><![CDATA[" . $row["TTDATE"] . "]]></TTDATE>";
            $ResponseXML .= "<AC_REFNO><![CDATA[" . $row["AC_REFNO"] . "]]></AC_REFNO>";
            $ResponseXML .= "<cashtype><![CDATA[" . $row["cashtype"] . "]]></cashtype>";
            $ResponseXML .= "<remarks1><![CDATA[" . $row["remarks"] . "]]></remarks1>";

            $sql1 = "select * from s_salrep where REPCODE='" . $row["CA_SALESEX"] . "'";
            $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
            if ($row1 = mysqli_fetch_array($result1)) {
                $ResponseXML .= "<repname><![CDATA[" . $row1["Name"] . "]]></repname>";
            }
            
                    $TOTOUT=0;
    
        $sql_out = mysqli_query($GLOBALS['dbinv'], "Select SUM(GRAND_TOT-TOTPAY) AS OUT1 from s_salma where C_CODE='" . $row["CA_CODE"] . "' and GRAND_TOT > TOTPAY and (GRAND_TOT-TOTPAY)>0 and CANCELL='0' and DEV='" . $_SESSION['dev'] . "'  ORDER BY SDATE") or die(mysqli_error());
        if ($row_out = mysqli_fetch_array($sql_out)) {
            $TOTOUT=$row_out['OUT1'];
        }
        
        $ResponseXML .= "<totalout><![CDATA[" . number_format($TOTOUT,2) . "]]></totalout>";
        
        }
        
        
        
        
        

        
        
         $sql = "select * from a_ledger where L_REFNO='" . $_GET["recno"] . "' and L_FLAG1='DEB'";
        $result = mysqli_query($GLOBALS['dbinv'], $sql);
        if ($row = mysqli_fetch_array($result)) {
            $ResponseXML .= "<deb><![CDATA[" . $row["L_CODE"] . "]]></deb>";
        }else{
             $ResponseXML .= "<deb><![CDATA[]]></deb>";
        }
        
        $sql = "select * from a_ledger where L_REFNO='" . $_GET["recno"] . "' and L_FLAG1='CRE'";
        $result = mysqli_query($GLOBALS['dbinv'], $sql);
        if ($row = mysqli_fetch_array($result)) {
            $ResponseXML .= "<cre><![CDATA[" . $row["L_CODE"] . "]]></cre>";
        }else{
            $ResponseXML .= "<cre><![CDATA[]]></cre>";
        }
        
        $sql = "select * from s_invcheq where refno='" . $_GET["recno"] . "'";
        $result = mysqli_query($GLOBALS['dbinv'], $sql);
        if ($row = mysqli_fetch_array($result)) {
            $ResponseXML .= "<collectcode><![CDATA[" . $row["ch_owner"] . "]]></collectcode>";
        } else {
            $ResponseXML .= "<collectcode><![CDATA[]]></collectcode>";
        }

        $sql = "delete from tmp_cash_chq where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "'";
        $result = mysqli_query($GLOBALS['dbinv'], $sql);

        $sql = "delete from tmp_cash_chq where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "'";
        $result = mysqli_query($GLOBALS['dbinv'], $sql);

        $sql = "delete from tmp_utilization where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "'";
        $result = mysqli_query($GLOBALS['dbinv'], $sql);

        // $sql = "select * from s_invcheq where refno='" . $_GET["recno"] . "' order by ID";
        // $result = mysqli_query($GLOBALS['dbinv'], $sql);
        // while ($row = mysqli_fetch_array($result)) {
        //     $sql1 = "insert into tmp_cash_chq(recno, chqno, chqdate, chqbank, chqamt, tmp_no) values ('" . $row["refno"] . "', '" . $row["cheque_no"] . "', '" . $row["che_date"] . "', '" . $row["bank"] . "', " . $row["che_amount"] . ", '" . $_SESSION["tmp_no_cashrec"] . "')";
        //     //echo $sql1;
        //     $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
        // }

        $ResponseXML .= "<chq_table><![CDATA[ <table border=1 cellspacing=\"0\" cellpadding=\"0\" class=\"bcgl1\"><tr height=\"25\">
					<td width=\"200\"  background=\"\">Cheque No</td>
					<td width=\"100\"  background=\"\">Cheque Date</td>
					<td width=\"230\"  background=\"\">Bank</td>
					<td width=\"140\"  background=\"\">Amount</td>
					</tr>";

        $sql = "select * from s_invcheq1 where refno='" . $_GET["recno"] . "' order by ID";
        $result = mysqli_query($GLOBALS['dbinv'], $sql);
        while ($row = mysqli_fetch_array($result)) {
            $ResponseXML .= "<tr>
					<td>" . $row["cheque_no"] . "</td>
					<td>" . $row["che_date"] . "</td>
					<td>" . $row["bank"] . "</td>
					<td align=right>" . number_format($row["che_amount"], 2, ".", ",") . "</td>
					</tr>";
            $totchq = $totchq + $row["che_amount"];
        }

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

        $sql = "delete from tmp_utilization where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "' ";
        $result = mysqli_query($GLOBALS['dbinv'], $sql);

        // $sql = "select * from s_sttr where ST_REFNO='" . $_GET["recno"] . "' ";
        // $result = mysqli_query($GLOBALS['dbinv'], $sql);
        // while ($row = mysqli_fetch_array($result)) {
        //     $sql1 = "select * from s_salma where REF_NO='" . $row["ST_INVONO"] . "' ";
        //     $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
        //     $row1 = mysqli_fetch_array($result1);

        //     $sql2 = "insert into tmp_utilization(recno, invno, invdate, org_invdate, chqno, chqdate, chbank, settled, days, tmp_no) values ('" . $_GET["recno"] . "', '" . $row["ST_INVONO"] . "', '" . $row1["SDATE"] . "', '" . $row1["SDATE"] . "', '" . $row["ST_CHNO"] . "', '" . $row["st_chdate"] . "', '" . $row["st_chbank"] . "', " . $row["ST_PAID"] . ", " . $row["st_days"] . ", '" . $_SESSION["tmp_no_cashrec"] . "')";
        //     //echo $sql2;
        //     $result2 = mysqli_query($GLOBALS['dbinv'], $sql2);
        // }

        $ResponseXML .= "<uti_table><![CDATA[ <table border=1 cellspacing=\"0\" cellpadding=\"0\" class=\"bcgl1\"><tr height=\"25\">
					<td width=\"200\"  background=\"\">Invoice No</td>
					<td width=\"100\"  background=\"\">Invoice Date</td>
					<td width=\"230\"  background=\"\">Cheque No</td>
					<td width=\"140\"  background=\"\">Cheque Date</td>
					<td width=\"140\"  background=\"\">Settled</td>
					<td width=\"140\"  background=\"\">Days</td>
					</tr>";

        $sql = "select * from s_sttr where ST_REFNO='" . $_GET["recno"] . "'";
        $result = mysqli_query($GLOBALS['dbinv'], $sql);
        while ($row = mysqli_fetch_array($result)) {
            $sql1 = "select * from s_salma where REF_NO='" . $row["ST_INVONO"] . "' ";
            $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
            $row1 = mysqli_fetch_array($result1);
            
            $ResponseXML .= "<tr>
					<td>" . $row["ST_INVONO"] . "</td>
					<td>" . $row1["SDATE"] . "</td>
					<td>" . $row["ST_CHNO"] . "</td>
					<td>" . $row["st_chdate"] . "</td>
					<td align=right>" . number_format($row["ST_PAID"], 2, ".", ",") . "</td>
					<td>" . $row["st_days"] . "</td>
					</tr>";
        }
        

        $ResponseXML .= "   </table>]]></uti_table>";
        $ResponseXML .= "</salesdetails>";

        echo $ResponseXML;
    }

    if ($_GET["Command"] == "delete_rec") {
        $ResponseXML = "";

        include_once ("connectioni.php");

        $sql_status = 0;

        mysqli_query($GLOBALS['dbinv'], "SET AUTOCOMMIT=0");
        mysqli_query($GLOBALS['dbinv'], "START TRANSACTION");

        // mysqli_query($GLOBALS['dbacc'], "SET AUTOCOMMIT=0");
        // mysqli_query($GLOBALS['dbacc'], "START TRANSACTION");

        // if (date("Y-m-d") == date("Y-m-d", strtotime($_GET["invdate"]))) {
            $sql = "select * from c_bal where REFNO='" . $_GET["recno"] . "'";
            $result = mysqli_query($GLOBALS['dbinv'], $sql);

            if ($row = mysqli_fetch_array($result)) {
                if ($row["AMOUNT"] == $row["BALANCE"]) {
                 
                    $sql1 = "update   s_crec set CANCELL='1' where CA_REFNO='" . $_GET["recno"] . "'";
                    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                    if ($result1 != 1) {
                        $sql_status = 1;
                    }

                    $sql1 = "delete from s_led where REF_NO='" . $_GET["recno"] . "'";
                    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                    if ($result1 != 1) {
                        $sql_status = 2;
                    }

                    $sql1 = "delete from s_invcheq where refno='" . $_GET["recno"] . "'";
                    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                    if ($result1 != 1) {
                        $sql_status = 3;
                    }
                    
                    $sql1 = "update   s_invcheq1 set cancell='1' where refno='" . $_GET["recno"] . "'";
                    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                    if ($result1 != 1) {
                        $sql_status = 1;
                    }
                    
                    

                    //$sql1="update vendor set CUR_BAL=CUR_BAL+ ".$_GET["txtpaytot"]." where CODE='".$_GET["cuscode"]."'";
                    //$result1=mysqli_query($GLOBALS['dbinv'],$sql1);
                    //if ($result1!=1){ $sql_status=5; }

                    $sql1 = "delete from c_bal where REFNO='" . $_GET["recno"] . "'";
                    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                    if ($result1 != 1) {
                        $sql_status = 4;
                    }

                    //$sql1="update br_trn set credit=credit- ".$_GET["txtpaytot"]." where cus_code='".$_GET["cuscode"]."' and Rep='".$_GET["salesrep"]."'";
                    //$result1=mysqli_query($GLOBALS['dbinv'],$sql1);
                    //if ($result1!=1){ $sql_status=7; }

                    $sql1 = "select * from s_sttr where ST_REFNO='" . $_GET["recno"] . "'";
                    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                    //if ($result1!=1){ $sql_status=5; }

                    while ($row1 = mysqli_fetch_array($result1)) {
                        $sql2 = "update s_salma set TOTPAY=TOTPAY- " . $row1["ST_PAID"] . " where REF_NO='" . $row1["ST_INVONO"] . "'";
                        $result2 = mysqli_query($GLOBALS['dbinv'], $sql2);
                        if ($result2 != 1) {
                            $sql_status = 6;
                        }

                        //$sql2="update s_salma set CANCELL= '1' where recno='".$row1["invno"]."'";
                        //$result2 =mysqli_query($GLOBALS['dbinv'],$sql2);

                        if ((strtoupper($row1["ST_CHNO"]) == "CAS") or ($row1["ST_CHNO"] == "Cash") or ($row1["ST_CHNO"] == "Cash TT") or ($row1["ST_CHNO"] == "J/Entry")) {
                            $sql2 = "update s_salma set CASH=CASH- " . $row1["ST_PAID"] . " where REF_NO='" . $row1["ST_INVONO"] . "'";
                            $result2 = mysqli_query($GLOBALS['dbinv'], $sql2);
                            if ($result2 != 1) {
                                $sql_status = 7;
                            }
                        }
                    }

                    $sql1 = "delete from s_sttr where ST_REFNO='" . $_GET["recno"] . "'";
                    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                    if ($result1 != 1) {
                        $sql_status = 8;
                    }

                    $sql_l = "delete from a_ledger where L_REFNO = '" . trim($_GET["recno"]) . "'";
                    $result_l = mysqli_query($GLOBALS['dbinv'], $sql_l);
                    if ($result_l != 1) {
                        $sql_status = 9;
                    }

                    $sql_l = "delete from a_bankdeptrn where refno = '" . trim($_GET["recno"]) . "'";
                    $result_l = mysqli_query($GLOBALS['dbinv'], $sql_l);
                    if ($result_l != 1) {
                        $sql_status = 10;
                    }

                    $sql_l = "delete from a_bankdepche where refno = '" . trim($_GET["recno"]) . "'";
                    $result_l = mysqli_query($GLOBALS['dbinv'], $sql_l);
                    if ($result_l != 1) {
                        $sql_status = 11;
                    }

                    $sql_l = "delete from tmp_ret_chq_sett where recno = '" . trim($_GET["recno"]) . "'";
                    $result_l = mysqli_query($GLOBALS['dbinv'], $sql_l);
                    if ($result_l != 1) {
                        $sql_status = 12;
                    }

                    $sql1 = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values ('" . trim($_GET["recno"]) . "', '" . $_SESSION["CURRENT_USER"] . "', 'Cash Reciept', 'Cancel', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";
                    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                    if ($result1 != 1) {
                        $sql_status = 13;
                    }

                    if ($sql_status == 0) {
                        mysqli_query($GLOBALS['dbinv'], "COMMIT");
                        // mysqli_query($GLOBALS['dbacc'], "COMMIT");

                        $ResponseXML = "Reciept Canceled";
                    } else {
                        mysqli_query($GLOBALS['dbinv'], "ROLLBACK");
                        // mysqli_query($GLOBALS['dbacc'], "ROLLBACK");
                        $ResponseXML = "Error has occured. Can't Cancel";
                    }
                } else {
                    $ResponseXML .= "Sorry over payment utilized.... Cant Cancel";
                }
            } else {
                $sql1 = "update s_crec set CANCELL='1' where CA_REFNO='" . $_GET["recno"] . "'";
                $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                if ($result1 != 1) {
                    $sql_status = 1;
                }
                 
                    
              

                $sql1 = "delete from s_led where REF_NO='" . $_GET["recno"] . "'";
                $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                if ($result1 != 1) {
                    $sql_status = 1;
                }

                $sql1 = "delete from s_invcheq where refno='" . $_GET["recno"] . "'";
                $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                if ($result1 != 1) {
                    $sql_status = 1;
                }
                
                
                
                $sql1 = "update s_invcheq1 set cancell='1' where refno='" . $_GET["recno"] . "'";
                $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                if ($result1 != 1) {
                    $sql_status = 1;
                }

                //	$sql1="update vendor set CUR_BAL=CUR_BAL+ ".$_GET["txtpaytot"]." where CODE='".$_GET["cuscode"]."'";
                //	$result1=mysqli_query($GLOBALS['dbinv'],$sql1);
                //	if ($result1!=1){ $sql_status=5; }

                $sql1 = "delete from c_bal where REFNO='" . $_GET["recno"] . "'";
                $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                if ($result1 != 1) {
                    $sql_status = 1;
                }

                //$sql1="update br_trn set credit=credit- ".$_GET["txtpaytot"]." where cus_code='".$_GET["cuscode"]."' and Rep='".$_GET["salesrep"]."'";
                //$result1=mysqli_query($GLOBALS['dbinv'],$sql1);
                //if ($result1!=1){ $sql_status=7; }
                $sql1 = "select * from s_sttr where ST_REFNO='" . $_GET["recno"] . "'";
                // $sql1 = "select * from tmp_utilization where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "'";
                $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                while ($row1 = mysqli_fetch_array($result1)) {
                    $sql2 = "update s_salma set TOTPAY=TOTPAY- " . $row1["ST_PAID"] . " where REF_NO='" . $row1["ST_INVONO"] . "'";
                    // $sql2 = "update s_salma set TOTPAY=TOTPAY- " . $row1["settled"] . " where REF_NO='" . $row1["invno"] . "'";
                    $result2 = mysqli_query($GLOBALS['dbinv'], $sql2);
                    if ($result2 != 1) {
                        $sql_status = 1;
                    }

                    //$sql2="update s_salma set CANCELL= '1' where recno='".$row1["invno"]."'";
                    //$result2 =mysqli_query($GLOBALS['dbinv'],$sql2);
                    if ((strtoupper($row1["ST_CHNO"]) == "CAS") or ($row1["ST_CHNO"] == "Cash") or ($row1["ST_CHNO"] == "Cash TT") or ($row1["ST_CHNO"] == "J/Entry")) {
                    // if (($row1["chqno"] == "Cash") or ($row1["chqno"] == "Cash TT") or ($row1["chqno"] == "J/Entry")) {
                        $sql2 = "update s_salma set CASH=CASH- " . $row1["ST_PAID"] . " where REF_NO='" . $row1["ST_INVONO"] . "'";
                        // $sql2 = "update s_salma set CASH=CASH- " . $row1["settled"] . " where REF_NO='" . $row1["invno"] . "'";
                        $result2 = mysqli_query($GLOBALS['dbinv'], $sql2);
                        if ($result2 != 1) {
                            $sql_status = 1;
                        }
                    }
                }
                
                  $sql1 = "delete from s_sttr where ST_REFNO='" . $_GET["recno"] . "'";
                $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                if ($result1 != 1) {
                    $sql_status = 1;
                }

                $sql_l = "delete from a_ledger where L_REFNO = '" . trim($_GET["recno"]) . "'";
                $result_l = mysqli_query($GLOBALS['dbinv'], $sql_l);
                if ($result_l != 1) {
                    $sql_status = 1;
                }

                $sql_l = "delete from a_bankdeptrn where refno = '" . trim($_GET["recno"]) . "'";
                $result_l = mysqli_query($GLOBALS['dbinv'], $sql_l);
                if ($result_l != 1) {
                    $sql_status = 1;
                }

                $sql_l = "delete from a_bankdepche where refno = '" . trim($_GET["recno"]) . "'";
                $result_l = mysqli_query($GLOBALS['dbinv'], $sql_l);
                if ($result_l != 1) {
                    $sql_status = 1;
                }

                $sql_l = "delete from tmp_ret_chq_sett where recno = '" . trim($_GET["recno"]) . "'";
                $result_l = mysqli_query($GLOBALS['dbinv'], $sql_l);
                if ($result_l != 1) {
                    $sql_status = 1;
                }

                $sql1 = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values ('" . trim($_GET["recno"]) . "', '" . $_SESSION["CURRENT_USER"] . "', 'Cash Reciept', 'Cancel', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";

                $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                if ($result1 != 1) {
                    $sql_status = 1;
                }

                if ($sql_status == 0) {
                    mysqli_query($GLOBALS['dbinv'], "COMMIT");
                    // mysqli_query($GLOBALS['dbacc'], "COMMIT");
                    $ResponseXML = "Reciept Canceled";
                } else {
                    mysqli_query($GLOBALS['dbinv'], "ROLLBACK");
                    // mysqli_query($GLOBALS['dbacc'], "ROLLBACK");
                    $ResponseXML = "Error has occured. Can't Cancel";
                }
            }

            echo $sql_status;
        // } else {
        //     $ResponseXML = "Sorry Cant Cancel.... please check reciept date";
        // }

        echo $ResponseXML;
    }
}

if (isset($_POST["Command"])) {
    if ($_POST["Command"] == "utilization") {
        require_once ("connectioni.php");
    
        if ($_SESSION["tmp_no_cashrec"] != "") {
            
            error_log("UTILIZATION DEBUG: tmp_no_cashrec = " . $_SESSION["tmp_no_cashrec"]);
            error_log("UTILIZATION DEBUG: paytype = " . ($_POST["paytype"] ?? "MISSING"));
            error_log("UTILIZATION DEBUG: mcount = " . ($_POST["mcount"] ?? "MISSING"));
    
            // Clear existing utilization records
            $sql = "delete from tmp_utilization where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "'";
            $result = mysqli_query($GLOBALS['dbinv'], $sql);
    
            // Get cheque details from tmp_cash_chq
            $a_chq_no = array();
            $a_chq_date = array();
            $a_chq_amt = array();
            $a_chq_bank = array();
    
            $sql = "select * from tmp_cash_chq where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "'";
            $result = mysqli_query($GLOBALS['dbinv'], $sql);
            $i = 1;
            while ($row = mysqli_fetch_array($result)) {
                $a_chq_no[$i] = $row["chqno"];
                $a_chq_date[$i] = $row["chqdate"];
                $a_chq_amt[$i] = $row["chqamt"];
                $a_chq_bank[$i] = $row["chqbank"];
                $i++;
            }
            $mcou = $i - 1; // Number of cheques
    
            error_log("UTIL DEBUG: mcou(cheque rows)=" . $mcou . " mcount(POST)=" . ($_POST["mcount"] ?? "MISSING"));
    
            // If no cheques and payment type is Cheque, show error
            if ($_POST["paytype"] == "Cheque" && $mcou == 0) {
                error_log("UTIL DEBUG: No cheque details found for cheque payment type");
                $ResponseXML = "<salesdetails>";
                $ResponseXML .= "<uti_table><![CDATA[ <table border=1 cellspacing=\"0\" cellpadding=\"0\" class=\"bcgl1\"><tr height=\"25\">
                        <td width=\"200\">Invoice No</td>
                        <td width=\"100\">Invoice Date</td>
                        <td width=\"230\">Cheque No</td>
                        <td width=\"140\">Cheque Date</td>
                        <td width=\"140\">Settled</td>
                        <td width=\"140\">Days</td>
                        </tr>
                        <tr><td colspan='6' align='center' style='color:red;'>No cheque details found. Please add cheques first.</td></tr>
                        </table>]]></uti_table>";
                $ResponseXML .= "<chq_validity><![CDATA[yes]]></chq_validity>";
                $ResponseXML .= "</salesdetails>";
                echo $ResponseXML;
                exit;
            }
    
            // Build chq_pay array from POST data (invoice cheque payments)
            $chq_pay_8 = array();
            $j = 1;
            while ($j < $_POST["mcount"]) {
                $chq_pay = "chq_pay" . $j;
                $post_val = $_POST[$chq_pay] ?? "";
                $post_val = str_replace(",", "", $post_val);
                $chq_pay_8[$j] = $post_val != "" ? $post_val : "";
                $j++;
            }
    
            // Process each cheque against invoices
            $i = 1;
            $invpos = 1;
            $totalSettled = 0;
    
            while ($i <= $mcou) {
                $chqbal = $a_chq_amt[$i];
                $j = ($invpos > 1) ? $invpos : 1;
                
                while ($j < $_POST["mcount"] && $chqbal > 0) {
                    $invno = "invno" . $j;
                    $delidate = "delidate" . $j;
                    $invval = "invval" . $j;
                    $sdate = "sdate" . $j;
                    
                    // Get the invoice cheque payment amount
                    $invset = $chq_pay_8[$j] ?? 0;
                    $chq_pay_8[$j] = ""; // Clear to avoid reuse
                    
                    if ($invset > 0) {
                        if ($invset <= $chqbal) {
                            // Full allocation of this invoice amount
                            $chqbal = $chqbal - $invset;
                            $settledAmount = $invset;
                            
                            // Get invoice date
                            if (($_POST[$delidate] == "0000-00-00") || ($_POST[$delidate] == "")) {
                                $date1 = $_POST[$sdate] ?? date("Y-m-d");
                            } else {
                                $date1 = $_POST[$delidate];
                            }
                            
                            // Calculate days difference
                            $date2 = $a_chq_date[$i];
                            $diff = abs(strtotime($date2) - strtotime($date1));
                            $days = floor($diff / (60 * 60 * 24));
                            
                            // Insert utilization record
                            $col2 = str_replace(",", "", $_POST[$invval] ?? 0);
                            $sql1 = "insert into tmp_utilization(recno, invno, invdate, org_invdate, chqno, chqdate, chbank, settled, days, invval, c1, tmp_no) 
                                    values ('" . $_POST["recno"] . "', '" . $_POST[$invno] . "', '" . $date1 . "', '" . ($_POST[$sdate] ?? $date1) . "', 
                                    '" . $a_chq_no[$i] . "', '" . $a_chq_date[$i] . "', '" . $a_chq_bank[$i] . "', " . $settledAmount . ", " . $days . ", " . $col2 . ", 0, '" . $_SESSION["tmp_no_cashrec"] . "')";
                            $result = mysqli_query($GLOBALS['dbinv'], $sql1);
                            if (!$result) {
                                error_log("UTIL DEBUG: Insert failed: " . mysqli_error($GLOBALS['dbinv']) . " | SQL: " . $sql1);
                            } else {
                                $totalSettled += $settledAmount;
                                error_log("UTIL DEBUG: Inserted record for invoice " . $_POST[$invno] . " with cheque " . $a_chq_no[$i] . " amount " . $settledAmount);
                            }
                            $invset = 0;
                        } else {
                            // Partial allocation - cheque amount less than invoice amount
                            $settledAmount = $chqbal;
                            $remainingInvoice = $invset - $chqbal;
                            $chqbal = 0;
                            
                            // Get invoice date
                            if (($_POST[$delidate] == "0000-00-00") || ($_POST[$delidate] == "")) {
                                $date1 = $_POST[$sdate] ?? date("Y-m-d");
                            } else {
                                $date1 = $_POST[$delidate];
                            }
                            
                            // Calculate days difference
                            $date2 = $a_chq_date[$i];
                            $diff = abs(strtotime($date2) - strtotime($date1));
                            $days = floor($diff / (60 * 60 * 24));
                            
                            // Insert partial utilization record
                            $col2 = str_replace(",", "", $_POST[$invval] ?? 0);
                            $sql1 = "insert into tmp_utilization(recno, invno, invdate, org_invdate, chqno, chqdate, chbank, settled, days, invval, c1, tmp_no) 
                                    values ('" . $_POST["recno"] . "', '" . $_POST[$invno] . "', '" . $date1 . "', '" . ($_POST[$sdate] ?? $date1) . "', 
                                    '" . $a_chq_no[$i] . "', '" . $a_chq_date[$i] . "', '" . $a_chq_bank[$i] . "', " . $settledAmount . ", " . $days . ", " . $col2 . ", 0, '" . $_SESSION["tmp_no_cashrec"] . "')";
                            $result = mysqli_query($GLOBALS['dbinv'], $sql1);
                            if (!$result) {
                                error_log("UTIL DEBUG: Partial insert failed: " . mysqli_error($GLOBALS['dbinv']));
                            } else {
                                $totalSettled += $settledAmount;
                                error_log("UTIL DEBUG: Inserted partial record for invoice " . $_POST[$invno]);
                            }
                            $invpos = $j;
                        }
                    }
                    $j++;
                }
                $i++;
            }
    
            // Process Cash payments (if any)
            $ii = 1;
            while ($_POST["mcount"] > $ii) {
                $cash_pay = "cash_pay" . $ii;
                $cash_pay_val = str_replace(",", "", ($_POST[$cash_pay] ?? 0));
                
                if ($cash_pay_val > 0) {
                    $invno = "invno" . $ii;
                    $invval = "invval" . $ii;
                    $delidate = "delidate" . $ii;
                    $sdate = "sdate" . $ii;
                    
                    // Get invoice date
                    if (($_POST[$delidate] == "0000-00-00") || ($_POST[$delidate] == "")) {
                        $date1 = $_POST[$sdate] ?? date("Y-m-d");
                    } else {
                        $date1 = $_POST[$delidate];
                    }
                    
                    // For cash payments, use receipt date
                    $date2 = $_POST["invdate"];
                    $diff = abs(strtotime($date2) - strtotime($date1));
                    $days = floor($diff / (60 * 60 * 24));
                    
                    $col2 = str_replace(",", "", $_POST[$invval] ?? 0);
                    $sql1 = "insert into tmp_utilization(recno, invno, invdate, org_invdate, chqno, chqdate, chbank, settled, days, invval, c1, tmp_no) 
                            values ('" . $_POST["recno"] . "', '" . $_POST[$invno] . "', '" . $date1 . "', '" . ($_POST[$sdate] ?? $date1) . "', 
                            'Cash', '" . $_POST["invdate"] . "', '', " . $cash_pay_val . ", " . $days . ", " . $col2 . ", 0, '" . $_SESSION["tmp_no_cashrec"] . "')";
                    $result = mysqli_query($GLOBALS['dbinv'], $sql1);
                    if (!$result) {
                        error_log("UTIL DEBUG: Cash insert failed: " . mysqli_error($GLOBALS['dbinv']));
                    } else {
                        error_log("UTIL DEBUG: Inserted cash record for invoice " . $_POST[$invno] . " amount " . $cash_pay_val);
                    }
                }
                $ii++;
            }
    
            // Build response XML
            $ResponseXML = "";
            $ResponseXML .= "<salesdetails>";
    
            $ResponseXML .= "<uti_table><![CDATA[ <table border=1 cellspacing=\"0\" cellpadding=\"0\" class=\"bcgl1\"><tr height=\"25\">
                    <td width=\"200\"  background=\"\">Invoice No</td>
                    <td width=\"100\"  background=\"\">Invoice Date</td>
                    <td width=\"230\"  background=\"\">Cheque No</td>
                    <td width=\"140\"  background=\"\">Cheque Date</td>
                    <td width=\"140\"  background=\"\">Settled</td>
                    <td width=\"140\"  background=\"\">Days</td>
                    </tr>";
    
            $sql = "select * from tmp_utilization where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "'";
            $result = mysqli_query($GLOBALS['dbinv'], $sql);
            $uti_row_count = 0;
            while ($row = mysqli_fetch_array($result)) {
                $uti_row_count++;
                $ResponseXML .= "<tr>
                    <td>" . $row["invno"] . "</td>
                    <td>" . $row["invdate"] . "</td>
                    <td>" . $row["chqno"] . "</td>
                    <td>" . $row["chqdate"] . "</td>
                    <td align=right>" . number_format($row["settled"], 2, ".", ",") . "</td>
                    <td>" . $row["days"] . "</td>
                    </tr>";
            }
            
            error_log("UTIL DEBUG: uti_table rows returned=" . $uti_row_count . " for tmp_no=" . $_SESSION["tmp_no_cashrec"]);
    
            $ResponseXML .= "   </table>]]></uti_table>";
            $ResponseXML .= "<chq_validity><![CDATA[yes]]></chq_validity>";
            $ResponseXML .= " </salesdetails>";
            echo $ResponseXML;
        } else {
            $ResponseXML = " <salesdetails>";
            $ResponseXML .= "<chq_validity><![CDATA[no]]></chq_validity>";
            $ResponseXML .= " </salesdetails>";
            echo $ResponseXML;
        }
    }

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



        if ($_SESSION["dev"] == "") {
            exit("no");
        }
        
        if ($_SESSION["company"] == "") {
            exit("no");
        }
       
        if ($_SESSION["tmp_no_cashrec"] != "") {



            $sqltmp = "select * from invpara  where flag='".$_SESSION['company']."'"; 
            $resulttmp = mysqli_query($GLOBALS['dbinv'], $sqltmp);
            $rowtmp = mysqli_fetch_array($resulttmp);

            if ($rowtmp["form_loc"] == "1") {
                exit("no11");
            }

            $sql_status = 0;

            mysqli_query($GLOBALS['dbinv'], "SET AUTOCOMMIT=0");
            mysqli_query($GLOBALS['dbinv'], "START TRANSACTION");

            // mysqli_query($GLOBALS['dbacc'], "SET AUTOCOMMIT=0");
            // mysqli_query($GLOBALS['dbacc'], "START TRANSACTION");

           

                if ($_SESSION['dev'] == "0") {

                    $sql = "select INV_RECNO from invpara  where flag='".$_SESSION['company']."'"; 
                    $result = mysqli_query($GLOBALS['dbinv'], $sql);
                    $row = mysqli_fetch_array($result);

                    $tmprecno = "000000" . $row["INV_RECNO"];
                    $lenth = strlen($tmprecno);
                    $recno = trim("".$_SESSION['company']."/PR/ ") . substr($tmprecno, $lenth - 6);
                    $_SESSION["recno"] = $recno;
                }

                if ($_SESSION['dev'] == "1") {

                    $sql = "select INV_RECNO_M from invpara  where flag='".$_SESSION['company']."'"; 
                    $result = mysqli_query($GLOBALS['dbinv'], $sql);
                    $row = mysqli_fetch_array($result);

                    $tmprecno = "000000" . $row["INV_RECNO_M"];
                    $lenth = strlen($tmprecno);
                    $recno = trim("".$_SESSION['company']."/PR/ ") . substr($tmprecno, $lenth - 6); 
                    $_SESSION["recno"] = $recno;
                }
            


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

            $sql = "select * from s_crec where CA_REFNO='" . $_POST["recno"] . "'";

            $result = mysqli_query($GLOBALS['dbinv'], $sql);
            if ($row = mysqli_fetch_array($result)) {
                $ResponseXML .= "<msg_dup><![CDATA[Reciept No Already Saved]]></msg_dup>";
                exit('Reciept No Already Saved');
            } else {
                $ResponseXML .= "<msg_dup><![CDATA[0]]></msg_dup>";
            }

            //	Call accsave====================================================================================???????????????
            //=============================================================================
            if ($recno != '') {
                if (is_numeric($_POST["cashtot"]) == true) {
                    $cashtot = $_POST["cashtot"];
                } else {
                    $cashtot = 0;
                }

                if (is_numeric($_POST["txtpaytot"]) == true) {
                    $txtpaytot = $_POST["txtpaytot"];
                } else {
                    $txtpaytot = 0;
                }

                if (is_numeric($_POST["txtoverpay"]) == true) {
                    $txtoverpay = $_POST["txtoverpay"];
                } else {
                    $txtoverpay = 0;
                }
                $recDate = $_POST["invdate"];
                $sql = "insert into s_crec(CA_REFNO, CA_DATE, CA_CODE, CA_CASSH, CA_AMOUNT, overpay, FLAG, pay_type, CA_SALESEX, CANCELL, tmp_no, DEPARTMENT, cus_ref, AC_REFNO, TTDATE, DEV,cashtype,company,remarks) values
	  ('" . $recno . "', '" . $recDate . "', '" . $_POST["cuscode"] . "', " . $cashtot . ", " . $txtpaytot . ", " . $txtoverpay . ", 'REC', '" . $_POST["paytype"] . "', '" . $_POST["salesrep"] . "', '0', '" . $_SESSION["tmp_no_cashrec"] . "', 'O', '0', '" . $_POST["ca_refno"] . "', '" . $_POST["dt"] . "',  '" . $_SESSION['dev'] . "' , '" . $_POST["cashtype"] . "','".$_SESSION['company']."', '" . $_POST["remarks"] . "')";
                //echo $sql;
                $result = mysqli_query($GLOBALS['dbinv'], $sql);
                if ($result != 1) {
                    $sql_status = 1;
                }
                $tmputiamou = 0;
                $sql = "select * from tmp_utilization where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "'";
                //echo $sql;
                $result = mysqli_query($GLOBALS['dbinv'], $sql);
                while ($row = mysqli_fetch_array($result)) {
                 
                    if ($row["chqno"] == "Cash") {
                        $ST_flag = "CAS";
                        $ST_CHDATE = $recDate;
                         $paytypenew='Cash';
                    } else if ($row["chqno"] == "Cash TT") {
                        $ST_flag = "Cash TT";
                        $ST_CHDATE = $_POST["dt"];
                         $paytypenew='Bank Deposit';
                    } else if ($row["chqno"] == "J/Entry") {
                        $ST_flag = "J/Entry";
                        $ST_CHDATE = $recDate;
                         $paytypenew='J/Entry';
                    } else {
                        $ST_flag = "CHK";
                        $ST_CHDATE = $row["chqdate"];
                         $paytypenew='Cheque';
                    }
                    
                    
                    if (is_numeric($row["settled"]) == true) {
                        $settled = $row["settled"];
                    } else {
                        $settled = 0;
                    }
                    $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,PAY_TYPE) values
	  ('" . $recno . "', '" . $recDate . "', '" . $row["invno"] . "', " . $settled . ", '" . $row["chqno"] . "', '" . $ST_CHDATE . "', '" . $ST_flag . "', '" . $row["days"] . "', '" . $row["days"] . "', '" . $row["chbank"] . "', '" . $_POST["cuscode"] . "', '" . $_SESSION['dev'] . "', 0, 0, 0, '0', 'O','".$_SESSION['company']."','".$paytypenew."')";
                    //  echo $sql1;
                    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                    if ($result1 != 1) {
                        $sql_status = 2;
                    }

                    $sql1 = "insert into s_sttr_all(ST_REFNO, ST_DATE, ST_INVONO, ST_PAID, netamount, ST_CHNO, st_chdate, ST_FLAG, st_days, ap_days, st_chbank, cus_code, cusname, sal_ex, DEV, del_days, deliin_days, deliin_amo, deliin_lock, department, form_type, trn_type,company) values
	  ('" . $recno . "', '" . $recDate . "', '" . $row["invno"] . "', " . $settled . ", " . (-1 * $settled) . ", '" . $row["chqno"] . "', '" . $ST_CHDATE . "', '" . $ST_flag . "', '" . $row["days"] . "', '" . $row["days"] . "', '" . $row["chbank"] . "', '" . $_POST["cuscode"] . "', '" . $_POST["cusname"] . "', '" . $_POST["salesrep"] . "', '" . $_SESSION['dev'] . "', 0, 0, 0, '0', 'O', 'REC', 'SET','".$_SESSION['company']."')";
                    //  echo $sql1;
                    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                    if ($result1 != 1) {
                        $sql_status = 3;
                    }

                    $tmputiamou = $tmputiamou + $row["settled"];
                }

                $i = 1;
                while ($_POST["mcount"] >= $i) {
                    $chq_pay = "chq_pay" . $i;
                    $cash_pay = "cash_pay" . $i;
                    $invno = "invno" . $i;
                    $cash = 0;
                    $chk = 0;
                    $tot = 0;

                    // Safely extract from $_POST falling back to 0
                    $post_chq_pay = $_POST[$chq_pay] ?? 0;
                    $post_cash_pay = $_POST[$cash_pay] ?? 0;

                    if ((is_numeric($post_chq_pay) == true and $post_chq_pay != 0) or (is_numeric($post_cash_pay) == true and $post_cash_pay != 0)) {

                        if (is_numeric($post_cash_pay) == true) {
                            $cash = $post_cash_pay;
                        } else {
                            $cash = 0;
                        }
                        if (is_numeric($post_chq_pay) == true) {
                            $chk = $post_chq_pay;
                        } else {
                            $chk = 0;
                        }

                        $tot = $cash + $chk;

                        $sql = "select brand from s_salma where REF_NO='" . $_POST[$invno] . "'";
                        $result = mysqli_query($GLOBALS['dbinv'], $sql);
                        if ($row = mysqli_fetch_array($result)) {
                            $sql1 = "select class from brand_mas where barnd_name='" . $row["brand"] . "'";
                            $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                            if ($row1 = mysqli_fetch_array($result1)) {
                                $sql2 = "update br_trn set credit=credit - " . $tot . " where cus_code = '" . $_POST["cuscode"] . "' and Class = '" . $row1["class"] . "'";
                                $result2 = mysqli_query($GLOBALS['dbinv'], $sql2);
                                if ($result2 != 1) {
                                    $sql_status = 4;
                                }
                            }
                        }

                        $sql2 = "update vendor set CUR_BAL=CUR_BAL - " . $tot . " where CODE = '" . $_POST["cuscode"] . "'";
                        $result2 = mysqli_query($GLOBALS['dbinv'], $sql2);
                        if ($result2 != 1) {
                            $sql_status = 5;
                        }

                        $sql2 = "update br_trn set credit=credit - " . $tot . " where cus_code = '" . $_POST["cuscode"] . "' and Rep='" . $_POST["salesrep"] . "'";
                        $result2 = mysqli_query($GLOBALS['dbinv'], $sql2);
                        if ($result2 != 1) {
                            $sql_status = 6;
                        }

                        $sql2 = "update s_salma set TOTPAY=TOTPAY + " . $tot . " where REF_NO = '" . $_POST[$invno] . "'";
                        $result2 = mysqli_query($GLOBALS['dbinv'], $sql2);
                        if ($result2 != 1) {
                            $sql_status = 7;
                        }

                        $sql2 = "update s_salma set CASH=CASH + " . $cash . " where REF_NO = '" . $_POST[$invno] . "'";
                        $result2 = mysqli_query($GLOBALS['dbinv'], $sql2);
                        if ($result2 != 1) {
                            $sql_status = 8;
                        }
                    }
                    $i = $i + 1;
                }

                $chqno = "";
                $tmpchkamou = 0;
                $sql = "select * from tmp_cash_chq where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "'";
                $result = mysqli_query($GLOBALS['dbinv'], $sql);
                while ($row = mysqli_fetch_array($result)) {
                    $sql1 = "select * from vendor where CODE='" . $_POST["cuscode"] . "'";
                    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                    $row1 = mysqli_fetch_array($result1);

                    if (is_numeric($row["chqamt"]) == true) {
                        $chqamt = $row["chqamt"];
                    } else {
                        $chqamt = 0;
                    }

                    $sql2 = "insert into s_invcheq(refno, Sdate, cus_code, CUS_NAME, cheque_no, che_date, bank, che_amount, sal_ex, trn_type, ex_flag, ch_owner, noof, ret_refno, ch_count_ret, department,company) values
	  ('" . $recno . "', '" . $recDate . "', '" . $_POST["cuscode"] . "', '" . $row1["NAME"] . "', '" . $row["chqno"] . "', '" . $row["chqdate"] . "', '" . $row["chqbank"] . "',  " . $chqamt . ", '" . $_POST["salesrep"] . "', 'REC', 'N', '" . $_POST["chqcollect"] . "', 0, '1', '0', 'O','".$_SESSION['company']."')";
                    $result2 = mysqli_query($GLOBALS['dbinv'], $sql2);
                    if ($result2 != 1) {
                        $sql_status = 9;
                    }
                    
                    $sql2 = "insert into s_invcheq1(refno, Sdate, cus_code, CUS_NAME, cheque_no, che_date, bank, che_amount, sal_ex, trn_type, ex_flag, ch_owner, noof, ret_refno, ch_count_ret, department,company) values
	  ('" . $recno . "', '" . $recDate . "', '" . $_POST["cuscode"] . "', '" . $row1["NAME"] . "', '" . $row["chqno"] . "', '" . $row["chqdate"] . "', '" . $row["chqbank"] . "',  " . $chqamt . ", '" . $_POST["salesrep"] . "', 'REC', 'N', '" . $_POST["chqcollect"] . "', 0, '1', '0', 'O','".$_SESSION['company']."')";
                    $result2 = mysqli_query($GLOBALS['dbinv'], $sql2);
                    if ($result2 != 1) {
                        $sql_status = 28;
                    }

                    $chqno = $chqno . " " . $row["chqno"] . "-" . $row["chqamt"];
                    $tmpchkamou = $tmpchkamou + $row["chqamt"];
                }

//                $n1 = floatval($tmputiamou + $_POST["txtoverpay"]);
//                $n2 = floatval($tmpchkamou);
                $n1 = floatval($_POST["txtpaytot"] + $_POST["txtoverpay"]);
                $n2 = floatval($_POST["chqtot"]);
                // if (($_POST["paytype"] == "Cheque")) {
                //     if ($n1 != $n2) {
                //         exit("Check amount and settlement amount not equal");
                //     }
                // }
//                $n1 = floatval($tmputiamou + $_POST["txtoverpay"]);
//                $n2 = floatval($tmpchkamou);
//                if (($_POST["paytype"] == "Cheque")) {
//                    if ($n1 != $n2) {
//                        exit("Check amount and settlement amount not equal");
//                    }
//                }
//                if (($_POST["paytype"] == "Cheque")) {
//                    if (number_format($tmputiamou, 1, ".", ",") + number_format($_POST["txtoverpay"], 1, ".", ",") != number_format($tmpchkamou, 1, ".", ",")) {
////                    if ($tmputiamou +$_POST["txtoverpay"] != $tmpchkamou) {
//                        exit("Check amount and settlement amount not equal");
//                    } else {
//                        
//                    }
//                }

                if (is_numeric($_POST["chqtot"]) == true) {
                    $chqtot = $_POST["chqtot"];
                } else {
                    $chqtot = 0;
                }

                if (is_numeric($_POST["cashtot"]) == true) {
                    $cashtot = $_POST["cashtot"];
                } else {
                    $cashtot = 0;
                }

                $cash_chq_tot = $chqtot + $cashtot;
                $sql = "insert into s_led(REF_NO, C_CODE, SDATE, FLAG, AMOUNT,company) values
	             ('" . $recno . "', '" . $_POST["cuscode"] . "', '" . $recDate . "', 'REC', " . $cash_chq_tot . ",'".$_SESSION['company']."')";
                $result = mysqli_query($GLOBALS['dbinv'], $sql);
                if ($result != 1) {
                    $sql_status = 10;
                }
                $sqlrep = "select * from s_salrep where REPCODE = '" . $_POST["salesrep"] . "'";
                $resultrep = mysqli_query($GLOBALS['dbinv'], $sqlrep);
                if ($rowrep = mysqli_fetch_array($resultrep)) {
                    $maindepart = $rowrep['RGROUP1'];
                } else {
                    $maindepart = "";
                }
                
                if ($_POST["txtoverpay"] > 0) {
                    $sql = "insert into c_bal(REFNO, SDATE, trn_type, CUSCODE, AMOUNT, BALANCE, SAL_EX, DEV,maindepartment,sdate1,c_code1,company) values
	  ('" . $recno . "', '" . $recDate . "', 'REC', '" . $_POST["cuscode"] . "',  " . $_POST["txtoverpay"] . ", " . $_POST["txtoverpay"] . ", '" . $_POST["salesrep"] . "', '" . $_SESSION["dev"] . "','" . $maindepart . "','".$recDate."','" . $_POST["cuscode"] . "','".$_SESSION['company']."')";
                    // echo $sql;
                    $result = mysqli_query($GLOBALS['dbinv'], $sql);
                    if ($result != 1) {
                        $sql_status = 11;
                    }

                    $sql1 = "insert into s_sttr_all(ST_REFNO, ST_DATE, ST_INVONO, ST_PAID, balance, netamount, cus_code, cusname, sal_ex, DEV, del_days, deliin_days, deliin_amo, deliin_lock, form_type, trn_type,company) values
	  ('" . $recno . "', '" . $recDate . "', '" . $recno . "', " . $_POST["txtoverpay"] . ", " . $_POST["txtoverpay"] . ", " . (-1 * $_POST["txtoverpay"]) . ", '" . $_POST["cuscode"] . "', '" . $_POST["cusname"] . "', '" . $_POST["salesrep"] . "', '" . $_SESSION['dev'] . "', 0, 0, 0, '0', 'REC', 'OVER','".$_SESSION['company']."')";
                    //echo $sql1;
                    $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                    if ($result1 != 1) {
                        $sql_status = 12;
                    }
                }
                if ($_SESSION['dev'] == "0") {
                    $sql2 = "update invpara set INV_RECNO=INV_RECNO + 1  where flag='".$_SESSION['company']."'"; 
                    $result2 = mysqli_query($GLOBALS['dbinv'], $sql2);
                    if ($result2 != 1) {
                        $sql_status = 13;
                    }
                }

                if ($_SESSION['dev'] == "1") {
                    $sql2 = "update invpara set INV_RECNO_M=INV_RECNO_M + 1  where flag='".$_SESSION['company']."'"; 
                    $result2 = mysqli_query($GLOBALS['dbinv'], $sql2);
                    if ($result2 != 1) {
                        $sql_status = 14;
                    }
                }

                //////////////////////////////		Account System////////////////////
                //include_once("connectioni.php");
                if (($_POST["paytype"] == "Cash") or ($_POST["paytype"] == "Cash TT") or ($_POST["paytype"] == "Cheque") or ($_POST["paytype"] == "Credit Card Commision")) {
//                if ($_POST["paytype"] == "Cash TT") {
                    //$mHead="CASH TT RECEIVED FROM ".$_POST["cuscode"]." ".$_POST["cusname"]." TO SETTLE RET.CHE.NO.- ".$chqno;
                    $mHead = $_POST["paytype"] . " RECEIVED FROM " . $_POST["cuscode"] . " " . $_POST["cusname"];

                    if (trim($_POST["ledg_ref_no"]) != "") {
                        $sql_rst = "update a_ledger set l_recno='" . $_POST["recno"] . "' where refno='" . trim($_POST["ledg_ref_no"]) . "'";
                        //echo $sql_rst;
                        $result_rst = mysqli_query($GLOBALS['dbinv'], $sql_rst);
                        if ($result_rst != 1) {
                            $sql_status = 15;
                        }
                    }

                    $sql_rst = "delete from a_bankdepmas where refno='" . trim($recno) . "'";
                    $result_rst = mysqli_query($GLOBALS['dbinv'], $sql_rst);
                    if ($result_rst != 1) {
                        $sql_status = 16;
                    }

                    $fulltot = $txtpaytot + $txtoverpay;

                    $sql_rst = "Insert into a_bankdepmas (refno, bdate, heading, code, name, amount, cash, comcode, cancel, type, tmp_no) Values 
                    ('" . trim($recno) . "', '" . $_POST["dt"] . "', '" . $mHead . "', '" . $_POST["accno"] . "', '" . $_POST["acc_name"] . "', " . $fulltot . ", 0,'" . $_SESSION['company'] . "', '0', 'D', '" . trim($recno) . "')";
                    //echo $sql_rst;
                    $result_rst = mysqli_query($GLOBALS['dbinv'], $sql_rst);
                    if ($result_rst != 1) {
                        $sql_status = 17;
                    }

                    $l_lmem = $_POST["paytype"] . " recieved from " . trim($_POST["cusname"]);
                    $l_lmem = $mHead;

                    $sql = "delete from  a_ledger where L_REFNO='" . trim($recno) . "'";
                    $result = mysqli_query($GLOBALS['dbinv'], $sql);
                    if ($result != 1) {
                        $sql_status = 18;
                    } 
                    
                    $sql = "insert into a_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) . "', '" . $_POST["dt"] . "', '" . trim($_POST["accno2"]) . "', " . $fulltot . ", 'REC', 'CRE', '" . $l_lmem . "', '0', 'R', 0, '" . $_SESSION['company'] . "', '', '0', " . date("Y") . ")";
                    //echo $sql;
                    $result = mysqli_query($GLOBALS['dbinv'], $sql);
                    if ($result != 1) {
                        $sql_status = 19;
                    }

                    $sql = "insert into a_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) . "', '" . $_POST["dt"] . "', '" . trim($_POST["accno"]) . "', " . $fulltot . ", 'REC', 'DEB', '" . $l_lmem . "', '0', 'R', 0, '" . $_SESSION['company'] . "', '', '0', " . date("Y") . ")";
                    //echo $sql;
                    $result = mysqli_query($GLOBALS['dbinv'], $sql);
                    if ($result != 1) {
                        $sql_status = 20;
                    }

                    $sql = "delete from a_bankdeptrn where refno='" . trim($recno) . "'";
                    $result = mysqli_query($GLOBALS['dbinv'], $sql);
                    if ($result != 1) {
                        $sql_status = 21;
                    }

                    $sql = "Insert into a_bankdeptrn(refno, bdate, code, amount, flag, nara, comcode, tmp_no) Values ('" . trim($recno) . "', '" . $_POST["dt"] . "', '" . $_POST["accno2"] . "', " . $_POST["txtpaytot"] . ", 'CRE', '" . trim($l_lmem) . "' ,'" . $_SESSION['company'] . "', '" . $_POST["recno"] . "')";
                    $result = mysqli_query($GLOBALS['dbinv'], $sql);
                    if ($result != 1) {
                        $sql_status = 22;
                    }

                    $sql = "select * from tmp_cash_chq where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "'";
                    $result = mysqli_query($GLOBALS['dbinv'], $sql);
                    while ($row = mysqli_fetch_array($result)) {

                        if (is_numeric($row["chqamt"]) == true) {
                            $chqamt = $row["chqamt"];
                        } else {
                            $chqamt = 0;
                        }

                        $sql2 = "delete from a_bankdeptrn where refno='" . trim($recno) . "'";
                        $result2 = mysqli_query($GLOBALS['dbinv'], $sql2);
                        if ($result2 != 1) {
                            $sql_status = 23;
                        }

                        $sql2 = "Insert into a_bankdeptrn(refno, bdate, code, amount, flag, nara, comcode, tmp_no) Values
                        ('" . trim($recno) . "', '" . $_POST["dt"] . "', '" . trim($_POST["accno"]) . "', " . $chqamt . ", 'DEB', '" . trim($l_lmem) . "', '" . $_SESSION['company'] . "', '" . trim($_POST["recno"]) . "' )";
                        $result2 = mysqli_query($GLOBALS['dbinv'], $sql2);
                        if ($result2 != 1) {
                            $sql_status = 24;
                        }

                        $sql1 = "delete from a_bankdepche where refno='" . trim($recno) . "'";
                        $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                        if ($result1 != 1) {
                            $sql_status = 25;
                        }

                        $sql1 = "Insert into a_bankdepche(refno, cheno, bdate, ven_code, ven_name, bank, amount ,comcode, id  ) Values ('" . trim($recno) . "', '" . $row["chqno"] . "', '" . $_POST["dt"] . "', '" . $l_lmem . "', '" . $l_lmem . "', '" . $row["chqbank"] . "', " . $chqamt . ",'" . $_SESSION['company'] . "', 0 )";
                        $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                        if ($result1 != 1) {
                            $sql_status = 26;
                        }
                    }
                }

                $sql1 = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values ('" . trim($recno) . "', '" . $_SESSION["CURRENT_USER"] . "', 'Cash Reciept', 'Save', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";
                //echo $sql1;
                $result1 = mysqli_query($GLOBALS['dbinv'], $sql1);
                if ($result1 != 1) {
                    $sql_status = 27;
                }

                //////////////////////////////////
                //include_once("connectioni.php");
                //echo $sql_status;
                if ($sql_status == 0) {

                    mysqli_query($GLOBALS['dbinv'], "COMMIT");
                    // mysqli_query($GLOBALS['dbacc'], "COMMIT");
                    $ResponseXML .= "<recno><![CDATA[" . $recno . "]]></recno>";
                    $ResponseXML .= "<msg_incom><![CDATA[0]]></msg_incom>";
                    //echo "Saved";
                } else {

                    mysqli_query($GLOBALS['dbinv'], "ROLLBACK");
                    // mysqli_query($GLOBALS['dbacc'], "ROLLBACK");
                    $ResponseXML .= "<msg_incom><![CDATA[Error has occured. Not Saved]]></msg_incom>";
                    //echo "Error has occured. Not Saved";
                }
            } else {
                $ResponseXML .= "<msg_incom><![CDATA[Incomplete Detail]]></msg_incom>";
            }

            $ResponseXML .= "<chq_rec_validity><![CDATA[yes]]></chq_rec_validity>";
            $ResponseXML .= "</salesdetails>";

            //$sql="delete from tmp_cash_chq where recno='".$recno."'";
            //$result =mysqli_query($GLOBALS['dbinv'],$sql) ;

            echo $ResponseXML;
        } else {

            $ResponseXML .= "<salesdetails>";
            $ResponseXML .= "<chq_rec_validity><![CDATA[no]]></chq_rec_validity>";
            $ResponseXML .= "</salesdetails>";
        }
    }
}
?>