<?php

session_start();

////////////////////////////////////////////// Database Connector /////////////////////////////////////////////////////////////
require_once("config.inc.php");
require_once("DBConnector1.php");
$db = new DBConnector();

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

date_default_timezone_set('Asia/Colombo');

/////////////////////////////////////// GetValue //////////////////////////////////////////////////////////////////////////
///////////////////////////////////// Registration /////////////////////////////////////////////////////////////////////////

if ($_GET["Command"] == "add_address") {
    //echo "Regt=".$Regt."RegimentNo=".RegimentNo."Command=".$Command;


    /* 		$sql="Select * from tmp_army_no where edu= '".$_GET['edu']."'";
      $result =$db->RunQuery($sql);
      if($row = mysqli_fetch_array($result)){
      $ResponseXML .= "exist";



      }	else { */

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

    /* 	$sql1="Select * from mas_educational_qualifications where str_Educational_Qualification= '".$_GET['edu']."'";
      $result1 =$db->RunQuery($sql1);
      $row1 = mysqli_fetch_array($result1);
      $ResponseXML .=  $row1["int_Educational_Qulifications"]; */

    $sqlt = "Select * from customer_mast where id ='" . $_GET['customerid'] . "'";

    $resultt = $db->RunQuery($sqlt);
    if ($rowt = mysqli_fetch_array($resultt)) {
        echo $rowt["str_address"];
    }
}


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


    $_SESSION["insert"] = 1;


    $sql = "Select MR_NO from invpara where COMCODE='" . $_SESSION['company'] . "'";
    $result = $db->RunQuery($sql);
    $row = mysqli_fetch_array($result);
    $tmpinvno = "0000000" . $row["MR_NO"];
    $lenth = strlen($tmpinvno);
    $invno = $_SESSION['company'] . "/".trim("GNNE/PR/") . substr($tmpinvno, $lenth - 8);
    $_SESSION["invno"] = $invno;

    $sql = "Select MR_NO from tmpinvpara";
    $result = $db->RunQuery($sql);
    $row = mysqli_fetch_array($result);
    $_SESSION["tmp_no_purord"] = $_SESSION['company'] . "MRD/" . $row["MR_NO"];

    $sql1 = "delete from tmp_matreqesion_data where tmp_no='" . $_SESSION["tmp_no_purord"] . "'";
    $result1 = $db->RunQuery($sql1);

    $sql1 = "update tmpinvpara set MR_NO=MR_NO+1";
    $result1 = $db->RunQuery($sql1);

    echo $invno;
}


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

			$department=$_GET["department"];

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


			//$sql="delete from tmp_purord_data where str_code='".$_GET['itemcode']."' and tmp_no='".$_SESSION['tmp_no_purord']."' ";
			//$ResponseXML .= $sql;
			//$result =$db->RunQuery($sql);

			//echo $_GET['rate'];
			//echo $_GET['qty'];

			$qty=str_replace(",", "", $_GET["qty"]);

			$itemdesc=$_GET['item'];
			$sql="Insert into tmp_matreqesion_data (tmp_no, str_invno, str_code, str_description, partno, qty,uom,unitprice)values
			('".$_SESSION["tmp_no_purord"]."', '".$_GET['invno']."', '".$_GET['itemcode']."', '".$itemdesc."', '".$_GET["partno"]."', ".$qty.", '".$_GET["uom"]."', '".$_GET["up"]."') ";
			//$ResponseXML .= $sql;
// 			echo $sql;
			$result =$db->RunQuery($sql);


				$ResponseXML .= "<sales_table><![CDATA[ <table><tr>
                              <td width=\"200\"  background=\"\">Stock No</td> 
                              <td width=\"500\"  background=\"\"><font color=\"#FFFFFF\">Description</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Gen No</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">UOM</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Unit Price</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Qty</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Total</font></td>

                            </tr>";

$tot=0;
			$sql="Select * from tmp_matreqesion_data where tmp_no='".$_SESSION['tmp_no_purord']."'";
// 			echo $sql;
			$result =$db->RunQuery($sql);
			while($row = mysqli_fetch_array($result)){
			    $sql1 = "Select * from s_mas where STK_NO='" . $row['str_code'] . "'";
        $result1 = $db->RunQuery($sql1);
        $row1= mysqli_fetch_array($result1);
             	$ResponseXML .= "<tr>
                             <td >".$row['str_code']."</a></td> 
							 <td >".$row['str_description']."</a></td>
						   	<td >".$row['partno']."</a></td>
                              <td >".$row['uom']."</a></td>
                              <td >".$row['unitprice']."</a></td>
							 <td >".number_format($row['qty'], 2, ".", ",")."</a></td>
							 <td >".number_format($row['qty']*$row['unitprice'], 2, ".", ",")."</a></td>
							 <td width=\"100\"><img src=\"images/delete_01.png\" width=\"20\" height=\"20\" id=".$row['str_code']."  name=".$row['str_code']." onClick=\"del_item('".$row['str_code']."');\"></td></tr>";

                $tot=$tot+$row['qty']*$row['unitprice'];
                
				}

                
                $ResponseXML .= "   </table>]]></sales_table>";
                $ResponseXML .= "<tot12><![CDATA[".number_format($tot, 2, ".", ",")."]]></tot12>";
				$ResponseXML .= " </salesdetails>";

		//	}


				echo $ResponseXML;


	}

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

      //$department=$_GET["department"];
      echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";

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

      $_SESSION["update"] = 1;

      $sql = "select * from s_matrequesionmas where REFNO='" . $_GET['invno'] . "' ";
  //    ECHO $sql;
      $result = $db->RunQuery($sql);
      if ($row = mysqli_fetch_array($result)) {
  				$ResponseXML .= "<REFNO><![CDATA[".$row['REFNO']."]]></REFNO>";
          $ResponseXML .= "<REQ_NO><![CDATA[".$row['REQ_NO']."]]></REQ_NO>";
          $ResponseXML .= "<SDATE><![CDATA[" . $row['SDATE'] . "]]></SDATE>";
          $ResponseXML .= "<SUP_CODE><![CDATA[" . $row['SUP_CODE'] . "]]></SUP_CODE>";
          $ResponseXML .= "<SUP_NAME><![CDATA[" . $row['SUP_NAME'] . "]]></SUP_NAME>";
          $ResponseXML .= "<REMARK><![CDATA[" . $row['REMARK'] . "]]></REMARK>";
          $ResponseXML .= "<reason><![CDATA[" . $row['reason'] . "]]></reason>";
          $ResponseXML .= "<DEP_CODE><![CDATA[" . $row['DEP_CODE'] . "]]></DEP_CODE>";
          $ResponseXML .= "<DEP_NAME><![CDATA[" . $row['DEP_NAME'] . "]]></DEP_NAME>";
          $ResponseXML .= "<REP_CODE><![CDATA[" . $row['REP_CODE'] . "]]></REP_CODE>";
          $ResponseXML .= "<REP_NAME><![CDATA[" . $row['REP_NAME'] . "]]></REP_NAME>";
          $ResponseXML .= "<S_date><![CDATA[" . $row['S_date'] . "]]></S_date>";
          $ResponseXML .= "<LC_No><![CDATA[" . $row['LC_No'] . "]]></LC_No>";
          $ResponseXML .= "<pi_no><![CDATA[" . $row['pi_no'] . "]]></pi_no>";
          $ResponseXML .= "<Brand><![CDATA[" . $row['Brand'] . "]]></Brand>";

           $ResponseXML .= "<address><![CDATA[" . $row['SUP_ADDR'] . "]]></address>";
          $ResponseXML .= "<isoNo><![CDATA[" . $row['isoNo'] . "]]></isoNo>";
          $ResponseXML .= "<purchasingGroup><![CDATA[" . $row['purchasingGroup'] . "]]></purchasingGroup>";
          $ResponseXML .= "<vatNo><![CDATA[" . $row['vatNo'] . "]]></vatNo>";
          $ResponseXML .= "<prNo><![CDATA[" . $row['prNo'] . "]]></prNo>";
          $ResponseXML .= "<cat><![CDATA[" . $row['cat'] . "]]></cat>";
          $ResponseXML .= "<som><![CDATA[" . $row['som'] . "]]></som>";
          $ResponseXML .= "<ordrfor><![CDATA[" . $row['ordrfor'] . "]]></ordrfor>";

          $ResponseXML .= "<ARRIVED><![CDATA[" . $row['cancel'] . "]]></ARRIVED>";
          $_SESSION['tmp_no_purord'] = $row["tmp_no"];
      }

      $sql = "delete from tmp_matreqesion_data where tmp_no='" . $_SESSION['tmp_no_purord'] . "' ";
      //$ResponseXML .= $sql;
      $result = $db->RunQuery($sql);

      $sql = "select * from s_materialreqtrn where REFNO='" . $_GET['invno'] . "' ";
      // echo $sql;
      // echo $sql;
      //$ResponseXML .= $sql;
      $result = $db->RunQuery($sql);
      $tempremove = 0;
      while ($row = mysqli_fetch_array($result)) {
      
          $sql1 = "Insert into tmp_matreqesion_data (tmp_no, str_invno, str_code, str_description, partno, qty,uom,unitprice)values
  				('" . $_SESSION['tmp_no_purord'] . "', '" . $row['REFNO'] . "', '" . $row['STK_NO'] . "', '" . $row['DESCRIPT'] . "', '" . $row["partno"] . "', " . $row["ORD_QTY"] . ", '" . $row["uom"] . "', " . $row["unitprice"] . ") ";
          //$ResponseXML .= $sql;
          // echo $sql1;
          $result1 = $db->RunQuery($sql1);
      }



      $ResponseXML .= "<sales_table><![CDATA[ <table><tr>
                                <td width=\"200\"  background=\"\">Stock No</td> 
                                <td width=\"700\"  background=\"\">Description</td>
                                <td width=\"100\"  background=\"\">Gen No</td>
                                <td width=\"100\"  background=\"\">UOM</td>
                                <td width=\"100\"  background=\"\">Unit Price</td>
                                <td width=\"100\"  background=\"\">Qty</td>
                                <td width=\"100\"  background=\"\">Total</td>

                              </tr>";

$tot=0;
      $sql = "Select * from tmp_matreqesion_data where tmp_no='" . $_SESSION['tmp_no_purord'] . "'";
      $result = $db->RunQuery($sql);
      while ($row = mysqli_fetch_array($result)) {
          $sql1 = "Select * from s_mas where STK_NO='" . $row['str_code'] . "'";
        $result1 = $db->RunQuery($sql1);
        $row1= mysqli_fetch_array($result1);
          $ResponseXML .= "<tr>
                               <td >" . $row['str_code'] . "</a></td> 
  							 <td>" . $row['str_description'] . "</a></td>
  							 <td >" . $row['partno'] . "</a></td>
                 <td >" . $row['uom'] . "</a></td>
                 <td >" . $row['unitprice'] . "</a></td>
  							 <td >" . number_format($row['qty'], 2, ".", ",") . "</a></td>
  							 <td >" . number_format($row['qty']*$row['unitprice'] , 2, ".", ",") . "</a></td>
  							 <td ><img src=\"images/delete_01.png\" width=\"20\" height=\"20\" id=" . $row['str_code'] . "  name=" . $row['str_code'] . " onClick=\"del_item('" . $row['str_code'] . "');\"></td></tr>";
         $tot=$tot+$row['qty']*$row['unitprice'];
                
				}

                
                $ResponseXML .= "   </table>]]></sales_table>";
$ResponseXML .= "<tot12><![CDATA[".number_format($tot, 2, ".", ",")." ]]></tot12>";

      $ResponseXML .= " </salesdetails>";

      //	}


      echo $ResponseXML;
  }

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

      //$department=$_GET["department"];
      echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";

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

      $_SESSION["update"] = 1;

      $_SESSION["uniq"] = uniqid();

      $sql = "select * from s_matrequesionmas where REFNO='" . $_GET['invno'] . "' ";
  //    ECHO $sql;
      $result = $db->RunQuery($sql);
      if ($row = mysqli_fetch_array($result)) { 
          $ResponseXML .= "<REQNO><![CDATA[".$row['LC_No']."]]></REQNO>";
          $ResponseXML .= "<SDATE><![CDATA[" . $row['SDATE'] . "]]></SDATE>";
          $ResponseXML .= "<SUP_CODE><![CDATA[" . $row['SUP_CODE'] . "]]></SUP_CODE>";
          $ResponseXML .= "<SUP_NAME><![CDATA[" . $row['SUP_NAME'] . "]]></SUP_NAME>";
          $ResponseXML .= "<REMARK><![CDATA[" . $row['REMARK'] . "]]></REMARK>";
          $ResponseXML .= "<DEP_CODE><![CDATA[" . $row['DEP_CODE'] . "]]></DEP_CODE>";
          $ResponseXML .= "<DEP_NAME><![CDATA[" . $row['DEP_NAME'] . "]]></DEP_NAME>";
          $ResponseXML .= "<REP_CODE><![CDATA[" . $row['REP_CODE'] . "]]></REP_CODE>";
          $ResponseXML .= "<REP_NAME><![CDATA[" . $row['REP_NAME'] . "]]></REP_NAME>";
          $ResponseXML .= "<S_date><![CDATA[" . $row['S_date'] . "]]></S_date>"; 
          $ResponseXML .= "<Brand><![CDATA[" . $row['Brand'] . "]]></Brand>";

           $ResponseXML .= "<address><![CDATA[" . $row['SUP_ADDR'] . "]]></address>";
          $ResponseXML .= "<isoNo><![CDATA[" . $row['isoNo'] . "]]></isoNo>";
          $ResponseXML .= "<purchasingGroup><![CDATA[" . $row['purchasingGroup'] . "]]></purchasingGroup>";
          $ResponseXML .= "<vatNo><![CDATA[" . $row['vatNo'] . "]]></vatNo>";
          $ResponseXML .= "<pr_No><![CDATA[" . $row['prNo'] . "]]></pr_No>";
          $ResponseXML .= "<sdate><![CDATA[" . $row['S_date'] . "]]></sdate>";
          $ResponseXML .= "<pino><![CDATA[" . $row['pi_no'] . "]]></pino>";
          $ResponseXML .= "<prNo><![CDATA[" . $row['REFNO'] . "]]></prNo>";
           $ResponseXML .= "<ordrfor><![CDATA[" . $row['ordrfor'] . "]]></ordrfor>";
           $ResponseXML .= "<REMARK><![CDATA[" . $row['REMARK'] . "]]></REMARK>";
                   $ResponseXML .= "<som><![CDATA[" . $row['som'] . "]]></som>";
 
          $ResponseXML .= "<ARRIVED><![CDATA[" . $row['cancel'] . "]]></ARRIVED>";
          $_SESSION['tmp_no_purord'] = $row["tmp_no"];
      }

      $sql = "delete from tmp_purord_data where tmp_no='" . $_SESSION['tmp_no_purord'] . "' ";
      //$ResponseXML .= $sql;
      $result = $db->RunQuery($sql);

      $sql = "select * from s_materialreqtrn where REFNO='" . $_GET['invno'] . "' ";
      //$ResponseXML .= $sql;
      $result = $db->RunQuery($sql);
      while ($row = mysqli_fetch_array($result)) {
          $sql1 = "Insert into tmp_purord_data (tmp_no, str_invno, str_code, str_description, partno, qty,uom, uniq,unitprice)values
  				('" . $_SESSION['tmp_no_purord'] . "', '" . $row['REFNO'] . "', '" . $row['STK_NO'] . "', '" . $row['DESCRIPT'] . "', '" . $row["partno"] . "', " . $row["ORD_QTY"] . ", '" . $row["uom"] . "', '" . $_SESSION["uniq"] . "', '" . $row['unitprice'] . "') ";
          // echo $sql1;
          //$ResponseXML .= $sql;
          $result1 = $db->RunQuery($sql1);
      }




 $sql = "Select * from tmp_purord_data where str_invno='" . $_GET['invno'] . "'";

      $ResponseXML .= "<sales_table><![CDATA[ <table><tr>
                                <td width=\"200\"  background=\"\">Stock No</td> 
                              <td width=\"500\"  background=\"\"><font color=\"#FFFFFF\">Description</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Gen No</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">UOM</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Unit Price</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Qty</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Total</font></td>

                              </tr>";


     
      $result = $db->RunQuery($sql);

$tot=0;
      $count = 0;
      while ($row = mysqli_fetch_array($result)) {
          $identity = $row['str_code'];
            $sql1 = "Select * from s_mas where STK_NO='" . $row['str_code'] . "'";
        $result1 = $db->RunQuery($sql1);
        $row1= mysqli_fetch_array($result1);
          $ResponseXML .= "<tr>
              <td >".$row['str_code']."</a></td> 
							 <td >".$row['str_description']."</a></td>
						   	<td >".$row['partno']."</a></td>
                              <td >".$row['uom']."</a></td>
                              <td >".$row['unitprice']."</a></td>
							 <td >".number_format($row['qty'], 2, ".", ",")."</a></td>
							 <td >".number_format($row['qty']*$row['unitprice'], 2, ".", ",")."</a></td> 
                                 <td ><img src=\"images/delete_01.png\" width=\"20\" height=\"20\" id=".$row['str_code']."  name=".$row['str_code']." onClick=\"del_item('".$row['str_code']."');\"></td></tr>"; 
                  
                 ++$count;

              $tot=$tot+$row['qty']*$row['unitprice'];
				}

				$ResponseXML .= "   </table>]]></sales_table>";
                 
                  $ResponseXML .= "<tot12><![CDATA[".number_format($tot, 2, ".", ",")."]]></tot12>";
 $ResponseXML .= " </salesdetails>";
      echo $ResponseXML;
  }


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

    include_once("connectioni.php");

    $len = strlen($_GET["salesord1"]);
    $need = substr($_GET["salesord1"], $len - 7, $len);
    $salesord1 = trim("ORD/ ") . $_GET["salesrep"] . trim(" / ") . $need;


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



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

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

    $cuscode = $_GET["custno"];
    $salesrep = $_GET["salesrep"];
    $brand = $_GET["brand"];

    //		$ResponseXML .= "<salesord><![CDATA[".$salesord1."]]></salesord>";
    //Call SETLIMIT ====================================================================



    /* 	$sql = mysqli_query($GLOBALS['dbinv'],"DROP VIEW view_s_salma") or die(mysqli_error($GLOBALS['dbinv']));
      $sql = mysqli_query($GLOBALS['dbinv'],"CREATE VIEW view_s_salma
      AS
      SELECT     s_salma.*, brand_mas.class AS class
      FROM         brand_mas RIGHT OUTER JOIN
      s_salma ON brand_mas.barnd_name = s_salma.brand") or die(mysqli_error($GLOBALS['dbinv'])); */

    $OutpDAMT = 0;
    $OutREtAmt = 0;
    $OutInvAmt = 0;
    $InvClass = "";

    $sqlclass = mysqli_query($GLOBALS['dbinv'], "select class from brand_mas where barnd_name='" . trim($brand) . "'") or die(mysqli_error($GLOBALS['dbinv']));
    if ($rowclass = mysqli_fetch_array($sqlclass)) {
        if (is_null($rowclass["class"]) == false) {
            $InvClass = $rowclass["class"];
        }
    }

    $sqloutinv = mysqli_query($GLOBALS['dbinv'], "select sum(GRAND_TOT-TOTPAY) as totout from view_s_salma where GRAND_TOT>TOTPAY and CANCELL='0' and C_CODE='" . trim($cuscode) . "' and SAL_EX='" . trim($salesrep) . "' and class='" . $InvClass . "'") or die(mysqli_error($GLOBALS['dbinv']));
    if ($rowoutinv = mysqli_fetch_array($sqloutinv)) {
        if (is_null($rowoutinv["totout"]) == false) {
            $OutInvAmt = $rowoutinv["totout"];
        }
    }

    $sqlinvcheq = mysqli_query($GLOBALS['dbinv'], "SELECT * FROM s_invcheq WHERE che_date>'" . date("d-m-Y") . "' AND cus_code='" . trim($cuscode) . "' and trn_type='REC' and sal_ex='" . trim($salesrep) . "'") or die(mysqli_error($GLOBALS['dbinv']));
    while ($rowinvcheq = mysqli_fetch_array($sqlinvcheq)) {

        $sqlsttr = mysqli_query($GLOBALS['dbinv'], "select * from s_sttr where ST_REFNO='" . trim($rowinvcheq["refno"]) . "' and ST_CHNO ='" . trim($rowinvcheq["cheque_no"]) . "'") or die(mysqli_error($GLOBALS['dbinv']));
        while ($rowsttr = mysqli_fetch_array($sqlsttr)) {
            $sqlview_s_salma = mysqli_query($GLOBALS['dbinv'], "select class from view_s_salma where REF_NO='" . trim($rowsttr["ST_INVONO"]) . "'") or die(mysqli_error($GLOBALS['dbinv']));
            if ($rowview_s_salma = mysqli_fetch_array($sqlview_s_salma)) {

                if (trim($rowview_s_salma["class"]) == $InvClass) {
                    $OutpDAMT = $OutpDAMT + $rowsttr["ST_PAID"];
                }
            }
        }
    }



    $pend_ret_set = 0;

    $sqlinvcheq = mysqli_query($GLOBALS['dbinv'], "SELECT sum(che_amount) as  che_amount FROM s_invcheq WHERE che_date >'" . date("d-m-Y") . "' AND cus_code='" . trim($cuscode) . "' and trn_type='RET'") or die(mysqli_error($GLOBALS['dbinv']));
    if ($rowinvcheq = mysqli_fetch_array($sqlinvcheq)) {
        if (is_null($rowinvcheq["che_amount"]) == false) {
            $pend_ret_set = $rowinvcheq["che_amount"];
        }
    }


    $sqlcheq = mysqli_query($GLOBALS['dbinv'], "Select sum(CR_CHEVAL-PAID) as tot from s_cheq where CR_C_CODE='" . trim($cuscode) . "'  and CR_CHEVAL-PAID>0 and CR_FLAG='0' and S_REF='" . trim($salesrep) . "'") or die(mysqli_error($GLOBALS['dbinv']));
    if ($rowcheq = mysqli_fetch_array($sqlcheq)) {
        if (is_null($rowcheq["tot"]) == false) {
            $OutREtAmt = $rowcheq["tot"];
        } else {
            $OutREtAmt = 0;
        }
    }



    $ResponseXML .= "<sales_table_acc><![CDATA[ <table  bgcolor=\"#0000FF\" border=1  cellspacing=0>
						<tr><td>Outstanding Invoice Amount</td><td>" . number_format($OutInvAmt, 2, ".", ",") . "</td></tr>
						 <td>Return Cheque Amount</td><td>" . number_format($OutREtAmt, 2, ".", ",") . "</td></tr>
						 <td>Pending Cheque Amount</td><td>" . number_format($OutpDAMT, 2, ".", ",") . "</td></tr>
						 <td>PSD Cheque Settlments</td><td>" . number_format($pend_ret_set, 2, ".", ",") . "</td></tr>
						 <td>Total</td><td>" . number_format($OutInvAmt + $OutREtAmt + $OutpDAMT + $pend_ret_set, 2, ".", ",") . "</td></tr>
						 </table></table>]]></sales_table_acc>";


    $sqlbr_trn = mysqli_query($GLOBALS['dbinv'], "select * from br_trn where Rep='" . trim($salesrep) . "' and brand='" . trim($InvClass) . "' and cus_code='" . trim($cuscode) . "'") or die(mysqli_error($GLOBALS['dbinv']));
    if ($rowbr_trn = mysqli_fetch_array($sqlbr_trn)) {
        if (is_null($rowbr_trn["credit_lim"]) == false) {
            $crLmt = $rowbr_trn["credit_lim"];
        } else {
            $crLmt = 0;
        }


        if (is_null($rowbr_trn["tmpLmt"]) == false) {
            $tmpLmt = $rowbr_trn["tmpLmt"];
        } else {
            $tmpLmt = 0;
        }

        if (is_null($rowbr_trn["CAT"]) == false) {
            $cuscat = trim($rowbr_trn["cat"]);
            if ($cuscat = "A") {
                $m = 2.5;
            }
            if ($cuscat = "B") {
                $m = 2.5;
            }
            if ($cuscat = "C") {
                $m = 1;
            }
            $txt_crelimi = "0";
            $txt_crebal = "0";
            $txt_crelimi = number_format($crLmt, 2, ".", ",");
            $crebal = $crLmt * $m + $tmpLmt - $OutInvAmt - $OutREtAmt - $OutpDAMT - $pend_ret_set;
            $txt_crebal = number_format($crebal, "2", ".", ",");
        } else {
            $txt_crelimi = "0";
            $txt_crebal = "0";
        }
        $creditbalance = $OutInvAmt + $OutREtAmt + $OutpDAMT;
    }
    $ResponseXML .= "<txt_crelimi><![CDATA[" . $txt_crelimi . "]]></txt_crelimi>";
    $ResponseXML .= "<txt_crebal><![CDATA[" . $txt_crebal . "]]></txt_crebal>";

    $ResponseXML .= "<creditbalance><![CDATA[" . $creditbalance . "]]></creditbalance>";


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


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


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


    $sql = "delete from tmp_matreqesion_data where str_code='" . $_GET['code'] . "' and str_invno='" . $_GET['invno'] . "' ";

    $result = $db->RunQuery($sql);

    $ResponseXML .= "<sales_table><![CDATA[ <table><tr>
                               <td width=\"200\"  background=\"\">Stock No</td> 
                              <td width=\"500\"  background=\"\"><font color=\"#FFFFFF\">Description</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Gen No</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">UOM</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Unit Price</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Qty</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Total</font></td>
                            </tr>";
$tot=0;

    $sql = "Select * from tmp_matreqesion_data where str_invno='" . $_GET['invno'] . "' and tmp_no='".$_SESSION['tmp_no_purord']."'";
    $result = $db->RunQuery($sql);
    while ($row = mysqli_fetch_array($result)) {
        $sql1 = "Select * from s_mas where STK_NO='" . $row['str_code'] . "'";
        $result1 = $db->RunQuery($sql1);
        $row1= mysqli_fetch_array($result1);
        $ResponseXML .= "<tr>
                             <td >".$row['str_code']."</a></td> 
							 <td >".$row['str_description']."</a></td>
						   	<td >".$row['partno']."</a></td>
                              <td >".$row['uom']."</a></td>
                              <td >".$row['unitprice']."</a></td>
							 <td >".number_format($row['qty'], 2, ".", ",")."</a></td>
							 <td >".number_format($row['qty']*$row['unitprice'], 2, ".", ",")."</a></td> 
               
			 <td ><img src=\"images/delete_01.png\" width=\"20\" height=\"20\" id=" . $row['str_code'] . "  name=" . $row['str_code'] . " onClick=\"del_item('" . $row['str_code'] . "');\"></td></tr>";
     $tot=$tot+$row['qty']*$row['unitprice'];
				}

				$ResponseXML .= "   </table>]]></sales_table>";
                 
                  $ResponseXML .= "<tot12><![CDATA[".number_format($tot, 2, ".", ",")."]]></tot12>";

    $ResponseXML .= " </salesdetails>";


    //	}


    echo $ResponseXML;
}



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

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

    //	$_SESSION["CURRENT_DOC"] = 1;      //document ID
    //	$_SESSION["VIEW_DOC"] = false ;     //view current document
    //	$_SESSION["FEED_DOC"] = true;       //save  current document
    //	$_GET["MOD_DOC"] = false  ;         //delete   current document
    //	$_GET["PRINT_DOC"] = false  ;       //get additional print   of  current document
    //	$_GET["PRICE_EDIT"] = false ;       //edit selling price
    //	$_GET["CHECK_USER"] = false ;       //check user permission again
    //$cre_balance=str_replace(",", "", $_GET["balance"]);
    
    $sql = "select * from s_matrequesionmas where REFNO='" . $_GET["invno"] . "'";
    $result = $db->RunQuery($sql);
    //echo $sql;
    if ($row = mysqli_fetch_array($result)) {
        exit("Duplicate ....!!!");
    }

    $supplier_name = str_replace("~", "&", $_GET["supplier_name"]);

    $sql = "select * from s_matrequesionmas where REFNO='" . $_GET["invno"] . "'";
    $result = $db->RunQuery($sql);
    //echo $sql;
    if ($row = mysqli_fetch_array($result)) {
        $sql1 = "delete from s_matrequesionmas where REFNO='" . $_GET["invno"] . "'";
        $result1 = $db->RunQuery($sql1);

        $sql1 = "delete from s_materialreqtrn where REFNO='" . $_GET["invno"] . "'";
        $result1 = $db->RunQuery($sql1);
    } else {
        $sql = "Select MR_NO from invpara where COMCODE='" . $_SESSION['company'] . "'";
    $result = $db->RunQuery($sql);
    $row = mysqli_fetch_array($result);
    $tmpinvno = "0000000" . $row["MR_NO"];
    $lenth = strlen($tmpinvno);
    $invno = $_SESSION['company'] . "/".trim("GNNE/PR/") . substr($tmpinvno, $lenth - 8);
        $_SESSION["invno"] = $invno;

        $sql1 = "update invpara set MR_NO=MR_NO+1 where COMCODE='" . $_SESSION['company'] . "'";
        $result1 = $db->RunQuery($sql1);
    }
    $sql1 = "select * from s_stomas where CODE='" . $_GET["department"] . "'";
    $result1 = $db->RunQuery($sql1);
    $row1 = mysqli_fetch_array($result1);
    $dep_name = $row1["DESCRIPTION"];

    $sql1 = "select * from s_salrep where REPCODE='" . $_GET["salesrep"] . "'";
    $result1 = $db->RunQuery($sql1);
    $row1 = mysqli_fetch_array($result1);
    $rep_name = $row1["Name"];

    $sql1 = "insert into s_matrequesionmas(company,REFNO, REQ_NO, SDATE, SUP_CODE, SUP_NAME,SUP_ADDR, REMARK, DEP_CODE, DEP_NAME, REP_CODE, REP_NAME, cancel, S_date, LC_No, pi_no, Brand, tmp_no,som,isoNo,purchasingGroup,vatNo,prNo, reason,cat,ordrfor) values 
    ('".$_SESSION['company']."','" . $_GET["invno"] . "','" . $_GET["req_no"] . "', '" . $_GET["invdate"] . "', '" . $_GET["supplier_code"] . "','" . $supplier_name . "','" .  $_GET["address"]. "','" . $_GET["remarks"] . "','" . $_GET["department"] . "','" . $dep_name . "','" . $_GET["salesrep"] . "','" . $rep_name . "', '0','" . $_GET["dte_shedule"] . "','" . $_GET["lc_no"] . "', '" . $_GET["pi_no"] . "', '" . $_GET["brand"] . "',  '" . $_SESSION['tmp_no_purord'] . "' , '" . $_GET["som"] . "', '" . $_GET["isoNo"] . "', '" . $_GET["purchasingGroup"] . "', '" . $_GET["vatNo"] . "',  '" . $_GET['prNo'] . "',  '" . $_GET['reason'] . "',  '" . $_GET['cat'] . "', '" . $_GET['ordrfor'] . "')";
    //echo $sql1;
    $result1 = $db->RunQuery($sql1);


    $sql = "select * from tmp_matreqesion_data where tmp_no='" . $_SESSION['tmp_no_purord'] . "'";
    $result = $db->RunQuery($sql);
    while ($row = mysqli_fetch_array($result)) {
        $sql1 = "insert into s_materialreqtrn(REFNO, SDATE, STK_NO, DESCRIPT, ORD_QTY, partno, CANCEL, tmp_no,uom,unitprice) values ('" . $row["str_invno"] . "', '" . $_GET["invdate"] . "', '" . $row["str_code"] . "','" . $row["str_description"] . "','" . $row["qty"] . "','" . $row["partno"] . "','0', '" . $_SESSION['tmp_no_purord'] . "','" . $row["uom"] . "','" . $row["unitprice"] . "')";
        //echo $sql1;
        $result1 = $db->RunQuery($sql1);
    }
    //$_SESSION["print"]=1;


    echo "Saved";
}


if ($_GET["Command"] == "cancel_inv") {
    $sql1 = "update s_matrequesionmas set cancel='1' where REFNO='" . $_GET["invno"] . "'";
    $result1 = $db->RunQuery($sql1);

    $sql1 = "update s_materialReqtrn set CANCEL='1' where REFNO='" . $_GET["invno"] . "'";
    $result1 = $db->RunQuery($sql1);
    echo "Canceled!";
}

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

    echo $_SESSION["print"];
}


if ($_GET["Command"] == "tmp_crelimit") {
    //echo "abc";
    $crLmt = 0;
    $cat = "";

    $rep = trim(substr($_GET["Com_rep1"], 0, 5));

    $sql = "select * from br_trn where rep='" . $rep . "' and cus_code='" . trim($_GET["txt_cuscode"]) . "' and brand='" . trim($_GET["cmbbrand1"]) . "'";
    echo $sql;
    $result = $db->RunQuery($sql);
    if ($row = mysqli_fetch_array($result)) {
        $crLmt = $row["credit_lim"];
        If (is_null($row["cat"]) == false) {
            $cat = trim($row["cat"]);
        } else {
            $crLmt = 0;
        }
    }
    /*
      $_SESSION["CURRENT_DOC"] = 66     //document ID
      //$_SESSION["VIEW_DOC"] = true      //  view current document
      $_SESSION["FEED_DOC"] = true      //  save  current document
      //$_SESSION["MOD_DOC"] = true       //   delete   current document
      //$_SESSION["PRINT_DOC"] = true     // get additional print   of  current document
      //$_SESSION["PRICE_EDIT"]= true     // edit selling price
      $_SESSION["CHECK_USER"] = true    // check user permission again
      $crLmt = $crLmt;
      setlocale(LC_MONETARY, "en_US");
      $CrTmpLmt = number_format($_GET["txt_tmeplimit"], 2, ".", ",");

      $REFNO = trim($_GET["txt_cuscode"]) ;

      $AUTH_USER="tmpuser";

      $sql = "insert into tmpCrLmt (sdate, stime, username, tmpLmt, class, rep, cuscode, crLmt, cat) values
      ('".date("Y-m-d")."','".date("H:i:s", time())."' ,'".$AUTH_USER."',".$CrTmpLmt." ,'".trim($_GET["cmbbrand1"])."','".$rep."','".trim($_GET["txt_cuscode"])."',".$crLmt.",'".$cat"' )";
      $result =$db->RunQuery($sql);

      $sql = "select * from  br_trn where cus_code='".trim($_GET["txt_cuscode"])."' and rep='".$rep."' and brand='".$_GET["cmbbrand1"]."'";
      $result =$db->RunQuery($sql);
      if ($row = mysqli_fetch_array($result)) {
      $sqlbrtrn= "insert into br_trn (cus_code, rep, credit_lim, brand, tmplmt) values ('".trim($_GET["txt_cuscode"])."','".$rep."','0','".trim($_GET["cmbbrand1"])."',".$CrTmpLmt." )";
      $resultbrtrn =$db->RunQuery($sqlbrtrn);

      } else {

      $sqlbrtrn= "update br_trn set tmplmt= ".$CrTmpLmt."  where cus_code='".trim($_GET["txt_cuscode"])."' and rep='".$rep."' and brand='".$_GET["cmbbrand1"]."' ";
      $resultbrtrn =$db->RunQuery($sqlbrtrn);

      //	$sqlbrtrn= "update vendor set temp_limit= ".$CrTmpLmt."  where code='".trim($_GET["txt_cuscode"])."' "
      }

      If ($_GET["Check1"] == 1) {
      $sqlblack= "update vendor set blacklist= '1'  where code='".trim($_GET["txt_cuscode"])."' ";
      $resultblack =$db->RunQuery($sqlblack);
      } else {
      $sqlblack= "update vendor set blacklist= '0'  where code='".trim($_GET["txt_cuscode"])."' ";
      $resultblack =$db->RunQuery($sqlblack);
      }

      echo "Tempory limit updated"; */
}

if ($_GET["Command"] == "calcu") {
   header('Content-Type: text/xml');
     


    $ResponseXML = "";
    $ResponseXML .= "<salesdetails>";
    $total = 0;
    // if ($_GET["dis1"] != '') {
        
        // if ($_GET["dis2"] != '') {
//            var tot = document.getElementById('tot').value;
            $total = $_GET["up"] * $_GET["qty"]; 
        // } else {

        //     $total = ($_GET["sal"] - ($_GET["sal"] * $_GET["dis1"]) / 100);
        // }
    // }

   
    $tot = number_format($total);

    $ResponseXML .= "<toot><![CDATA[$tot]]></toot>";
 $ResponseXML .= "</salesdetails>";
    echo $ResponseXML;
}
?>
