<?php

/*
	include_once("config.inc.php");
	include_once("DBConnector1.php");
	$letters = $_GET['letters'];
	
	$sql="SELECT * FROM mast_family where name like '".$letters."%'";
		$result =$db->RunQuery($sql);
			
			
			while($row = mysqli_fetch_array($result))
			{
			
			echo $row["name"];
			
			}
			
		*/	
		
		
session_start();


	include_once("connectioni.php");

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\">ARN No</font></td>
                              <td width=\"424\"  background=\"images/headingbg.gif\"><font color=\"#FFFFFF\">LC 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\">ARN 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 (($_SESSION['COMCODE']=="A") or ($_SESSION['COMCODE']=="B") or ($_SESSION['COMCODE']=="R")){
									$sql = mysqli_query($GLOBALS['dbinv'],"SELECT * from s_purrmas where mid(REFNO, 1, 1)='".$_SESSION['COMCODE']."' and REFNO like  '$letters%' and CANCEL!='1' order by id desc") or die(mysqli_error($GLOBALS['dbinv']));
								} else {
									$sql = mysqli_query($GLOBALS['dbinv'],"SELECT * from s_purrmas where  REFNO like  '$letters%' and CANCEL!='1' order by id desc") or die(mysqli_error($GLOBALS['dbinv']));
								}	
							} else if ($_GET["mstatus"]=="lcno"){
								$letters = $_GET['lcno'];
								//$letters = preg_replace("/[^a-z0-9 ]/si","",$letters);
								if (($_SESSION['COMCODE']=="A") or ($_SESSION['COMCODE']=="B") or ($_SESSION['COMCODE']=="R")){
									$sql = mysqli_query($GLOBALS['dbinv'],"SELECT * from s_purrmas where mid(REFNO, 1, 1)='".$_SESSION['COMCODE']."' and LCNO like  '$letters%' and CANCEL!='1' order by id desc") or die(mysqli_error($GLOBALS['dbinv'])) or die(mysqli_error($GLOBALS['dbinv']));
								} else {
									$sql = mysqli_query($GLOBALS['dbinv'],"SELECT * from s_purrmas where LCNO like  '$letters%' and CANCEL!='1' order by id desc") or die(mysqli_error($GLOBALS['dbinv'])) or die(mysqli_error($GLOBALS['dbinv']));
								}
							} else if ($_GET["mstatus"]=="customername"){
								$letters = $_GET['customername'];
								//$letters = preg_replace("/[^a-z0-9 ]/si","",$letters);
								if (($_SESSION['COMCODE']=="A") or ($_SESSION['COMCODE']=="B") or ($_SESSION['COMCODE']=="R")){
									$sql = mysqli_query($GLOBALS['dbinv'],"SELECT * from s_purrmas where mid(REFNO, 1, 1)='".$_SESSION['COMCODE']."' and SUP_NAME like  '$letters%' and CANCEL!='1' order by id desc") or die(mysqli_error($GLOBALS['dbinv'])) or die(mysqli_error($GLOBALS['dbinv']));
								} else {
									$sql = mysqli_query($GLOBALS['dbinv'],"SELECT * from s_purrmas where SUP_NAME like  '$letters%' and CANCEL!='1' order by id desc") or die(mysqli_error($GLOBALS['dbinv'])) or die(mysqli_error($GLOBALS['dbinv']));
								}	
							} else {
								$letters = $_GET['customername'];
								//$letters = preg_replace("/[^a-z0-9 ]/si","",$letters);
								if (($_SESSION['COMCODE']=="A") or ($_SESSION['COMCODE']=="B") or ($_SESSION['COMCODE']=="R")){
									$sql = mysqli_query($GLOBALS['dbinv'],"SELECT * from s_purrmas where mid(REFNO, 1, 1)='".$_SESSION['COMCODE']."' and SUP_NAME like  '$letters%' and CANCEL!='1' order by id desc") or die(mysqli_error($GLOBALS['dbinv'])) or die(mysqli_error($GLOBALS['dbinv']));
								} else {
									$sql = mysqli_query($GLOBALS['dbinv'],"SELECT * from s_purrmas where SUP_NAME like  '$letters%' and CANCEL!='1' order by id desc") or die(mysqli_error($GLOBALS['dbinv'])) or die(mysqli_error($GLOBALS['dbinv']));
								}	
							}
							
													
				// 		ECHO $sql;
							while($row = mysqli_fetch_array($sql)){
								$REF_NO = $row['REF_NO'];
								$stname = "arr";
							$ResponseXML .= "<tr>
                           	  <td onclick=\"arr('".$row['REFNO']."', '".$_GET['stname']."');\">".$row['REFNO']."</a></td>
							  <td onclick=\"arr('".$row['REFNO']."', '".$_GET['stname']."');\">".$row['LCNO']."</a></td>
                              <td onclick=\"arr('".$row['REFNO']."', '".$_GET['stname']."');\">".$row['SUP_NAME']."</a></td>
                              <td onclick=\"arr('".$row['REFNO']."', '".$_GET['stname']."');\">".$row['SDATE']."</a></td>
                                                        	
                            </tr>";
							}
							  
                    $ResponseXML .= "   </table>";
		
										
					echo $ResponseXML;
}

if ($_GET["Command"]=="pass_arr"){
	header('Content-Type: text/xml'); 
	echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
			
			$ResponseXML = "";
			$ResponseXML .= "<salesdetails>";
					
			
			$inv= $_GET['invno']; 
							
						//	echo "Select * from s_purrmas where REFNO='".$inv."'";
				$sql = mysqli_query($GLOBALS['dbinv'],"Select * from s_purrmas where REFNO='".$inv."'") or die(mysqli_error($GLOBALS['dbinv']));
				
				if($row = mysqli_fetch_array($sql)){
				
					$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 .= "<AMOUNT><![CDATA[".number_format($row['AMOUNT'], 2, ".", ",")."]]></AMOUNT>";
					$ResponseXML .= "<ORDNO><![CDATA[".$row['ORDNO']."]]></ORDNO>";
					$ResponseXML .= "<DEPARTMENT><![CDATA[".$row['DEPARTMENT']."]]></DEPARTMENT>";
					$ResponseXML .= "<SUP_CODE><![CDATA[".$row['SUP_CODE']."]]></SUP_CODE>";
					$ResponseXML .= "<cancel><![CDATA[" . $row['CANCEL'] . "]]></cancel>";
					
					
				
				$ResponseXML .= "<sales_table><![CDATA[ <table><tr>
                              <td width=\"100\"  background=\"images/headingbg.gif\" ><font color=\"#FFFFFF\">Stock No</font></td>
                              <td width=\"300\"  background=\"images/headingbg.gif\"><font color=\"#FFFFFF\">Description</font></td>
                              <td width=\"100\"  background=\"images/headingbg.gif\"><font color=\"#FFFFFF\">Unit</font></td>
                              <td width=\"100\"  background=\"images/headingbg.gif\"><font color=\"#FFFFFF\">Qty</font></td>
                              <td width=\"100\"  background=\"images/headingbg.gif\"><font color=\"#FFFFFF\">Pre.Ret.Qty</font></td>
							  <td width=\"100\"  background=\"images/headingbg.gif\"><font color=\"#FFFFFF\">Price</font></td>
							  <td width=\"100\"  background=\"images/headingbg.gif\"><font color=\"#FFFFFF\">Ret.Qty</font></td>
                              <td width=\"100\"  background=\"images/headingbg.gif\"><font color=\"#FFFFFF\">Value</font></td>
                            </tr>";
					
					$tot=0;		
					$sqlcustomer = mysqli_query($GLOBALS['dbinv'],"Select * from s_purrtrn where REFNO='".$inv."'") or die(mysqli_error($GLOBALS['dbinv']));
					while($rowcustomer = mysqli_fetch_array($sqlcustomer)){
						$ResponseXML .= "<tr><td >".$rowcustomer['STK_NO']."</td>
  							 			<td >".$rowcustomer['DESCRIPT']."</td>
										<td ></td>
										<td ></td>
										<td ></td>
										<td >".$rowcustomer['acc_COST']."</td>
										<td >".number_format($rowcustomer['REC_QTY'], 0, ".", ",")."</td>
										<td >".number_format($rowcustomer['acc_COST']*$rowcustomer['REC_QTY'], 2, ".", ",")."</td></tr>";
										$tot=$tot+($rowcustomer['acc_COST']*$rowcustomer['REC_QTY']);
										
					}
					$ResponseXML .= "   </table>]]></sales_table>";
				}				
		  	
				$ResponseXML .= "<tot><![CDATA[".number_format($tot, 2, ".", ",")."]]></tot>";
			
		
			
		$ResponseXML .= "</salesdetails>";	
		echo $ResponseXML; 
}	
?>
