<?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_customername1><![CDATA[" . $row['ADD1'] . "]]></str_customername1>";
   
    }


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

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

    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 .= "<remtable><![CDATA[<table class='table table-hover'>";


    $sql = "SELECT * from dlr_shr where c_code = '$cuscode' order by date desc";

//    echo $sql;



    $ResponseXML .= "<tr>";

    $ResponseXML .= "<td style=\"width: 350px;\">Dealer C.</td>";
    $ResponseXML .= "<td style=\"width: 350px;\">Name</td>";
    $ResponseXML .= "<td style=\"width: 350px;\">Date</td>";
    $ResponseXML .= "<td style=\"width: 350px;\">RefNo</td>";
    $ResponseXML .= "<td style=\"width: 350px;\">Amount</td>";
    $ResponseXML .= "<td style=\"width: 350px;\">Remark</td>";

    $ResponseXML .= "</tr>";



    foreach ($conn->query($sql) as $row) {

        $ResponseXML .= "<tr>";

        $ResponseXML .= "<td>" . $row['c_code'] . "</td>";
        $ResponseXML .= "<td>" . $row['c_name'] . "</td>";
        $ResponseXML .= "<td>" . $row['date'] . "</td>";
        $ResponseXML .= "<td>" . $row['refno'] . "</td>";
        $ResponseXML .= "<td>" . $row['amt'] . "</td>";
        $ResponseXML .= "<td>" . $row['remark'] . "</td>";

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

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



    $ResponseXML .= "</salesdetails>";

    echo $ResponseXML;
}

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

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

    $sql = "select * from user_mast_dlr where dlr_code = '" . $cuscode . "' ";

    $sql = $conn->query($sql);
    if ($row = $sql->fetch()) {
        $ResponseXML .= "<isRegistered><![CDATA[yes]]></isRegistered>";
        $ResponseXML .= "<user_name><![CDATA[" . $row["user_name"] . "]]></user_name>";
    } else {
        $ResponseXML .= "<isRegistered><![CDATA[no]]></isRegistered>";
    }



    $ResponseXML .= "</salesdetails>";

    echo $ResponseXML;
}

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

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

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

    $cuscode = $_GET["custno"];

    $sql = "select * from dlr_inf where c_code = '$cuscode' and date = '" . date('Y-m-d') . "'";
    $sql = $conn->query($sql);
    $count = $sql->rowCount();

    if ($count > 0) {
        $ResponseXML .= "<terminate><![CDATA[yes]]></terminate>";
    } else {
        $ResponseXML .= "<terminate><![CDATA[no]]></terminate>";
        $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 .= "</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 .= " 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;
}
?>
