<?php
header("Content-type: text/html; charset=windows-1256");
session_start();

if (!function_exists('ConvertCurrency')) {
	function ConvertCurrency($FromCurr, $ToCurr, $FromDate, $Amount, $connection)
	{
		$query = "SELECT ifNull(convertcurfunc('".$FromCurr."', '".$ToCurr."', '".$FromDate."', 0, 0, '', '', ifNull($Amount,0)),0) From Dual ";
		$result = selectData($connection, $query);
		//echo $query;
		Return	$result[0];

	}
}

if (!empty($_GET["action"])) {
	$dftorderby   = $_GET["dftorderby"];
	$action 	  = $_GET["action"];
	$orderby 	  = $_GET["orderby"];
	$morelines	  = $_GET["morelines"];
	$startrange   = $_GET["startrange"];
	$perpage 	  = $_GET["perpage"];
	$totalrows    = $_GET["totalrows"];
	$direction    = $_GET["direction"];
	$filepath     = $_GET["filepath"];
	$transid      = $_GET["transid"];
	$transdtid    = $_GET["transdtid"];
	$form      	  = $_GET["form"];

		include("config.inc.php");
	include("Functions_Sales.php");
	
	$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
	mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");
	
	if ($form == 'DI') {
		ShowDeliveryDtGrid($connection,$form,$transid,$_SESSION['gdl_parameters'],$_SESSION['gdl_arrayaccess'],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	}	
	else if ($form == 'DI-TOT') {
		ShowDeliveryTotals($connection,$transid,$_SESSION['gdl_parameters'],$_SESSION['gdl_arrayaccess']);
	}
	else if ($form == 'DR') {
		ShowReturnDeliveryDtGrid($connection,$form,$transid,$_SESSION['grdl_parameters'],$_SESSION['grdl_arrayaccess'],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	}	
	else if ($form == 'DR-TOT') {
		ShowReturnDeliveryTotals($connection,$transid,$_SESSION['grdl_parameters'],$_SESSION['grdl_arrayaccess']);
	}
}


function ShowDeliveryDtGrid($connection,$form,$PSaId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	//ECHO "XXX";
	
	$query_setup="select ifnull(delivery_maxrecords,1000000) from setup";
	$result_setup=mysql_query($query_setup,$connection);
	$row_numofrows=mysql_fetch_row($result_setup);
	$numofrows = $row_numofrows[0];
	
	
	
	
	
	$FI_DeliverySalesPrice = $arrayparams['FI_DeliverySalesPrice'];
	$FI_SalesCheckSalesman = $arrayparams['FI_SalesCheckSalesman'];
	$Login_NegativeQty = $arrayparams['Login_NegativeQty'];
	$Login_ModifySellingPrice = $arrayparams['Login_ModifySellingPrice'];
	$Login_ModifyDiscount = $arrayparams['Login_ModifyDiscount'];
	$Login_MinAllowCostPer = $arrayparams['Login_MinAllowCostPer'];
	$Login_MaxAllowCostPer = $arrayparams['Login_MaxAllowCostPer'];
	$Module_NegativeQuantity = $arrayparams['Module_NegativeQuantity'];
	$Module_Unit1Method = $arrayparams['Module_Unit1Method'];
	$Module_Pack = $arrayparams['Module_Pack'];
	$Module_DoubleUnit = $arrayparams['Module_DoubleUnit'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	$ShowCostCentLayout = $arrayparams['ShowCostCentLayout'];
	$Module_SerialNo = $arrayparams['Module_SerialNo'];
	$Module_Delivery = $arrayparams['Module_Delivery'];
	$Module_ExpireDate = $arrayparams['Module_ExpireDate'];
	$Module_DeliveryClosingQty = $arrayparams['Module_DeliveryClosingQty'];
	$Module_DeliveryWithSorder = $arrayparams['Module_DeliveryWithSorder'];
	$Sale_MaxExceedQty = $arrayparams['Sale_MaxExceedQty'];
	
	$SetupDftWareHouse = $arrayparams['SetupDftWareHouse'];
	$defaultwarehouse  = $arrayparams['defaultwarehouse'];
	$txtCurrCode  = $arrayparams['txtCurrCode'];
	$Module_Heatno = $arrayparams['Module_Heatno'];
	
	
	$SalesTTC = $arrayparams['SalesTTC'];
	$DecimalBase = $arrayparams['DecimalBase'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$txtDate         = $arrayparams['txtDate'];
	$txtClientCode   = $arrayparams['txtClientCode'];
	$txtClientDetail = $arrayparams['txtClientDetail'];
	$delivery_export  = $arrayparams['delivery_export'];
	 
	 if ($Login_ModifyDiscount == 0)	$ModeDiscount = ' readonly ';
	 
	if($delivery_export==''){
	$query = " Select delivery_export
					  From Temp_Deliveries
					 Where Temp_Deliveries.Delivery_ID = ".$PSaId;
					 
					 
			//echo $query;
					
			$result=mysql_query($query,$connection);
		 	$rows=mysql_fetch_array($result);
			$delivery_export  = $rows[0];
	}
	
	/*
$sub_query = "SELECT currency_code FROM Temp_Deliveries WHERE Delivery_ID = '".$PSaId."'";
		//echo "<br><br>".$sub_query;
		$sub_result = selectData($connection, $sub_query);
		if($txtCurrCode=='')
			$txtCurrCode   = $sub_result[0];
		//echo "sad".$txtCurrCode;
		*/
	
$sub_query = "SELECT currency_code FROM Temp_Deliveries WHERE Delivery_ID = '".$PSaId."'";
		//echo "<br><br>".$sub_query;
		$sub_result = selectData($connection, $sub_query);
		//echo "Currency".$txtCurrCode;
		if($txtCurrCode=='')
			$txtCurrCode   = $sub_result[0];
	//echo "Currency".$txtCurrCode;
	if ($txtClientCode == '' && $PSaId != '') {
		$sub_query = "SELECT Client_Code FROM Temp_Deliveries WHERE Delivery_ID = '".$PSaId."'";
		//echo "<br><br>".$sub_query;
		$sub_result = selectData($connection, $sub_query);
		$txtClientCode   = $sub_result[0];
	}

	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid_Delivery.php';
	
	$i = 0;
	
	$PriceLabel = 'Price';	
	if ($SalesTTC == 1)  $PriceLabel = 'Price TTC';	
	
	$ModeModifySellPrice = '';
	if ($Login_ModifySellingPrice != 1) $ModeModifySellPrice = 'readonly';
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 50;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
	$columnsarray = array("itemcode", "itemdescription", "warehousecode");
	
	/*if($Module_Heatno==1)
		array_push($columnsarray, "deliverydt_heatno");
	*/
	
	if($delivery_export==1){ 
	    array_push($columnsarray, 'deliverydt_quantitybox');
		array_push($columnsarray, 'deliverydt_itemunit');
		array_push($columnsarray, 'deliverydt_itempackage');
	}
	if ($Module_DeliveryClosingQty == 1) array_push($columnsarray, 'sadtcloseqty');
	array_push($columnsarray, 'sadtquantity');
	if ($Module_DoubleUnit == 1) array_push($columnsarray, 'unit2qty');
	
		
		
		 
	if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1) {
		array_push($columnsarray, 'price');
		array_push($columnsarray, 'deliverydt_itemdiscount');
		array_push($columnsarray, 'sadttotal');
		//xxxxxxxxxxxxxxxxx
	}	
	if ($Module_ProjectDt == 1) array_push($columnsarray, 'projectcodedt');
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) array_push($columnsarray, 'costcentcodedt');
	if ($Module_ExpireDate == 1) {
		array_push($columnsarray, 'batchno');
		array_push($columnsarray, 'expiredate');
	}	
	//array_push($columnsarray, 'remarkdt');
	
	
	$columnsarrayall = array("itemcode", "itemdescription", "warehousecode", "sorderref", "sorderqty", "sadtcloseqty", "sadtquantity", "unit2qty","deliverydt_quantitybox","deliverydt_itemunit","deliverydt_itempackage", "price", "deliverydt_itemdiscount", "lastprice", "sadttotal", "projectcodedt", "costcentcodedt", "expiredate", "batchno", "remarkdt", "sadtid", "existingqty", "bookingqty", "availableqty", "returnqty", "soldqty", "itemunit1", "itemunit2", "itemvat", "itemnonstock", "itemexpire", "itemunitcoef", "costprice", "sellprice");
	$orderbyarray = array("DeliveryDt.Item_CodePacked", "Items.Item_Code", "Items.Item_Description", "DeliveryDt.Warehouse_Code", "Items.Item_UnitSalesDesc", "Items.Item_UnitPurchaseDesc");
	
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

		
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
	
	$TableWidth = 1145;
	if ($Module_SerialNo == 1) $TableWidth += 25;
	if ($Module_Pack == 1)     $TableWidth += 125;
	if ($Module_DeliveryClosingQty == 1)     $TableWidth += 75;
	if ($Module_DoubleUnit == 1)   $TableWidth += 145;
	if ($Module_SerialNo == 1)     $TableWidth += 55;
	if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1)     $TableWidth += 345;
	if ($Module_ProjectDt == 1)    $TableWidth += 160;
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) $TableWidth += 160;
	if ($Module_ExpireDate == 1)   $TableWidth += 190;
	
	$TableWidthStyle = 'width:'.$TableWidth.'px;';
	
	echo"<div id='container' style='position: relative;z-index:1;' >";
	echo"	<div id='Loader' class=''></div>";
	echo" 	<div id='grid_headerDiv' class='grid_headerDiv'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
	
		echo"<tr>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		
		if ($Module_SerialNo == 1) {
			echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		}	
		  
		if ($Module_Pack == 1)
			echo"  <th width='125px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','DeliveryDt.Item_CodePacked','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code Packed".$orderimg1."</span></th>";
			
		echo"  <th width='165px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code".$orderimg2."</span></th>";
		echo"  <th width='205px;' align='left' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Description".$orderimg3."</span></th>";
		echo"  <th width='75px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','DeliveryDt.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>W.H".$orderimg4."</span></th>";
		
		if($Module_Heatno==1)
		echo"  <th width='210px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPorId."', 'datagrid', 1, '".$dftorderby."', 'COR','DeliveryDt.DeliveryDt_Heatno','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>HeatNo</span></th>";
	
	
		echo"  <th width='75px;' align='center'><span>Order Ref</span></th> ";	
		echo"  <th width='75px;' align='center'><span>Rem. Order Qty</span></th>";
		
		if ($Module_DoubleUnit == 1) 		
				echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit1".$orderimg5."</span></th>";	
		
		if($delivery_export==1){
				echo"  <th width='60px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPorId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Pack/Box".$orderimg5."</span></th>";
				echo"  <th width='60px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPorId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Unit".$orderimg5."</span></th>";
				echo"  <th width='60px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPorId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Qty Box".$orderimg5."</span></th>";
		
		}
		
		if ($Module_DeliveryClosingQty == 1) {
			echo"  <th width='75px;' align='center'><span>Closing Qty</span></th> ";	
		}	
		echo"  <th width='75px;' align='center'><span>Quantity</span></th> ";	
		  
		if ($Module_DoubleUnit == 1) {
			echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitPurchaseDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit2".$orderimg6."</span></th>";
			echo"  <th width='80px;' align='center'><span>Unit2 Qty</span></th>";
		}
		
		if ($Module_SerialNo == 1) {
			echo"  <th width='55px;' align='center'><span>S/N Bal</span></th>";
		}
		  
		if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1) {
			echo"  <th width='85px;' align='center'><span>".$PriceLabel."</span></th>";
			echo"  <th width='85px;' align='center'><span>Last Sell. Price-Disc</span></th> ";
			echo"  <th width='70px;' align='center'><span>Disc(%)</span></th>";
			echo"  <th width='105px;' align='center'><span>Total</span></th>";
		}	
		
		if ($Module_ProjectDt == 1) {
			echo"  <th width='160px;' align='center'><span>Project</span></th>";	
		}
		if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
			echo"  <th width='160px;' align='center'><span>Costcenter</span></th>";	
		}
		
		if ($Module_ExpireDate == 1) {
			echo"  <th width='105px;' align='center'><span>Batch No</span></th>";	
			echo"  <th width='85px;' align='center'><span>Expire Date</span></th>";	
		}
	
		echo"  <th width='255px;' align='center'><span>Remark</span></th>";	
		echo"  <th width='50px;' align='center'><span>Promo</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='grid_contentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('grid_headerDiv','grid_contentDiv')\">";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
		echo"<input type='hidden' name='SelectedRec' id='SelectedRec' value='$SelectedRec'>
			 <input type='hidden' name='SelectedRecColor' id='SelectedRecColor' value='$SelectedRecColor'>
			 <input type='hidden' name='ModRec_G1' id='ModRec_G1' value='$ModRec_G1'>";  
		
			 
		
		
		if ($PSaId != '' ) {
		      
			$TotalQty = 0;
			$i_nonstock  = 0;
			$i_zeroprice = 0;
			$i_minprice  = 0;
			$i_unitcoef  = 1;
			$i_expire    = 0;
			
					   
			$query = " Select DeliveryDt.DeliveryDt_id, DeliveryDt.Item_CodePacked, DeliveryDt.Item_code, Item_Description, DeliveryDt.Warehouse_Code, ".
					 " DeliveryDt_Remark, DeliveryDt_Quantity, Round(DeliveryDt_Price, ".$DecimalBase."), DeliveryDt_PriceBase1, DeliveryDt_PriceBase2, ".
					 " Round(DeliveryDt_DiscountPer, ".$DecimalBase."),  ".
					 " Round(((DeliveryDt_Quantity*DeliveryDt_Price)-((DeliveryDt_Quantity*DeliveryDt_Price*ifNull(DeliveryDt_DiscountPer,0))/100)), ".$DecimalBase.") As Total, ".
					 " Round(DeliveryDt_PriceTTC, ".$DecimalBase."), ".
					 " Round(((DeliveryDt_Quantity*DeliveryDt_PriceTTC)-((DeliveryDt_Quantity*DeliveryDt_PriceTTC*ifNull(DeliveryDt_DiscountPer,0))/100)), ".$DecimalBase.") As TotalTTC, ".
					 " DeliveryDt.Item_Unit2Qty, Item_UnitPurchaseDesc, Item_UnitSalesDesc, DeliveryDt.Project_code, DeliveryDt.CostCent_Code,  ".
					 " SorderDt.SorderDt_Quantity, ifNull(item_withserialno,0) As ItemWithSerialNo, Sorder.Sorder_reference, Count(SerialNo_Code) As CountSerialNo,  ".
					 " Date_Format(DeliveryDt_ExpireDate,'%d/%m/%Y') As DeliveryDt_ExpireDate, DeliveryDt_BatchNo, DeliveryDt.DeliveryDt_ClosingQuantity, ifNull(DeliveryDt_Promotion,0), ".
					 //" Round(ifNull(MeanCostCur(DeliveryDt.Item_code, DeliveryDt.Delivery_Date, 1, '".$Base1."', DeliveryDt.Currency_Code),0), ".$DecimalBase.") as ItemCost ".
					 " 0 as ItemCost ".
					 " ,DeliveryDt_heatNo , deliverydt_quantitybox, deliverydt_itemunit, deliverydt_itempackage, deliverydt_clientdiscount, deliverydt_itemdiscount ".
					 " From Temp_DeliveryDt As DeliveryDt ".
					 " Left Join Items on DeliveryDt.Item_code = Items.Item_code ".
					 " Left Join Temp_Purchasesserialno on DeliveryDt.DeliveryDt_ID = Temp_Purchasesserialno.PurchaseDt_ID ".
					 " And Temp_Purchasesserialno.Journal_Type = 'DI' ".
					 " And Temp_Purchasesserialno.DeliveryDt_ID Is Not Null ".
					 " Left Join SorderDt on SorderDt.SorderDt_ID = DeliveryDt.RelatedDt_Id  ".
					 " Left Join Sorder on Sorder.Sorder_ID = SorderDt.Sorder_ID  ".
					 " Where DeliveryDt.Delivery_ID = ".$PSaId.
					 " Group By  DeliveryDt.DeliveryDt_ID ".
					 " Order By $orderby $direction ";
					 
			//echo "sad".$query;
			//EXIT;
			
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);

			$query = $query." limit ".$startrange.",".$perpage;
			$resultdt=mysql_query($query,$connection);
			
			
			while($rowsdt=mysql_fetch_array($resultdt)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';

								
				$sub_query  = " Select QtyExistsWSorder('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', 0, 'DI', '', '') QtyExists From Dual ";
				
				$sub_query  = " Select 0 QtyExists From Dual ";
				//echo $sub_query;
				$sub_result = selectData($connection, $sub_query);
				$vItemQtyExists = 0;
				$vItemQtyExists = $sub_result[0];		
						
				$TextColor = 'style7';
				if ($vItemQtyExists < 0) {
					$Color="style='background-color:#FF0000;'";
					$TextColor = 'style8';
				}	
				$deliverydt_heatno[$i] = $rowsdt[28];
				
				$deliverydt_quantitybox  = $rowsdt[29];
				$deliverydt_itemunit  = $rowsdt[30];
				$deliverydt_itempackage  = $rowsdt[31];
				
				$deliverydt_clientdiscount[$i]	= $rowsdt[32];
				$deliverydt_itemdiscount[$i] = $rowsdt[33];
				
				$sub_query = " SELECT ifNull(Item_Pack, 0), ifNull(item_zeroPrice, 0), Item_UnitSalesDesc, Item_UnitPurchaseDesc, ifNull(Item_Unit,1), ifNull(Item_NonStock, 0), ".
							 " ifNull(Item_MinimumPrice, 0), ifNull(Item_Expire,0), ifNull(item_withserialno,0), Currency_Code, ifNull(Item_CostMethod,0), ".
							 " Item_SellingPrice1, Item_SellingPrice2, Item_SellingPrice3, Item_SellingPrice4, Item_SellingPrice5, Item_SellingPrice6 ".
							 " FROM Items ".
							 " WHERE Items.Item_Code = '".$rowsdt[2]."'";
					
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$i_pack       = $sub_result[0];
				$i_zeroprice  = $sub_result[1];
				$i_unit1      = $sub_result[2];
				$i_unit2 	  = $sub_result[3]; 
				$i_unitcoef	  = $sub_result[4];
				$i_nonstock   = $sub_result[5];
				$i_minprice   = $sub_result[6];
				$i_expire	  = $sub_result[7];
				$i_withserial = $sub_result[8];
				$i_curr  	  = $sub_result[9];
				$i_costmethod = $sub_result[10];
				
				$sub_query1 = "SELECT ifNull(Client_SellingPrice,1) FROM Clients WHERE Ledger_Number = '".$txtClientCode."'";
				//echo "<br><br>".$sub_query;
				$sub_result1 = selectData($connection, $sub_query1);
				$c_sp   = $sub_result1[0];
				
				$txtItemWithSerial[$i] = $i_withserial;
				$txtItemNonStock[$i]   = $i_nonstock;
				$txtItemUnitCoef[$i]   = $i_unitcoef;
				$txtItemExpire[$i]     = $i_expire;
				
				
				
				/*switch ($c_sp){
					case "I":
						$i_sp = $sub_result[11]; break;
					case "II":
						$i_sp = $sub_result[12]; break;
					case "III":
						$i_sp = $sub_result[13]; break;
					case "IV":
						$i_sp = $sub_result[14]; break;
					case "V":
						$i_sp = $sub_result[15]; break;
					case "VI":
						$i_sp = $sub_result[16]; break;
				}
				
				//Convert SellPrice To Invoice Currency
				if ($txtCurrCode != $i_curr){
					$sub_query1 = "SELECT ifNull(convertcurfunc('".$i_curr."', '".$txtCurrCode."', '".insertMySQLDate($txtDate)."', 0, 0, '', '', ifNull(".$i_sp.",0)),0) From Dual ";
					$sub_result1 = selectData($connection, $sub_query1);
					$i_sp	= $sub_result1[0];
					if (empty($i_sp)) $i_sp = 0;
				}
				
				$txtItemSellPrice[$i] = $i_sp;*/
				
				
				$Qry_QtyBookTemp = "SELECT ifnull(QtyBooking_Temp('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0]),0) ";
				$Qry_QtyBookTemp = "SELECT 0 from dual";
				$result = selectData($connection, $Qry_QtyBookTemp);
				if ($result[0] < 0){$result[0] = 0;}
				$min_book_qty = $result[0];
								 
				$Qry_QtyExists = " SELECT ifnull(QtyExists('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'DI', '', ''),0) As QtyExists ";	
				$Qry_QtyExists = " SELECT 0 As QtyExists ";					
				$result = selectData($connection, $Qry_QtyExists);
				$min_exists_qty = $result[0];
				//echo $Qry_QtyExists;
				
				$Qry_Unit2QtyExists = " SELECT ifnull(Unit2QtyExists('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'DI', '', ''),0) As QtyExists ";
				$Qry_Unit2QtyExists = " SELECT 0 As QtyExists ";
				$result = selectData($connection, $Qry_Unit2QtyExists);
				$min_exists_unit2qty = $result[0];
				//echo $Qry_Unit2QtyExists;
				
				$Qry_QtyAvailable = " Select QtyExistsWSorder_Temp('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'DI', '', '', 0) QtyExists From Dual ";
				$Qry_QtyAvailable = " Select 0 QtyExists From Dual ";
				$result = selectData($connection, $Qry_QtyAvailable);
				$min_available_qty = $result[0];
				//echo $Qry_QtyAvailable;
		
				$Qry_QtyInTemp = " Select ifNull(Sum(ifNull(DeliveryDt_Quantity,0)),0) FROM Temp_Deliverydt ".
								 " WHERE Item_code = '".$rowsdt[2]."'  And  Warehouse_code = '".$rowsdt[4]."' And Delivery_ID = $PSaId And DeliveryDt_ID != ifNull('".$rowsdt[0]."',0) ";
				$result = selectData($connection, $Qry_QtyInTemp);
				$QtyTemp_DI = $result[0];
				
					
				$min_available_qty = $min_available_qty - $QtyTemp_DI;
				$min_exists_qty = $min_exists_qty - $QtyTemp_DI;
				$min_book_qty = $min_book_qty;
		
				$txtBookingQty[$i]    = $min_book_qty;
				$txtAvailableQty[$i]  = $min_available_qty;
				$txtExistingQty[$i]   = $min_exists_qty;
				$txtExistUnit2Qty[$i] = $min_exists_unit2qty;
				
				$sub_query = " Select ifNull(Sum(ifNull(DeliveryDt1.DeliveryDt_Quantity,0)),0) ".
							 " From Temp_DeliveryDt As DeliveryDt, DeliveryDt As DeliveryDt1 ".
							 " Where DeliveryDt.DeliveryDt_id = '".$rowsdt[0]."' ".
							 " And DeliveryDt.Journal_Type = 'DI' ".
							 " And (ifNull(DeliveryDt.RelatedDt_Id,0) != 0 and DeliveryDt.RelatedDt_Id != '') ".
							 " And DeliveryDt1.Journal_Type = 'DI' ".
							 " And DeliveryDt1.Delivery_ID != '".$PSaId."' ".
							 " And DeliveryDt1.RelatedDt_Id = DeliveryDt.RelatedDt_Id ";

				$sub_result = selectData($connection, $sub_query);
				$DeliveredQty = $sub_result[0];
				if ($DeliveredQty == '') $DeliveredQty = 0;
				$OrderedQty = $rowsdt[19];
				$RemainedQty = $OrderedQty - $DeliveredQty;
				
				
				$sub_query = " Select ifNull(Sum(ifNull(DeliveryDt1.DeliveryDt_Quantity,0)),0) ".
							 " From Temp_DeliveryDt As DeliveryDt, DeliveryDt As DeliveryDt1 ".
							 " Where DeliveryDt.DeliveryDt_id = ".$rowsdt[0].
							 " And DeliveryDt1.Journal_Type = 'DR' ".
							 " And DeliveryDt1.RelatedDt_Id = DeliveryDt.DeliveryDt_id ";

				$sub_result = selectData($connection, $sub_query);
				$txtSaDtReturnQty = $sub_result[0];
						
				
				$sub_query = " Select ifNull(Sum(ifNull(SalesDt.SaleDt_Quantity,0)),0) ".
							 " From Temp_DeliveryDt As DeliveryDt, SalesDt ".
							 " Where DeliveryDt.DeliveryDt_id = ".$rowsdt[0].
							 " And SalesDt.Journal_Type = 'SI' ".
							 " And SalesDt.RelatedDt_Id = DeliveryDt.DeliveryDt_id ";

				$sub_result = selectData($connection, $sub_query);
				$txtSaDtSoldQty = $sub_result[0];
					
				
				$sub_query = " Select Count(*) ".
							 " From SalesDt ".
							 " Where SalesDt.RelatedDt_Id = '".$rowsdt[0]."' ".
							 " And SalesDt.Journal_Type = 'SI' ";

				$sub_result = selectData($connection, $sub_query);
				$vSalesExists = $sub_result[0];
				
				//Checking For Difference Btw Qty and Count SerialNo
				if (($rowsdt[6] != $rowsdt[22] && $rowsdt[20] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0 && $rowsdt[20] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0)) {
					$RecStyle = "style='background-color:#FF0000;'";
				}
				
			 
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
				
				echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1'>";		
				echo"	<td width='25px;' $RecStyle>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteRecordGrid('".$filepath."','".$form."', '".$PSaId."','".$rowsdt[0]."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_Delivery);return false;\" title='Delete Record'  ></td>";
				
				// If ItemWithSerialNo True And ModuleSerialNo True	
				if ($rowsdt[20] == 1 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'><img src='img/button_addserial.png' onMouseOver=\"style.cursor='hand'\" onclick=\"AddSerialNo('".$rowsdt[0]."','".htmlentities($rowsdt[2])."','".$RemainedQty."','".$rowsdt[4]."')\" title='Add Serial Nos'  ></td>";
				}
				else if ($rowsdt[20] == 0 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
				}
				
				if ($Module_Pack == 1)
					echo "	<td width='125px;' align='left'><span class='".$TextColor."'>".$rowsdt[1]."</span></td>";
					
				echo "	<td width='185px;' align='left'>
							<input type='hidden' name='txtSaDtId[$i]' id='sadtid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
							<input type='hidden' name='txtItemCostPrice[$i]' id='costprice[$i]' value='$rowsdt[27]' >
							<input type='hidden' name='txtItemSellPrice[$i]' id='sellprice[$i]' value='$txtItemSellPrice[$i]' >
							<input type='hidden' name='txtItemNonStock[$i]' id='itemnonstock[$i]' value='$txtItemNonStock[$i]' >
							<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
							<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
							<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
							<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
							<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
							<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
							<input type='hidden' name='txtExistUnit2Qty[$i]' id='exitunit2qty[$i]' value='$txtExistUnit2Qty[$i]' >
							<input type='hidden' name='txtSaDtReturnQty[$i]' id='returnqty[$i]' value='$txtSaDtReturnQty[$i]' >
							<input type='hidden' name='txtSaDtSoldQty[$i]' id='soldqty[$i]' value='$txtSaDtSoldQty[$i]' >
							<input type='hidden' name='deliverydt_clientdiscount[$i]' id='deliverydt_clientdiscount[$i]' value='$deliverydt_clientdiscount[$i]' style='width:60'>
							
							
						 ";
							
							if($Module_Delivery==0)
							echo"<input type='button' name='$recno' value='H' style='width:15px;text-align:left;font-size:10px' onclick=openhistory('$rowsdt[2]')>";
							
							echo"<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"ShowSlidingDiv('#slidingDiv$i');\" style='vertical-align:bottom;'/></span></label>";
							
							if($Module_DeliveryWithSorder==1)
							echo"<input type='text' readonly name='txtItemCode[$i]' id='itemcode[$i]' value='".$rowsdt[2]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:110px; height:20px;'".$KeyDown.">";
							else
							echo"<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='".$rowsdt[2]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:110px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','".$txtClientCode."');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\"    onblur='checkduplicate($i,this.value)' ".$KeyDown.">";
							
							if($Module_DeliveryWithSorder==0)
							echo"<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>";
							echo"<div id='slidingDiv$i' class='slidingDiv slidingDivHide'>
								<table align='center' border='0' cellpadding='0' cellspacing='0'>
									<tr height='25px;'><td width='100px;'><span class='style1'>Existing Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtExistingQty[$i],2)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Booking Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtBookingQty[$i],2)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Available Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtAvailableQty[$i],2)."</span></td></tr>";
									
							if ($Module_DoubleUnit == 1 && $rowsdt[15] != $rowsdt[16] && $rowsdt[15] != '') {
								echo"<tr height='25px;'><td width='100px;'><span class='style1'>Existing Unit2Qty</span></td><td width='100px;' align='right'><span class='style1'>".$rowsdt[15] ."  ".number_format($txtExistUnit2Qty[$i],2)."</span></td></tr>";
							}		
									
				echo"			</table>
								<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv$i');\"><span style='font-weight:normal;'>Close</span></a></div>
							</div>
						</td>";
										
				if($Module_DeliveryWithSorder==0)
					echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />";
				else
					echo"  	<td width='205px;' align='center'><input type='text' readonly name='txtItemDesc[$i]' id='itemdescription[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:195px; height:20px;' ".$KeyDown." />";

				echo"  				<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
						</td>";
				echo"  	<td width='70px;' align='center'>
							<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='".$rowsdt[4]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:35px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCodeWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
				if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='deliverydt_heatno[$i]' id='deliverydt_heatno[$i]' value='".$deliverydt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
						</td>";		
				else 	echo"<input type='hidden' name='deliverydt_heatno[$i]' id='deliverydt_heatno[$i]' value='".$deliverydt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />";
				
				echo"	<td width='65px;' align='center'><input type='text' name='txtSaDtSorderRef[$i]' id='sorderref[$i]' value='".$rowsdt[21]."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' readonly></td>";
				echo"   <td width='65px;' align='center'><input type='text' name='txtSaDtSorderQty[$i]' id='sorderqty[$i]' value='".$RemainedQty."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' readonly  ></td>";
				if ($Module_DoubleUnit == 1) 
					echo "	<td width='60px;' align='center'><span class='".$TextColor."'>".$rowsdt[16]."</span></td>";
				
				if($delivery_export==1){
							echo"  <td width='60px;' align='center'><input type='text' name='deliverydt_quantitybox' id='deliverydt_quantitybox[$i]' value='$deliverydt_quantitybox' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:50px; height:20px;' ".$KeyDown." ></td>";
							echo"  <td width='60px;' align='center'><input type='text' name='deliverydt_itemunit' id='deliverydt_itemunit[$i]' value='$deliverydt_itemunit' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:50px; height:20px;' ".$KeyDown." ></td>";
							echo"  <td width='60px;' align='center'><input type='text' name='deliverydt_itempackage' id='deliverydt_itempackage[$i]' value='$deliverydt_itempackage' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:50px; height:20px;' ".$KeyDown." ></td>";
				} else{
							echo"  <input type='hidden' name='deliverydt_quantitybox' id='deliverydt_quantitybox[$i]' value='$deliverydt_quantitybox' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:50px; height:20px;' ".$KeyDown." >";
							echo"  <input type='hidden' name='deliverydt_itemunit' id='deliverydt_itemunit[$i]' value='$deliverydt_itemunit' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:50px; height:20px;' ".$KeyDown." >";
							echo"  <input type='hidden' name='deliverydt_itempackage' id='deliverydt_itempackage[$i]' value='$deliverydt_itempackage' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:50px; height:20px;' ".$KeyDown." >";
				
				}
				
				
				if ($Module_DeliveryClosingQty == 1) {
					echo " <td width='75px;' align='center'><input type='text' name='txtSaDtCloseQty[$i]' id='sadtcloseqty[$i]' value='".$rowsdt[25]."' maxlength='20'  onchange=\"CalcFinalQty($i); SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' ".$KeyDown." ></td>";
				} else {
					echo"<input type='hidden' name='txtSaDtCloseQty[$i]' id='sadtcloseqty[$i]' value='' >";
				}
				echo"	<td width='75px;' align='center'><input type='text' name='txtSaDtQty[$i]' id='sadtquantity[$i]' value='".$rowsdt[6]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; border-width:0px; width:70px; height:20px;' onKeyup=\"CalcSaDtTotal($DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
					
				
				if ($Module_DoubleUnit == 1) { 
					echo"  <td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[15]."</span></td>";	
					echo"  <td width='80px;' align='center'><input type='text' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='".$rowsdt[14]."' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
				} else {
					echo"  <input type='hidden' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='' >";
				}
				
				if ($Module_SerialNo == 1) {
					echo"  <td width='55px;' align='center'><span class='".$TextColor."'>".$rowsdt[22]."</span></td>";
				}
				
				if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1) {
					if ($SalesTTC == 1) 
						echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='".$rowsdt[12]."' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
					else
						echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='".$rowsdt[7]."' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
						
					echo "	<td width='85px;' align='right'><span class='".$TextColor."'>&nbsp;</span></td>";
					
					
					
					echo"	<td width='70px;' align='center'>";
					
					echo"<input type='hidden' name='txtSaDtDiscount[$i]' id='sadtdiscount[$i]' value='".$rowsdt[10]."' maxlength='20' onblur='UpdateDiscount($i,this.value)' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown." $ModeDiscount >";
					echo"<input type='text' name='deliverydt_itemdiscount[$i]' id='deliverydt_itemdiscount[$i]' value='$deliverydt_itemdiscount[$i]'  onblur='UpdateDiscount($i,this.value,$DecimalBase)'  onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown." $ModeDiscount  >";
					
					echo"</td>";
					
							
					if ($SalesTTC == 1) 
						echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='".$rowsdt[13]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
					else
						echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='".$rowsdt[11]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				} else {
					echo"	<input type='hidden' name='txtSaDtPrice[$i]' id='price[$i]' value=''  >
							<input type='hidden' name='txtItemLastSellPrice[$i]' id='lastprice[$i]' value=''  >
							<input type='hidden' name='txtSaDtDiscount[$i]' id='sadtdiscount[$i]' value='' >
							<input type='hidden' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='' >";
				}
				
				if ($Module_ProjectDt == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='".$rowsdt[17]."' class='".$ClassE."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
				}
				if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='".$rowsdt[18]."' class='".$ClassE."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
				}
				
				if ($Module_ExpireDate == 1) {
					echo" 	<td width='105px;' align='center'><input type='text' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($rowsdt[24], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
								<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNo(1,$i); ' style='vertical-align:bottom;'/></span></label></td>
							<td width='85px;' align='center'><input type='text' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value='".$rowsdt[23]."' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
				} else {
					echo" <input type='hidden' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value=''>";
					echo" <input type='hidden' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value=''>";
				}
				
				echo"	<td width='255px;'><input type='text' name='txtSaDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($rowsdt[5], ENT_QUOTES)."'  maxlength='200'  class='".$ClassE."' style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
				echo"	<td width='50px;' align='center'><input type='checkbox' name='chkSaDtPromo[$i]' id='chkpromo[$i]' value='1'  onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown;  if ($rowsdt[26] == 1) echo " checked ";    echo" ></td>";	
				//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_NegativeQuantity,$Login_NegativeQty,$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo,$Module_DeliveryWithSorder,$Sale_MaxExceedQty,$FI_SalesCheckSalesman,$Module_Delivery); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_NegativeQuantity,$Login_NegativeQty,$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo,$Module_DeliveryWithSorder,$Sale_MaxExceedQty,$FI_SalesCheckSalesman,$Module_Delivery,$Login_MinAllowCostPer,$Login_MaxAllowCostPer); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo "</tr>";
						
				
				$i+=1;
				
				$totdtqty = $totdtqty + $rowsdt[6];
				$LastItemWarehouse = $rowsdt[4];
			}
			
			if ($totdtqty!=''){
			echo '<script type="text/javascript" language="javascript">';
						echo ' x = document.sales; ';
						echo ' x.txtRealQty.value='.$totdtqty.'; ';
						echo " if(x.txtRealQty.value=='') x.txtRealQty.value = 0; ";
						echo " if(x.txtVehicleWeight.value=='') x.txtVehicleWeight.value = 0; ";
						echo ' if(x.txtVehicleLoadWeight)';
						echo ' x.txtVehicleLoadWeight.value= parseInt(x.txtVehicleWeight.value) + parseInt(x.txtRealQty.value)';
						echo '</script>';
						}
			
		}
		
		if($i>=$numofrows)
		echo"<tr><td colspan=4>Maximum No of rows </td></tr>";
		//	$k=2;
		//else
		
		for ($k=1;$k <= 1;$k++) {
			$recno = $i+1;
			$mode = 'i';
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';	
			
			if (empty($txtWarehouseCode[$i])) {
				if (!empty($LastItemWarehouse))
					$txtWarehouseCode[$i] = $LastItemWarehouse;
				else if (!empty($SetupDftWareHouse))
					$txtWarehouseCode[$i] = $SetupDftWareHouse;
				else	
					$txtWarehouseCode[$i] = $defaultwarehouse;
			}

			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
			
				
			echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1' >";
			echo"	<td width='25px;'>$recno</td>";
			echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G1');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
			if ($Module_SerialNo == 1) {
				echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
			} 
			if ($Module_Pack == 1)
				echo"  <td width='125px;' align='center'><span>&nbsp;</span></td>";
				
			echo "	<td width='165px;' align='left'>
						<input type='hidden' name='txtSaDtId[$i]' id='sadtid[$i]' value='$txtSaDtId[$i]' >
						<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
						<input type='hidden' name='txtItemCostPrice[$i]' id='costprice[$i]' value='$txtItemCostPrice[$i]' >
						<input type='hidden' name='txtItemSellPrice[$i]' id='sellprice[$i]' value='$txtItemSellPrice[$i]' >
						<input type='hidden' name='txtItemNonStock[$i]' id='itemnonstock[$i]' value='$txtItemNonStock[$i]' >
						<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
						<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
						<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
						<input type='hidden' name='txtSaDtReturnQty[$i]' id='returnqty[$i]' value='$txtSaDtReturnQty[$i]' >
						<input type='hidden' name='txtSaDtSoldQty[$i]' id='soldqty[$i]' value='$txtSaDtSoldQty[$i]' >
						<input type='hidden' name='deliverydt_clientdiscount[$i]' id='deliverydt_clientdiscount[$i]' value='$deliverydt_clientdiscount[$i]' >
						
						<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');ShowSlidingDiv('#slidingDiv$i');\" style='vertical-align:bottom;'/></span></label>
						<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='$txtItemCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:110px; height:20px;'  onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','".$txtClientCode."'); GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\"  onblur='checkduplicate($i,this.value)' ".$KeyDown.">
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
						<div id='slidingDiv$i' class='slidingDiv slidingDivHide'>
							<table align='center' border='0' cellpadding='0' cellspacing='0'>
								<tr height='25px;'><td width='100px;'><span class='style1'>Existing Qty</span></td><td width='100px;' align='right'><input type='text' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
								<tr height='25px;'><td width='100px;'><span class='style1'>Booking Qty</span></td><td width='100px;' align='right'><input type='text' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
								<tr height='25px;'><td width='100px;'><span class='style1'>Available Qty</span></td><td width='100px;' align='right'><input type='text' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>";
						
						if ($Module_DoubleUnit == 1) {
							echo"<tr height='25px;'><td width='100px;'><span class='style1'>Existing Unit2Qty</span></td><td width='100px;' align='right'><input type='text' name='txtExistUnit2Qty[$i]' id='exitunit2qty[$i]' value='$txtExistUnit2Qty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>";
						}	
								
			echo"			</table>
							<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv$i');\"><span style='font-weight:normal;'>Close</span></a></div>
						</div>
					</td>";
									
			echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='$txtItemDesc[$i]'  class='".$ClassI."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
					</td>";
			echo"  	<td width='70px;' align='center'>
						<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='$txtWarehouseCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:35px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]'); GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\"  ".$KeyDown."  />
						<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
					</td>";
			if($Module_Heatno==1)
			echo"  	<td width='210px;' align='center'>
							<input type='text' name='deliverydt_heatno[$i]' id='deliverydt_heatno[$i]' value='".$deliverydt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:180px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
					</td>";
			else echo"<input type='hidden' name='deliverydt_heatno[$i]' id='deliverydt_heatno[$i]' value='".$deliverydt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:180px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); ".$KeyDown."   />";
			
			echo"	<td width='75px;' align='center'><input type='text' name='txtSaDtSorderRef[$i]' id='sorderref[$i]' value='' maxlength='20' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' readonly></td>";
			echo"   <td width='75px;' align='center'><input type='text' name='txtSaDtSorderQty[$i]' id='sorderqty[$i]' value='' maxlength='20' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' readonly  ></td>";
			if ($Module_DoubleUnit == 1) {
					echo"   <td width='65px;' align='center'><input type='text' name='txtItemUnit1' id='itemunit1[$i]' value='$txtItemUnit1' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
			} else {
				echo"   <input type='hidden' name='txtItemUnit1' id='itemunit1[$i]' value='$txtItemUnit1' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly>";
			}
				if($delivery_export==1){
						$deliverydt_quantitybox=0;
						$deliverydt_itemunit=0;
						$deliverydt_itempackage='';
							echo"  <td width='60px;' align='center'><input type='text' name='deliverydt_quantitybox' id='deliverydt_quantitybox[$i]' value='$deliverydt_quantitybox' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:50px; height:20px;' ".$KeyDown." ></td>";
							echo"  <td width='60px;' align='center'><input type='text' name='deliverydt_itemunit' id='deliverydt_itemunit[$i]' value='$deliverydt_itemunit' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:50px; height:20px;'".$KeyDown." ></td>";
							echo"  <td width='60px;' align='center'><input type='text' name='deliverydt_itempackage' id='deliverydt_itempackage[$i]' value='$deliverydt_itempackage' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:50px; height:20px;' ".$KeyDown."></td>";
				}
				
				
			if ($Module_DeliveryClosingQty == 1) {
				echo " <td width='75px;' align='center'><input type='text' name='txtSaDtCloseQty[$i]' id='sadtcloseqty[$i]' value='$txtSaDtCloseQty[$i]' maxlength='20'  onchange=\"CalcFinalQty($i); SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' ".$KeyDown." ></td>";
			} else {
				echo"<input type='hidden' name='txtSaDtCloseQty[$i]' id='sadtcloseqty[$i]' value='' >";
			}
			echo"	<td width='75px;' align='center'><input type='text' name='txtSaDtQty[$i]' id='sadtquantity[$i]' value='$txtSaDtQty[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onKeyup=\"CalcSaDtTotal($DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
					
				
		  
			if ($Module_DoubleUnit == 1) {
				echo"  <td width='65px;' align='center'><input type='text' name='txtItemUnit2' id='itemunit2[$i]' value='$txtItemUnit2' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
				echo"  <td width='80px;' align='center'><input type='text' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='$txtSaDtQty2[$i]' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassI."'  style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
			} else {
				echo "<input type='hidden' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='' >";
			}
			
			if ($Module_SerialNo == 1) {
				echo"  <td width='55px;' align='center'><span class='".$TextColor."'>&nbsp;</span></td>";
			}
		
			if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1) {
				if ($SalesTTC == 1) 
					echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='$txtSaDtPrice[$i]' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
				else
					echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='$txtSaDtPrice[$i]' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
						
				echo"   <td width='85px;' align='center'><input type='text' name='txtItemLastSellPrice' id='lastprice[$i]' value='$txtItemLastSellPrice' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:70px; height:20px;' readonly></td>";
			
echo"	<td width='70px;' align='center'>";
					
					echo"<input type='hidden' name='txtSaDtDiscount[$i]' id='sadtdiscount[$i]' value='".$rowsdt[10]."' maxlength='20' onblur='UpdateDiscount($i,this.value)' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown." $ModeDiscount >";
					echo"<input type='text' name='deliverydt_itemdiscount[$i]' id='deliverydt_itemdiscount[$i]' value='$deliverydt_itemdiscount[$i]'  onblur='UpdateDiscount($i,this.value,$DecimalBase)'  onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown." $ModeDiscount  >";
				
					echo"</td>";			
							
				if ($SalesTTC == 1) 
					echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='$txtSaDtTotal[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				else
					echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='$txtSaDtTotal[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
			} else {
				echo"	<input type='hidden' name='txtSaDtPrice[$i]' id='price[$i]' value=''  >
						<input type='hidden' name='txtItemLastSellPrice[$i]' id='lastprice[$i]' value=''  >
						<input type='hidden' name='txtSaDtDiscount[$i]' id='sadtdiscount[$i]' value='' >
						<input type='hidden' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='' >";
			}
			
			if ($Module_ProjectDt == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='$txtProjectCodeDt[$i]' class='".$ClassI."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
			}
			if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='$txtCostCentCodeDt[$i]' class='".$ClassI."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
			}
			
			if ($Module_ExpireDate == 1) {
				echo" 	<td width='105px;' align='center'><input type='text' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($txtSaDtBatchNo[$i], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
							<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNo(1,$i); ' style='vertical-align:bottom;'/></span></label></td>
						<td width='85px;' align='center'><input type='text' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value='$txtSaDtExpireDate[$i]' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
			} else {
				echo" <input type='hidden' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value=''>";
				echo" <input type='hidden' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value=''>";
			}
			
			echo"	<td width='255px;'><input type='text' name='txtSaDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($txtSaDtRemark[$i], ENT_QUOTES)."'  maxlength='200' class='".$ClassI."'  style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
			echo"	<td width='50px;' align='center'><input type='checkbox' name='chkSaDtPromo[$i]' id='chkpromo[$i]' value='1'  onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
			//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_NegativeQuantity,$Login_NegativeQty,$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo,$Module_DeliveryWithSorder,$Sale_MaxExceedQty,$FI_SalesCheckSalesman,$Module_Delivery); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
			echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_NegativeQuantity,$Login_NegativeQty,$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo,$Module_DeliveryWithSorder,$Sale_MaxExceedQty,$FI_SalesCheckSalesman,$Module_Delivery,$Login_MinAllowCostPer,$Login_MaxAllowCostPer); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
			echo"</tr>";  

			$i+=1;	
		}
		

	
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$PSaId,'datagrid',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	
}

function ShowDeliveryTotals($connection,$PSaId,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	$SalesTTC = $arrayparams['SalesTTC'];
	$Setup_VatPercentage = $arrayparams['Setup_VatPercentage'];
	$Module_AdditionalTaxes = $arrayparams['Module_AdditionalTaxes'];
	$Module_Delivery = $arrayparams['Module_Delivery'];
	$ExportTransportToJV = $arrayparams['ExportTransportToJV'];
	$ExportBonusToJV = $arrayparams['ExportBonusToJV'];
	$VatApplied = $arrayparams['VatApplied'];
	$AllowModifyVAT = $arrayparams['AllowModifyVAT'];
	$Login_ModifyDiscount = $arrayparams['Login_ModifyDiscount'];
	
	$txtSaDiscount_Temp = $arrayparams['txtSaDiscount_Temp'];
	
	$txtClientCode   = $arrayparams['txtClientCode'];
	$costcent_applyvat   = $arrayparams['costcent_applyvat'];
	$FI_SalesVAT   = $arrayparams['FI_SalesVAT'];
	if($FI_SalesVAT==0) $AllowModifyVAT=' disabled ';
	if ($Login_ModifyDiscount == 0)	$DiscountMode = ' readonly ';
				 
	if ($PSaId != "") {	
	
		$sub_query = " Select Round(if($SalesTTC=1,ifNull(Delivery_TaxableTTC,0),ifNull(Delivery_Taxable,0)), ".$DecimalBase."), Round(ifNull(Delivery_NoneTaxable,0), ".$DecimalBase."), ".
					 " Round(if($SalesTTC=1,ifNull(Delivery_TaxableTTC,0),ifNull(Delivery_Taxable,0)) + ifNull(Delivery_NoneTaxable,0), ".$DecimalBase.") As GrossTotal, ".
					 " Round(ifNull(Delivery_DiscountPer,0), ".$DecimalBase."), Round(ifNull(Delivery_DiscountAmount,0), ".$DecimalBase."),  ".
					 " ifNull(Delivery_VatChkbx,0) As Delivery_VatChkbx, Round(ifNull(Delivery_VAT,0), ".$DecimalBase.") As Delivery_VAT, ".
					 " Round((if($SalesTTC=1,ifNull(Delivery_TaxableTTC,0),ifNull(Delivery_Taxable,0)) + ifNull(Delivery_NoneTaxable,0) + if($SalesTTC=1,0,IfNull(Delivery_VAT,0)) + ifNull(Delivery_TransportAmount,0) + IfNull(Delivery_BonusAmount,0) - IfNull(Delivery_DiscountAmount,0) + (ifNull(Delivery_Tax1,0) + ifNull(Delivery_Tax2,0) + ifNull(Delivery_Tax3,0) + ifNull(Delivery_Tax4,0) + ifNull(Delivery_Tax5,0))), ".$DecimalBase.") As Delivery_NetTotal,  ".
					 " Round((ifNull(Delivery_Tax1,0) + ifNull(Delivery_Tax2,0) + ifNull(Delivery_Tax3,0) + ifNull(Delivery_Tax4,0) + ifNull(Delivery_Tax5,0)), ".$DecimalBase.") As TotalTaxes,  ".
					 " Round(if($SalesTTC=1,ifNull(Delivery_TransportTTC,0),ifNull(Delivery_TransportAmount,0)), ".$DecimalBase.") As TransportAmt, ".
					 " Round(if($SalesTTC=1,ifNull(Delivery_BonusTTC,0),ifNull(Delivery_BonusAmount,0)), ".$DecimalBase.") As BonusAmt ".
					 " From Temp_Deliveries As Deliveries ".
					 " Where Delivery_ID = ".$PSaId;			 
				 
		//echo $sub_query;
		$sub_result = selectData($connection, $sub_query);
		
		$txtSaTaxable      = $sub_result[0];
		$txtSaNoneTaxable  = $sub_result[1];
		$txtSaGrossTotal   = $sub_result[2];
		$txtSaDiscount     = $sub_result[3];
		$txtSaDiscountAmt  = $sub_result[4];
		$txtSaVatChk       = $sub_result[5];
		$txtSaVat          = $sub_result[6];
		$txtSaNetTotal     = $sub_result[7];
		$txtSaTotalTaxes   = $sub_result[8];
		$txtSaTransportAmt = $sub_result[9];
		$txtSaBonusAmt 	   = $sub_result[10];
	} else {
		$sub_query = " Select ifNull(Client_VAT,0) From Clients Where Ledger_Number = '".$txtClientCode."'";			 
		$sub_result = selectData($connection, $sub_query);
		$txtSaVatChk = $sub_result[0];
		if($costcent_applyvat==0) $txtSaVatChk=0;
		
	}
/*	
	if($txtSaDiscount_Temp!=''){
	
	$txtSaDiscount=$txtSaDiscount_Temp;
	
}*/
	
	echo"<table border='0' style='table-layout:fixed;width:100px' cellspacing=0>
			<tr>
		<td id='total-top' style='width:240px;height:12px;'></td>
		</tr>
		<tr><td id='total-content' style='width:240px;height:12px;'>";
		
	echo"	<table valign='top' border='0' cellspacing=0 cellpadding=0 style='width:240px;'>
			<col width=90>
			<col width=140>
				<tr><td valign='center' align='right' width='40%'><span class='style10'>Taxable</span>&nbsp;</td>
					<td align='left' ><input type='text' id='taxable'  name='txtSaTaxable' value='$txtSaTaxable' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' width='40%'><span class='style10'>None Taxable</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='nontaxable' name='txtSaNoneTaxable' value='$txtSaNoneTaxable' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' width='40%'><span class='style10'>Gross Total</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='grosstotal' name='txtSaGrossTotal' value='$txtSaGrossTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>	
				<tr><td valign='center' align='right'  width='40%'><span class='style10'>Discount</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='sadiscount' name='txtSaDiscount' value='$txtSaDiscount' maxlength='5' style='text-align:right; width:38px; height=20px;' onKeyup='CalcSaDiscount(1, $DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);' $DiscountMode ><span class='style10'> %</span>
						<input type='text' id='sadiscountamt' name='txtSaDiscountAmt' value='$txtSaDiscountAmt' maxlength='20' style='text-align:right; width:58px; height=20px;' onKeyup='CalcSaDiscount(2, $DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);' $DiscountMode ></td>
				</tr>";
				
	if ($ExportTransportToJV == 1) {
		echo"	<tr><td valign='center' align='right' width='40%' ><span class='style10'>Transport</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='satransportamt' name='txtSaTransportAmt' value='$txtSaTransportAmt' maxlength='20'  style='text-align:right; width:120px; height=20px;' onKeyup='CalcNetTotal($DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);'></td>
				</tr>";
	} else {
		echo "	<input type='hidden' id='satransportamt' name='txtSaTransportAmt' value='$txtSaTransportAmt'>";
	}	

	if ($ExportBonusToJV == 1) {
		echo"	<tr><td valign='center' align='right' width='40%'><span class='style10'>Bonus</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='sabonusamt' name='txtSaBonusAmt' value='$txtSaBonusAmt' maxlength='20'  style='text-align:right; width:120px; height=20px;' onKeyup='CalcNetTotal($DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);'></td>
				</tr>";
	} else {
		echo "	<input type='hidden' id='sabonusamt' name='txtSaBonusAmt' value='$txtSaBonusAmt'>";
	}
	
	echo"		<tr><td valign='center' align='right' width='40%' ><span class='style10'>
						<input type='hidden' name='txtSaVatChk' id='txtSaVatChk' value='$txtSaVatChk' size='1'/>
						<input name='chkSaVat' type='checkbox' id='chkSaVat' value='1' onClick='SetSaVat($DecimalBase,$VatApplied,$Setup_VatPercentage,$Module_Delivery,$SalesTTC);'"; 
						if($txtSaVatChk==1) echo " checked  $AllowModifyVAT />";  else echo " $AllowModifyVAT />"; 
				  
	echo "				VAT $Setup_VatPercentage%&nbsp;</span>
					</td> 
					<td align='left' >
						<input type='text' id='savat' name='txtSaVat' value='$txtSaVat' maxlength='20' style='text-align:right; width:120px; height=20px;' >
					</td>
				</tr>";

	if ($Module_AdditionalTaxes == 1) {
		echo"	<tr>
					<td valign='center' align='right' width='40%'><span class='style10'>Taxes</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='tax' name='txtSaTotalTaxes' value='$txtSaTotalTaxes' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>";
	} else {
		echo "	<input type='hidden' name='txtSaTotalTaxes' value='$txtSaTotalTaxes'>";
	}
						
	echo "		<tr><td valign='middle' align='right' width='40%'><span class='style10'>Net Total</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='nettotal' name='txtSaNetTotal' value='$txtSaNetTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;";
					
			if($txtSaNetTotal<0) echo " color:#FF0000;";		
			
				echo" ' readonly></td>
				</tr> 
			</table></td>
				</tr><tr>
					<td id='total-bottom' style='width:240px;height:12px;'></td>
				</tr></table></td></tr>";
				
	/*				
	if($txtSaDiscount_Temp!=''){
	
	 
		print <<<HERE
				<script type="text/javascript" language="javascript">
				CalcSaDiscount(1, $DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC)	
				</script>
HERE;
}*/

}

function ShowReturnDeliveryDtGrid($connection,$form,$PSaId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	$query_setup="select ifnull(delivery_maxrecords,1000000) from setup";
	$result_setup=mysql_query($query_setup,$connection);
	$row_numofrows=mysql_fetch_row($result_setup);
	$numofrows = $row_numofrows[0];
	
	
	$FI_DeliverySalesPrice = $arrayparams['FI_DeliverySalesPrice'];
	$FI_SalesCheckSalesman = $arrayparams['FI_SalesCheckSalesman'];
	$Login_NegativeQty = $arrayparams['Login_NegativeQty'];
	$Login_ModifySellingPrice = $arrayparams['Login_ModifySellingPrice'];
	$Login_ModifyDiscount = $arrayparams['Login_ModifyDiscount'];
	$Module_NegativeQuantity = $arrayparams['Module_NegativeQuantity'];
	$Module_Unit1Method = $arrayparams['Module_Unit1Method'];
	$Module_Pack = $arrayparams['Module_Pack'];
	$Module_DoubleUnit = $arrayparams['Module_DoubleUnit'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	$ShowCostCentLayout = $arrayparams['ShowCostCentLayout'];
	$Module_SerialNo = $arrayparams['Module_SerialNo'];
	$Module_Delivery = $arrayparams['Module_Delivery'];
	$Module_ExpireDate = $arrayparams['Module_ExpireDate'];
	
	$SetupDftWareHouse = $arrayparams['SetupDftWareHouse'];
	$defaultwarehouse  = $arrayparams['defaultwarehouse'];
	$Module_Heatno = $arrayparams['Module_Heatno'];
	
	
	$SalesTTC = $arrayparams['SalesTTC'];
	$DecimalBase = $arrayparams['DecimalBase'];
	
	$txtDate         = $arrayparams['txtDate'];
	$txtClientCode   = $arrayparams['txtClientCode'];
	$txtClientDetail = $arrayparams['txtClientDetail'];
	$txtCurrCode     = $arrayparams['txtCurrCode'];
	
	$sub_query = "SELECT currency_code FROM Temp_Deliveries WHERE Delivery_ID = '".$PSaId."'";
		//echo "<br><br>".$sub_query;
		$sub_result = selectData($connection, $sub_query);
		//echo "Currency".$txtCurrCode;
		if($txtCurrCode=='')
			$txtCurrCode   = $sub_result[0];
	//echo "Currency".$txtCurrCode;
	if ($txtClientCode == '' && $PSaId != '') {
		$sub_query = "SELECT Client_Code FROM Temp_Deliveries WHERE Delivery_ID = '".$PSaId."'";
		//echo "<br><br>".$sub_query;
		$sub_result = selectData($connection, $sub_query);
		$txtClientCode   = $sub_result[0];
	}

if ($Login_ModifyDiscount == 0)	$ModeDiscount = ' readonly ';
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid_Delivery.php';
	
	$i = 0;
	
	$PriceLabel = 'Price';	
	if ($SalesTTC == 1)  $PriceLabel = 'Price TTC';	
	
	$ModeModifySellPrice = '';
	if ($Login_ModifySellingPrice != 1) $ModeModifySellPrice = 'readonly';
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 50;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
	$columnsarray = array("itemcode", "itemdescription");
	
		
	array_push($columnsarray,"warehousecode");
	
	if($Module_Heatno==1)
		array_push($columnsarray, "deliverydt_heatno");
	
	
	array_push($columnsarray, 'sadtquantity');
	if ($Module_DoubleUnit == 1) array_push($columnsarray, 'unit2qty');
	if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1) {
		array_push($columnsarray, 'price');
		array_push($columnsarray, 'deliverydt_itemdiscount');
		array_push($columnsarray, 'sadttotal');
	}	
	if ($Module_ProjectDt == 1) array_push($columnsarray, 'projectcodedt');
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) array_push($columnsarray, 'costcentcodedt');
	if ($Module_ExpireDate == 1) {
		array_push($columnsarray, 'batchno');
		array_push($columnsarray, 'expiredate');
	}	
	//array_push($columnsarray, 'remarkdt');
	
	
	$columnsarrayall = array("itemcode", "itemdescription", "warehousecode", "deliveryref", "deliveryqty", "sadtquantity", "unit2qty", "price", "deliverydt_itemdiscount", "lastprice", "sadttotal", "projectcodedt", "costcentcodedt", "expiredate", "batchno", "remarkdt", "sadtid", "existingqty", "bookingqty", "availableqty", "itemunit1", "itemunit2", "itemvat", "itemnonstock", "itemexpire", "itemunitcoef");
	$orderbyarray = array("DeliveryDt.Item_CodePacked", "Items.Item_Code", "Items.Item_Description", "DeliveryDt.Warehouse_Code", "Items.Item_UnitSalesDesc", "Items.Item_UnitPurchaseDesc");
	
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

		
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
	$TableWidth = 1095;
	if ($Module_SerialNo == 1) $TableWidth += 25;
	if ($Module_Pack == 1)     $TableWidth += 125;
	if ($Module_DeliveryClosingQty == 1)     $TableWidth += 75;
	if ($Module_DoubleUnit == 1)   $TableWidth += 145;
	if ($Module_SerialNo == 1)     $TableWidth += 55;
	if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1)     $TableWidth += 345;
	if ($Module_ProjectDt == 1)    $TableWidth += 160;
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) $TableWidth += 160;
	if ($Module_ExpireDate == 1)   $TableWidth += 190;
	
	$TableWidthStyle = 'width:'.$TableWidth.'px;';
	
	echo"<div id='container' style='position: relative;z-index:1;' >";
	echo"	<div id='Loader' class=''></div>";
	echo" 	<div id='grid_headerDiv' class='grid_headerDiv'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
	
		echo"<tr>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		
		if ($Module_SerialNo == 1) {
			echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		}	
		  
		if ($Module_Pack == 1)
			echo"  <th width='125px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','DeliveryDt.Item_CodePacked','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code Packed".$orderimg1."</span></th>";
			
		echo"  <th width='165px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code".$orderimg2."</span></th>";
		echo"  <th width='205px;' align='left' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Description".$orderimg3."</span></th>";
		echo"  <th width='70px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','DeliveryDt.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>W.H".$orderimg4."</span></th>";
		
		if($Module_Heatno==1)
		echo"  <th width='210px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPorId."', 'datagrid', 1, '".$dftorderby."', 'COR','DeliveryDt.DeliveryDt_Heatno','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>HeatNo</span></th>";
	
	
	
		echo"  <th width='75px;' align='center'><span>Invoice Ref</span></th> ";	
		echo"  <th width='75px;' align='center'><span>Rem. Invoice Qty</span></th>";	
		echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit1".$orderimg5."</span></th>";
		echo"  <th width='75px;' align='center'><span>Quantity</span></th> ";	
		  
		if ($Module_DoubleUnit == 1) {
			echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitPurchaseDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit2".$orderimg6."</span></th>";
			echo"  <th width='80px;' align='center'><span>Unit2 Qty</span></th>";
		}
		
		if ($Module_SerialNo == 1) {
			echo"  <th width='55px;' align='center'><span>S/N Bal</span></th>";
		}
		  
		if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1) {
			echo"  <th width='85px;' align='center'><span>".$PriceLabel."</span></th>";
			echo"  <th width='85px;' align='center'><span>Last Sell. Price</span></th> ";
			echo"  <th width='70px;' align='center'><span>Disc(%)</span></th>";
			echo"  <th width='105px;' align='center'><span>Total</span></th>";
		}	
		
		if ($Module_ProjectDt == 1) {
			echo"  <th width='160px;' align='center'><span>Project</span></th>";	
		}
		if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
			echo"  <th width='160px;' align='center'><span>Costcenter</span></th>";	
		}
		
		if ($Module_ExpireDate == 1) {
			echo"  <th width='105px;' align='center'><span>Batch No</span></th>";	
			echo"  <th width='85px;' align='center'><span>Expire Date</span></th>";	
		}
	
		echo"  <th width='255px;' align='center'><span>Remark</span></th>";	
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='grid_contentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('grid_headerDiv','grid_contentDiv')\">";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
		echo"<input type='hidden' name='SelectedRec' id='SelectedRec' value='$SelectedRec'>
			 <input type='hidden' name='SelectedRecColor' id='SelectedRecColor' value='$SelectedRecColor'>
			 <input type='hidden' name='ModRec_G1' id='ModRec_G1' value='$ModRec_G1'>";  
		
			 
		
		
		if ($PSaId != '' ) {
		      
			$TotalQty = 0;
			$i_nonstock   = 0;
			$i_zeroprice  = 0;
			$i_minprice   = 0;
			$i_unitcoef   = 1;
			$i_expire     = 0;
			$i_withserial = 0;
						   
			$query = " Select DeliveryDt.DeliveryDt_id, DeliveryDt.Item_CodePacked, DeliveryDt.Item_code, Item_Description, DeliveryDt.Warehouse_Code, ".
					 " DeliveryDt_Remark, DeliveryDt_Quantity, Round(DeliveryDt_Price, ".$DecimalBase."), DeliveryDt_PriceBase1, DeliveryDt_PriceBase2, ".
					 " Round(DeliveryDt_DiscountPer, ".$DecimalBase."),  ".
					 " Round(((DeliveryDt_Quantity*DeliveryDt_Price)-((DeliveryDt_Quantity*DeliveryDt_Price*ifNull(DeliveryDt_DiscountPer,0))/100)), ".$DecimalBase.") As Total, ".
					 " Round(DeliveryDt_PriceTTC, ".$DecimalBase."), ".
					 " Round(((DeliveryDt_Quantity*DeliveryDt_PriceTTC)-((DeliveryDt_Quantity*DeliveryDt_PriceTTC*ifNull(DeliveryDt_DiscountPer,0))/100)), ".$DecimalBase.") As TotalTTC, ".
					 " DeliveryDt.Item_Unit2Qty, Item_UnitPurchaseDesc, Item_UnitSalesDesc, DeliveryDt.Project_code, DeliveryDt.CostCent_Code,  ".
					 " DeliveryDt.Related_Ref, DeliveryDt.RelatedDt_Id, ifNull(item_withserialno,0) As ItemWithSerialNo, Count(SerialNo_Code) As CountSerialNo,  ".
					 " Date_Format(DeliveryDt_ExpireDate,'%d/%m/%Y') As DeliveryDt_ExpireDate, DeliveryDt_BatchNo, DeliveryDt_HeatNo,deliverydt_clientdiscount,deliverydt_itemdiscount ".
					 " From Temp_DeliveryDt As DeliveryDt ".
					 " Left Join Items on DeliveryDt.Item_code = Items.Item_code ".
					 " Left Join Temp_Purchasesserialno on DeliveryDt.DeliveryDt_ID = Temp_Purchasesserialno.PurchaseDt_ID ".
					 " And Temp_Purchasesserialno.Journal_Type = 'DR' ".
					 " And Temp_Purchasesserialno.DeliveryDt_ID Is Not Null ".
					 " Where DeliveryDt.Delivery_ID = ".$PSaId.
					 " Group By  DeliveryDt.DeliveryDt_ID ".
					 " Order By $orderby $direction ";
					 
			//echo $query;
					
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);

			$query = $query." limit ".$startrange.",".$perpage;
			$resultdt=mysql_query($query,$connection);
			
			
			while($rowsdt=mysql_fetch_array($resultdt)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';

								
				$sub_query  = " Select QtyExistsWSorder('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', 0, 'DI', '', '') QtyExists From Dual ";
				$sub_query  = " Select 0 From Dual ";
				//echo $sub_query;
				$sub_result = selectData($connection, $sub_query);
				$vItemQtyExists = 0;
				$vItemQtyExists = $sub_result[0];		
						
				$TextColor = 'style7';
				if ($vItemQtyExists < 0) {
					$Color="style='background-color:#FF0000;'";
					$TextColor = 'style8';
				}	
				$deliverydt_heatno[$i] = $rowsdt[25];
				$sub_query = " SELECT ifNull(Item_Pack, 0), ifNull(item_zeroPrice, 0), Item_UnitSalesDesc, Item_UnitPurchaseDesc, ifNull(Item_Unit,1), ifNull(Item_NonStock, 0), ifNull(Item_MinimumPrice, 0), ifNull(Item_Expire,0), ifNull(item_withserialno,0) ".
							 " FROM Items ".
							 " WHERE Items.Item_Code = '".$rowsdt[2]."'";
					
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$i_pack       = $sub_result[0];
				$i_zeroprice  = $sub_result[1];
				$i_unit1      = $sub_result[2];
				$i_unit2 	  = $sub_result[3]; 
				$i_unitcoef	  = $sub_result[4];
				$i_nonstock   = $sub_result[5];
				$i_minprice   = $sub_result[6];
				$i_expire	  = $sub_result[7];
				$i_withserial = $sub_result[8];
				
				$sub_query = "SELECT ifNull(Client_SellingPrice,1) FROM Clients WHERE Ledger_Number = '".$txtClientCode."'";
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$c_sp   = $sub_result[0];
				
				$txtItemWithSerial[$i] = $i_withserial;
				$txtItemNonStock[$i]   = $i_nonstock;
				$txtItemUnitCoef[$i]   = $i_unitcoef;
				$txtItemExpire[$i]     = $i_expire;
				
				$Qry_QtyBookTemp = "SELECT ifnull(QtyBooking_Temp('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0]),0) ";
				$Qry_QtyBookTemp = "SELECT 0 from dual ";
				$result = selectData($connection, $Qry_QtyBookTemp);
				if ($result[0] < 0){$result[0] = 0;}
				$min_book_qty = $result[0];
								 
				$Qry_QtyExists = " SELECT ifnull(QtyExists('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'DR', '', ''),0) As QtyExists ";	
				$Qry_QtyExists = " SELECT 0 As QtyExists ";				
				$result = selectData($connection, $Qry_QtyExists);
				$min_exists_qty = $result[0];
				//echo $Qry_QtyExists;
				
				$Qry_Unit2QtyExists = " SELECT ifnull(Unit2QtyExists('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'DR', '', ''),0) As QtyExists ";
				$Qry_Unit2QtyExists = " SELECT 0 As QtyExists ";
				$result = selectData($connection, $Qry_Unit2QtyExists);
				$min_exists_unit2qty = $result[0];
				//echo $Qry_Unit2QtyExists;
				
				$Qry_QtyAvailable = " Select QtyExistsWSorder_Temp('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'DR', '', '', 0) as QtyExists From Dual ";
				$Qry_QtyAvailable = " Select 0 as QtyExists From Dual ";
				$result = selectData($connection, $Qry_QtyAvailable);
				$min_available_qty = $result[0];
				//echo $Qry_QtyAvailable;
		
				$Qry_QtyInTemp = " Select ifNull(Sum(ifNull(DeliveryDt_Quantity,0)),0) FROM Temp_Deliverydt ".
								 " WHERE Item_code = '".$rowsdt[2]."'  And  Warehouse_code = '".$rowsdt[4]."' And Delivery_ID = $PSaId And DeliveryDt_ID != ifNull('".$rowsdt[0]."',0) ";
			   // $Qry_QtyInTemp:=$Qry_QtyInTemp.	 " and costcent_Code='".lstCostCentCode[$i]."'"; 
			   //ECHO lstCostCentCode[$i];
				$result = selectData($connection, $Qry_QtyInTemp);
				$QtyTemp_DI = $result[0];
				
					
				$min_available_qty = $min_available_qty - $QtyTemp_DI;
				$min_exists_qty = $min_exists_qty - $QtyTemp_DI;
				$min_book_qty = $min_book_qty;
		
				$txtBookingQty[$i]    = $min_book_qty;
				$txtAvailableQty[$i]  = $min_available_qty;
				$txtExistingQty[$i]   = $min_exists_qty;
				$txtExistUnit2Qty[$i] = $min_exists_unit2qty;
				
				
				$sub_query = " Select ifNull(DeliveryDt.DeliveryDt_Quantity,0) - ifNull(Sum(ifNull(ReturnDeliveryDt.DeliveryDt_Quantity,0)),0) ".
							 " From DeliveryDt As DeliveryDt, DeliveryDt As ReturnDeliveryDt ".
							 " Where DeliveryDt.DeliveryDt_Id = '".$rowsdt[20]."' ".
							 " And DeliveryDt.Journal_Type = 'DI' ".
							 " And ReturnDeliveryDt.Journal_Type = 'DR' ".
							 " And ReturnDeliveryDt.DeliveryDt_Id != '".$rowsdt[0]."' ".
							 " And ReturnDeliveryDt.RelatedDt_Id = DeliveryDt.DeliveryDt_Id ";

				$sub_result = selectData($connection, $sub_query);
				$txtRemainedQty = $sub_result[0];
				if ($txtRemainedQty == '') $txtRemainedQty = 0;
				
				//Checking For Difference Btw Qty and Count SerialNo
				if (($rowsdt[6] != $rowsdt[22] && $rowsdt[21] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0 && $rowsdt[21] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0)) {
					$RecStyle = "style='background-color:#FF0000;'";
				}
								
			 
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
				
				echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1'>";		
				echo"	<td width='25px;' $RecStyle>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteRecordGrid('".$filepath."','".$form."', '".$PSaId."','".$rowsdt[0]."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_Delivery);return false;\" title='Delete Record'  ></td>";
				
				// If ItemWithSerialNo True And ModuleSerialNo True	
				if ($rowsdt[21] == 1 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'><img src='img/button_addserial.png' onMouseOver=\"style.cursor='hand'\" onclick=\"AddSerialNo('".$rowsdt[0]."','".htmlentities($rowsdt[2])."','".$RemainedQty."','".$rowsdt[4]."')\" title='Add Serial Nos'  ></td>";
				}
				else if ($rowsdt[21] == 0 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
				}
				
				if ($Module_Pack == 1)
					echo "	<td width='125px;' align='left'><span class='".$TextColor."'>".$rowsdt[1]."</span></td>";
				
				$deliverydt_clientdiscount[$i]	= $rowsdt[26];
				$deliverydt_itemdiscount[$i] = $rowsdt[27];

				
				echo "	<td width='185px;' align='left'>
							<input type='hidden' name='txtSaDtId[$i]' id='sadtid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
							<input type='hidden' name='txtItemNonStock[$i]' id='itemnonstock[$i]' value='$txtItemNonStock[$i]' >
							<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
							<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
							<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
							<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
							<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
							<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
							<input type='hidden' name='txtExistUnit2Qty[$i]' id='exitunit2qty[$i]' value='$txtExistUnit2Qty[$i]' >
							<input type='hidden' name='deliverydt_clientdiscount[$i]' id='deliverydt_clientdiscount[$i]' value='$deliverydt_clientdiscount[$i]' style='width:60'>
							
							";
							if($Module_Delivery==0)
							echo"<input type='button' name='$recno' value='H' style='width:15px;text-align:left;font-size:10px' onclick=openhistory('$rowsdt[2]')>";
							
							echo"<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"ShowSlidingDiv('#slidingDiv$i');\" style='vertical-align:bottom;'/></span></label>
							<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='".$rowsdt[2]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:110px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','".$txtClientCode."');\"   onblur='checkduplicate($i,this.value)'  onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\"  ".$KeyDown.">
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
							<div id='slidingDiv$i' class='slidingDiv slidingDivHide'>
								<table align='center' border='0' cellpadding='0' cellspacing='0'>
									<tr height='25px;'><td width='100px;'><span class='style1'>Existing Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtExistingQty[$i],2)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Booking Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtBookingQty[$i],2)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Available Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtAvailableQty[$i],2)."</span></td></tr>";
					
							if ($Module_DoubleUnit == 1 && $rowsdt[15] != $rowsdt[16] && $rowsdt[15] != '') {
								echo"<tr height='25px;'><td width='100px;'><span class='style1'>Existing Unit2Qty</span></td><td width='100px;' align='right'><span class='style1'>".$rowsdt[15] ."  ".number_format($txtExistUnit2Qty[$i],2)."</span></td></tr>";
							}
									
				echo"			</table>
								<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv$i');\"><span style='font-weight:normal;'>Close</span></a></div>
							</div>
						</td>";
										
				echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
						</td>";
				echo"  	<td width='70px;' align='center'>
							<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='".$rowsdt[4]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:35px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
				
				if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='deliverydt_heatno[$i]' id='deliverydt_heatno[$i]' value='".$deliverydt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
						</td>";		
				else echo"<input type='hidden' name='deliverydt_heatno[$i]' id='deliverydt_heatno[$i]' value='".$deliverydt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />";
				
				echo"	<td width='70px;' align='center'><input type='text' name='txtSaDtDeliveryRef[$i]' id='deliveryref[$i]' value='".$rowsdt[19]."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:65px; height:20px;' readonly></td>";
				echo"   <td width='70px;' align='center'><input type='text' name='txtSaDtDeliveryQty[$i]' id='deliveryqty[$i]' value='".$txtRemainedQty."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:65px; height:20px;' readonly  ></td>";
				echo "	<td width='60px;' align='Center'><span class='".$TextColor."'>".$rowsdt[16]."</span></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtSaDtQty[$i]' id='sadtquantity[$i]' value='".$rowsdt[6]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; border-width:0px; width:70px; height:20px;' onKeyup=\"CalcSaDtTotal($DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
					
				
				if ($Module_DoubleUnit == 1) { 
					echo"  <td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[15]."</span></td>";	
					echo"  <td width='80px;' align='center'><input type='text' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='".$rowsdt[14]."' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
				} else {
					echo"  <input type='hidden' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='' >";
				}
				
				if ($Module_SerialNo == 1) {
					echo"  <td width='55px;' align='center'><span class='".$TextColor."'>".$rowsdt[22]."</span></td>";
				}
				
				if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1) {
					if ($SalesTTC == 1) 
						echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='".$rowsdt[12]."' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
					else
						echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='".$rowsdt[7]."' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
						
					echo "	<td width='85px;' align='right'><span class='".$TextColor."'>&nbsp;</span></td>";
						echo"	<td width='70px;' align='center'>";
						echo"<input type='hidden' name='txtSaDtDiscount[$i]' id='sadtdiscount[$i]' value='".$rowsdt[10]."' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown." $ModeDiscount >";
					echo"<input type='text' name='deliverydt_itemdiscount[$i]' id='deliverydt_itemdiscount[$i]' value='$deliverydt_itemdiscount[$i]'  onblur='UpdateDiscount($i,this.value,$DecimalBase)'  onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown." $ModeDiscount  >";
				echo"</td>";
				//	echo"	<td width='70px;' align='center'><input type='text' name='txtSaDtDiscount[$i]'   onblur='UpdateDiscount($i,this.value)' id='sadtdiscount[$i]' value='".$rowsdt[10]."' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown." $ModeDiscount ></td>";
					
							
					if ($SalesTTC == 1) 
						echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='".$rowsdt[13]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
					else
						echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='".$rowsdt[11]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				} else {
					echo"	<input type='hidden' name='txtSaDtPrice[$i]' id='price[$i]' value=''  >
							<input type='hidden' name='txtItemLastSellPrice[$i]' id='lastprice[$i]' value=''  >
							<input type='hidden' name='txtSaDtDiscount[$i]' id='sadtdiscount[$i]' value='' >
							<input type='hidden' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='' >";
				}
				
				if ($Module_ProjectDt == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='".$rowsdt[17]."' class='".$ClassE."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
				}
				if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='".$rowsdt[18]."' class='".$ClassE."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
				}
				
				if ($Module_ExpireDate == 1) {
					echo" 	<td width='105px;' align='center'><input type='text' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($rowsdt[24], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
								<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNo(1,$i); ' style='vertical-align:bottom;'/></span></label></td>
							<td width='85px;' align='center'><input type='text' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value='".$rowsdt[23]."' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
				} else {
					echo" <input type='hidden' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value=''>";
					echo" <input type='hidden' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value=''>";
				}
				
				echo"	<td width='255px;'><input type='text' name='txtSaDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($rowsdt[5], ENT_QUOTES)."'  maxlength='200'  class='".$ClassE."' style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
				//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$FI_SalesCheckSalesman,$Module_Delivery); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$FI_SalesCheckSalesman,$Module_Delivery); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo "</tr>";
						
				
				$i+=1;
				
				$LastItemWarehouse = $rowsdt[4];
			}
			
			
		}
		if($i>=$numofrows)
		echo"<tr><td colspan=4>Maximum No of rows </td></tr>";
		
		for ($k=1;$k <= 1;$k++) {
			$recno = $i+1;
			$mode = 'i';
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';	
			
			if (empty($txtWarehouseCode[$i])) {
				if (!empty($LastItemWarehouse))
					$txtWarehouseCode[$i] = $LastItemWarehouse;
				else if (!empty($SetupDftWareHouse))
					$txtWarehouseCode[$i] = $SetupDftWareHouse;
				else	
					$txtWarehouseCode[$i] = $defaultwarehouse;
			}

			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
			
				
			echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1' >";
			echo"	<td width='25px;'>$recno</td>";
			echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G1');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
			if ($Module_SerialNo == 1) {
				echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
			} 
			if ($Module_Pack == 1)
				echo"  <td width='125px;' align='center'><span>&nbsp;</span></td>";
				
			echo "	<td width='165px;' align='left'>
						<input type='hidden' name='txtSaDtId[$i]' id='sadtid[$i]' value='$txtSaDtId[$i]' >
						<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
						<input type='hidden' name='txtItemNonStock[$i]' id='itemnonstock[$i]' value='$txtItemNonStock[$i]' >
						<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
						<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
						<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
						<input type='hidden' name='deliverydt_clientdiscount[$i]' id='deliverydt_clientdiscount[$i]' value='$deliverydt_clientdiscount[$i]' style='width:60'>
						
						<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');ShowSlidingDiv('#slidingDiv$i');\" style='vertical-align:bottom;'/></span></label>
						<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='$txtItemCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:110px; height:20px;'  onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','".$txtClientCode."');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\"   onblur='checkduplicate($i,this.value)'  ".$KeyDown.">
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
						<div id='slidingDiv$i' class='slidingDiv slidingDivHide'>
							<table align='center' border='0' cellpadding='0' cellspacing='0'>
								<tr height='25px;'><td width='100px;'><span class='style1'>Existing Qty</span></td><td width='100px;' align='right'><input type='text' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
								<tr height='25px;'><td width='100px;'><span class='style1'>Booking Qty</span></td><td width='100px;' align='right'><input type='text' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
								<tr height='25px;'><td width='100px;'><span class='style1'>Available Qty</span></td><td width='100px;' align='right'><input type='text' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>";
								
						if ($Module_DoubleUnit == 1) {
							echo"<tr height='25px;'><td width='100px;'><span class='style1'>Existing Unit2Qty</span></td><td width='100px;' align='right'><input type='text' name='txtExistUnit2Qty[$i]' id='exitunit2qty[$i]' value='$txtExistUnit2Qty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>";
						}	
						
		echo"				</table>
							<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv$i');\"><span style='font-weight:normal;'>Close</span></a></div>
						</div>
					</td>";
									
			echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='$txtItemDesc[$i]'  class='".$ClassI."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
					</td>";
			echo"  	<td width='70px;' align='center'>
						<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='$txtWarehouseCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:35px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\"  ".$KeyDown."  />
						<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
					</td>";
			if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='deliverydt_heatno[$i]' id='deliverydt_heatno[$i]' value='".$deliverydt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
						</td>";				
				else echo"<input type='hidden' name='deliverydt_heatno[$i]' id='deliverydt_heatno[$i]' value='".$deliverydt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />";		
						
			echo"	<td width='75px;' align='center'><input type='text' name='txtSaDtDeliveryRef[$i]' id='deliveryref[$i]' value='' maxlength='20' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' readonly></td>";
			echo"   <td width='75px;' align='center'><input type='text' name='txtSaDtDeliveryQty[$i]' id='deliveryqty[$i]' value='' maxlength='20' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' readonly  ></td>";
			echo"   <td width='65px;' align='center'><input type='text' name='txtItemUnit1' id='itemunit1[$i]' value='$txtItemUnit1' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtSaDtQty[$i]' id='sadtquantity[$i]' value='$txtSaDtQty[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onKeyup=\"CalcSaDtTotal($DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
					
				
		  
			if ($Module_DoubleUnit == 1) {
				echo"  <td width='65px;' align='center'><input type='text' name='txtItemUnit2' id='itemunit2[$i]' value='$txtItemUnit2' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
				echo"  <td width='80px;' align='center'><input type='text' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='$txtSaDtQty2[$i]' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassI."'  style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
			} else {
				echo "<input type='hidden' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='' >";
			}
			
			if ($Module_SerialNo == 1) {
				echo"  <td width='55px;' align='center'><span class='".$TextColor."'>&nbsp;</span></td>";
			}
				
			if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1) {
				if ($SalesTTC == 1) 
					echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='$txtSaDtPrice[$i]' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
				else
					echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='$txtSaDtPrice[$i]' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
						
				echo"   <td width='85px;' align='center'><input type='text' name='txtItemLastSellPrice' id='lastprice[$i]' value='$txtItemLastSellPrice' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:70px; height:20px;' readonly></td>";
				
				
					echo"	<td width='70px;' align='center'>";
						echo"<input type='hidden' name='txtSaDtDiscount[$i]' id='sadtdiscount[$i]' value='".$rowsdt[10]."' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown." $ModeDiscount >";
					echo"<input type='text' name='deliverydt_itemdiscount[$i]' id='deliverydt_itemdiscount[$i]' value='$deliverydt_itemdiscount[$i]'  onblur='UpdateDiscount($i,this.value,$DecimalBase)'  onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown." $ModeDiscount  >";
				echo"</td>";
				//echo"	<td width='70px;' align='center'><input type='text' name='txtSaDtDiscount[$i]'   onblur='UpdateDiscount($i,this.value)' id='sadtdiscount[$i]' value='$txtSaDtDiscount[$i]' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown." $ModeDiscount ></td>";
					
							
				if ($SalesTTC == 1) 
					echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='$txtSaDtTotal[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				else
					echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='$txtSaDtTotal[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
			} else {
				echo"	<input type='hidden' name='txtSaDtPrice[$i]' id='price[$i]' value=''  >
						<input type='hidden' name='txtItemLastSellPrice[$i]' id='lastprice[$i]' value=''  >
						<input type='hidden' name='txtSaDtDiscount[$i]' id='sadtdiscount[$i]' value='' >
						<input type='hidden' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='' >";
			}
			
			if ($Module_ProjectDt == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='$txtProjectCodeDt[$i]' class='".$ClassI."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
			}
			if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='$txtCostCentCodeDt[$i]' class='".$ClassI."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
			}
			
			if ($Module_ExpireDate == 1) {
				echo" 	<td width='105px;' align='center'><input type='text' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($txtSaDtBatchNo[$i], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
							<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNo(1,$i); ' style='vertical-align:bottom;'/></span></label></td>
						<td width='85px;' align='center'><input type='text' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value='$txtSaDtExpireDate[$i]' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
			} else {
				echo" <input type='hidden' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value=''>";
				echo" <input type='hidden' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value=''>";
			}
			
			echo"	<td width='255px;'><input type='text' name='txtSaDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($txtSaDtRemark[$i], ENT_QUOTES)."'  maxlength='200' class='".$ClassI."'  style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
			//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$FI_SalesCheckSalesman,$Module_Delivery); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
			echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$FI_SalesCheckSalesman,$Module_Delivery); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
			echo"</tr>";  

			$i+=1;	
		}
		

	
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$PSaId,'datagrid',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	
}

function ShowReturnDeliveryTotals($connection,$PSaId,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	$SalesTTC = $arrayparams['SalesTTC'];
	$Setup_VatPercentage = $arrayparams['Setup_VatPercentage'];
	$Module_AdditionalTaxes = $arrayparams['Module_AdditionalTaxes'];
	$Module_Delivery = $arrayparams['Module_Delivery'];
	$ExportTransportToJV = $arrayparams['ExportTransportToJV'];
	$ExportBonusToJV = $arrayparams['ExportBonusToJV'];
	$VatApplied = $arrayparams['VatApplied'];
	$costcent_applyvat   = $arrayparams['costcent_applyvat'];
	$AllowModifyVAT = $arrayparams['AllowModifyVAT'];
	$Login_ModifyDiscount = $arrayparams['Login_ModifyDiscount'];
	$FI_ReturnSalesVAT = $arrayparams['FI_ReturnSalesVAT'];
	$txtSaDiscount_Temp = $arrayparams['txtSaDiscount_Temp'];
 


	if($FI_ReturnSalesVAT==0) $AllowModifyVAT=' disabled ';
	
	$txtClientCode   = $arrayparams['txtClientCode'];
	
	if ($Login_ModifyDiscount == 0)	$DiscountMode = ' readonly ';
				 
	if ($PSaId != "") {	
	
		$sub_query = " Select Round(if($SalesTTC=1,ifNull(Delivery_TaxableTTC,0),ifNull(Delivery_Taxable,0)), ".$DecimalBase."), Round(ifNull(Delivery_NoneTaxable,0), ".$DecimalBase."), ".
					 " Round(if($SalesTTC=1,ifNull(Delivery_TaxableTTC,0),ifNull(Delivery_Taxable,0)) + ifNull(Delivery_NoneTaxable,0), ".$DecimalBase.") As GrossTotal, ".
					 " Round(ifNull(Delivery_DiscountPer,0), ".$DecimalBase."), Round(ifNull(Delivery_DiscountAmount,0), ".$DecimalBase."),  ".
					 " ifNull(Delivery_VatChkbx,0) As Delivery_VatChkbx, Round(ifNull(Delivery_VAT,0), ".$DecimalBase.") As Delivery_VAT, ".
					 " Round((if($SalesTTC=1,ifNull(Delivery_TaxableTTC,0),ifNull(Delivery_Taxable,0)) + ifNull(Delivery_NoneTaxable,0) + if($SalesTTC=1,0,IfNull(Delivery_VAT,0)) + ifNull(Delivery_TransportAmount,0) + IfNull(Delivery_BonusAmount,0) - IfNull(Delivery_DiscountAmount,0) + (ifNull(Delivery_Tax1,0) + ifNull(Delivery_Tax2,0) + ifNull(Delivery_Tax3,0) + ifNull(Delivery_Tax4,0) + ifNull(Delivery_Tax5,0))), ".$DecimalBase.") As Delivery_NetTotal,  ".
					 " Round((ifNull(Delivery_Tax1,0) + ifNull(Delivery_Tax2,0) + ifNull(Delivery_Tax3,0) + ifNull(Delivery_Tax4,0) + ifNull(Delivery_Tax5,0)), ".$DecimalBase.") As TotalTaxes,  ".
					 " Round(if($SalesTTC=1,ifNull(Delivery_TransportTTC,0),ifNull(Delivery_TransportAmount,0)), ".$DecimalBase.") As TransportAmt, ".
					 " Round(if($SalesTTC=1,ifNull(Delivery_BonusTTC,0),ifNull(Delivery_BonusAmount,0)), ".$DecimalBase.") As BonusAmt ".
					 " From Temp_Deliveries As Deliveries ".
					 " Where Delivery_ID = ".$PSaId;			 
				 
		//echo $sub_query;
		$sub_result = selectData($connection, $sub_query);
		
		$txtRSaTaxable      = $sub_result[0];
		$txtRSaNoneTaxable  = $sub_result[1];
		$txtRSaGrossTotal   = $sub_result[2];
		$txtRSaDiscount     = $sub_result[3];
		$txtRSaDiscountAmt  = $sub_result[4];
		$txtRSaVatChk       = $sub_result[5];
		$txtRSaVat          = $sub_result[6];
		$txtRSaNetTotal     = $sub_result[7];
		$txtRSaTotalTaxes   = $sub_result[8];
		$txtRSaTransportAmt = $sub_result[9];
		$txtRSaBonusAmt 	= $sub_result[10];
	} else {
		$sub_query = " Select ifNull(Client_VAT,0) From Clients Where Ledger_Number = '".$txtClientCode."'";			 
		$sub_result = selectData($connection, $sub_query);
		$txtRSaVatChk = $sub_result[0];
		
		if($costcent_applyvat==0) $txtRSaVatChk=0;
	}
	
	/*
if($txtSaDiscount_Temp!=''){
		$txtRSaDiscount=$txtSaDiscount_Temp;
		
	 
}*/

	echo"<table border='0' style='table-layout:fixed;width:100px' cellspacing=0>
		<tr>
		<td id='total-top' style='width:240px;height:12px;'></td>
		</tr>
		<tr><td id='total-content' style='width:240px;height:12px;'>";
	
			
	echo"	<table valign='top' border='0' cellspacing=0 cellpadding=0 style='width:240px;'><col width=90>
			<col width=140>
				<tr><td valign='center' align='right' ><span class='style10'>Taxable</span>&nbsp;</td>
					<td align='left' ><input type='text' id='taxable'  name='txtRSaTaxable' value='$txtRSaTaxable' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' ><span class='style10'>None Taxable</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='nontaxable' name='txtRSaNoneTaxable' value='$txtRSaNoneTaxable' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' ><span class='style10'>Gross Total</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='grosstotal' name='txtRSaGrossTotal' value='$txtRSaGrossTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>	
				<tr><td valign='center' align='right'  ><span class='style10'>Discount</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='sadiscount' name='txtRSaDiscount' value='$txtRSaDiscount' maxlength='5' style='text-align:right; width:38px; height=20px;' onKeyup='CalcRSaDiscount(1, $DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);' $DiscountMode ><span class='style10'> %</span> 
						<input type='text' id='sadiscountamt' name='txtRSaDiscountAmt' value='$txtRSaDiscountAmt' maxlength='20' style='text-align:right; width:58px; height=20px;' onKeyup='CalcRSaDiscount(2, $DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);' $DiscountMode ></td>
				</tr>";
				
	if ($ExportTransportToJV == 1) {
		echo"	<tr><td valign='center' align='right'  ><span class='style10'>Transport</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='satransportamt' name='txtRSaTransportAmt' value='$txtRSaTransportAmt' maxlength='20'  style='text-align:right; width:120px; height=20px;' onKeyup='CalcNetTotal($DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);'></td>
				</tr>";
	} else {
		echo "	<input type='hidden' id='satransportamt' name='txtRSaTransportAmt' value='$txtRSaTransportAmt'>";
	}	

	if ($ExportBonusToJV == 1) {
		echo"	<tr><td valign='center' align='right' ><span class='style10'>Bonus</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='sabonusamt' name='txtRSaBonusAmt' value='$txtRSaBonusAmt' maxlength='20'  style='text-align:right; width:120px; height=20px;' onKeyup='CalcNetTotal($DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);'></td>
				</tr>";
	} else {
		echo "	<input type='hidden' id='sabonusamt' name='txtRSaBonusAmt' value='$txtRSaBonusAmt'>";
	}
	
	echo"		<tr><td valign='center' align='right'  ><span class='style10'>
						<input type='hidden' name='txtRSaVatChk' id='txtRSaVatChk' value='$txtRSaVatChk' size='1'/>
						<input name='chkRSaVat' type='checkbox' id='chkRSaVat' value='1' onClick='SetRSaVat($DecimalBase,$VatApplied,$Setup_VatPercentage,$Module_Delivery,$SalesTTC);'"; 
						if($txtRSaVatChk==1) 
						echo " checked $AllowModifyVAT />";  
						else echo " $AllowModifyVAT />"; 
				  
	echo "				VAT $Setup_VatPercentage%&nbsp;</span>
					</td> 
					<td align='left' >
						<input type='text' id='savat' name='txtRSaVat' value='$txtRSaVat' maxlength='20' style='text-align:right; width:120px; height=20px;' >
					</td>
				</tr>";

	if ($Module_AdditionalTaxes == 1) {
		echo"	<tr>
					<td valign='center' align='right' ><span class='style10'>Taxes</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='tax' name='txtRSaTotalTaxes' value='$txtRSaTotalTaxes' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>";
	} else {
		echo "	<input type='hidden' name='txtRSaTotalTaxes' value='$txtRSaTotalTaxes'>";
	}
			
	echo "		<tr><td valign='middle' align='right' ><span class='style10'>Net Total</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='nettotal' name='txtRSaNetTotal' value='$txtRSaNetTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;";
				
					if($txtRSaNetTotal<0) echo " color:#FF0000;";
					
					echo" ' readonly>";
				 echo"</td>
		  </tr>
		  
		  </table></td>
				</tr><tr>
					<td id='total-bottom' style='width:240px;height:12px;'></td>
				</tr></table></td></tr>";



}

function showDataGridFooter($filepath,$form,$transid,$gridid,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction) {
	
	echo"<div class='grid_toolbarDiv'>
			<table width='100%' border=0 cellpadding='0' cellspacing='0'>
			<tr>";
	
	$rangefrom = ($startrange + 1);
	if (($startrange + $perpage) >= $totalrows) $rangeto = $totalrows; else $rangeto = ($startrange + $perpage);
				
	if ($totalrows > 0)			
		echo"	<td align='left' style='width:33%; padding-left:10px;border-top:1px solid #000000;'><span class='toolbar_style'>View ".$rangefrom." - ".$rangeto." of ".$totalrows."</span></td>";
	else
		echo"	<td align='left' style='width:33%; padding-left:10px;border-top:1px solid #000000;'><span class='toolbar_style'>No records to view</span></td>";
	
	echo"		<td align='center' style='width:15%;border-top:1px solid #000000;'><span class='toolbar_style'>Per Page </span><span class='toolbar_style'><select name='perpage' id='perpage' value='$perpage' onchange=\"ReloadGrid('".$filepath."', '".$form."', '".$transid."', '".$gridid."', 1, '".$dftorderby."', 'CP','".$orderby."','".$morelines."',0,this.value,'".$totalrows."','".$direction."');\">";
				  
				if ($perpage==10)		 $s10 = 'selected';
				else if ($perpage==20)	 $s20 = 'selected';
				else if ($perpage==30)	 $s30 = 'selected';
				else if ($perpage==40)	 $s40 = 'selected';
				else if ($perpage==50)	 $s50 = 'selected';
				else if ($perpage==100)	 $s100 = 'selected';
				else if ($perpage==1000) $s1000 = 'selected';

								
				echo'<option '.$s10.' value="10">10</option>
					 <option '.$s20.' value="20">20</option>
					 <option '.$s30.' value="30">30</option>
					 <option '.$s40.' value="40">40</option>
					 <option '.$s50.' value="50">50</option>
					 <option '.$s100.' value="100">100</option>
					 <option '.$s1000.' value="1000">1000</option>
					 </select></span>
				</td>';
				
	echo"		<td  style='width:10%;border-right:1px solid #000000;text-align:right;border-top:1px solid #000000;'>";
				
				if($startrange > 0)
					echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='pointer'\"    onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$transid."', '".$gridid."', 1, '".$dftorderby."', 'F','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');return false;\" >";

				$diff= $startrange - $perpage;
					
				if (($diff) >= 0 ){
					$prvstartrange = $startrange - $perpage;
					echo"&nbsp;&nbsp;<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='pointer'\"  onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$transid."', '".$gridid."', 1, '".$dftorderby."', 'P','".$orderby."','".$morelines."','".$prvstartrange."','".$perpage."','".$totalrows."','".$direction."');return false;\" >";
				}
				else
					echo "&nbsp;";
			
	echo"		</td><td  align='left' style='width:10%;border-top:1px solid #000000;'>&nbsp;";
			
			
				if ($startrange <= $totalrows){
					$startrange1 = $startrange + $perpage;
					if  ($startrange1 < $totalrows){ 
						echo"<img src='img/arrowright.png' title='Next' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$transid."', '".$gridid."', 1, '".$dftorderby."', 'N','".$orderby."','".$morelines."','".$startrange1."','".$perpage."','".$totalrows."','".$direction."');return false;\" onmouseover=\"style.cursor='pointer'\" >";
						echo"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='pointer'\" onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$transid."', '".$gridid."', 1, '".$dftorderby."', 'L','".$orderby."','".$morelines."','".$prvstartrange."','".$perpage."','".$totalrows."','".$direction."');return false;\" >";	
					}
				} else
					echo "&nbsp;";
			
				
				$currentpage = ($startrange / $perpage) + 1;
				$totalpages  = ceil($totalrows / $perpage);
				
	echo"		</td><td style='text-align:center; width:14%;border-top:1px solid #000000;'>&nbsp;<span class='toolbar_style'>Page $currentpage / $totalpages</span></td>";
	

	
	echo"		<td style='text-align:center; width:6%;border-top:1px solid #000000;'><img src='img/Refresh-icon.png'  onMouseOver=\"style.cursor='pointer'\"  width='20' height='20' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$transid."', '".$gridid."', 1, '".$dftorderby."', 'R','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\" ></td>";
	echo"		<td align='right' style='width:12%; padding-right:10px;border-top:1px solid #000000;'>&nbsp;</td>";
	
	echo"	</tr>";
	echo"	</table></div>";

}
?>