<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
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(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}


	//echo $_SESSION['dbName']."<br>";
	
	
	 if ($form == 'ASS') {
		ShowAssembleDtGrid($connection,$form,$transid,$_SESSION['gass_parameters'],$_SESSION['gass_arrayaccess'],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	}
	else if ($form == 'ASS-TOT') {
		ShowAssembleTotals($connection,$transid,$_SESSION['gass_parameters'],$_SESSION['gass_arrayaccess']);
	}
	
	
	
}


function ShowAssembleDtGrid($connection,$form,$PPrdId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	$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_ExpireDate 			= $arrayparams['Module_ExpireDate'];
	
	$SetupDftWareHouse 			= $arrayparams['SetupDftWareHouse'];
	$defaultwarehouse  			= $arrayparams['defaultwarehouse'];
	$FI_ShowCostPrice  			= $arrayparams['FI_ShowCostPrice'];
	
	$DecimalBase 				= $arrayparams['DecimalBase'];
	$Base1 						= $arrayparams['Base1'];
	$Base2 						= $arrayparams['Base2'];
	
	$txtDate         			= $arrayparams['txtDate'];
	$txtCurrCode     			= $arrayparams['txtCurrCode'];
	$DisableIdeal    			= $arrayparams['DisableIdeal'];
	$actionmode    				= $arrayparams['actionmode'];
	
	$ShowStage 					= $arrayparams['ShowStage'];
	$ShowAdditions 				= $arrayparams['ShowAdditions'];
	
	$Module_Heatno 				= $arrayparams['Module_Heatno'];
	$Actual_Zero 				= $arrayparams['Actual_Zero'];
	
	if ($PPrdId != '' ) {
		$sub_query = " Select Date_Format(Production_Date,'%d/%m/%Y') As Production_Date, Production.Currency_Code ".
					 " From Temp_Production As Production ".
					 " Where Production_ID = ".$PPrdId;
		$sub_result = selectData($connection, $sub_query);
		$txtDate 	 = $sub_result[0];
		$txtCurrCode = $sub_result[1];
	}
	
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid_Assemble.php';
	
	$i = 0;
	$k = 0;
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 100;
	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, "HeatNo");
	
	if($ShowAdditions == 1){
		array_push($columnsarray, "prddtinitialquantity");
		array_push($columnsarray, "prddtaddquantity");
	}	
	array_push($columnsarray, "prddtquantity");
	
	
		
	if ($DisableIdeal == 0) 
	{
		array_push($columnsarray, 'prddtwaste');
		
	}
	array_push($columnsarray, 'costprice');
	
	
		
	if ($DisableIdeal == 0) {
		array_push($columnsarray, 'prddttotalideal');
		
	}
	
	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", "prddtquantity", "prddtidealqty", "prddtwastecalc", "prddtwaste", "costprice", "prddttotal", 
							"prddttotalideal", "projectcodedt", "costcentcodedt", "expiredate", "batchno", "remarkdt", "prddtid", "existingqty", "bookingqty", "availableqty", 
							"itemunit1", "itemvat", "itemexpire", "itemunitcoef");
	$orderbyarray = array("ProductionDt.Item_CodePacked", "Items.Item_Code", "Items.Item_Description", "ProductionDt.Warehouse_Code", "Items.Item_UnitSalesDesc");
	
	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 = 1160;
	//if ($Module_SerialNo == 1)     $TableWidth += 20;
	if ($Module_Pack == 1)         $TableWidth += 125;
	if ($DisableIdeal == 0)	       $TableWidth += 330;
	//if ($Module_SerialNo == 1)     $TableWidth += 50;
	if ($Module_ProjectDt == 1)    $TableWidth += 160;
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) $TableWidth += 160;
	if ($Module_ExpireDate == 1)   $TableWidth += 170;
	if($ShowStage == 1) $TableWidth += 90;
	if($ShowAdditions == 1) $TableWidth += 160;
	$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 height='25px;'>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		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."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','ProductionDt.Item_CodePacked','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code Packed".$orderimg1."</span></th>";
			
		echo"  <th width='190px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', '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."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Description".$orderimg3."</span></th>";
		echo"  <th width='105px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','ProductionDt.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Warehouse".$orderimg4."</span></th>";
		
		if($ShowStage == 1)
			echo"  <th width='85px;' align='center'><span>Stage</span></th>";
		if($Module_Heatno==1)
		echo"  <th width='210px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','ProductionDt.ProductionDt_Heatno','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>HeatNo</span></th>";
	
	
		echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit1".$orderimg5."</span></th>";
		
		
		if ($DisableIdeal == 0) echo"  <th width='75px;' align='center'><span>Ideal Qty</span></th>";
		if($ShowAdditions == 1){
			echo"  <th width='75px;' align='center'><span>Initial Qty</span></th> ";	
			echo"  <th width='75px;' align='center'><span>Addition</span></th> ";	
		}
		echo"  <th width='75px;' align='center'><span>Actual Qty</span></th> ";	
		echo"  <th width='75px;' align='center'><span>Qty Exists</span></th> ";	
		  
		if ($DisableIdeal == 0) {
			echo"  <th width='75px;' align='center'><span>Waste</span></th>";
			echo"  <th width='75px;' align='center'><span>Actual Waste</span></th>";
		}
		
		/*if ($Module_SerialNo == 1) {
			echo"  <th width='55px;' align='center'><span>S/N Bal</span></th>";
		}*/
		
		if($FI_ShowCostPrice == 1){
		echo"  <th width='85px;' align='center'><span>Cost Price</span></th>";
		echo"  <th width='105px;' align='center'><span>Actual Total</span></th>";
		
		if ($DisableIdeal == 0) {
			echo"  <th width='105px;' align='center'><span>Ideal 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 ($PPrdId != '' ) {
		      
			$TotalQty = 0;
			$i_nonstock  = 0;
			$i_zeroprice = 0;
			$i_minprice  = 0;
			$i_unitcoef  = 1;
			$i_expire    = 0;
			
			$query = " Select ProductionDt.ProductionDt_ID, ProductionDt.Item_CodePacked, ProductionDt.Item_code, Item_Description, ProductionDt.Warehouse_Code, ".
					 " ProductionDt_Remark, round(ProductionDt_Quantity,".$DecimalBase.") as ProductionDt_Quantity, round(ProductionDt_IdealQty,".$DecimalBase.") as ProductionDt_IdealQty, Round(ProductionDt_Cost, ".$DecimalBase."), ProductionDt_CostBase1, ProductionDt_CostBase2, ".
					 " round(ifNull(ProductionDt_IdealQty,0) - ifNull(ProductionDt_Quantity,0),".$DecimalBase.") As ProductionDtWasteCalc, round(ProductionDt_Waste,".$DecimalBase.") as ProductionDt_Waste, ".
					 " Round((ProductionDt_Quantity*ProductionDt_Cost), ".$DecimalBase.") As TotalActual, ".
					 " Round((ProductionDt_IdealQty*ProductionDt_Cost), ".$DecimalBase.") As TotalIdeal, ".
					 " ifNull(item_withserialno,0) As ItemWithSerialNo, Count(Temp_Purchasesserialno.SerialNo_Code) As CountSerialNo, Item_UnitSalesDesc, ".
					 " Date_Format(ProductionDt_ExpireDate,'%d/%m/%Y') As ProductionDt_ExpireDate, ProductionDt_BatchNo, ProductionDt.Project_code, ".
					 " ProductionDt.CostCenter_Code, ifNull(ProductionDt_Checked,1) as ProductionDt_Checked, productionDt_heatNo, item_stage, ".
					 " round(productiondt_initialQty,".$DecimalBase.") as productiondt_initialQty, round(productiondt_addition,".$DecimalBase.") as productiondt_addition, productiondt_stage, productiondt_oldItem ".
					 " From Temp_ProductionDt As ProductionDt Left Join Items on ProductionDt.Item_code = Items.Item_code ".
					 " Left Join Temp_Purchasesserialno on ProductionDt.ProductionDt_ID = Temp_Purchasesserialno.PurchaseDt_ID ".
					 " And Temp_Purchasesserialno.Journal_Type = 'PD' ".
					 " And Temp_Purchasesserialno.PurchaseDt_ID Is Not Null ".
					 " left join packdt on packdt.Item_CodePackDt = ProductionDt.Item_code ".
					 " Where ProductionDt.Production_ID = ".$PPrdId.
					 " Group By  ProductionDt.ProductionDt_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;
				$k = $k + 1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$TextColor = 'style7';
				
				$productiondt_heatno[$i] = $rowsdt[23];
				
				$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];
				
				
				$txtItemWithSerial[$i] = $i_withserial;
				$txtItemUnitCoef[$i]   = $i_unitcoef;
				$txtItemExpire[$i]     = $i_expire;
				
			
				
							
				//Checking For Difference Btw Qty and Count SerialNo
				if (($rowsdt[6] != $rowsdt[16] && $rowsdt[15] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0 && $rowsdt[15] != 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."', '".$PPrdId."','".$rowsdt[0]."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."');return false;\" title='Delete Record'  ></td>";
				
				$SelRecord  = 'R'.$k;
				$$SelRecord = $rowsdt[0];
			   
				echo " 	<td width='25px;' align='center'><input type='checkbox' name='$SelRecord' id='$SelRecord' value='".$$SelRecord."' onclick='UnSelectRecord();' ";
				if ($rowsdt[22] == 1) echo "checked /></td>"; else echo "/></td>";
				
				// If ItemWithSerialNo True And ModuleSerialNo True	
				/*if ($rowsdt[15] == 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])."','".$rowsdt[6]."','".$rowsdt[4]."')\" title='Add Serial Nos'  ></td>";
				}
				else if ($rowsdt[15] == 0 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
				}*/
				
				$Qry_QtyBookTemp = "SELECT ifnull(QtyBooking_Temp('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0]),0) ";
				$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 ";				 
				$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 ";
				$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 ";
				$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;
				
				
				
				if ($Module_Pack == 1)
					echo "	<td width='125px;' align='left'><span class='".$TextColor."'>".$rowsdt[1]."</span></td>";
					
				echo "	<td width='190px;' align='left'>
							<input type='hidden' name='txtPrdDtId[$i]' id='prddtid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$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]' >
							<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'); GetItemCost(this.id,'costprice[$i]','getitemcost','".$i."','".$txtDate."','".$txtCurrCode."'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');getQtyExists('$i','$rowsdt[0]');\" ".$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>
							<label><span><img src='img/openlist.png' title='Select Item Option' id='lstItem[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsOpt(1,$i); ' style='vertical-align:bottom;'/></span></label>
							<input type='hidden' name='txtOldItem[$i]' id='olditem[$i]' value='".$rowsdt[28]."'  maxlength='20'>
							<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='105px;' align='center'>
							<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='".$rowsdt[4]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; 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($ShowStage == 1){
					if($rowsdt[27]!='') $itemstage=$rowsdt[27];
					else $itemstage=$rowsdt[24];
					echo"<td width='75px'><input type='text' name='item_stage[$i]' id='item_stage[$i]' value='".$itemstage."'  maxlength='20' class='".$ClassE."' style='font-size:12px;  width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); ".$KeyDown."  /></td>";	
					
				}
				if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='productiondt_heatno[$i]' id='productiondt_heatno[$i]' value='".$productiondt_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='<input type='text' name='productiondt_heatno[$i]' id='productiondt_heatno[$i]' value='".$productiondt_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='left'><span class='".$TextColor."'>".$rowsdt[17]."</span></td>";
				
				
				if ($DisableIdeal == 0){
					if($actionmode == 'loadmaterials')	$readonlyval = '';
					else 	$readonlyval = 'readonly';				
					echo"  <td width='75px;' align='right'><input type='text' name='txtPrdDtQtyIdeal[$i]' id='prddtidealqty[$i]' value='".$rowsdt[7]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:right; width:70px; height:20px;' $readonlyval ></td>";
				}
				if($ShowAdditions == 1){
					echo"	<td width='75px;' align='right'><input type='text' name='txtPrdDtIniQty[$i]' id='prddtinitialquantity[$i]' value='".$rowsdt[25]."' maxlength='20' onKeyup=\"CalcAddition($DecimalBase,$i);\"  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:right; width:70px; height:20px;'  onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
					
					echo"	<td width='75px;' align='right'><input type='text' name='txtPrdDtAddQty[$i]' id='prddtaddquantity[$i]' value='".$rowsdt[26]."' maxlength='20' onKeyup=\"CalcAddition($DecimalBase,$i);\" class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:right; width:70px; height:20px;'  onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
				}
				echo"	<td width='75px;' align='right'><input type='text' name='txtPrdDtQty[$i]' id='prddtquantity[$i]' value='".$rowsdt[6]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:right; width:70px; height:20px;' onKeyup=\"CalcPrdDtTotal($DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
				echo"	<td width='75px;' align='right'><input type='text' name='txtPrdDtQtyExists[$i]' id='prddtqtyexists[$i]' value='$txtAvailableQty[$i]' maxlength='20'  readonly class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:right; width:70px; height:20px;'></td>";
					
				if ($DisableIdeal == 0) {
					echo"  <td width='75px;' align='right'><input type='text' name='txtPrdDtWasteCalc[$i]' id='prddtwastecalc[$i]' value='".$rowsdt[11]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:right; width:70px; height:20px;' readonly ></td>";
					echo"  <td width='75px;' align='right'><input type='text' name='txtPrdDtWaste[$i]' id='prddtwaste[$i]' value='".$rowsdt[12]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:right; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
				} else {
					echo"  <input type='hidden' name='txtPrdDtQtyIdeal[$i]' id='prddtidealqty[$i]' value='' >";
					echo"  <input type='hidden' name='txtPrdDtWasteCalc[$i]' id='prddtwastecalc[$i]' value='' >";
					echo"  <input type='hidden' name='txtPrdDtWaste[$i]' id='prddtwaste[$i]' value='' >";
				}
				
				
				
				/*if ($Module_SerialNo == 1) {
					echo"  <td width='55px;' align='center'><span class='".$TextColor."'>".$rowsdt[16]."</span></td>";
				}*/
				
				if($FI_ShowCostPrice == 1){
				echo"	<td width='85px;' align='center'><input type='text' name='txtPrdDtCostPrice[$i]' id='costprice[$i]' value='".$rowsdt[8]."' maxlength='20' onKeyup='CalcPrdDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
				echo"   <td width='105px;' align='center'><input type='text' name='txtPrdDtTotal[$i]' id='prddttotal[$i]' value='".$rowsdt[13]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				
				if ($DisableIdeal == 0) {
					echo"   <td width='105px;' align='center'><input type='text' name='txtPrdDtTotalIdeal[$i]' id='prddttotalideal[$i]' value='".$rowsdt[14]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				}	
			}
				if ($Module_ProjectDt == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='".$rowsdt[20]."' 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[21]."' 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='txtPurDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($rowsdt[19], 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='txtPurDtExpireDate[$i]' id='expiredate[$i]' value='".$rowsdt[18]."' 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='txtPurDtExpireDate[$i]' id='expiredate[$i]' value=''>";
					echo" <input type='hidden' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value=''>";
				}
				
				echo"	<td width='255px;'><input type='text' name='txtPurDtRemark[$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."', '".$PPrdId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo); 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."', '".$PPrdId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo); 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];
			}
			
			
		}
		
		echo"<input type='hidden' name='ChkCounter' id='ChkCounter' value='$k'>";
							
		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>";
			echo"   <td width='25px;' align='center'><span>&nbsp;</span></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='txtPrdDtId[$i]' id='prddtid[$i]' value='$txtPrdDtId[$i]' >
						<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$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='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'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','-2'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]');getQtyExists('$i','');\" 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>
						<input type='hidden' name='txtOldItem[$i]' id='olditem[$i]' value='$txtOldItem[$i]'  maxlength='20'>
					</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='105px;' align='center'>
						<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='$txtWarehouseCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:75px; 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($ShowStage == 1)		
				echo"<td width='75px'><input type='text' name='item_stage[$i]' id='item_stage[$i]' value='".$item_stage[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px;  width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); ".$KeyDown."  /></td>";	
			if($Module_Heatno==1)
			echo"  	<td width='210px;' align='center'>
							<input type='text' name='productiondt_heatno[$i]' id='productiondt_heatno[$i]' value='".$productiondt_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='productiondt_heatno[$i]' id='productiondt_heatno[$i]' value='".$productiondt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:180px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); ".$KeyDown."   />";
			
			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>";
		if ($DisableIdeal == 0) 
			echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtQtyIdeal[$i]' id='prddtidealqty[$i]' value='$txtPrdDtQtyIdeal[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' readonly ></td>";
		else echo"  <input type='hidden' name='txtPrdDtQtyIdeal[$i]' id='prddtidealqty[$i]' value='' >";
		if($ShowAdditions == 1){
					echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtIniQty[$i]' id='prddtinitialquantity[$i]' value='$txtPrdDtAddQty[$i]' maxlength='20' onKeyup=\"CalcAddition($DecimalBase,$i);\"  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;'  onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
					
					echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtAddQty[$i]' id='prddtaddquantity[$i]' value='$txtPrdDtAddQty[$i]' maxlength='20'  onKeyup=\"CalcAddition($DecimalBase,$i);\" class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;'  onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
				}		
		echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQty[$i]' id='prddtquantity[$i]' value='$txtPrdDtQty[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onKeyup=\"CalcPrdDtTotal($DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." ></td>";
					
		echo"	<td width='75px;' align='right'><input type='text' name='txtPrdDtQtyExists[$i]' id='prddtqtyexists[$i]' value='' maxlength='20'  readonly class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:right; width:70px; height:20px;'></td>";				
			if ($DisableIdeal == 0) {
				echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtWasteCalc[$i]' id='prddtwastecalc[$i]' value='$txtPrdDtWasteCalc[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' readonly ></td>";
				echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtWaste[$i]' id='prddtwaste[$i]' value='$txtPrdDtWaste[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
			} else {
				
				echo"  <input type='hidden' name='txtPrdDtWasteCalc[$i]' id='prddtwastecalc[$i]' value='' >";
				echo"  <input type='hidden' name='txtPrdDtWaste[$i]' id='prddtwaste[$i]' value='' >";
			}	
		  
			
			/*if ($Module_SerialNo == 1) {
				echo"  <td width='55px;' align='center'><span class='".$TextColor."'>&nbsp;</span></td>";
			}*/
			if($FI_ShowCostPrice == 1){
			echo"	<td width='85px;' align='center'><input type='text' name='txtPrdDtCostPrice[$i]' id='costprice[$i]' value='$txtPrdDtCostPrice[$i]' maxlength='20' onKeyup='CalcPrdDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
			echo"   <td width='105px;' align='center'><input type='text' name='txtPrdDtTotal[$i]' id='prddttotal[$i]' value='$txtPrdDtTotal[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				
			if ($DisableIdeal == 0) {
				echo"   <td width='105px;' align='center'><input type='text' name='txtPrdDtTotalIdeal[$i]' id='prddttotalideal[$i]' value='$txtPrdDtTotalIdeal[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
			}	
			}	
				
			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='txtPurDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($txtPurDtBatchNo[$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='txtPurDtExpireDate[$i]' id='expiredate[$i]' value='$txtPurDtExpireDate[$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='txtPurDtExpireDate[$i]' id='expiredate[$i]' value=''>";
				echo" <input type='hidden' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value=''>";
			}
			
			echo"	<td width='255px;'><input type='text' name='txtPurDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($txtPurDtRemark[$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."', '".$PPrdId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo); 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."', '".$PPrdId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo); 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,$PPrdId,'datagrid',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	
}


function ShowAssembleTotals($connection,$PPrdId,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	$txtCurr = $arrayparams['txtCurrCode']; 
	$Production_extraInfo = $arrayparams['Production_extraInfo']; 
	if ($PPrdId != "") {
		$sub_query = " Select Round(Production_Total, ".$DecimalBase.") As ProductionTotal, ".
					 " Round(Production_IdealTotal, ".$DecimalBase.") As ProductionIdealTotal, ".
					 " if($Production_extraInfo=1,Round(Production_Total/if(Quantity_Ill is null or Quantity_Ill=0,1,Quantity_Ill), ".$DecimalBase."),Round(Production_Total/ifNull(Production_Quantity,1), ".$DecimalBase.")) As CostPricePerUnit, ".
					 " Round(Production_IdealTotal/ifNull(Production_IdealQty,1), ".$DecimalBase.") As CostPricePerUnitIdeal, production_date as ProductionDate, item_code as ItemCode ".
					 " From Temp_Production As Production ".
					 " Where Production_ID = ".$PPrdId;
			 
		//echo $sub_query;
		$sub_result = selectData($connection, $sub_query);
		
		$txtPrdTotal      		= $sub_result[0];
		$txtPrdTotalIdeal		= $sub_result[1];
		$txtPrdCostPerUnit   	= $sub_result[2];
		$txtPrdCostPerUnitIdeal = $sub_result[3];
		$ProductionDate         = $sub_result[4];
		$ItemCode               = $sub_result[5];
		
		$query_lastcost = "select production_reference as LastCostRef, format(production_total, $DecimalBase) as LastCostTotal, format((production_total/production_quantity), $DecimalBase) as LastCostUnit, production_date
from production where item_code = '$ItemCode' and production_date < '$ProductionDate'
order by production_date desc limit 1";
//echo $query_lastcost;
        $result_lastcost = mysql_query($query_lastcost, $connection);
		$row_lastcost    = mysql_fetch_array($result_lastcost);
		extract($row_lastcost);
		
	} 
	
	
	
	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;'>
			<table border='0' style='table-layout:fixed;' cellspacing=0 >
			<col width=90>
			<col width=140>
			
				<tr><td valign='center' align='right' width='45%'><span class='style10'>Total Cost Actual:</span>&nbsp;</td>
					<td align='left' ><input type='text' id='totalactual'  name='txtPrdTotal' value='$txtPrdTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' width='45%'><span class='style10'>Cost Price/Unit: <br>Actual</span>&nbsp;</td> 
					<td align='left' >
						<input type='text' id='unitcostactual' name='txtPrdCostPerUnit' value='$txtPrdCostPerUnit' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;";
						
						
			 
			 
			 echo"	' readonly>
						<input type='hidden' id='totalideal' name='txtPrdTotalIdeal' value='$txtPrdTotalIdeal'>
						<input type='hidden' id='unitcostideal' name='txtPrdCostPerUnitIdeal' value='$txtPrdCostPerUnitIdeal'>
					</td>
				</tr>";
		  
			
		  
		 echo" </table></td>
				</tr><tr>
					<td id='total-bottom' style='width:240px;height:12px;'></td>
				</tr></table>";
				echo "<div style='text-align: center; font-size: 12px; font-weight: bold; margin-top: 8px;margin-bottom: 8px;'>Last Cost</div>";
				echo "<div style='padding: 15px; border-radius: 15px; background-color: #016a84; width: 221px;'>
				<table width=100% style='border-collapse: collapse'>
				<tr><td valign='center' align='right' width='45%'><span class='style10'>Reference:</span>&nbsp;</td>
					<td align='left' ><input type='text' id='totalactual'  name='LastCostRef' value='$LastCostRef' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' width='45%'><span class='style10'>Unit Cost:</span>&nbsp;</td>
					<td align='left' ><input type='text' id='totalactual'  name='LastCostUnit' value='$LastCostUnit' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' width='45%'><span class='style10'>Total Cost:</span>&nbsp;</td>
					<td align='left' ><input type='text' id='totalactual'  name='LastCostTotal' value='$LastCostTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				</table>
				</div>";
				echo"</td></tr>";
			


}


function showDataGridFooter($filepath,$form,$transid,$gridid,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction, $query_journal_labels, $connection) {
	//echo $query_journal_labels;
	$result_journal_labels = mysql_query($query_journal_labels, $connection);
	//echo $result_journal_labels;
	$row_journal_labels    = mysql_fetch_array($result_journal_labels);
	//echo $row_journal_labels;
	extract($row_journal_labels);
	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'>".$NoRecords."</span></td>";
	
	echo"		<td align='center' style='width:15%;border-top:1px solid #000000;'><span class='toolbar_style'>".$Perpage." </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>";

}


?>