<?php

session_start();



include_once './connection_sql.php';






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




    $_SESSION["custno"] = $_GET['custno'];

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

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

    $cuscode = $_GET["custno"];

    $sql = "Select * from vendor where code='" . $cuscode . "' ";

    $sql = $conn->query($sql);
    if ($row = $sql->fetch()) {


        $ResponseXML .= "<id><![CDATA[" . $row['CODE'] . "]]></id>";
        $ResponseXML .= "<str_customername><![CDATA[" . $row['NAME'] . "]]></str_customername>";
					$ResponseXML .= "<str_vatno><![CDATA[".$row["vatno"]."]]></str_vatno>";
        $ResponseXML .= "<stname><![CDATA[" . $_GET['stname'] . "]]></stname>";
        $ResponseXML .= "<add><![CDATA[" . $row['ADD1'] . "]]></add>";
        $ResponseXML .= "<rep><![CDATA[" . $row['rep'] . "]]></rep>";

        $i = 1;
        if (isset($_GET['stname'])) {
            if (($_GET['stname'] == "rec") or ( $_GET['stname'] == "cont")) {
                $tb = "<table class='table table-striped'>";
                $tb .= "<tr>";
                $tb .= "<th>Ref. No</th>";
                $tb .= "<th>Date</th>";
                $tb .= "<th>Amount</th>";
                $tb .= "<th>Paid</th>";

                $tb .= "<th>Balance</th>";
                $tb .= "<th>Payment</th>";
                $tb .= "<th>Running Bal.</th>";

                $tb .= "</tr>";

                $sql = "select * from  s_salma where c_code = '" . $cuscode . "' and curamo > curtotpay and curamo-curtotpay>=0.01 and cur='" . $_GET['cur'] . "' and cancell='0' order by sdate";
                foreach ($conn->query($sql) as $row) {
                    $pay = "pay" . $i;
                    $refno = "refno" . $i;
                    $rate = "rate" . $i;
                    $bal = "bal" . $i;
                    $cashbal = "cashbal" . $i;
                    $tb .= "<tr>";
                    $tb .= "<td><input disabled=disabled type='text' id='" . $refno . "' value = '" . $row['REF_NO'] . "'></td>";
                    $tb .= "<td>" . $row['SDATE'] . "</td>";
                    $tb .= "<td>" . $row['curamo'] . "</td>";
                    $tb .= "<td>" . $row['curtotpay'] . "</td>";

                    $tb .= "<td><input disabled=disabled type='text' id='" . $bal . "' value = '" . ($row['curamo'] - $row['curtotpay']) . "'></td>";
                    $tb .= "<td><input onkeyup='calamo();' type='text' id='" . $pay . "'></td>";
                    $tb .= "<td><input  disabled=disabled type='text' id='" . $cashbal . "'></td>";
                    $i = $i + 1;

                    $tb .= "</tr>";
                }

                $tb .= "</table>";

                $t = 1;
                $tb1 = "<table class='table table-striped'>";
                $tb1 .= "<tr>";
                $tb1 .= "<th>Ref. No</th>";
                $tb1 .= "<th>Date</th>";
                $tb1 .= "<th>Amount</th>";
                $tb1 .= "<th>Paid</th>";
                $tb1 .= "<th>Balance</th>";
                $tb1 .= "<th>Payment</th>";

                $tb1 .= "</tr>";

                $sql = "select * from  c_bal where cuscode = '" . $cuscode . "' and curbal > 0.01 and cur='" . $_GET['cur'] . "' and cancell='0' order by sdate";
                foreach ($conn->query($sql) as $row) {
                    $pay = "cpay" . $t;
                    $refno = "crefno" . $t;
                    $rate = "crate" . $t;
                    $bal = "cbal" . $t;

                    $tb1 .= "<tr>";
                    $tb1 .= "<td><input disabled=disabled type='text' id='" . $refno . "' value = '" . $row['REFNO'] . "'></td>";
                    $tb1 .= "<td>" . $row['SDATE'] . "</td>";
                    $tb1 .= "<td>" . $row['curamo'] . "</td>";
                    $tb1 .= "<td>" . ($row['curamo'] - $row['curbal']) . "</td>";

                    $tb1 .= "<td><input disabled=disabled type='text' id='" . $bal . "' value = '" . ($row['curbal']) . "'></td>";
                    $tb1 .= "<td><input onkeyup='calamo1();' type='text' id='" . $pay . "'></td>";

                    $t = $t + 1;

                    $tb1 .= "</tr>";
                }

                $tb1 .= "</table>";
                $ResponseXML .= "<tb><![CDATA[" . $tb . "]]></tb>";
                $ResponseXML .= "<count><![CDATA[" . $i . "]]></count>";
                $ResponseXML .= "<tb1><![CDATA[" . $tb1 . "]]></tb1>";
                $ResponseXML .= "<count1><![CDATA[" . $t . "]]></count1>";
            }else if(($_GET['stname'] == "inv")){
                $msg = "";
                $sql = "select SDATE from  s_salma where  C_CODE='".$cuscode."' and GRAND_TOT-TOTPAY >1 and CANCELL=0  order by SDATE";
                $sql = $conn->query($sql);
                if ($row = $sql->fetch()) {
                    $date1 = $row["SDATE"];
                    $date2 = date("Y-m-d");

                    $diff = abs(strtotime($date2) - strtotime($date1));

                    $years = floor($diff / (365*60*60*24));

                    $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));

                    $mday = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
                    $mdays=$mday+($months*30)+($years*365);

                    $sqlVen = "select crprd, cLIMIT from vendor where CODE = '".$cuscode."'";
                    $result = $conn->query($sqlVen);
                    $rowCus = $result->fetch();
                    
                    $crDays = $rowCus["crprd"];
                    $crLmt = $rowCus["cLIMIT"];
                    
                    if ($mdays > $crDays){
                        $msg .= "Over $crDays Outsnding Avilabale ";
                    }
                }
                
                
                $OutInvAmt = 0;
                $OutREtAmt = 0;
                $sql = "select sum(GRAND_TOT-TOTPAY) as totout from view_s_salma where GRAND_TOT>TOTPAY and CANCELL='0' and C_CODE='".$cuscode."'";
                $row = $conn->query($sql)->fetch();
                $OutInvAmt=$row["totout"];
                
                $sql = "Select sum(CR_CHEVAL-PAID) as tot from s_cheq where CR_C_CODE='".$cuscode."'  and CR_CHEVAL-PAID>0 and CR_FLAG='0'";
                $row = $conn->query($sql)->fetch();
                $OutREtAmt=$row["tot"];
                
                $chkOstValue =   $OutInvAmt + $OutREtAmt;
                
                if ($chkOstValue > $crLmt){
                        if($msg != ""){
                            $msg .= "and Credit Limit over $crLmt";
                        }else{
                            $msg .= "Credit Limit over $crLmt";
                        }
                }
                if ($OutREtAmt > 0){
                        if($msg != ""){
                            $msg .= "and Outstanding Return Cheque amount is $OutREtAmt";
                        }else{
                            $msg .= "Outstanding Return Cheque amount is $OutREtAmt";
                        }
                }
                
                $ResponseXML .= "<msg><![CDATA[" . $msg . "]]></msg>";
            }
        }
    }


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


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


    $ResponseXML = "";




    $ResponseXML .= "<table   class=\"table table-bordered\">
                            <tr>
                    <th width=\"121\"  >Customer No</th>
                    <th width=\"424\"  >Customer Name</th>
                    <th width=\"300\"  >Address</th>

                </tr>";

    if ($_GET["mstatus"] == "cusno") {
        $letters = $_GET['cusno'];
        //$letters = preg_replace("/[^a-z0-9 ]/si","",$letters);
        $sql = "select code,name,add1 from vendor where  code like '%$letters%' ";
    } else if ($_GET["mstatus"] == "customername") {
        $letters = $_GET['customername'];
        //$letters = preg_replace("/[^a-z0-9 ]/si","",$letters);
        $sql = "select code,name,add1 from vendor where  name like '%$letters%' ";
    } else {

        $letters = $_GET['customername'];
        //$letters = preg_replace("/[^a-z0-9 ]/si","",$letters);
        $sql = "select code,name,add1 from vendor where  name like '%$letters%' ";
    }
    $sql .= " and commoncode = '" .$_SESSION["company"]. "'";
    $sql .= " ORDER BY code limit 50";


    foreach ($conn->query($sql) as $row) {
        $cuscode = $row['code'];
        $stname = $_GET["stname"];

        $ResponseXML .= "<tr>               
                              <td onclick=\"custno('$cuscode', '$stname');\">" . $row['code'] . "</a></td>
                              <td onclick=\"custno('$cuscode', '$stname');\">" . $row['name'] . "</a></td>
                              <td onclick=\"custno('$cuscode', '$stname');\">" . $row['add1'] . "</a></td>
                            </tr>";
    }


    $ResponseXML .= "   </table>";


    echo $ResponseXML;
}


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


    $sql = "select Clid,ClName from masterclient where  Clid like '" . $_GET['term'] . "%' or ClName like '" . $_GET['term'] . "%'";
    $sql .= " ORDER BY Clid limit 50";
    $result = array();
    foreach ($conn->query($sql) as $items) {
        array_push($result, array("id" => $items['Clid'], "label" => $items['Clid'] . '-' . $items['ClName'], "name" => $items['ClName']));
    }


    echo json_encode($result);
}

?>
