<?php
   

session_start();
require_once ("connection_sql.php");
header('Content-Type: text/xml');
date_default_timezone_set('Asia/Colombo');

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

    $ResponseXML = "";
    $ResponseXML .= "<salesdetails>";
    
    $sql = "delete from tmp_cusreg where C_USER = '" . $_SESSION["CURRENT_USER"] . "'";
    $result = $conn->query($sql);
    
    $ResponseXML .= "<sales_table><![CDATA[<table class=\"table table-bordered\"  style=\"width:100%\">
    <tr class='info'>               
        <td style='text-align:left;width: 75%;'><b>Customer Name</b></td> 
        <td style='text-align:left;width: 25%;'><b>Balance</b></td>
        </tr>";

    $sql = "select * from vendor  where CAT IN ('B', 'C') ";  
    
    if ($_POST['cus_name'] != "") {
        $sql .= " and NAME like '%" . $_POST['cus_name'] . "%'";
    }
     
    $sql .=" order by NAME ASC limit 25 ";
    $out=0;
    foreach ($conn->query($sql) as $row) {
       
        $cuscode = $row['CODE']; 
        
        $sql_bal = "Select SUM(GRAND_TOT-TOTPAY) AS TOTBALANCE from s_salma where C_CODE='".$row['CODE']."' and CANCELL='0'";
        
        $result_bal = $conn->query($sql_bal);
        $row_bal = $result_bal->fetch();
        
        $out=$row_bal['TOTBALANCE'];
        
        $ResponseXML .= "<tr >               
        <td onclick=\"pass_cus_inv('$cuscode');\" style='text-align:left;width: 75%;'> " . $row['NAME'] . " - " . $row['ADD1'] . "</a></td> 
        <td onclick=\"pass_cus_inv('$cuscode');\" style='text-align:right;width: 25%;'><b>" . number_format($out,2) . "</b></td>
        </tr>";
                 
    }

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

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

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

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

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

    $cuscode = $_GET["custno"];

    $sql = "select * from vendor where CODE ='" . $cuscode . "'";
    $result = $conn->query($sql);

    if ($row_ven = $result->fetch()) {
        $ResponseXML .= "<cus_code><![CDATA[" . $row_ven['CODE'] . "]]></cus_code>";
        $ResponseXML .= "<cus_name><![CDATA[" . $row_ven['NAME'] . "]]></cus_name>";
    }

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

if ($_POST["Command"] == "pass_data") { 
    
    $insert = "delete from tmp_cusreg where C_USER = '" . $_SESSION["CURRENT_USER"] . "'";
    $result = $conn->query($insert);
    
    $ResponseXML = "";
    $ResponseXML .= "<salesdetails>";
    
    if (($_POST['type'] == "INVOICE") ) {
        
        $sql_salma = "select SDATE,REF_NO,C_CODE,CUS_NAME,TYPE,GRAND_TOT,TOTPAY,DEPARTMENT,SAL_EX,c_code1,deli_date,company from s_salma where   REF_NO <> ''  ";
        
        if ($_POST['type'] == "INVOICE") {
            $sql_salma .= " and CANCELL='0' ";
        }
        if ($_POST['datecheck'] == "True") {
            $sql_salma .= "  and SDATE>='" . $_POST["dtfrom"] . "' and SDATE<='" . $_POST["dtto"] . "' ";
        }
        if ($_POST['dep'] != "") {
            $sql_salma .= " and DEPARTMENT='".$_POST['dep']."' ";
        }
        if ($_POST['salerep'] != "") {
            $sql_salma .= " and SAL_EX='".$_POST['salerep']."' ";
        }
        if ($_POST['cus_code'] != "") {
            $sql_salma .= " and C_CODE='".$_POST['cus_code']."' ";
        }
        // if ($_SESSION['company'] != "") {
        //     $sql_salma .= " and company='".$_SESSION['company']."' ";
        // }

        foreach ($conn->query($sql_salma) as $row_salma) { 
            
            $sql_log = "select refno,username,stime from entry_log where refno='".$row_salma["REF_NO"]."' and trnType='Save' "; 
            $result_log = $conn->query($sql_log);
            $row_log = $result_log->fetch();

            $salma[] = "('" . $row_salma["REF_NO"] . "', '" . $row_salma["SDATE"] . "', '" . $row_salma["deli_date"] . "','" . $row_salma["C_CODE"] . "','" . $row_salma["c_code1"] . "', '" . $row_salma["CUS_NAME"] . "', '" . $row_salma["GRAND_TOT"] . "', '" . $row_salma["TOTPAY"] . "', '" . $row_salma["DEPARTMENT"] . "', '" . $row_salma["SAL_EX"] . "', 'INVOICE','" . $_SESSION["CURRENT_USER"] . "','" . $row_log["username"] . "','" . $row_log["stime"] . "', '" . $row_salma["company"] . "')";
        
        }
        if (isset($salma)) {
            $insert = "insert into tmp_cusreg(REFNO, SDATE,DELI_DATE,CUS_CODE,CUS_CODE1,CUS_NAME,TOTAL,PAID,DEP,SAL_EX,TYPE,C_USER,USER,USERDTIME,COMPANY) values  
            " . implode($salma, ","); 
             $result = $conn->query($insert);
        }
        
    }
      
    if (($_POST['type'] == "RETURN") ) {
        
        $sql_crnma = "select REF_NO,SDATE,TRN_TYPE,DDATE,C_CODE,CUS_NAME,SAL_EX,GRAND_TOT,DEPARTMENT,INVOICENO,company from s_crnma where  REF_NO <> '' and TRN_TYPE='GRN'  ";
        
        if ($_POST['type'] == "RETURN") {
            $sql_crnma .= " and CANCELL='0' ";
        }
        if ($_POST['datecheck'] == "True") {
            $sql_crnma .= "  and SDATE>='" . $_POST["dtfrom"] . "' and SDATE<='" . $_POST["dtto"] . "' ";
        }
        if ($_POST['dep'] != "") {
            $sql_crnma .= " and DEPARTMENT='".$_POST['dep']."' ";
        }
        if ($_POST['salerep'] != "") {
            $sql_crnma .= " and SAL_EX='".$_POST['salerep']."' ";
        }
        if ($_POST['cus_code'] != "") {
            $sql_crnma .= " and C_CODE='".$_POST['cus_code']."' ";
        }
        // if ($_SESSION['company'] != "") {
        //     $sql_crnma .= " and company='".$_SESSION['company']."' ";
        // }
        
        $ret_paid=0;
        foreach ($conn->query($sql_crnma) as $row_crnma) { 
            
            $sql_log = "select refno,username,stime from entry_log where refno='".$row_crnma["REF_NO"]."' and trnType='Save' "; 
            $result_log = $conn->query($sql_log);
            $row_log = $result_log->fetch();
            
            $sql_cble = "select AMOUNT,BALANCE from c_bal where REFNO='".$row_crnma["REF_NO"]."'"; 
            $result_cble = $conn->query($sql_cble);
            $row_cble = $result_cble->fetch();
            
            $ret_paid=$row_cble["AMOUNT"]-$row_cble["BALANCE"];

            $crnma[] = "('" . $row_crnma["REF_NO"] . "', '" . $row_crnma["SDATE"] . "', '" . $row_crnma["DDATE"] . "','" . $row_crnma["C_CODE"] . "','" . $row_crnma["C_CODE"] . "', '" . $row_crnma["CUS_NAME"] . "', '" . $row_crnma["GRAND_TOT"] . "', '". $ret_paid ."', '" . $row_crnma["DEPARTMENT"] . "', '" . $row_crnma["SAL_EX"] . "', 'RETURN','" . $_SESSION["CURRENT_USER"] . "','" . $row_log["username"] . "','" . $row_log["stime"] . "', '" . $row_crnma["INVOICENO"] . "','" . $row_crnma["company"] . "')";
        
        }
        if (isset($crnma)) {
            $insert = "insert into tmp_cusreg(REFNO, SDATE,DELI_DATE,CUS_CODE,CUS_CODE1,CUS_NAME,TOTAL,PAID,DEP,SAL_EX,TYPE,C_USER,USER,USERDTIME,ORDNO,COMPANY) values  
            " . implode($crnma, ","); 
             $result = $conn->query($insert);
        }
    }  
    
    if (($_POST['type'] == "DEFECTIVE") ) {
        
        $sql_crnma = "select REF_NO,SDATE,TRN_TYPE,DDATE,C_CODE,CUS_NAME,SAL_EX,GRAND_TOT,DEPARTMENT,INVOICENO,company from s_crnma where  REF_NO <> '' and TRN_TYPE='DGRN'  ";
        
        if ($_POST['type'] == "DEFECTIVE") {
            $sql_crnma .= " and CANCELL='0' ";
        }
        if ($_POST['datecheck'] == "True") {
            $sql_crnma .= "  and SDATE>='" . $_POST["dtfrom"] . "' and SDATE<='" . $_POST["dtto"] . "' ";
        }
        if ($_POST['dep'] != "") {
            $sql_crnma .= " and DEPARTMENT='".$_POST['dep']."' ";
        }
        if ($_POST['salerep'] != "") {
            $sql_crnma .= " and SAL_EX='".$_POST['salerep']."' ";
        }
        if ($_POST['cus_code'] != "") {
            $sql_crnma .= " and C_CODE='".$_POST['cus_code']."' ";
        }
        // if ($_SESSION['company'] != "") {
        //     $sql_crnma .= " and company='".$_SESSION['company']."' ";
        // }
        
        $ret_paid=0;
        foreach ($conn->query($sql_crnma) as $row_crnma) { 
            
            $sql_log = "select refno,username,stime from entry_log where refno='".$row_crnma["REF_NO"]."' and trnType='Save' "; 
            $result_log = $conn->query($sql_log);
            $row_log = $result_log->fetch();
            
            $sql_cble = "select AMOUNT,BALANCE from c_bal where REFNO='".$row_crnma["REF_NO"]."'"; 
            $result_cble = $conn->query($sql_cble);
            $row_cble = $result_cble->fetch();
            
            $ret_paid=$row_cble["AMOUNT"]-$row_cble["BALANCE"];

            $crnma[] = "('" . $row_crnma["REF_NO"] . "', '" . $row_crnma["SDATE"] . "', '" . $row_crnma["DDATE"] . "','" . $row_crnma["C_CODE"] . "','" . $row_crnma["C_CODE"] . "', '" . $row_crnma["CUS_NAME"] . "', '" . $row_crnma["GRAND_TOT"] . "', '". $ret_paid ."', '" . $row_crnma["DEPARTMENT"] . "', '" . $row_crnma["SAL_EX"] . "', 'DEFECTIVE','" . $_SESSION["CURRENT_USER"] . "','" . $row_log["username"] . "','" . $row_log["stime"] . "', '" . $row_crnma["INVOICENO"] . "','" . $row_crnma["company"] . "')";
        
        }
        if (isset($crnma)) {
            $insert = "insert into tmp_cusreg(REFNO, SDATE,DELI_DATE,CUS_CODE,CUS_CODE1,CUS_NAME,TOTAL,PAID,DEP,SAL_EX,TYPE,C_USER,USER,USERDTIME,ORDNO,COMPANY) values  
            " . implode($crnma, ","); 
             $result = $conn->query($insert);
        }
    }  
    
    if (($_POST['type'] == "CREDIT NOTE") ) {
        
        $sql_cred = "select C_REFNO,C_DATE,C_CODE,C_SALEX,C_PAYMENT,maindepartment,C_INVNO,company from cred where   C_REFNO <> ''  ";
        
        if ($_POST['type'] == "CREDIT NOTE") {
            $sql_cred .= " and CANCELL='0' ";
        }
        if ($_POST['datecheck'] == "True") {
            $sql_cred .= "  and C_DATE>='" . $_POST["dtfrom"] . "' and C_DATE<='" . $_POST["dtto"] . "' ";
        }
        if ($_POST['dep'] != "") {
            $sql_cred .= " and maindepartment='".$_POST['dep']."' ";
        }
        if ($_POST['salerep'] != "") {
            $sql_cred .= " and C_SALEX='".$_POST['salerep']."' ";
        }
        if ($_POST['cus_code'] != "") {
            $sql_cred .= " and C_CODE='".$_POST['cus_code']."' ";
        }
        // if ($_SESSION['company'] != "") {
        //     $sql_cred .= " and company='".$_SESSION['company']."' ";
        // }
        
        $cnt_paid=0;
        foreach ($conn->query($sql_cred) as $row_cred) { 
            
            $sql_log = "select refno,username,stime from entry_log where refno='".$row_cred["C_REFNO"]."' and trnType='Save'"; 
            $result_log = $conn->query($sql_log);
            $row_log = $result_log->fetch();
            
            $sql_cble = "select AMOUNT,BALANCE from c_bal where REFNO='".$row_cred["C_REFNO"]."'"; 
            $result_cble = $conn->query($sql_cble);
            $row_cble = $result_cble->fetch();
            
            $cnt_paid=$row_cble["AMOUNT"]-$row_cble["BALANCE"];
            
            $sql_ven = "select CODE,NAME from vendor where CODE='".$row_cred["C_CODE"]."'"; 
            $result_ven = $conn->query($sql_ven);
            $row_ven = $result_ven->fetch();
            
            $cred[] = "('" . $row_cred["C_REFNO"] . "', '" . $row_cred["C_DATE"] . "','" . $row_cred["C_CODE"] . "','" . $row_cred["C_CODE"] . "', '" . $row_ven["NAME"] . "', '" . $row_cred["C_PAYMENT"] . "', '". $cnt_paid ."', '" . $row_cred["maindepartment"] . "', '" . $row_cred["C_SALEX"] . "', 'CREDIT NOTE','" . $_SESSION["CURRENT_USER"] . "','" . $row_log["username"] . "','" . $row_log["stime"] . "','" . $row_cred["C_INVNO"] . "','" . $row_cred["company"] . "')";
        
        }
        if (isset($cred)) {
            $insert = "insert into tmp_cusreg(REFNO, SDATE,CUS_CODE,CUS_CODE1,CUS_NAME,TOTAL,PAID,DEP,SAL_EX,TYPE,C_USER,USER,USERDTIME,ORDNO,COMPANY) values  
            " . implode($cred, ","); 
             $result = $conn->query($insert);
        }
    }

    if (($_POST['type'] == "UTILIZATION") ) {
        
        $sql_utmas = "select C_REFNO,C_DATE,C_CODE,C_Amount,C_CRNNo,company from s_utmas where   C_REFNO <> ''  ";
        
        if ($_POST['type'] == "UTILIZATION") {
            $sql_utmas .= " and CANCEL='0' ";
        }
        if ($_POST['datecheck'] == "True") {
            $sql_utmas .= "  and C_DATE>='" . $_POST["dtfrom"] . "' and C_DATE<='" . $_POST["dtto"] . "' ";
        }
         if ($_POST['cus_code'] != "") {
            $sql_utmas .= " and C_CODE='".$_POST['cus_code']."' ";
        }
        // if ($_SESSION['company'] != "") {
        //     $sql_utmas .= " and company='".$_SESSION['company']."' ";
        // }
        
        $ut_paid=0;
        foreach ($conn->query($sql_utmas) as $row_utmas) { 
            
            $sql_log = "select refno,username,stime from entry_log where refno='".$row_utmas["C_REFNO"]."' and trnType='Save'"; 
            $result_log = $conn->query($sql_log);
            $row_log = $result_log->fetch();
            
            $sql_cble = "select AMOUNT,BALANCE from c_bal where REFNO='".$row_utmas["C_REFNO"]."'"; 
            $result_cble = $conn->query($sql_cble);
            $row_cble = $result_cble->fetch();
            
            $ut_paid=$row_cble["AMOUNT"]-$row_cble["BALANCE"];
            
            $sql_ven = "select CODE,NAME from vendor where CODE='".$row_utmas["C_CODE"]."'"; 
            $result_ven = $conn->query($sql_ven);
            $row_ven = $result_ven->fetch();
            
            $utmas[] = "('" . $row_utmas["C_REFNO"] . "', '" . $row_utmas["C_DATE"] . "','" . $row_utmas["C_CODE"] . "','" . $row_utmas["C_CODE"] . "', '" . $row_ven["NAME"] . "', '" . $row_utmas["C_Amount"] . "', '". $ut_paid ."', 'UTILIZATION','" . $_SESSION["CURRENT_USER"] . "','" . $row_log["username"] . "','" . $row_log["stime"] . "','" . $row_utmas["C_CRNNo"] . "','" . $row_utmas["company"] . "')";
        
        }
        if (isset($utmas)) {
            $insert = "insert into tmp_cusreg(REFNO, SDATE,CUS_CODE,CUS_CODE1,CUS_NAME,TOTAL,PAID,TYPE,C_USER,USER,USERDTIME,ORDNO,COMPANY) values  
            " . implode($utmas, ","); 
             $result = $conn->query($insert);
        }
    }
    
    if (($_POST['type'] == "CANCEL INVOICE") ) {
        
        $sql_salma = "select SDATE,REF_NO,C_CODE,CUS_NAME,TYPE,GRAND_TOT,TOTPAY,DEPARTMENT,SAL_EX,c_code1,deli_date,company from s_salma where   REF_NO <> ''  ";
        
        if ($_POST['type'] == "CANCEL INVOICE") {
            $sql_salma .= " and CANCELL='1' ";
        }
        if ($_POST['datecheck'] == "True") {
            $sql_salma .= "  and SDATE>='" . $_POST["dtfrom"] . "' and SDATE<='" . $_POST["dtto"] . "' ";
        }
        if ($_POST['dep'] != "") {
            $sql_salma .= " and DEPARTMENT='".$_POST['dep']."' ";
        }
        if ($_POST['salerep'] != "") {
            $sql_salma .= " and SAL_EX='".$_POST['salerep']."' ";
        }
        if ($_POST['cus_code'] != "") {
            $sql_salma .= " and C_CODE='".$_POST['cus_code']."' ";
        }
        // if ($_SESSION['company'] != "") {
        //     $sql_salma .= " and company='".$_SESSION['company']."' ";
        // }

        foreach ($conn->query($sql_salma) as $row_salma) { 
            
            $sql_log = "select refno,username,stime from entry_log where refno='".$row_salma["REF_NO"]."' and trnType='Cancel' "; 
            $result_log = $conn->query($sql_log);
            $row_log = $result_log->fetch();

            $salma[] = "('" . $row_salma["REF_NO"] . "', '" . $row_salma["SDATE"] . "', '" . $row_salma["deli_date"] . "','" . $row_salma["C_CODE"] . "','" . $row_salma["c_code1"] . "', '" . $row_salma["CUS_NAME"] . "', '" . $row_salma["GRAND_TOT"] . "', '" . $row_salma["TOTPAY"] . "', '" . $row_salma["DEPARTMENT"] . "', '" . $row_salma["SAL_EX"] . "', 'CANCEL INVOICE','" . $_SESSION["CURRENT_USER"] . "','" . $row_log["username"] . "','" . $row_log["stime"] . "', '" . $row_salma["company"] . "')";
        
        }
        if (isset($salma)) {
            $insert = "insert into tmp_cusreg(REFNO, SDATE,DELI_DATE,CUS_CODE,CUS_CODE1,CUS_NAME,TOTAL,PAID,DEP,SAL_EX,TYPE,C_USER,USER,USERDTIME,COMPANY) values  
            " . implode($salma, ","); 
             $result = $conn->query($insert);
        }
    }
    
    if (($_POST['type'] == "RECEIPT DETAILS") ) {
        
        $sql_salma_stttr = "select ST_REFNO,ST_DATE,ST_INVONO,PAY_TYPE,C_CODE,CUS_NAME,SALMA_DEP,SAL_EX,CANCELL,cus_code,GRAND_TOT,ST_PAID,company from view_salma_s_sttr where  ST_REFNO <> ''  ";
        
        if ($_POST['type'] == "RECEIPT DETAILS") {
            $sql_salma_stttr .= " and CANCELL='0' ";
        }
        if ($_POST['datecheck'] == "True") {
            $sql_salma_stttr .= "  and ST_DATE>='" . $_POST["dtfrom"] . "' and ST_DATE<='" . $_POST["dtto"] . "' ";
        }
        if ($_POST['dep'] != "") {
            $sql_salma_stttr .= " and SALMA_DEP='".$_POST['dep']."' ";
        }
        if ($_POST['salerep'] != "") {
            $sql_salma_stttr .= " and SAL_EX='".$_POST['salerep']."' ";
        }
        if ($_POST['cus_code'] != "") {
            $sql_salma_stttr .= " and C_CODE='".$_POST['cus_code']."' ";
        }
        // if ($_SESSION['company'] != "") {
        //     $sql_salma_stttr .= " and company='".$_SESSION['company']."' ";
        // }

        foreach ($conn->query($sql_salma_stttr) as $row_salma_stttr) { 
            
            $sql_log = "select refno,username,stime from entry_log where refno='".$row_salma_stttr["ST_REFNO"]."' and trnType='Save' "; 
            $result_log = $conn->query($sql_log);
            $row_log = $result_log->fetch();

            $salma_stttr[] = "('" . $row_salma_stttr["ST_REFNO"] . "', '" . $row_salma_stttr["ST_DATE"] . "', '" . $row_salma_stttr["SDATE"] . "','" . $row_salma_stttr["C_CODE"] . "','" . $row_salma_stttr["cus_code"] . "', '" . $row_salma_stttr["CUS_NAME"] . "', '', '" . $row_salma_stttr["ST_PAID"] . "', '" . $row_salma_stttr["SALMA_DEP"] . "', '" . $row_salma_stttr["SAL_EX"] . "', 'RECEIPT DETAILS','" . $_SESSION["CURRENT_USER"] . "','" . $row_log["username"] . "','" . $row_log["stime"] . "','" . $row_salma_stttr["ST_INVONO"] . "','" . $row_salma_stttr["PAY_TYPE"] . "','" . $row_salma_stttr["company"] . "')";
        
        }
        if (isset($salma_stttr)) {
            $insert = "insert into tmp_cusreg(REFNO, SDATE,DELI_DATE,CUS_CODE,CUS_CODE1,CUS_NAME,TOTAL,PAID,DEP,SAL_EX,TYPE,C_USER,USER,USERDTIME,ORDNO,PAY_TYPE,COMPANY) values  
            " . implode($salma_stttr, ","); 
             $result = $conn->query($insert);
        }
    }

    if (($_POST['type'] == "RECEIPT SUMMRY") ) {
        
        $sql_crec = "select CA_REFNO,CA_CODE,CA_AMOUNT,CANCELL,pay_type,CA_DATE,INVNO,company,CA_CASSH,overpay from s_crec where  CA_REFNO <> ''  ";
        
        if ($_POST['type'] == "RECEIPT SUMMRY") {
            $sql_crec .= " and CANCELL='0' ";
        }
        if ($_POST['datecheck'] == "True") {
            $sql_crec .= "  and CA_DATE>='" . $_POST["dtfrom"] . "' and CA_DATE<='" . $_POST["dtto"] . "' ";
        }
        if ($_POST['dep'] != "") {
            $sql_crec .= " and SALMA_DEP='".$_POST['dep']."' ";
        }
        if ($_POST['salerep'] != "") {
            $sql_crec .= " and SAL_EX='".$_POST['salerep']."' ";
        }
        if ($_POST['cus_code'] != "") {
            $sql_crec .= " and CA_CODE='".$_POST['cus_code']."' ";
        }
        // if ($_SESSION['company'] != "") {
        //     $sql_crec .= " and company='".$_SESSION['company']."' ";
        // }
        
        $tot_settel=0;
        foreach ($conn->query($sql_crec) as $row_crec) { 
            
            $sql_log = "select refno,username,stime from entry_log where refno='".$row_crec["ST_REFNO"]."' and trnType='Save' "; 
            $result_log = $conn->query($sql_log);
            $row_log = $result_log->fetch();
            
            $sql_ven = "select CODE,NAME from vendor where CODE='".$row_crec["CA_CODE"]."'"; 
            $result_ven = $conn->query($sql_ven);
            $row_ven = $result_ven->fetch();
            
            $tot_settel=$row_crec["CA_AMOUNT"]+$row_crec["overpay"];
            
            $crec[] = "('" . $row_crec["CA_REFNO"] . "', '" . $row_crec["CA_DATE"] . "', '" . $row_crec["CA_DATE"] . "','" . $row_crec["CA_CODE"] . "','" . $row_crec["CA_CODE"] . "', '" . $row_ven["NAME"] . "', '". $tot_settel ."', '" . $row_crec["CA_AMOUNT"] . "', '', '" . $row_crec["CA_SALSEX"] . "', 'RECEIPT SUMMRY','" . $_SESSION["CURRENT_USER"] . "','" . $row_log["username"] . "','" . $row_log["stime"] . "','" . $row_crec["INVNO"] . "','" . $row_crec["pay_type"] . "','" . $row_crec["company"] . "','" . $row_crec["overpay"] . "')";
        
        }
        if (isset($crec)) {
            $insert = "insert into tmp_cusreg(REFNO, SDATE,DELI_DATE,CUS_CODE,CUS_CODE1,CUS_NAME,TOTAL,PAID,DEP,SAL_EX,TYPE,C_USER,USER,USERDTIME,ORDNO,PAY_TYPE,COMPANY,OVER_PAY) values  
            " . implode($crec, ","); 
             $result = $conn->query($insert);
        }
    }

    if (($_POST['type'] == "OVER PAY") ) {
        
        $sql_cbal = "select REFNO,SDATE,CUSCODE,AMOUNT,BALANCE,DEP,SAL_EX,CANCELL,trn_type,c_code1,company from c_bal where  REFNO <> '' and trn_type !='ARN' ";
        
        if ($_POST['type'] == "OVER PAY") {
            $sql_cbal .= " and CANCELL='0' ";
        }
        if ($_POST['datecheck'] == "True") {
            $sql_cbal .= "  and SDATE>='" . $_POST["dtfrom"] . "' and SDATE<='" . $_POST["dtto"] . "' ";
        }
        if ($_POST['dep'] != "") {
            $sql_cbal .= " and DEP='".$_POST['dep']."' ";
        }
        if ($_POST['salerep'] != "") {
            $sql_cbal .= " and SAL_EX='".$_POST['salerep']."' ";
        }
        if ($_POST['cus_code'] != "") {
            $sql_cbal .= " and CUSCODE='".$_POST['cus_code']."' ";
        }
        // if ($_SESSION['company'] != "") {
        //     $sql_cbal .= " and company='".$_SESSION['company']."' ";
        // }
        
        $ordno="";
        $paid_cbal=0;
        foreach ($conn->query($sql_cbal) as $row_cbal) { 
            
            $sql_log = "select refno,username,stime from entry_log where refno='".$row_cbal["REFNO"]."' and trnType='Save' "; 
            $result_log = $conn->query($sql_log);
            $row_log = $result_log->fetch();
            
            $sql_ven = "select CODE,NAME from vendor where CODE='".$row_cbal["CUSCODE"]."'"; 
            $result_ven = $conn->query($sql_ven);
            $row_ven = $result_ven->fetch();
            
            $paid_cbal=$row_cbal["AMOUNT"]-$row_cbal["BALANCE"];
            
            if ($row_cbal["trn_type"]=='CNT'){
                $type2='CREDIT NOTE';
                
                $sql_cnt = "select C_REFNO,C_INVNO from cred where C_REFNO='".$row_cbal["REFNO"]."'"; 
                $result_cnt = $conn->query($sql_cnt);
                $row_cnt = $result_cnt->fetch();
                
                $ordno=$row_cnt["C_INVNO"];
            
            } else if ($row_cbal["trn_type"]=='GRN'){
                $type2='SALES RETURN';
                
                $sql_grn = "select REF_NO,INVOICENO from s_crnma where REF_NO='".$row_cbal["REFNO"]."'"; 
                $result_grn = $conn->query($sql_grn);
                $row_grn = $result_grn->fetch();
                
                $ordno=$row_grn["INVOICENO"];
                
            } else if ($row_cbal["trn_type"]=='DGRN'){
                $type2='DEFECTIVE';
                
                $sql_grn = "select REF_NO,INVOICENO from s_crnma where REF_NO='".$row_cbal["REFNO"]."'"; 
                $result_grn = $conn->query($sql_grn);
                $row_grn = $result_grn->fetch();
                
                $ordno=$row_grn["INVOICENO"];
                
            } else if ($row_cbal["trn_type"]=='REC'){
                $type2='RECEIPT';
                
                $sql_rec = "select CA_REFNO,INVNO from s_crec where CA_REFNO='".$row_cbal["REFNO"]."'"; 
                $result_rec = $conn->query($sql_rec);
                $row_rec = $result_rec->fetch();
                
                $ordno=$row_rec["INVNO"];
                
            } else if ($row_cbal["trn_type"]=='PAY'){
                $type2='ADVANCED PAYMENT';
                
                $sql_rec = "select C_REFNO,C_INVNO from s_crec where CA_REFNO='".$row_cbal["REFNO"]."'"; 
                $result_rec = $conn->query($sql_rec);
                $row_rec = $result_rec->fetch();
                
                $ordno=$row_rec["C_INVNO"];
            }

            $cbal[] = "('" . $row_cbal["REFNO"] . "', '" . $row_cbal["SDATE"] . "','" . $row_cbal["CUSCODE"] . "','" . $row_cbal["c_code1"] . "', '" . $row_ven["NAME"] . "','" . $row_cbal["AMOUNT"] . "', '" . $paid_cbal . "', '" . $row_cbal["DEP"] . "', '" . $row_cbal["SAL_EX"] . "', 'OVER PAY','" . $_SESSION["CURRENT_USER"] . "','" . $row_log["username"] . "','" . $row_log["stime"] . "', '" . $row_cbal["company"] . "','".$type2."','". $ordno ."')";
        
        }
        if (isset($cbal)) {
            $insert = "insert into tmp_cusreg(REFNO, SDATE,CUS_CODE,CUS_CODE1,CUS_NAME,TOTAL,PAID,DEP,SAL_EX,TYPE,C_USER,USER,USERDTIME,COMPANY,TYPE2,ORDNO) values  
            " . implode($cbal, ","); 
             $result = $conn->query($insert);
        }
    }
    
    if (($_POST['type'] == "CHEQUE LIST") ) {
        
        $sql_invcheq = "select refno,Sdate,cus_code,CUS_NAME,sal_ex,cheque_no,che_date,bank,che_amount,CANCELL,company from s_invcheq where   refno <> ''  ";
        
        if ($_POST['type'] == "CHEQUE LIST") {
            $sql_invcheq .= " and CANCELL='0' ";
        }
        if ($_POST['datecheck'] == "True") {
            $sql_invcheq .= "  and Sdate>='" . $_POST["dtfrom"] . "' and Sdate<='" . $_POST["dtto"] . "' ";
        }
        if ($_POST['dep'] != "") {
            $sql_invcheq .= " and DEPARTMENT='".$_POST['dep']."' ";
        }
        if ($_POST['salerep'] != "") {
            $sql_invcheq .= " and SAL_EX='".$_POST['salerep']."' ";
        }
        if ($_POST['cus_code'] != "") {
            $sql_invcheq .= " and cus_code='".$_POST['cus_code']."' ";
        }
        // if ($_SESSION['company'] != "") {
        //     $sql_invcheq .= " and company='".$_SESSION['company']."' ";
        // }
        
        foreach ($conn->query($sql_invcheq) as $row_invcheq) { 
            
            $sql_log = "select refno,username,stime from entry_log where refno='".$row_invcheq["refno"]."' and trnType='Save' "; 
            $result_log = $conn->query($sql_log);
            $row_log = $result_log->fetch();

            $invcheq[] = "('" . $row_invcheq["refno"] . "', '" . $row_invcheq["Sdate"] . "','" . $row_invcheq["cus_code"] . "','" . $row_invcheq["cus_code"] . "', '" . $row_invcheq["CUS_NAME"] . "', '" . $row_invcheq["che_amount"] . "', '', '" . $row_invcheq["sal_ex"] . "', 'CHEQUE LIST','" . $_SESSION["CURRENT_USER"] . "','" . $row_log["username"] . "','" . $row_log["stime"] . "', '" . $row_invcheq["company"] . "', '" . $row_invcheq["cheque_no"] . "', '" . $row_invcheq["che_date"] . "', '" . $row_invcheq["bank"] . "')";
        
        }
        if (isset($invcheq)) {
            $insert = "insert into tmp_cusreg(REFNO, SDATE,CUS_CODE,CUS_CODE1,CUS_NAME,TOTAL,PAID,SAL_EX,TYPE,C_USER,USER,USERDTIME,COMPANY,CHQ_NO,CHQ_DATE,CHQ_BANK) values  
            " . implode($invcheq, ","); 
             $result = $conn->query($insert);
        }
        
    }
    
    if (($_POST['type'] == "SETTLEMENT") ) {
       
        $sql_settlement = "select * from view_sttr where   ST_REFNO <> ''  ";
        
        if ($_POST['type'] == "SETTLEMENT") {
            $sql_settlement .= "  and CANCELL='0' ";
        }
        if ($_POST['datecheck'] == "True") {
            $sql_settlement .= "  and ST_DATE>='" . $_POST["dtfrom"] . "' and ST_DATE<='" . $_POST["dtto"] . "' ";
        }
        if ($_POST['dep'] != "") {
            $sql_settlement .= " and SALMADEP='".$_POST['dep']."' ";
        }
        if ($_POST['salerep'] != "") {
            $sql_settlement .= " and SAL_EX='".$_POST['salerep']."' ";
        }
        if ($_POST['cus_code'] != "") {
            $sql_settlement .= " and C_CODE='".$_POST['cus_code']."' ";
        }
        // if ($_SESSION['company'] != "") {
        //     $sql_settlement .= " and company='".$_SESSION['company']."' ";
        // }  
        
        foreach ($conn->query($sql_settlement) as $row_settlement) { 
            
            $sql_log = "select refno,username,stime from entry_log where refno='".$row_settlement["ST_REFNO"]."' and trnType='Save' "; 
            $result_log = $conn->query($sql_log);
            $row_log = $result_log->fetch();

            $settlement[] = "('" . $row_settlement["ST_REFNO"] . "', '" . $row_settlement["SDATE"] . "','" . $row_settlement["C_CODE"] . "','" . $row_settlement["C_CODE"] . "', '" . $row_settlement["CUS_NAME"] . "', '" . $row_settlement["ST_INVONO"] . "', '" . $row_settlement["GRAND_TOT"] . "', '" . $row_settlement["TOTPAY"] . "',  '" . $row_settlement["sal_ex"] . "', 'SETTLEMENT','" . $_SESSION["CURRENT_USER"] . "','" . $row_log["username"] . "','" . $row_log["stime"] . "', '" . $row_settlement["company"] . "', '" . $row_settlement["ST_CHNO"] . "', '" . $row_settlement["st_chdate"] . "', '" . $row_settlement["st_chbank"] . "', '" . $row_settlement["STTR_PAYTYPE"] . "', '" . $row_settlement["ST_PAID"] . "')";
        
        }
         
        if (isset($settlement)) {
            $insert = "insert into tmp_cusreg(REFNO, SDATE,CUS_CODE,CUS_CODE1,CUS_NAME,ORDNO,TOTAL,PAID,SAL_EX,TYPE,C_USER,USER,USERDTIME,COMPANY,CHQ_NO,CHQ_DATE,CHQ_BANK,PAY_TYPE,PAY) values  
            " . implode($settlement, ","); 
             $result = $conn->query($insert);
        }
        
    }
        
    $ResponseXML = "";
    $ResponseXML .= "<salesdetails>";

    $ResponseXML .= "<item_details><![CDATA[ <table class='table table-bordered'> ";
    
    if ($_POST['type'] == "INVOICE") {
        $ResponseXML .= "<tr class='info'>
          <td>No</td>
          <td>Ref No</td>
          <td>Date</td>
          <td>Deli: Date</td>
          <td>Customer Code</td>
          <td>Customer Name</td>
          <td>Rep</td>
          <td>Total</td>
          <td>Paid</td>
          <td>Balance</td>
          <td>Type</td>
          <td>User Date & Time</td>
        </tr>";
    }
    
    if ($_POST['type'] == "RETURN") {
        $ResponseXML .= "<tr class='info'>
          <td>No</td>
          <td>Ref No</td>
          <td>Date</td>
          <td>Invoice No</td>
          <td>Customer Code</td>
          <td>Customer Name</td>
          <td>Rep</td>
          <td>Total</td>
          <td>Paid</td>
          <td>Balance</td>
          <td>Type</td>
          <td>User Date & Time</td>
        </tr>";
    }
    
    if ($_POST['type'] == "DEFECTIVE") {
        $ResponseXML .= "<tr class='info'>
          <td>No</td>
          <td>Ref No</td>
          <td>Date</td>
          <td>Invoice No</td>
          <td>Customer Code</td>
          <td>Customer Name</td>
          <td>Rep</td>
          <td>Total</td>
          <td>Paid</td>
          <td>Balance</td>
          <td>Type</td>
          <td>User Date & Time</td>
        </tr>";
    }
    
    if ($_POST['type'] == "CREDIT NOTE") {
        $ResponseXML .= "<tr class='info'>
          <td>No</td>
          <td>Ref No</td>
          <td>Date</td>
          <td>Invoice No</td>
          <td>Customer Code</td>
          <td>Customer Name</td>
          <td>Rep</td>
          <td>Total</td>
          <td>Paid</td>
          <td>Balance</td>
          <td>Type</td>
          <td>User Date & Time</td>
        </tr>";
    }
    
    if ($_POST['type'] == "UTILIZATION") {
        $ResponseXML .= "<tr class='info'>
          <td>No</td>
          <td>Ref No</td>
          <td>Date</td>
          <td>Order No</td>
          <td>Customer Code</td>
          <td>Customer Name</td>
          <td>Total</td>
          <td>Type</td>
          <td>User Date & Time</td>
        </tr>";
    }

    if ($_POST['type'] == "CANCEL INVOICE") {
        $ResponseXML .= "<tr class='info'>
          <td>No</td>
          <td>Ref No</td>
          <td>Date</td>
          <td>Deli: Date</td>
          <td>Customer Code</td>
          <td>Customer Name</td>
          <td>Rep</td>
          <td>Total</td>
          <td>Paid</td>
          <td>Balance</td>
          <td>Type</td>
          <td>User Date & Time</td>
        </tr>";
    }
    
    if ($_POST['type'] == "RECEIPT DETAILS") {
        $ResponseXML .= "<tr class='info'>
          <td>No</td>
          <td>Ref No</td>
          <td>Date</td>
          <td>Invoice No</td>
          <td>Customer Code</td>
          <td>Customer Name</td>
          <td>Cash</td>
          <td>Cheque</td>
          <td>Cash TT</td>
          <td>Card</td>
          <td>Online Pay</td>
          <td>Gift Card</td>
          <td>Utilize</td>
          <td>Type</td>
          <td>User Date & Time</td>
        </tr>";
    }
    
    if ($_POST['type'] == "RECEIPT SUMMRY") {
        $ResponseXML .= "<tr class='info'>
          <td>No</td>
          <td>Ref No</td>
          <td>Date</td>
          <td>Invoice No</td>
          <td>Customer Code</td>
          <td>Customer Name</td>
          <td>Over Pay</td>
          <td>Paid</td>
          <td>Total</td>
          <td>Type</td>
          <td>User Date & Time</td>
        </tr>";
    }

    if ($_POST['type'] == "OVER PAY") {
        $ResponseXML .= "<tr class='info'>
          <td>No</td>
          <td>Ref No</td>
          <td>Date</td>
          <td>Invoice No</td>
          <td>Customer Code</td>
          <td>Customer Name</td>
          <td>Rep</td>
          <td>Total</td>
          <td>Paid</td>
          <td>Balance</td>
          <td>Type</td>
          <td>Type</td>
          <td>User Date & Time</td>
        </tr>";
    }

    if ($_POST['type'] == "CHEQUE LIST") {
        $ResponseXML .= "<tr class='info'>
          <td>No</td>
          <td>Ref No</td>
          <td>Date</td>
          <td>Customer Code</td>
          <td>Customer Name</td>
          <td>Rep</td>
          <td>Cheque No</td>
          <td>Cheque Date</td>
          <td>Cheque Bank</td>
          <td>Total</td>
          <td>Type</td>
          <td>User Date & Time</td>
        </tr>";
    }
    
    if ($_POST['type'] == "SETTLEMENT") {
        $ResponseXML .= "<tr class='info'><td>Date</td>
            <td>Inv.No</td>
            <td>Customer</td>
            <td>Inv.Amo</td>
            <td>Due.</td>
            <td widtd=150>Rec.No</td>
            <td>Che.No</td>
            <td>Che.Date</td>
            <td>Bank</td>
            <td>Days</td> 
            <td>Cash</td>
            <td>Cheque</td>
            <td>Cash TT</td>
            <td>Card</td>
            <td>Online Pay</td>
            <td>Gift Card</td>
            <td>Utilize</td>
		</tr>";
    }
    
    $i =1;
    $k = 0;
    $j = 0;
    $gtot=0;
    $totpaid=0;
    $totbal=0;
    $pay=0;
    
    $cashpay=0;
    $chequepay=0;
    $cashttpay=0;
    $cardpay=0;
    $onlinepay=0;
    $giftcardpay=0;
    $utlizepay=0;
    
    $totgross = 0;
    $totvat = 0;
    $totnet = 0;   
            
    $sql_data = "select * from tmp_cusreg where C_USER = '" . $_SESSION["CURRENT_USER"] . "' order by SDATE,ORDNO desc";
    foreach ($conn->query($sql_data) as $row_data) { 
        
        $sql_rep = "select * from s_salrep where REPCODE = '" . $row_data['SAL_EX'] . "'";
        $result_rep = $conn->query($sql_rep);
        $row_rep = $result_rep->fetch();

        if ($row_data['TYPE'] == "INVOICE") {
            $ResponseXML .= "<tr>                              
                                <td> " . $i . "</td>
                                <td><a href=\"\" class=\"INV\" onClick=\"NewWindow('invoice_view.php?refno=" . $row_data['REFNO'] . "&trn_type=" . $row_data['TYPE'] . "','mywin','900','700','yes','center');return false\" onFocus=\"this.blur()\"> " . $row_data['REFNO'] . "</a></td>  
                                <td align=\"center\"> " . $row_data['SDATE'] . "</td>
                                <td align=\"center\"> " . $row_data['DELI_DATE'] . "</td>
                                <td align=\"center\"> " . $row_data['CUS_CODE'] . "</td>
                                <td> " . $row_data['CUS_NAME'] . "</td>
                                <td> " . $row_rep['Name'] . "</td>
                                <td align=\"right\"> " . number_format($row_data['TOTAL'], 2, ".", ",") . "</td>
                                <td align=\"right\"> " . number_format($row_data['PAID'], 2, ".", ",") . "</td>
                                <td align=\"right\"> " . number_format($row_data['TOTAL']-$row_data['PAID'], 2, ".", ",") . "</td>
                                <td> " . $row_data['TYPE'] . "</td>
                                <td align=\"left\"> " . $row_data['USER'] . "  /  " . $row_data['USERDTIME'] . "</td>
                            </tr>";
            
        } else if ($row_data['TYPE'] == "RETURN") {

            $ResponseXML .= "<tr>                              
                                <td> " . $i . "</td>
                                <td><a href=\"\" class=\"INV\" onClick=\"NewWindow('invoice_view.php?refno=" . $row_data['REFNO'] . "&trn_type=" . $row_data['TYPE'] . "','mywin','900','700','yes','center');return false\" onFocus=\"this.blur()\"> " . $row_data['REFNO'] . "</a></td>  
                                <td align=\"center\"> " . $row_data['SDATE'] . "</td>
                                <td align=\"left\"> " . $row_data['ORDNO'] . "</td>
                                <td align=\"center\"> " . $row_data['CUS_CODE'] . "</td>
                                <td> " . $row_data['CUS_NAME'] . "</td>
                                <td> " . $row_rep['Name'] . "</td>
                                <td align=\"right\"> " . number_format($row_data['TOTAL'], 2, ".", ",") . "</td>
                                <td align=\"right\"> " . number_format($row_data['PAID'], 2, ".", ",") . "</td>
                                <td align=\"right\"> " . number_format($row_data['TOTAL']-$row_data['PAID'], 2, ".", ",") . "</td>
                                <td> " . $row_data['TYPE'] . "</td>
                                <td align=\"left\"> " . $row_data['USER'] . "  /  " . $row_data['USERDTIME'] . "</td>
                            </tr>";
        
        } else if ($row_data['TYPE'] == "DEFECTIVE") {

            $ResponseXML .= "<tr>                              
                                <td> " . $i . "</td>
                                <td><a href=\"\" class=\"INV\" onClick=\"NewWindow('invoice_view.php?refno=" . $row_data['REFNO'] . "&trn_type=" . $row_data['TYPE'] . "','mywin','900','700','yes','center');return false\" onFocus=\"this.blur()\"> " . $row_data['REFNO'] . "</a></td>  
                                <td align=\"center\"> " . $row_data['SDATE'] . "</td>
                                <td align=\"left\"> " . $row_data['ORDNO'] . "</td>
                                <td align=\"center\"> " . $row_data['CUS_CODE'] . "</td>
                                <td> " . $row_data['CUS_NAME'] . "</td>
                                <td> " . $row_rep['Name'] . "</td>
                                <td align=\"right\"> " . number_format($row_data['TOTAL'], 2, ".", ",") . "</td>
                                <td align=\"right\"> " . number_format($row_data['PAID'], 2, ".", ",") . "</td>
                                <td align=\"right\"> " . number_format($row_data['TOTAL']-$row_data['PAID'], 2, ".", ",") . "</td>
                                <td> " . $row_data['TYPE'] . "</td>
                                <td align=\"left\"> " . $row_data['USER'] . "  /  " . $row_data['USERDTIME'] . "</td>
                            </tr>";
        
        } else if ($row_data['TYPE'] == "CREDIT NOTE") {

            $ResponseXML .= "<tr>                              
                                <td> " . $i . "</td>
                                <td><a href=\"\" class=\"INV\" onClick=\"NewWindow('invoice_view.php?refno=" . $row_data['REFNO'] . "&trn_type=" . $row_data['TYPE'] . "','mywin','900','700','yes','center');return false\" onFocus=\"this.blur()\"> " . $row_data['REFNO'] . "</a></td>  
                                <td align=\"center\"> " . $row_data['SDATE'] . "</td>
                                <td align=\"left\"> " . $row_data['ORDNO'] . "</td>
                                <td align=\"center\"> " . $row_data['CUS_CODE'] . "</td>
                                <td> " . $row_data['CUS_NAME'] . "</td>
                                <td> " . $row_rep['Name'] . "</td>
                                <td align=\"right\"> " . number_format($row_data['TOTAL'], 2, ".", ",") . "</td>
                                <td align=\"right\"> " . number_format($row_data['PAID'], 2, ".", ",") . "</td>
                                <td align=\"right\"> " . number_format($row_data['TOTAL']-$row_data['PAID'], 2, ".", ",") . "</td>
                                <td> " . $row_data['TYPE'] . "</td>
                                <td align=\"left\"> " . $row_data['USER'] . "  /  " . $row_data['USERDTIME'] . "</td>
                            </tr>";
        
        } else if ($row_data['TYPE'] == "UTILIZATION") {

            $ResponseXML .= "<tr>                              
                                <td> " . $i . "</td>
                                <td><a href=\"\" class=\"INV\" onClick=\"NewWindow('invoice_view.php?refno=" . $row_data['REFNO'] . "&trn_type=" . $row_data['TYPE'] . "','mywin','900','700','yes','center');return false\" onFocus=\"this.blur()\"> " . $row_data['REFNO'] . "</a></td>  
                                <td align=\"center\"> " . $row_data['SDATE'] . "</td>
                                <td align=\"left\"> " . $row_data['ORDNO'] . "</td>
                                <td align=\"center\"> " . $row_data['CUS_CODE'] . "</td>
                                <td> " . $row_data['CUS_NAME'] . "</td>
                                <td align=\"right\"> " . number_format($row_data['TOTAL'], 2, ".", ",") . "</td>
                                <td> " . $row_data['TYPE'] . "</td>
                                <td align=\"left\"> " . $row_data['USER'] . "  /  " . $row_data['USERDTIME'] . "</td>
                            </tr>";
        
        } else if ($row_data['TYPE'] == "CANCEL INVOICE") {

            $ResponseXML .= "<tr>                              
                                <td> " . $i . "</td>
                                <td><a href=\"\" class=\"INV\" onClick=\"NewWindow('invoice_view.php?refno=" . $row_data['REFNO'] . "&trn_type=" . $row_data['TYPE'] . "','mywin','900','700','yes','center');return false\" onFocus=\"this.blur()\"> " . $row_data['REFNO'] . "</a></td>  
                                <td align=\"center\"> " . $row_data['SDATE'] . "</td>
                                <td align=\"center\"> " . $row_data['DELI_DATE'] . "</td>
                                <td align=\"center\"> " . $row_data['CUS_CODE'] . "</td>
                                <td> " . $row_data['CUS_NAME'] . "</td>
                                <td> " . $row_rep['Name'] . "</td>
                                <td align=\"right\"> " . number_format($row_data['TOTAL'], 2, ".", ",") . "</td>
                                <td align=\"right\"> " . number_format($row_data['PAID'], 2, ".", ",") . "</td>
                                <td align=\"right\"> " . number_format($row_data['TOTAL']-$row_data['PAID'], 2, ".", ",") . "</td>
                                <td> " . $row_data['TYPE'] . "</td>
                                <td align=\"left\"> " . $row_data['USER'] . "  /  " . $row_data['USERDTIME'] . "</td>
                            </tr>";
            
        } else if ($row_data['TYPE'] == "RECEIPT DETAILS") {
            
            if($row_data['PAY_TYPE'] =='Cash'){
                $cashpay=$row_data['PAID'];
                $chequepay=0;
                $cashttpay=0;
                $cardpay=0;
                $onlinepay=0;
                $jentrypay=0;
                $utlizepay=0;
            } else if($row_data['PAY_TYPE'] =='Cheque'){
                $cashpay=0;
                $chequepay=$row_data['PAID'];
                $cashttpay=0;
                $cardpay=0;
                $onlinepay=0;
                $jentrypay=0;
                $utlizepay=0;
            } else if($row_data['PAY_TYPE'] =='Cash TT'){
                $cashpay=0;
                $chequepay=0;
                $cashttpay=$row_data['PAID'];
                $cardpay=0;
                $onlinepay=0;
                $jentrypay=0;
                $utlizepay=0;
            } else if($row_data['PAY_TYPE'] =='Card'){
                $cashpay=0;
                $chequepay=0;
                $cashttpay=0;
                $cardpay=$row_data['PAID'];
                $onlinepay=0;
                $jentrypay=0;
                $utlizepay=0;
            } else if($row_data['PAY_TYPE'] =='Online Payment'){
                $cashpay=0;
                $chequepay=0;
                $cashttpay=0;
                $cardpay=0;
                $onlinepay=$row_data['PAID'];
                $jentrypay=0;
                $utlizepay=0;
            } else if($row_data['PAY_TYPE'] =='Gift Card'){
                $cashpay=0;
                $chequepay=0;
                $cashttpay=0;
                $cardpay=0;
                $onlinepay=0;
                $jentrypay=$row_data['PAID'];
                $utlizepay=0;
            } else if($row_data['PAY_TYPE'] =='UT'){
                $cashpay=0;
                $chequepay=0;
                $cashttpay=0;
                $cardpay=0;
                $onlinepay=0;
                $jentrypay=0;
                $utlizepay=$row_data['PAID'];
            }

            $ResponseXML .= "<tr>                              
                                <td> " . $i . "</td>
                                <td><a href=\"\" class=\"INV\" onClick=\"NewWindow('invoice_view.php?refno=" . $row_data['REFNO'] . "&trn_type=" . $row_data['TYPE'] . "','mywin','900','700','yes','center');return false\" onFocus=\"this.blur()\"> " . $row_data['REFNO'] . "</a></td>  
                                <td align=\"center\"> " . $row_data['SDATE'] . "</td>
                                <td align=\"left\"> " . $row_data['ORDNO'] . "</td>
                                <td align=\"center\"> " . $row_data['CUS_CODE'] . "</td>
                                <td> " . $row_data['CUS_NAME'] . "</td>
                                <td align=\"right\"> " . number_format($cashpay, 2, ".", ",") . "</td>
                                <td align=\"right\"> " . number_format($chequepay, 2, ".", ",") . "</td>
                                <td align=\"right\"> " . number_format($cashttpay, 2, ".", ",") . "</td>
                                <td align=\"right\"> " . number_format($cardpay, 2, ".", ",") . "</td>
                                <td align=\"right\"> " . number_format($onlinepay, 2, ".", ",") . "</td>
                                <td align=\"right\"> " . number_format($jentrypay, 2, ".", ",") . "</td>
                                <td align=\"right\"> " . number_format($utlizepay, 2, ".", ",") . "</td>
                                <td> " . $row_data['TYPE'] . "</td>
                                <td align=\"left\"> " . $row_data['USER'] . "  /  " . $row_data['USERDTIME'] . "</td>
                            </tr>";
            
        } else if ($row_data['TYPE'] == "RECEIPT SUMMRY") {

            $ResponseXML .= "<tr>                              
                                <td> " . $i . "</td>
                                <td><a href=\"\" class=\"INV\" onClick=\"NewWindow('invoice_view.php?refno=" . $row_data['REFNO'] . "&trn_type=" . $row_data['TYPE'] . "','mywin','900','700','yes','center');return false\" onFocus=\"this.blur()\"> " . $row_data['REFNO'] . "</a></td>  
                                <td align=\"center\"> " . $row_data['SDATE'] . "</td>
                                <td align=\"left\"> " . $row_data['ORDNO'] . "</td>
                                <td align=\"center\"> " . $row_data['CUS_CODE'] . "</td>
                                <td> " . $row_data['CUS_NAME'] . "</td>
                                <td align=\"right\"> " . number_format($row_data['OVER_PAY'], 2, ".", ",") . "</td>
                                <td align=\"right\"> " . number_format($row_data['PAID'], 2, ".", ",") . "</td>
                                <td align=\"right\"> " . number_format($row_data['TOTAL'], 2, ".", ",") . "</td>
                                <td> " . $row_data['TYPE'] . "</td>
                                <td align=\"left\"> " . $row_data['USER'] . "  /  " . $row_data['USERDTIME'] . "</td>
                            </tr>";
            
        }  else if ($row_data['TYPE'] == "OVER PAY") {

            $ResponseXML .= "<tr>                              
                                <td> " . $i . "</td>
                                <td><a href=\"\" class=\"INV\" onClick=\"NewWindow('invoice_view.php?refno=" . $row_data['REFNO'] . "&trn_type=" . $row_data['TYPE'] . "','mywin','900','700','yes','center');return false\" onFocus=\"this.blur()\"> " . $row_data['REFNO'] . "</a></td>  
                                <td align=\"center\"> " . $row_data['SDATE'] . "</td>
                                <td align=\"left\"> " . $row_data['ORDNO'] . "</td>
                                <td align=\"center\"> " . $row_data['CUS_CODE'] . "</td>
                                <td> " . $row_data['CUS_NAME'] . "</td>
                                <td> " . $row_rep['Name'] . "</td>
                                <td align=\"right\"> " . number_format($row_data['TOTAL'], 2, ".", ",") . "</td>
                                <td align=\"right\"> " . number_format($row_data['PAID'], 2, ".", ",") . "</td>
                                <td align=\"right\"> " . number_format($row_data['TOTAL']-$row_data['PAID'], 2, ".", ",") . "</td>
                                <td> " . $row_data['TYPE'] . "</td>
                                <td> " . $row_data['TYPE2'] . "</td>
                                <td align=\"left\"> " . $row_data['USER'] . "  /  " . $row_data['USERDTIME'] . "</td>
                            </tr>";
        
        } else if ($row_data['TYPE'] == "CHEQUE LIST") {

            $ResponseXML .= "<tr>                              
                                <td> " . $i . "</td>
                                <td><a href=\"\" class=\"INV\" onClick=\"NewWindow('invoice_view.php?refno=" . $row_data['REFNO'] . "&trn_type=" . $row_data['TYPE'] . "','mywin','900','700','yes','center');return false\" onFocus=\"this.blur()\"> " . $row_data['REFNO'] . "</a></td>  
                                <td align=\"center\"> " . $row_data['SDATE'] . "</td>
                                <td align=\"center\"> " . $row_data['CUS_CODE'] . "</td>
                                <td> " . $row_data['CUS_NAME'] . "</td>
                                <td> " . $row_rep['Name'] . "</td>
                                <td align=\"center\"> " . $row_data['CHQ_NO'] . "</td>
                                <td align=\"center\"> " . $row_data['CHQ_DATE'] . "</td>
                                <td align=\"left\"> " . $row_data['CHQ_BANK'] . "</td>
                                <td align=\"right\"> " . number_format($row_data['TOTAL'], 2, ".", ",") . "</td>
                                <td> " . $row_data['TYPE'] . "</td>
                                <td align=\"left\"> " . $row_data['USER'] . "  /  " . $row_data['USERDTIME'] . "</td>
                            </tr>";
        
        }else if ($row_data['TYPE'] == "SETTLEMENT") {
            
            if ($ORDNO != $row_data["ORDNO"]) {

                    if ($k == 1) {
                        $ResponseXML .= "<tr><th colspan=10></th>
                                <td align=\"right\"><b >" . number_format($cashpay,2) . "</b></td>
                                <td align=\"right\"><b >" . number_format($chequepay ,2). "</b></td>
                                <td align=\"right\"><b >" . number_format($cashttpay ,2). "</b></td>
                                <td align=\"right\"><b >" . number_format($cardpay ,2). "</b></td>
                                <td align=\"right\"><b >" . number_format($onlinepay ,2). "</b></td>
                                <td align=\"right\"><b >" . number_format($giftcardpay ,2). "</b></td>
                                <td align=\"right\"><b >" . number_format($utlizepay ,2). "</b></td></tr>";
                        $k = 0;
                    }
                    
                     
                    if ($k == 0) {
                        $cashpay=0;
                        $chequepay=0;
                        $cashttpay=0;
                        $cardpay=0;
                        $onlinepay=0;
                        $giftcardpay=0;
                        $utlizepay=0;
                        $sdate = $row_data["SDATE"];
                        $ResponseXML .= "<tr>
                                            <td>" . $sdate . "</td>
                                            <td>" . $row_data["ORDNO"] . "</td>
                                            <td>" . $row_data["CUS_CODE"] . " " . $row_data["CUS_NAME"] . "</td>
                                            <td align=\"right\">" . number_format($row_data["TOTAL"],2) . "</td>";

                        $due = $row_data["TOTAL"] - $row_data["PAID"];
                        if (($due > 0) or ($due < 0)) {
                            $ResponseXML .= "<td align=\"right\">" . number_format($due,2) . "</td>";
                        } else {
                            $ResponseXML .= "<td align=\"right\"></td>";
                        }


                        $ORDNO = $row_data["ORDNO"];
                        $k = 1;
                        $j = 0;
                    }

                    $totgross = $totgross + $row_data["TOTAL"];
                    $totvat = $totvat + $vat;
                    $totnet = $totnet + $net;
                }

                if ($j != 0) {
                    $ResponseXML .= "<tr>
                        <td colspan=5></td>";
                } else {
                    $j = 1;
                }
                $ResponseXML .= "<td>" . $row_data["REFNO"] . "</td>
                                <td>" . $row_data["CHQ_NO"] . "</td>";

                                if ($row_data["CHQ_NO"] != "Cash TT") {
                                    if ($row_data["CHQ_DATE"] != "0000-00-00") {
                                        $ResponseXML .= "<td>" . $row_data["CHQ_DATE"] . "</td>";
                                        $ResponseXML .= "<td>" . $row_data["CHQ_BANK"] . "</td>";
                                    } else {
                                        $ResponseXML .= "<td></td>";
                                        $ResponseXML .= "<td></td>";
                                    }
                                } else {
                                    $ResponseXML .= "<td>" . $row_data["CHQ_DATE"] . "</td>";
                                    $ResponseXML .= "<td>" . $AC_REFNO . "</td>";
                                }


                $days = "";
                
                $date1 = $sdate;
                $date2 = $row_data["CHQ_DATE"];
                $diff = abs(strtotime($date2) - strtotime($date1));
                $days = floor($diff / (60 * 60 * 24));
             
                $ResponseXML .= "<td>" . $days . "</td>";

                if ($row_data['PAY_TYPE'] == 'Cash') {
                    $ResponseXML .= "<td align=\"right\">" . number_format($row_data['PAY'], 2, ".", ",") . "</td>";
                    $cashpay = $cashpay + $row_data['PAY'];
                } else {
                    $ResponseXML .= "<td></td>";
                }
                
                if (($row_data["PAY_TYPE"] == 'Cheque') ) {
                    $ResponseXML .= "<td align=\"right\">" . number_format($row_data['PAY'], 2, ".", ",") . "</td>";
                    $chequepay = $chequepay + $row_data['PAY'];
                } else {
                    $ResponseXML .= "<td></td>";
                }
                
                 if ($row_data['PAY_TYPE'] == 'Cash TT') {
                    $ResponseXML .= "<td align=\"right\">" . number_format($row_data['PAY'], 2, ".", ",") . "</td>";
                    $cashttpay = $cashttpay + $row_data['PAY'];
                } else {
                    $ResponseXML .= "<td></td>";
                } 

               if (($row_data["PAY_TYPE"] == 'Card')) {
                    $ResponseXML .= "<td align=\"right\">" . number_format($row_data['PAY'], 2, ".", ",") . "</td>";
                    $cardpay = $cardpay + $row_data['PAY'];
                } else {
                    $ResponseXML .= "<td></td>";
                }
                
                if (($row_data["PAY_TYPE"] == 'Online Payment')) {
                    $ResponseXML .= "<td align=\"right\">" . number_format($row_data['PAY'], 2, ".", ",") . "</td>";
                    $onlinepay = $onlinepay + $row_data['PAY'];
                } else {
                    $ResponseXML .= "<td></td>";
                }
                
                if (($row_data["PAY_TYPE"] == 'Gift Card')) {
                    $ResponseXML .= "<td align=\"right\">" . number_format($row_data['PAY'], 2, ".", ",") . "</td>";
                    $giftcardpay = $giftcardpay + $row_data['PAY'];
                } else {
                    $ResponseXML .= "<td></td>";
                }
                
                if (($row_data["PAY_TYPE"] == 'UT')) {
                    $ResponseXML .= "<td align=\"right\">" . number_format($row_data['PAY'], 2, ".", ",") . "</td>";
                    $utlizepay = $utlizepay + $row_data['PAY'];
                } else {
                    $ResponseXML .= "<td></td>";
                } 
                
                $ResponseXML .= "</tr>";
                
                
        }
        
        $i = $i + 1; 
        $gtot=$gtot+$row_data['TOTAL'];
        $totpaid=$totpaid+$row_data['PAID'];
        $totoverpay=$totoverpay+$row_data['OVER_PAY'];
        $totbal=$totbal+($row_data['TOTAL']-$row_data['PAID']);
        
        
        $cashpay_tot=$cashpay_tot+$cashpay;
        $chequepay_tot=$chequepay_tot+$chequepay;
        $cashttpay_tot=$cashttpay_tot+$cashttpay;
        $cardpay_tot=$cardpay_tot+$cardpay;
        $onlinepay_tot=$onlinepay_tot+$onlinepay;
        $jentrypay_tot=$jentrypay_tot+$jentrypay;
        $utlizepay_tot=$utlizepay_tot+$utlizepay;
        
        $giftcardpay_tot=$giftcardpay_tot+$giftcardpay;
     
    }
    
    
    
    if ($_POST['type'] == "INVOICE") {
        $ResponseXML .= "<tr>
                            <td align=\"right\" colspan='7'><b>TOTAL</b></td>
                            <td align=\"right\"><b>" . number_format($gtot, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($totpaid, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($totbal, 2, ".", ","). "</b></td>
                        </tr>";
    }
    
    if ($_POST['type'] == "RETURN") {
        $ResponseXML .= "<tr>
                            <td align=\"right\" colspan='7'><b>TOTAL</b></td>
                            <td align=\"right\"><b>" . number_format($gtot, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($totpaid, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($totbal, 2, ".", ","). "</b></td>
                        </tr>";
    }
    
    if ($_POST['type'] == "DEFECTIVE") {
        $ResponseXML .= "<tr>
                            <td align=\"right\" colspan='7'><b>TOTAL</b></td>
                            <td align=\"right\"><b>" . number_format($gtot, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($totpaid, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($totbal, 2, ".", ","). "</b></td>
                        </tr>";
    }
    
    if ($_POST['type'] == "CREDIT NOTE") {
        $ResponseXML .= "<tr>
                            <td align=\"right\" colspan='7'><b>TOTAL</b></td>
                            <td align=\"right\"><b>" . number_format($gtot, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($totpaid, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($totbal, 2, ".", ","). "</b></td>
                        </tr>";
    }
    
    if ($_POST['type'] == "UTILIZATION") {
        $ResponseXML .= "<tr>
                            <td align=\"right\" colspan='6'><b>TOTAL</b></td>
                            <td align=\"right\"><b>" . number_format($gtot, 2, ".", ","). "</b></td>
                        </tr>";
    }
    
    if ($_POST['type'] == "CANCEL INVOICE") {
        $ResponseXML .= "<tr>
                            <td align=\"right\" colspan='7'><b>TOTAL</b></td>
                            <td align=\"right\"><b>" . number_format($gtot, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($totpaid, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($totbal, 2, ".", ","). "</b></td>
                        </tr>";
    }
    
    if ($_POST['type'] == "RECEIPT DETAILS") {
        
        
        $ResponseXML .= "<tr>
                            <td align=\"right\" colspan='6'><b>TOTAL</b></td>
                            <td align=\"right\"><b>" . number_format($cashpay_tot, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($chequepay_tot, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($cashttpay_tot, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($cardpay_tot, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($onlinepay_tot, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($jentrypay_tot, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($utlizepay_tot, 2, ".", ","). "</b></td>
                        </tr>";
    }
    
    if ($_POST['type'] == "RECEIPT SUMMRY") {
        $ResponseXML .= "<tr>
                            <td align=\"right\" colspan='6'><b>TOTAL</b></td>
                            <td align=\"right\"><b>" . number_format($totoverpay, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($totpaid, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($gtot, 2, ".", ","). "</b></td>
                        </tr>";
    }
    
    if ($_POST['type'] == "OVER PAY") {
        $ResponseXML .= "<tr>
                            <td align=\"right\" colspan='7'><b>TOTAL</b></td>
                            <td align=\"right\"><b>" . number_format($gtot, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($totpaid, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($totbal, 2, ".", ","). "</b></td>
                        </tr>";
    }

    if ($_POST['type'] == "CHEQUE LIST") {
        $ResponseXML .= "<tr>
                            <td align=\"right\" colspan='9'><b>TOTAL</b></td>
                            <td align=\"right\"><b>" . number_format($gtot, 2, ".", ","). "</b></td>
                        </tr>";
    }
    
     if ($_POST['type'] == "SETTLEMENT") {
        
         $ResponseXML .= "<tr>
                            <td align=\"right\" colspan='10'><b></b></td>
                            <td align=\"right\"><b>" . number_format($cashpay, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($chequepay, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($cashttpay, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($cardpay, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($onlinepay, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($giftcardpay, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($utlizepay, 2, ".", ","). "</b></td>
                        </tr>";
        
        $ResponseXML .= "<tr>
                            <td align=\"right\" colspan='10'><b>TOTAL</b></td>
                            <td align=\"right\"><b>" . number_format($cashpay_tot, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($chequepay_tot, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($cashttpay_tot, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($cardpay_tot, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($onlinepay_tot, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($giftcardpay_tot, 2, ".", ","). "</b></td>
                            <td align=\"right\"><b>" . number_format($utlizepay_tot, 2, ".", ","). "</b></td>
                        </tr>";
    }

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















?>