<?php

session_start();

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


$GLOBALS['dbacc'] = $GLOBALS['dbinv'];

////////////////////////////////////////////// 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");

        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>";

                if (!isset($totchq)) { $totchq = 0.0; } 
                $totchq = $totchq + floatval(str_replace(",", "", $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>";

            if (!isset($totchq)) { $totchq = 0.0; } 
            $totchq = $totchq + floatval(str_replace(",", "", $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") {

        // Ensure we start with a clean XML header so JS can parse it
        $ResponseXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
    
        if ($_SESSION["dev"] != "") {
            
            $recno = ""; // Initialize variable to prevent undefined variable errors
    
            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);
            }
    
            // Cleaned up duplicate delete queries
            $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_utilization where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "'";
            $result = mysqli_query($GLOBALS['dbinv'], $sql);
            
            $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"] . "'";
        $result = mysqli_query($GLOBALS['dbinv'], $sql);

        $i = 1;
        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]);

            // FIXED: Use the sanitized string values directly instead of searching $_GET again
            if (($chq_pay_val != "" && $chq_pay_val != "0") || ($cash_pay_val != "" && $cash_pay_val != "0")) {
                // Convert to float safely before addition
                $paid = floatval($chq_pay_val) + floatval($cash_pay_val);
                
                $sql = "insert into  tmprct (SDATE, REFNO, AMOUNT, balance, paid, OVERDUE, flag) value ('" . $_GET[$sdate] . "', '" . $_GET[$invno] . "', '" . floatval($invval_val) . "', '" . floatval($overdue_val) . "', '" . $paid . "', 'INV')";
                $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>";
            }
        
            //sampath
        $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"] != "") {
            $i = 1;
            $a_chq_no = array();
            $a_chq_date = array();
            $a_chq_amt = array();
            $a_chq_bank = array();

            $chq_pay = "";
            $invno = "";
            $delidate = "";
            $available_inv_amt = 0;
            $available_chq_amt = 0;

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

            $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)) {
                $a_chq_date[$i] = $row["chqdate"];
                $a_chq_no[$i] = $row["chqno"];
                $a_chq_amt[$i] = floatval($row["chqamt"]); // Cast to float
                $a_chq_bank[$i] = $row["chqbank"];
                $i = $i + 1;
            }

            $mcou = $i;
            $j = 1;
            $chq_pay_8 = array();

            while ($j < $_POST["mcount"]) {
                $chq_pay = "chq_pay" . $j;
                if ($_POST[$chq_pay] != "") {
                    $chq_pay_8[$j] = floatval(str_replace(",", "", $_POST[$chq_pay])); // Safely cast
                } else {
                    $chq_pay_8[$j] = 0;
                }
                $j = $j + 1;
            }

            $invset = 0;
            $i = 1;
            $K = 1;
            $invpos = 1;

            while ($mcou >= $i) {
                if ($invset == 0) {
                    $j = 1;
                } else {
                    $j = $invpos;
                }
                $chqbal = $a_chq_amt[$i];
                $chqval = $a_chq_amt[$i];

                while (($j < $_POST["mcount"]) and ($chqbal > 0)) {
                    $chq_pay = "chq_pay" . $j;
                    $chq_balance = "chq_balance" . $j;
                    $invno = "invno" . $j;
                    $delidate = "delidate" . $j;
                    $invval = "invval" . $j;
                    $sdate = "sdate" . $j;

                    if ($invset == 0) {
                        $invset = $chq_pay_8[$j];
                        $chq_pay_8[$j] = 0;
                    }
                    if ($invset > 0) {
                        if ($invset <= $chqbal) {
                            $chqbal = $chqbal - $invset;

                            if (($_POST[$delidate] == "0000-00-00") or ($_POST[$delidate] == "")) {
                                $date1 = $_POST[$sdate];
                            } else {
                                $date1 = $_POST[$delidate];
                            }

                            $date2 = $a_chq_date[$i];
                            $diff = abs(strtotime($date2) - strtotime($date1));
                            $days = floor($diff / (60 * 60 * 24));

                            $col2 = floatval(str_replace(",", "", $_POST[$invval]));
                            $invset_val = floatval(str_replace(",", "", $invset));
                            if ($_POST[$sdate] == "") {
                                $msdate = "0000-00-00";
                            } else {
                                $msdate = $_POST[$sdate];
                            }
                            if ($date1 == "") {
                                $date1 = "0000-00-00";
                            }
                            
                            $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 . "', '" . $msdate . "', '" . $a_chq_no[$i] . "', '" . $a_chq_date[$i] . "', '" . $a_chq_bank[$i] . "', " . $invset_val . ", " . $days . ", " . $col2 . ", 0, '" . $_SESSION["tmp_no_cashrec"] . "')";
                            $result = mysqli_query($GLOBALS['dbinv'], $sql1);
                            $invset = 0;
                        } else {
                            if ($invset > 0) {
                                $invset = $invset - $chqbal;
                            }

                            if (($_POST[$delidate] == "0000-00-00") or ($_POST[$delidate] == "")) {
                                $date1 = $_POST[$sdate];
                            } else {
                                $date1 = $_POST[$delidate];
                            }
                            
                            $date2 = $a_chq_date[$i];
                            $diff = abs(strtotime($date2) - strtotime($date1));
                            $days = floor($diff / (60 * 60 * 24));

                            $col2 = floatval(str_replace(",", "", $_POST[$invval]));
                            $chqbal_val = floatval(str_replace(",", "", $chqbal));
                            if ($_POST[$sdate] == "") {
                                $msdate = "0000-00-00";
                            } else {
                                $msdate = $_POST[$sdate];
                            }

                            if ($date1 == "") {
                                $date1 = "0000-00-00";
                            }
                            $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 . "', '" . $msdate . "', '" . $a_chq_no[$i] . "', '" . $a_chq_date[$i] . "', '" . $a_chq_bank[$i] . "', " . $chqbal_val . ", " . $days . ", " . $col2 . ", 0, '" . $_SESSION["tmp_no_cashrec"] . "')";
                            $result = mysqli_query($GLOBALS['dbinv'], $sql1);

                            $chqbal = 0;
                            $invpos = $j;
                        }
                        $K = $K + 1;
                    }
                    $j = $j + 1;
                }
                $i = $i + 1;
            }
            $ii = 1;

            while ($_POST["mcount"] > $ii) {
                $cash_pay = "cash_pay" . $ii;
                $invno = "invno" . $ii;
                $delidate = "delidate" . $ii;
                $invval = "invval" . $ii;

                if ($_POST[$cash_pay] != "") {
                    if ($_POST["paytype"] == "Cash TT") {
                        $chqdate = $_POST["dt"];

                        if (($_POST[$delidate] == "0000-00-00") or ($_POST[$delidate] == "")) {
                            $date1 = $_POST[$sdate];
                        } else {
                            $date1 = $_POST[$delidate];
                        }
                        $date2 = $chqdate;
                        $diff = abs(strtotime($date2) - strtotime($date1));
                        $days = floor($diff / (60 * 60 * 24));
                    } else {
                        $chqdate = strtotime($_POST["invdate"]);

                        if (($_POST[$delidate] == "0000-00-00") or ($_POST[$delidate] == "")) {
                            $date1 = $_POST[$sdate];
                        } else {
                            $date1 = $_POST[$delidate];
                        }

                        $date2 = $_POST["invdate"];
                        $diff = abs(strtotime($date2) - strtotime($date1));
                        $days = floor($diff / (60 * 60 * 24));
                    }

                    $col2 = floatval(str_replace(",", "", $_POST[$invval]));
                    $cash_pay_val = floatval(str_replace(",", "", $_POST[$cash_pay]));
                    if ($_POST[$sdate] == "") {
                        $msdate = "0000-00-00";
                    } else {
                        $msdate = $_POST[$sdate];
                    }

                    if ($date1 == "") {
                        $date1 = "0000-00-00";
                    }

                    $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 . "', '" . $msdate . "', '" . $_POST["paytype"] . "', '" . $_POST["invdate"] . "', '" . $a_chq_bank[$i] . "', " . $cash_pay_val . ", " . $days . ", " . $col2 . ", 0, '" . $_SESSION["tmp_no_cashrec"] . "')";
                    $result = mysqli_query($GLOBALS['dbinv'], $sql1);
                    $K = $K + 1;
                }
                $ii = $ii + 1;
            }

            $invno_0 = array();
            $invdate_1 = array();
            $chqno_2 = array();
            $chqdate_3 = array();
            $settled_4 = array();
            $days_5 = array();

            $r = 1;
            $sql = "select * from tmp_utilization where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "'";
            $result = mysqli_query($GLOBALS['dbinv'], $sql);
            while ($row = mysqli_fetch_array($result)) {
                $id[$r] = $row["id"];
                $invno_0[$r] = $row["invno"];
                $invdate_1[$r] = $row["invdate"];
                $chqno_2[$r] = $row["chqno"];
                $chqdate_3[$r] = $row["chqdate"];
                $settled_4[$r] = floatval($row["settled"]); // Safely cast
                $days_5[$r] = $row["days"];
                $c1_6[$r] = floatval($row["c1"]); // Safely cast
                $r = $r + 1;
            }

            $S = 1;
            $deutot = 0.0;
            while ($_POST["mcount"] > $S) {
                $H = 10;
                while ($H != 0) {
                    $invno = "invno" . $S;
                    $cash_pay = "cash_pay" . $S;
                    $inv_balance = "inv_balance" . $S;
                    
                    // Securely format $_POST arrays to floats before mathematical operations
                    $posted_cash_pay = isset($_POST[$cash_pay]) ? floatval(str_replace(",", "", $_POST[$cash_pay])) : 0.0;
                    $posted_inv_balance = isset($_POST[$inv_balance]) ? floatval(str_replace(",", "", $_POST[$inv_balance])) : 0.0;
                    
                    $c1_next = isset($c1_6[$H + 1]) ? floatval($c1_6[$H + 1]) : 0.0;
                    $settled_next = isset($settled_4[$H + 1]) ? floatval($settled_4[$H + 1]) : 0.0;

                    if (isset($invno_0[$H]) && $_POST[$invno] == $invno_0[$H]) {
                        if (isset($invno_0[$H + 1]) && $invno_0[$H + 1] == $invno_0[$H]) {
                            if (trim($chqno_2[$H]) != "Cash") {
                                $c1_6[$H] = $c1_next + $settled_next - $posted_cash_pay;
                            } else {
                                $c1_6[$H] = $c1_next + $settled_next;
                            }
                            $sql11 = "update tmp_utilization set c1=" . $c1_6[$H] . " where id=" . $id[$H];
                            $result1 = mysqli_query($GLOBALS['dbinv'], $sql11);
                        } else {
                            if (trim($chqno_2[$H]) != "Cash") {
                                $c1_6[$H] = $posted_inv_balance - $posted_cash_pay;
                            } else {
                                $c1_6[$H] = $posted_inv_balance;
                            }
                            $sql11 = "update tmp_utilization set c1=" . $c1_6[$H] . " where id=" . $id[$H];
                            $result1 = mysqli_query($GLOBALS['dbinv'], $sql11);
                        }
                    }
                    $H = $H - 1;
                }
                
                // Add correctly casted string to Deutot
                $posted_inv_balance_for_deutot = isset($_POST["inv_balance".$S]) ? floatval(str_replace(",", "", $_POST["inv_balance".$S])) : 0.0;
                $deutot = $deutot + $posted_inv_balance_for_deutot;
                
                $S = $S + 1;
            }

            $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);
            while ($row = mysqli_fetch_array($result)) {
                $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>";
            }

            $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"] == "" || $_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");
    
            // 1. Determine Receipt No and Prepare the Update Query for invpara
            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);
                
                // Query to increment the counter
                $sql_invpara_update = "UPDATE invpara SET INV_RECNO = INV_RECNO + 1 WHERE flag='".$_SESSION['company']."'";
            }
    
            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); 
                
                // Query to increment the counter
                $sql_invpara_update = "UPDATE invpara SET INV_RECNO_M = INV_RECNO_M + 1 WHERE flag='".$_SESSION['company']."'";
            }
    
            $ResponseXML = "<salesdetails>";
    
            // 2. Check for Duplicates
            $sql_check = "select * from s_crec where CA_REFNO='" . $recno . "'";
            $result_check = mysqli_query($GLOBALS['dbinv'], $sql_check);
            if ($row_check = mysqli_fetch_array($result_check)) {
                mysqli_query($GLOBALS['dbinv'], "ROLLBACK");
                exit('Reciept No Already Saved');
            }
    
            if ($recno != '') {
                // 3. Update the invpara counter
                if (!mysqli_query($GLOBALS['dbinv'], $sql_invpara_update)) { $sql_status = 15; }
    
                // 4. Insert into s_crec - FIX: Ensure numeric values
                // Remove commas and convert to float
                $cashtot = isset($_POST["cashtot"]) ? floatval(str_replace(",", "", $_POST["cashtot"])) : 0;
                $txtpaytot = isset($_POST["txtpaytot"]) ? floatval(str_replace(",", "", $_POST["txtpaytot"])) : 0;
                $txtoverpay = isset($_POST["txtoverpay"]) ? floatval(str_replace(",", "", $_POST["txtoverpay"])) : 0;
                $recDate = $_POST["invdate"];
    
                $sql_ins = "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"] . "')";
                
                if (!mysqli_query($GLOBALS['dbinv'], $sql_ins)) { $sql_status = 1; }
    
                // 5. Utilization Processing - FIX: Ensure numeric values
                $sql_uti = "select * from tmp_utilization where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "'";
                $res_uti = mysqli_query($GLOBALS['dbinv'], $sql_uti);
                while ($row_uti = mysqli_fetch_array($res_uti)) {
                     if ($row_uti["chqno"] == "Cash") {
                        $ST_flag = "CAS"; $ST_CHDATE = $recDate; $paytypenew='Cash';
                    } else if ($row_uti["chqno"] == "Cash TT") {
                        $ST_flag = "Cash TT"; $ST_CHDATE = $_POST["dt"]; $paytypenew='Bank Deposit';
                    } else if ($row_uti["chqno"] == "J/Entry") {
                        $ST_flag = "J/Entry"; $ST_CHDATE = $recDate; $paytypenew='J/Entry';
                    } else {
                        $ST_flag = "CHK"; $ST_CHDATE = $row_uti["chqdate"]; $paytypenew='Cheque';
                    }
                    
                    $settled = isset($row_uti["settled"]) ? floatval($row_uti["settled"]) : 0;
                    
                    $sql_sttr = "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_uti["invno"] . "', " . $settled . ", '" . $row_uti["chqno"] . "', '" . $ST_CHDATE . "', '" . $ST_flag . "', '" . $row_uti["days"] . "', '" . $row_uti["days"] . "', '" . $row_uti["chbank"] . "', '" . $_POST["cuscode"] . "', '" . $_SESSION['dev'] . "', 0, 0, 0, '0', 'O','".$_SESSION['company']."','".$paytypenew."')";
                    if (!mysqli_query($GLOBALS['dbinv'], $sql_sttr)) { $sql_status = 2; }
    
                    $sql_all = "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_uti["invno"] . "', " . $settled . ", " . (-1 * $settled) . ", '" . $row_uti["chqno"] . "', '" . $ST_CHDATE . "', '" . $ST_flag . "', '" . $row_uti["days"] . "', '" . $row_uti["days"] . "', '" . $row_uti["chbank"] . "', '" . $_POST["cuscode"] . "', '" . $_POST["cusname"] . "', '" . $_POST["salesrep"] . "', '" . $_SESSION['dev'] . "', 0, 0, 0, '0', 'O', 'REC', 'SET','".$_SESSION['company']."')";
                    if (!mysqli_query($GLOBALS['dbinv'], $sql_all)) { $sql_status = 3; }
                }
    
                // 6. Update Balances - FIX: Ensure numeric values
                $i = 1;
                $mcount_val = isset($_POST['mcount']) ? intval($_POST['mcount']) : 0; 
                while ($mcount_val >= $i) {
                    $chq_pay = "chq_pay" . $i;
                    $cash_pay = "cash_pay" . $i;
                    $invno_key = "invno" . $i;
    
                    $chq_pay_val = isset($_POST[$chq_pay]) ? floatval(str_replace(",", "", $_POST[$chq_pay])) : 0;
                    $cash_pay_val = isset($_POST[$cash_pay]) ? floatval(str_replace(",", "", $_POST[$cash_pay])) : 0;
    
                    if ($chq_pay_val != 0 || $cash_pay_val != 0) {
                        $tot = $cash_pay_val + $chq_pay_val;
    
                        $sql_brand = "select brand from s_salma where REF_NO='" . $_POST[$invno_key] . "'";
                        $res_brand = mysqli_query($GLOBALS['dbinv'], $sql_brand); 
                        if ($row_b = mysqli_fetch_array($res_brand)) {
                            $sql_class = "select class from brand_mas where barnd_name='" . $row_b["brand"] . "'";
                            $res_class = mysqli_query($GLOBALS['dbinv'], $sql_class);
                            if ($row_c = mysqli_fetch_array($res_class)) {
                                $sql_br = "update br_trn set credit=credit - " . $tot . " where cus_code = '" . $_POST["cuscode"] . "' and Class = '" . $row_c["class"] . "'";
                                if (!mysqli_query($GLOBALS['dbinv'], $sql_br)) { $sql_status = 4; }
                            }
                        }
    
                        $sql_vendor = "update vendor set CUR_BAL=CUR_BAL - " . $tot . " where CODE = '" . $_POST["cuscode"] . "'";
                        if (!mysqli_query($GLOBALS['dbinv'], $sql_vendor)) { $sql_status = 5; }
    
                        $sql_br2 = "update br_trn set credit=credit - " . $tot . " where cus_code = '" . $_POST["cuscode"] . "' and Rep='" . $_POST["salesrep"] . "'";
                        if (!mysqli_query($GLOBALS['dbinv'], $sql_br2)) { $sql_status = 6; }
    
                        $sql_salma = "update s_salma set TOTPAY=TOTPAY + " . $tot . " where REF_NO = '" . $_POST[$invno_key] . "'";
                        if (!mysqli_query($GLOBALS['dbinv'], $sql_salma)) { $sql_status = 7; }
    
                        $sql_salma_cash = "update s_salma set CASH=CASH + " . $cash_pay_val . " where REF_NO = '" . $_POST[$invno_key] . "'";
                        if (!mysqli_query($GLOBALS['dbinv'], $sql_salma_cash)) { $sql_status = 8; }
                    }
                    $i++;
                }
    
                // 7. Cheque details - FIX: Ensure numeric values
                $sql_chq_tmp = "select * from tmp_cash_chq where tmp_no='" . $_SESSION["tmp_no_cashrec"] . "'";
                $res_chq_tmp = mysqli_query($GLOBALS['dbinv'], $sql_chq_tmp);
                while ($row_chq = mysqli_fetch_array($res_chq_tmp)) {
                    $sql_ven = "select * from vendor where CODE='" . $_POST["cuscode"] . "'";
                    $res_ven = mysqli_query($GLOBALS['dbinv'], $sql_ven);
                    $row_v = mysqli_fetch_array($res_ven);
    
                    $chqamt = isset($row_chq["chqamt"]) ? floatval($row_chq["chqamt"]) : 0;
    
                    $sql_ins_chq = "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"] . "', '" . $row_v["NAME"] . "', '" . $row_chq["chqno"] . "', '" . $row_chq["chqdate"] . "', '" . $row_chq["chqbank"] . "', " . $chqamt . ", '" . $_POST["salesrep"] . "', 'REC', 'N', '" . $_POST["chqcollect"] . "', 0, '1', '0', 'O','".$_SESSION['company']."')";
                    if (!mysqli_query($GLOBALS['dbinv'], $sql_ins_chq)) { $sql_status = 9; }
                    
                    $sql_ins_chq1 = "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"] . "', '" . $row_v["NAME"] . "', '" . $row_chq["chqno"] . "', '" . $row_chq["chqdate"] . "', '" . $row_chq["chqbank"] . "', " . $chqamt . ", '" . $_POST["salesrep"] . "', 'REC', 'N', '" . $_POST["chqcollect"] . "', 0, '1', '0', 'O','".$_SESSION['company']."')";
                    if (!mysqli_query($GLOBALS['dbinv'], $sql_ins_chq1)) { $sql_status = 28; }
                }
    
                // 8. Ledger and Overpayment - FIXED: Remove commas and convert properly
                $chqtot = isset($_POST["chqtot"]) ? floatval(str_replace(",", "", $_POST["chqtot"])) : 0;
                $cashtot = isset($_POST["cashtot"]) ? floatval(str_replace(",", "", $_POST["cashtot"])) : 0;
                $cash_chq_tot = $chqtot + $cashtot; // Both are now floats
    
                $sql_led = "insert into s_led(REF_NO, C_CODE, SDATE, FLAG, AMOUNT, company) values
                         ('" . $recno . "', '" . $_POST["cuscode"] . "', '" . $recDate . "', 'REC', " . $cash_chq_tot . ",'".$_SESSION['company']."')";
                if (!mysqli_query($GLOBALS['dbinv'], $sql_led)) { $sql_status = 10; }
    
                if ($txtoverpay > 0) {
                    $sql_over = "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"] . "', " . $txtoverpay . ", " . $txtoverpay . ", '" . $_POST["salesrep"] . "', '" . $_SESSION["dev"] . "','','".$recDate."','" . $_POST["cuscode"] . "','".$_SESSION['company']."')";
                    if (!mysqli_query($GLOBALS['dbinv'], $sql_over)) { $sql_status = 11; }
                }
    
                // Final Commitment
                if ($sql_status == 0) {
                    mysqli_query($GLOBALS['dbinv'], "COMMIT");
                    $ResponseXML .= "<recno><![CDATA[" . $recno . "]]></recno>";
                    $ResponseXML .= "<msg_incom><![CDATA[0]]></msg_incom>";
                    $ResponseXML .= "<msg_dup><![CDATA[0]]></msg_dup>";
                } else {
                    mysqli_query($GLOBALS['dbinv'], "ROLLBACK");
                    $ResponseXML .= "<msg_incom><![CDATA[Error: " . $sql_status . ". Not Saved]]></msg_incom>";
                }
            }
            $ResponseXML .= "<chq_rec_validity><![CDATA[yes]]></chq_rec_validity>";
            $ResponseXML .= "</salesdetails>";
            echo $ResponseXML;
        }
    }
}
?>