<?php session_start();

////////////////////////////////////////////// Database Connector /////////////////////////////////////////////////////////////
	require_once("config.inc.php");
	require_once("DBConnector.php");
	$db = new DBConnector(); $dbinv = $db->GetConnection();
	
	////////////////////////////////////////////// Write XML ////////////////////////////////////////////////////////////////////
	header('Content-Type: text/xml'); 
	
	 date_default_timezone_set('Asia/Colombo'); 
	
	/////////////////////////////////////// GetValue //////////////////////////////////////////////////////////////////////////
 
	
			
		if($_GET["Command"]=="new_inv")
		{
			
			
			$_SESSION["insert"]=1;
			
		/*	$sql="Select CAS_INV_NO_m from invpara";
			$result =$db->RunQuery($sql);
			$row = $result->fetch();
			$tmpinvno="000000".$row["CAS_INV_NO_m"];
			$lenth=strlen($tmpinvno);
			$invno="INV".substr($tmpinvno, $lenth-7);
			echo $invno;*/
			
			$sql="Select M_ORD_NO from invpara where COMCODE='".$_SESSION['company']."'"; 
			$result =$db->RunQuery($sql);
			$row = $result->fetch();
			$tmpinvno="000000".$row["M_ORD_NO"];
			$lenth=strlen($tmpinvno);
			$invno=$_SESSION['company'] . trim("MORD/ ").substr($tmpinvno, $lenth-7);
			$_SESSION["invno"]=$invno;
			
			$sql="Select M_ORD_NO from tmpinvpara";
			$result =$db->RunQuery($sql);
			$row = $result->fetch();
			$_SESSION["tmp_no_purord"]=$_SESSION['company'] . "MORD/".$row["M_ORD_NO"];
			
			$sql1="delete from tmp_purord_data where tmp_no='".$_SESSION["tmp_no_purord"]."'";
			$result1 =$db->RunQuery($sql1);
			
			$sql1="update tmpinvpara set M_ORD_NO=M_ORD_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']." ".$_GET["partno"];
			$sql="Insert into tmp_purord_data (tmp_no, str_invno, str_code, str_description, partno, qty,crate)values 
			('".$_SESSION["tmp_no_purord"]."', '".$_GET['invno']."', '".$_GET['itemcode']."', '".$itemdesc."', '".$_GET["partno"]."', ".$qty.", '".$_GET["crate"]."') ";
			//$ResponseXML .= $sql;
			$result =$db->RunQuery($sql);	
			
			
				$ResponseXML .= "<sales_table><![CDATA[ <table><tr>
                              <td width=\"100\"  background=\"\" ><font color=\"#FFFFFF\">Code</font></td>
                              <td width=\"500\"  background=\"\"><font color=\"#FFFFFF\">Description</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Part No</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Currency Rate</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Qty</font></td>
                             
                            </tr>";
							
			
			$sql="Select * from tmp_purord_data where tmp_no='".$_SESSION['tmp_no_purord']."' order by id";
			$result =$db->RunQuery($sql);	
			while($row = $result->fetch()){				
             	$ResponseXML .= "<tr>                              
                             <td >".$row['str_code']."</a></td>
							 <td >".$row['str_description']."</a></td>
							 <td >".$row['partno']."</a></td>
							 <td >".number_format($row['crate'], 2, ".", ",")."</a></td>
							 <td >".number_format($row['qty'], 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>";
							 
                           
				}			
							
                $ResponseXML .= "   </table>]]></sales_table>";
				
				$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;
			
			
				$ResponseXML .= "<stname><![CDATA[".$_GET['stname']."]]></stname>";
			
			$sql="select * from s_matereq where REFNO='".$_GET['invno']."' ";
			//$ResponseXML .= $sql;
			$result =$db->RunQuery($sql);
			if($row = $result->fetch()){
				$ResponseXML .= "<REFNO><![CDATA[".$row['REFNO']."]]></REFNO>";
				$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 .= "<LC_No><![CDATA[".$row['LC_No']."]]></LC_No>";
				$ResponseXML .= "<pi_no><![CDATA[".$row['pi_no']."]]></pi_no>";
				$ResponseXML .= "<Brand><![CDATA[".$row['Brand']."]]></Brand>";
				$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_matereq_trn where REFNO='".$_GET['invno']."' order by id asc";  
			//$ResponseXML .= $sql;
			$result =$db->RunQuery($sql);
			while($row = $result->fetch()){
				$sql1="Insert into tmp_purord_data (tmp_no, str_invno, str_code, str_description, partno, qty,crate)values 
				('".$_SESSION['tmp_no_purord']."', '".$row['REFNO']."', '".$row['STK_NO']."', '".$row['DESCRIPT']."', '".$row["partno"]."', ".$row["ORD_QTY"].", '".$row["CRATE"]."') ";
			//$ResponseXML .= $sql;
				$result1 =$db->RunQuery($sql1);	
			}
							
			
			
	
			
			
				$ResponseXML .= "<sales_table><![CDATA[ <table><tr>
                              <td width=\"70\"  background=\"\" >Code</td>
                              <td width=\"500\"  background=\"\">Description</td>
                              <td width=\"100\"  background=\"\">Part No</td>
                              <td width=\"100\"  background=\"\">Currency Rate</td>
                              <td width=\"100\"  background=\"\">Qty</td>
                             
                            </tr>";
							
// 			echo $_SESSION['tmp_no_purord'];
			$sql="Select * from tmp_purord_data where tmp_no='".$_SESSION['tmp_no_purord']."' order by id asc";
			$result =$db->RunQuery($sql);	
			while($row = $result->fetch()){				
             	$ResponseXML .= "<tr>                              
                             <td >".$row['str_code']."</a></td>
							 <td>".$row['str_description']."</a></td>
							 <td >".$row['partno']."</a></td>
							 <td >".number_format($row['crate'], 2, ".", ",")."</a></td>
							 <td >".number_format($row['qty'], 0, ".", ",")."</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>";
							 
                           
				}			
							
                $ResponseXML .= "   </table>]]></sales_table>";
				
				$ResponseXML .= " </salesdetails>";
				
		//	}	
				
				
				echo $ResponseXML;
				
			
	}
	
	
	if($_GET["Command"]=="setord")
	{
		
		include_once("connection.php");
		
		$len=strlen($_GET["salesord1"]);
		$need=substr($_GET["salesord1"],$len-7, $len);
		$salesord1=trim("MORD/ ").$_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 = $dbinv->query("DROP VIEW view_s_salma") or die("Error";
					$sql = mysql_query("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("Error";*/

	$OutpDAMT = 0;
	$OutREtAmt = 0;
	$OutInvAmt = 0;
	$InvClass="";
	
	$sqlclass = $dbinv->query("select class from brand_mas where barnd_name='".trim($brand)."'") or die("Error";
	if ($rowclass = $sqlclass->fetch()){
		if (is_null($rowclass["class"])==false){
			$InvClass=$rowclass["class"];
		}
	}
	
	$sqloutinv = $dbinv->query("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("Error";
	if ($rowoutinv = $sqloutinv->fetch()){
		if (is_null($rowoutinv["totout"])==false){
			$OutInvAmt=$rowoutinv["totout"];
		}
	}

$sqlinvcheq = $dbinv->query("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("Error";
	while($rowinvcheq = $sqlinvcheq->fetch()){
		
		$sqlsttr = $dbinv->query("select * from s_sttr where ST_REFNO='".trim($rowinvcheq["refno"])."' and ST_CHNO ='".trim($rowinvcheq["cheque_no"]) ."'") or die("Error";
		while($rowsttr = $sqlsttr->fetch()){
			$sqlview_s_salma = $dbinv->query("select class from view_s_salma where REF_NO='".trim($rowsttr["ST_INVONO"])."'") or die("Error";
			if($rowview_s_salma = $sqlview_s_salma->fetch()){
				
				if (trim($rowview_s_salma["class"]) == $InvClass){
                    $OutpDAMT = $OutpDAMT + $rowsttr["ST_PAID"];
                }
			}
		}
	}


        
        $pend_ret_set = 0;
		
		$sqlinvcheq = $dbinv->query("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("Error";
		if($rowinvcheq = $sqlinvcheq->fetch()){
			if (is_null($rowinvcheq["che_amount"])==false){
				$pend_ret_set=$rowinvcheq["che_amount"];
			}
		}
		
		
		$sqlcheq = $dbinv->query("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("Error";
		if($rowcheq = $sqlcheq->fetch()){
			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 = $dbinv->query("select * from br_trn where Rep='".trim($salesrep)."' and brand='".trim($InvClass)."' and cus_code='" .trim($cuscode)."'") or die("Error";  
	if($rowbr_trn = $sqlbr_trn->fetch()){
		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_purord_data where str_code='".$_GET['code']."' and tmp_no='".$_SESSION["tmp_no_purord"]."' ";
			
			$result =$db->RunQuery($sql);
			
			$ResponseXML .= "<sales_table><![CDATA[ <table><tr>
                              <td width=\"100\"  background=\"\" ><font color=\"#FFFFFF\">Code</font></td>
                              <td width=\"500\"  background=\"\"><font color=\"#FFFFFF\">Description</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Part No</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Currency Rate</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Qty</font></td>
                            </tr>";
							
			
			$sql="Select * from tmp_purord_data where str_invno='".$_GET['invno']."' order by id";
			$result =$db->RunQuery($sql);	
			while($row = $result->fetch()){				
             	$ResponseXML .= "<tr>
                              
                             <td >".$row['str_code']."</a></td>
							 <td >".$row['str_description']."</a></td>
							 <td >".$row['partno']."</a></td>
							 <td >".$row['crate']."</a></td>
							 <td >".$row['qty']."</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>";
							 
						
				}			
				
				$ResponseXML .= "   </table>]]></sales_table>";
							
                $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"]);

			$supplier_name=str_replace("~", "&", $_GET["supplier_name"]);
			
			$sql="select * from s_matereq where REFNO='".$_GET["invno"]."'";
			$result =$db->RunQuery($sql);
			//echo $sql;
			if($row = $result->fetch()){
				$sql1="delete from s_matereq where REFNO='".$_GET["invno"]."'";
				$result1 =$db->RunQuery($sql1);	
				
				$sql1="delete from s_matereq_trn where REFNO='".$_GET["invno"]."'";
				$result1 =$db->RunQuery($sql1);	
			} else {
				$sql="Select M_ORD_NO from invpara where COMCODE='".$_SESSION['company']."'";
				$result =$db->RunQuery($sql);
				$row = $result->fetch();
				$tmpinvno="000000".$row["M_ORD_NO"];
				$lenth=strlen($tmpinvno);
				$invno=trim("MORD/ ").substr($tmpinvno, $lenth-7);
				$_SESSION["invno"]=$invno;
				
				$sql1="update invpara set M_ORD_NO=M_ORD_NO+1 where COMCODE='".$_SESSION['company']."'";
				$result1 =$db->RunQuery($sql1);
			}
			$sql1="select * from s_stomas where CODE='".$_GET["department"]."'";
			$result1 =$db->RunQuery($sql1);
			$row1 = $result1->fetch();
			$dep_name=$row1["DESCRIPTION"];
			
			$sql1="select * from s_salrep where REPCODE='".$_GET["salesrep"]."'";
			$result1 =$db->RunQuery($sql1);
			$row1 = $result1->fetch();
			$rep_name=$row1["Name"];
			
			$sql1="insert into s_matereq(REFNO, SDATE, SUP_CODE, SUP_NAME, REMARK, DEP_CODE, DEP_NAME, REP_CODE, REP_NAME, cancel, S_date, LC_No, pi_no, Brand, tmp_no,CURRENCY) values 
			('".$_GET["invno"]."', '".$_GET["invdate"]."', '".$_GET["supplier_code"]."','".$supplier_name."','".$_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["currency"]."')";
			//echo $sql1;
			$result1 =$db->RunQuery($sql1);	
			
			
			$sql="select * from tmp_purord_data where tmp_no='".$_SESSION['tmp_no_purord']."'";
			$result =$db->RunQuery($sql);
			while($row = $result->fetch()){
    			$sql1="insert into s_matereq_trn(REFNO, SDATE, STK_NO, DESCRIPT, ORD_QTY, partno, CANCEL, tmp_no,CRATE) values 
    			('".$row["str_invno"]."', '".$_GET["invdate"]."', '".$row["str_code"]."','".$row["str_description"]."','".$row["qty"]."','".$row["partno"]."','0', '".$_SESSION['tmp_no_purord']."','".$row["crate"]."')";
				//echo $sql1;
				$result1 =$db->RunQuery($sql1);	
  			}
		//$_SESSION["print"]=1;

			
			echo "Saved";
			
			
	}
	
	if ($_GET["Command"] == "search_inv") {

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



    $ResponseXML .= "<table width=\"735\" border=\"0\" class=\"form-matrix-table\">
                            <tr>
                              <td width=\"121\"  background=\"images/headingbg.gif\" ><font color=\"#FFFFFF\">Order No</font></td>
                              <td width=\"424\"  background=\"images/headingbg.gif\"><font color=\"#FFFFFF\">Supplier</font></td>
							  <td width=\"176\"  background=\"images/headingbg.gif\"><font color=\"#FFFFFF\">Order Date</font></td>
                             
   							</tr>";

    if ($_GET["mstatus"] == "invno") {
        $letters = $_GET['invno'];
        //$letters = preg_replace("/[^a-z0-9 ]/si","",$letters);
        //$letters="/".$letters;
        //$a="SELECT * from s_salma where REF_NO like  '$letters%'";
        //echo $a;
		if (($_GET["stname"]!="arn")&&($_GET["stname"]!="arnOrg")){
                        $sql = "SELECT * from s_matereq where REFNO like  '$letters%' order by S_date desc  limit 50";
		} else {
                        if($_GET["stname"]!="arnOrg"){
                            $sql = "SELECT * from s_matereq where cancel='0' and REFNO like  '$letters%' order by S_date desc  limit 50";
                        }else{
                            $sql = "SELECT * from s_matereq where cancel='0' and flgLocal = '0' and REFNO like  '$letters%' order by S_date desc  limit 50";
                        }
			
		}	
    } else if ($_GET["mstatus"] == "customername") {
        $letters = $_GET['customername'];
        //$letters = preg_replace("/[^a-z0-9 ]/si","",$letters);
		if (($_GET["stname"]!="arn")&&($_GET["stname"]!="arnOrg")){
                        $sql = "SELECT * from s_matereq where SUP_NAME like  '$letters%' order by S_date desc  limit 50" ;
		} else {
                        if($_GET["stname"]!="arnOrg"){
                            $sql = "SELECT * from s_matereq where cancel='0' and SUP_NAME like  '$letters%' order by S_date desc  limit 50" ;
                        }else{
                            $sql = "SELECT * from s_matereq where cancel='0' and flgLocal = '0' and SUP_NAME like  '$letters%' order by S_date desc  limit 50" ;
                        }
			
		}	
    } else {
        $letters = $_GET['customername'];
        //$letters = preg_replace("/[^a-z0-9 ]/si","",$letters);
		if (($_GET["stname"]!="arn")&&($_GET["stname"]!="arnOrg")){
                        $sql = "SELECT * from s_matereq where SUP_NAME like  '$letters%' order by S_date desc  limit 50" ;
		} else {
                        if($_GET["stname"]!="arnOrg"){
                            $sql = "SELECT * from s_matereq where cancel='0' and SUP_NAME like  '$letters%' order by S_date desc  limit 50" ;
                        }else{
                            $sql = "SELECT * from s_matereq where cancel='0' and flgLocal = '0' and SUP_NAME like  '$letters%' order by S_date desc  limit 50" ;
                        }
			
		}	
    }

$result =$db->RunQuery($sql);
			while($row = $result->fetch()){
        $REF_NO = $row['REF_NO'];
        $stname = "inv_mast";
        $ResponseXML .= "<tr>
                           	  <td onclick=\"purord('" . $row['REFNO'] . "', '" . $_GET['stname'] . "');\">" . $row['REFNO'] . "</a></td>
                              <td onclick=\"purord('" . $row['REFNO'] . "', '" . $_GET['stname'] . "');\">" . $row['SUP_NAME'] . "</a></td>
                              <td onclick=\"purord('" . $row['REFNO'] . "', '" . $_GET['stname'] . "');\">" . $row['S_date'] . "</a></td>
                                                        	
                            </tr>";
    }

    $ResponseXML .= "   </table>";


    echo $ResponseXML;
}

if($_GET["Command"]=="cancel_inv")
{
	$sql1="update s_matereq set cancel='1' where REFNO='".$_GET["invno"]."'";
	$result1 =$db->RunQuery($sql1);
	
	$sql1="update s_matereq_trn set CANCEL='1' where REFNO='".$_GET["invno"]."'";
	$result1 =$db->RunQuery($sql1);
	echo "Canceled!";
}	
	
if ($_GET["Command"]=="check_print")
{
	
	echo $_SESSION["print"];
}


	
?>

