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

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

	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	$Journal_DefaultCur = $arrayparams['Journal_DefaultCur'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	
	$txtJourDate = $arrayparams['txtJourDate'];
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
		
		
	echo"
		
		 <input type='hidden' name='deliverydt_quantity' id='deliverydt_quantity' value='$deliverydt_quantity'>
		 <input type='hidden' name='SelectedRecDt' id='SelectedRecDt' value='$SelectedRecDt'>
		 <input type='hidden' name='SelectedRecColorDt' id='SelectedRecColorDt' value='$SelectedRecColorDt'>
		 
		 ";
	
				   
			$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 ".
					 " From DeliveryDt As DeliveryDt ".
					 " Left Join Items on DeliveryDt.Item_code = Items.Item_code ".
					 " Left Join Purchasesserialno on DeliveryDt.DeliveryDt_ID = Purchasesserialno.PurchaseDt_ID ".
					 " And Purchasesserialno.Journal_Type = 'DI' ".
					 " And 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 ";
					 
	       //  echo $query;
			 
			 
			 
		echo"<table onMouseOver=\"style.cursor='pointer'\"  border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:670px;'>";
		echo"<col width=90>";
		echo"<col width=190>";
		echo"<col width=60>";
		echo"<col width=40>";
		echo"<col width=200>";
		echo"  <th align='center' ><span>Item Code</span></th>";
		echo"  <th align='center' ><span>Description</span></th>";
		echo"  <th align='center' ><span>Warehouse</span></th>";
		echo"  <th align='center' ><span>Qty</span></th>";
		echo"  <th align='center' ><span>Remark</span></th>";
	//echo $query;
	$i=0;
	$result=mysql_query($query,$connection) or die(mysql_error());
    while ($row=mysql_fetch_array($result)){
	
	
	if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';

				
				
	echo"<tr height=20 id='deliverydt$row[0]' class='$bgcolor table-focus-g1'  
					onclick=\"returncolor(this,'SelectedRecDt','SelectedRecColorDt','$row[0]');showrelationtolot('$row[0]','$row[6]')\" 
					
					
					>";
	echo"<td>$row[2]</td>";
	echo"<td>$row[3]</td>";
	echo"<td>$row[4]</td>";
	echo"<td>$row[6]</td>";
	echo"<td>$row[5]</td>";
	echo"</tr>";
	$i+=1;
	}
	echo"</table>";
	

	
	

}

function Show_Delivery_DeliveryStatus($delivery_id,$connection,$arrayparams,$arrayaccess,$useraccount){

$query_checkStatus="select FI_DeliveryStatusFrom as statusfrom, FI_DeliveryStatusTo as statusto from ".$_SESSION['dbLabel'].".login 
				where user_account='".$_SESSION['useraccount']."'"; 
$res_checkstatus=mysql_query($query_checkStatus,$connection) or die(mysql_error());
$row_checkstatus = mysql_fetch_row($res_checkstatus);
$statusfrom = $row_checkstatus[0]; 
$statusto = $row_checkstatus[1];
echo"<input type='hidden' name='delivery_id' id='delivery_id' value='$delivery_id'>";



	$query="select deliverystatus_id, date_format(deliverystatus_date,'%d/%m/%Y'), status_id, username, remark, delivery_id 
			from delivery_deliverystatus,deliverystatus_setup
			where delivery_id = '$delivery_id' 
			and deliverystatus_setup.id =  status_id 
			order by deliverystatus_level
			";
//echo $query;		
$result=mysql_query($query,$connection) or die(mysql_error());
 

 
$i=0;

 


echo"<table onMouseOver=\"style.cursor='pointer'\"  border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:480px;'>";
echo"<tr height=25><td colspan=4><span class='style4'>
<font color=#1e87a5><b>Delivery Status</b></span></td></tr>";
echo"
	<col width=35>
	<col width=85>
	<col width=95>
	<col width=80>
	<col width=100>
	<col width=35>
	";
	
	 
	echo"<th ><span class='style4'></span></th>
		<th ><span class='style4'>Status</span></th>
		<th ><span class='style4'>Date</span></th>
		<th ><span class='style4'>Username</span></th>
		<th ><span class='style4'>Remark</span></th>
		<th ><span class='style4'></span></th>";
		
		echo"<input type='hidden' name='selectedtr_status' id='selectedtr_status' value='$selectedtr_status'>
		 <input type='hidden' name='selectedtrcolor_status' id='selectedtrcolor_status' value='$selectedtrcolor_status'>";

while($row=mysql_fetch_row($result)){
	
	
		if ($i%2==0)
		$bgcolor='#F5FAFF';
	else	
		$bgcolor='#FFFFFF';
	 
	 
		$deliverystatus_id[$i] = $row[0];
		$dd = 'deliverystatus_date'.$i;
		$$dd = $row[1];
		$status_id[$i] = $row[2];
		$remark[$i] = $row[4];
		$username[$i] = $row[3];
		
		 
			 
	
			$rownum = $i+1;
			echo"<tr height='20px' id='status$i' onclick=\"returncolor(this,'selectedtr_status','selectedtrcolor_status')\"  style='background-color:$bgcolor'>";
			
			echo"<td>";
			//<img src='img/delete.png' onmouseover=\"style.cursor='pointer'\" name='' title='Delete' onclick=\"deletenhmsremark('$row[0]','$row[4]')\">
			echo"</td>";
			
			
		echo"<td ><span class='style5'>";
			
			
			//<input type='text'  class='roundedtext_bonusdetail' style='width:500px' name='status_id[$i]' id='status_id[$i]' value='".$status_id[$i]."'>
			
			$query="select id,name,deliverystatus_level from deliverystatus_setup order by deliverystatus_level ";
			$resultstatus=mysql_query($query,$connection) or die(mysql_error());
				while($curf=mysql_fetch_row($resultstatus)){

					if ($status_id[$i]==$curf[0]){
						echo " $curf[1] ";
						$lastlevel=$curf[2];
						}
					
					}
			 
			
			echo"</span></td>";	
			
			$ClassE = 'grid_roundedtext1';
			$ClassI = 'grid_roundedtext2';
			echo'<td >';
			$dd = 'deliverystatus_date'.$i;
			echo"<input type='text'  class='roundeddate_attendcostcent'  style='width:70px' class='".$ClassE." date'   name='deliverystatus_date$i' id='deliverystatus_date$i'   value='".$$dd."'>";

			$name="deliverystatus_date".$i."";
			echo"
			<A HREF='#' onClick=\"displayCalendar(document.deliverystatus.".$name.",'dd/mm/yyyy',this);  return false; \" NAME=\"anchor1\" ID=\"anchor1\"><img src='./img/cal.gif' border=0 align=center></A>
";
			echo'</td>';
			
			//$username[$i] = $useraccount;
			echo"<td ><span class='style5'>
			<input type='text'  class='roundedtext_bonusdetail' style='width:70px' name='username[$i]'  readonly id='username[$i]' value='".$username[$i]."'>
			 
			</span></td>";	
			
		
			echo"<td ><span class='style5'>
			<input type='text'  class='roundedtext_bonusdetail' style='width:100px' name='remark[$i]' id='remark$i' value='".$remark[$i]."'>
			 
			</span></td>";	
			
			
			echo"<td><img src='img/confirm.png' onmouseover=\"style.cursor='pointer'\" name='' title='Save' onclick=\"submitnhmsremark('$delivery_id',$i)\"></td>";
			echo"</tr>";
			echo"<span class='style5'><input type='hidden'  name='deliverystatus_id[$i]' id='deliverystatus_id[$i]' value='".$deliverystatus_id[$i]."' ></span>";
			
			$i+=1;
			
		}

			echo"<tr height='20px' id='status$i' onclick=\"returncolor(this,'selectedtr_status','selectedtrcolor_status')\"  style='background-color:$bgcolor'>";
			
			echo"<td><img src='img/undo.png' onmouseover=\"style.cursor='pointer'\" name='' title='Clear' onclick=\"cleartdmsemployee('$i')\"></td>";
	
			echo"<td ><span class='style5'>";
			
			/* $querylog="SELECT ifnull(deliverystatus_level,0) deliverystatus_level from ".$_SESSION['dbLabel'].".users where User_account='".$_SESSION['useraccount']."' 	";

			$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
			while($rows=mysql_fetch_array($resultlog)){
				extract($rows);
			} */
			
			$query_checkStatus="select FI_DeliveryStatusFrom as statusfrom, FI_DeliveryStatusTo as statusto from ".$_SESSION['dbLabel'].".login 
				where User_account='".$_SESSION['useraccount']."' "; 
			$res_checkstatus=mysql_query($query_checkStatus,$connection) or die(mysql_error());
			$row_checkstatus = mysql_fetch_row($res_checkstatus);
			$statusfrom = $row_checkstatus[0]; 
			$statusto = $row_checkstatus[1];
		
			
	
			if($statusto > $lastlevel){
			//<input type='text'  class='roundedtext_bonusdetail' style='width:500px' name='status_id[$i]' id='status_id[$i]' value='".$status_id[$i]."'>
			$query="select id,name from deliverystatus_setup where deliverystatus_level>if('$lastlevel'='',-1,'$lastlevel') order by deliverystatus_level limit 1";
			//echo $query;
			echo"<select id='status_id[$i]' name='status_id[$i]'>";
			
			
			$resultstatus=mysql_query($query,$connection) or die(mysql_error());
				while($curf=mysql_fetch_row($resultstatus)){
				echo"<option ";
					if ($status_id[$i]==$curf[0])
						echo " selected ";
					echo" value='$curf[0]'>$curf[1] </option>";
					}
			 
			echo"</select>";
			echo"</span></td>";	
			
			$ClassE = 'grid_roundedtext1';
			$ClassI = 'grid_roundedtext2';
			echo'<td >';
			$dd = 'deliverystatus_date'.$i;
			if($$dd=='')
				$$dd = displayToday();
			echo"<input type='text'  class='roundeddate_attendcostcent'  style='width:70px' class='".$ClassE." date'   name='deliverystatus_date$i' id='deliverystatus_date$i'   value='".$$dd."'>";

			$name="deliverystatus_date".$i."";
			echo"
			<A HREF='#' onClick=\"displayCalendar(document.deliverystatus.".$name.",'dd/mm/yyyy',this);  return false; \" NAME=\"anchor1\" ID=\"anchor1\"><img src='./img/cal.gif' border=0 align=center></A>";
			echo'</td>';
			
			$username[$i] = $useraccount;
			echo"<td ><span class='style5'>
			<input type='text'  class='roundedtext_bonusdetail' style='width:70px' name='username[$i]'  readonly id='username[$i]' value='".$username[$i]."'>
			 
			</span></td>";	
			
		
			echo"<td ><span class='style5'>
			<input type='text'  class='roundedtext_bonusdetail' style='width:100px' name='remark[$i]' id='remark[$i]' value='".$remark[$i]."'>
			 
			</span></td>";	
			
		
			
			
			if($lastlevel>=($statusfrom-1) && $lastlevel <=$statusto  )
			echo"<td><img src='img/confirm.png' onmouseover=\"style.cursor='pointer'\" name='' title='Save' onclick=\"submitstatus('$i','i')\"></td>";
			echo"</tr>";
			echo"<span class='style5'><input type='hidden'  name='deliverystatus_id[$i]' id='deliverystatus_id[$i]' value='".$deliverystatus_id[$i]."' ></span>";
				}
echo"</table>";	


}	

function ShowDelivery($keyword,$keywordlot,$vers,$status,$connection,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	$Journal_DefaultCur = $arrayparams['Journal_DefaultCur'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	
	$txtJourDate = $arrayparams['txtJourDate'];
	
	
	$useraccount1 = $arrayparams['useraccount1'];
	$dbLabel = $arrayparams['dbLabel'];
	$Module_ProjectParent = $arrayparams['Module_ProjectParent'];
	$Module_CostCenterParent = $arrayparams['Module_CostCenterParent'];
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
		
	
$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$dbLabel.".loginstk on (($Module_ProjectParent=1 and project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%')) or ($Module_ProjectParent=0 and project.project_code = loginstk.project_code )) ".
			  " where user_account='".$useraccount1."' ".
			  " and (loginstk.project_code is not null and loginstk.project_code != '') ".
			  " order by project.project_code";
			  
$projectresult = mysql_query($queryproject,$connection) or die(mysql_error());
$projectexist  = mysql_num_rows($projectresult);		  

$querycostcent=" select distinct costcent.costcent_code, costcent_name ".
               " from costcent left join ".$dbLabel.".loginstk on (($Module_CostCenterParent=1 and costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%')) or ($Module_CostCenterParent=0 and costcent.costcent_code = loginstk.costcent_code )) ".
			   " where user_account='".$useraccount1."' ".
			   " and (loginstk.costcent_code is not null and loginstk.costcent_code != '') ".
			   " order by costcent.costcent_code";
//echo $querycostcent;
			   
$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
$costcentexist  = mysql_num_rows($costcentresult);		  

$queryledger= " select distinct ledger_number, ledger_include ".
              " from ".$dbLabel.".ledgerstk  ".
			  " where user_account='".$useraccount1."' ".
			  " and (ledger_number is not null and ledger_number != '') ".
			  " order by ledger_number";
$ledgerresult = mysql_query($queryledger,$connection) or die(mysql_error());
$ledgerexist  = mysql_num_rows($ledgerresult);	
$rows=mysql_fetch_array($ledgerresult) ;
$ledgerinclude = $rows[1];
if (empty($ledgerinclude)) $ledgerinclude = 0;

//Get Default Project
$querydflproject="select project_code as defaultproject from ".$dbLabel.".loginstk where user_account='".$useraccount1."' and default_project=1 ";
$resultdflproject=mysql_query($querydflproject,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflproject)){
	extract($rows);
}
	
//Get Default Coscenter
$querydflcostcent="select costcent_code as defaultcostcent from ".$dbLabel.".loginstk where user_account='".$useraccount1."' and default_costcenter=1 ";
$resultdflcostcent=mysql_query($querydflcostcent,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflcostcent)){
	extract($rows);
}

if ((!isset($PProjectCode)) && $Module_Project == 1)
	$PProjectCode = $defaultproject;
if ((!isset($PCostCentCode)) && $Module_CostCenter == 1)	
	$PCostCentCode = $defaultcostcent;	

	
	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($keyword == -1) $query = '';
	else {
	//Select Data From Delivery For Selected Record
	$query = " Select Distinct Deliveries.Delivery_ID, Delivery_Reference, Date_Format(Delivery_Date,'%d/%m/%Y') As Delivery_Date, ".
	         " Deliveries.Client_code As ClientCode, Clients.Ledger_name As ClientName,    ".
			 " Deliveries.Salesman_code As SalesmanCode, Salesmen.Ledger_name As SalesmanName, Delivery_Remark,Deliveries.USER_Account ".
			 " ,Delivery_Date as delivery_dateord, Deliveries.costcent_code ".
			 " From Deliveries As Deliveries ".
			 " Left Join Clients on Deliveries.Client_code = Clients.Ledger_Number ".
			 " Left Join ClientsDetail on Deliveries.Detail_Code = ClientsDetail.Detail_Code ".
			 " Left Join Salesmen on Deliveries.Salesman_code = Salesmen.Ledger_Number ".
			// " Left Join Journal on Deliveries.Journal_ID = Journal.Journal_ID ".
			 " Left Join delivery_deliverystatus on Deliveries.Delivery_id = delivery_deliverystatus.Delivery_id ";
if ($projectexist >	0)
  $query= $query." Inner join ( select distinct project_code from ".$dbLabel.".loginstk ".
							 "			   where loginstk.user_account='".$useraccount1."' ".
							 "             and (project_code is not null and project_code != '') ".
							 "			 ) Q1 on (($Module_ProjectParent=1 and Deliveries.project_code like concat(Q1.project_code,'%')) or ($Module_ProjectParent=0 and Deliveries.project_code = Q1.project_code ))  ";
							 
if ($costcentexist > 0)
  $query= $query." Inner join ( select distinct costcent_code from ".$dbLabel.".loginstk ".
							 "			   where loginstk.user_account='".$useraccount1."' ".
							 "             and (costcent_code is not null and costcent_code != '') ".
							 "			 ) Q2 on (($Module_CostCenterParent=1 and Deliveries.costcent_code like concat(Q2.costcent_code,'%')) or ($Module_CostCenterParent=0 and Deliveries.costcent_code = Q2.costcent_code )) ";							 
			


			$query=$query." Where 1=1    ";
	

if($ledgerinclude== 1 && $ledgerexist > 0) {
	$query = $query." And Exists ( select 1 from ".$dbLabel.".ledgerstk ".
								"              where ledgerstk.user_account='".$useraccount1."' ".
								"              and (ledger_number is not null and ledger_number != '') ".
								"              and Deliveries.Client_Code like concat(ledgerstk.ledger_number,'%') ".
								"            ) ";
}
if($ledgerinclude== 0 && $ledgerexist > 0){
	$query = $query." And Not Exists ( select 1 from ".$dbLabel.".ledgerstk ".
								"                  where ledgerstk.user_account='".$useraccount1."' ".
								"          	       and (ledger_number is not null and ledger_number != '') ".
								"                  and Deliveries.Client_Code like concat(ledgerstk.ledger_number,'%') ".
								"                ) ";	
}
	
 /* if($status=='')
	$query = $query." and ( delivery_deliverystatus.status_id is null
							)";  */
	$query = $query." and Deliveries.Journal_Type = 'DI' ";
if($vers==1 and $keyword!='')
	/*$query = $query." and ( Deliveries.delivery_reference like ('%$keyword%')  
							or  Deliveries.Client_code like ('%$keyword%')  
							or  Clients.Ledger_name  like ('%$keyword%')  
							or  Date_Format(Delivery_Date,'%d/%m/%Y')   like ('%$keyword%')
							)";
*/
     $query = $query." and ( Deliveries.delivery_reference = '$keyword'  
							
							)";

if($vers==2 and $keywordlot!='')
	$query = $query." and exists ( select 1 from delivery_lotnumber where lotnumber_number like ('%$keywordlot%')
									and deliverydt_id in (select deliverydt_id from deliverydt where delivery_id = Deliveries.delivery_id)
							)";
if(($vers==1 or $vers==3) and $status!='')
	$query = $query." and ( delivery_deliverystatus.status_id = $status and ifNull(deliverystatus_last,0) = 1
							)";

							
	$query = $query ."order by delivery_dateord desc ";
	}        
		//echo $query; 
		//exit;	 
			 
		echo"<table onMouseOver=\"style.cursor='pointer'\"  border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:720px;'>";
		echo"<col width=70>";
		echo"<col width=70>";
		echo"<col width=70>";
		echo"<col width=200>";
		echo"<col width=130>";
		echo"<col width=65>";
		echo"<col width=200>";
		echo"<col width=80>";
		echo"  <th align='center' ><span>Reference</span></th>";
		echo"  <th align='center' ><span>Date</span></th>";
		echo"  <th align='center' ><span>Client</span></th>";
		echo"  <th align='center' ><span>Client Name</span></th>";
		echo"  <th align='center' ><span>Salesmen</span></th>";
		echo"  <th align='center' ><span>User</span></th>";
		echo"  <th align='center' ><span>Remark</span></th>";
		echo"  <th align='center' ><span>Coscenter</span></th>";
	//echo $query;
	$i=0;
	if($query != '') {
	$result=mysql_query($query,$connection) or die(mysql_error());
    while ($row=mysql_fetch_array($result)){
	
	
	if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';

				
				
	echo"<tr height=20 id='$row[0]' class='$bgcolor table-focus-g1'  onclick=\"returncolor(this,'SelectedRec','SelectedRecColor','$row[0]');showrelationtodt('$row[0]')\" >";
	echo"<td>$row[1]</td>";
	echo"<td>$row[2]</td>";
	echo"<td>$row[3] </td>";
	echo"<td>$row[4]</td>";
	echo"<td>$row[6]</td>";
	echo"<td>$row[8]</td>";
	echo"<td>$row[7]</td>";
	echo"<td>$row[10]</td>";
	echo"</tr>";
	$i+=1;
	}
	}
	echo"</table>";
	

	
	

}

function Show_Delivery_Deliverylot($deliverydt_id,$connection,$arrayparams,$arrayaccess,$useraccount){


echo"<input type='hidden' name='deliverydt_id' id='deliverydt_id' value='$deliverydt_id'>";

$query="select item_code from deliverydt
			where deliverydt_id = '$deliverydt_id'";
$result=mysql_query($query,$connection) or die(mysql_error());
$row=mysql_fetch_row($result);
$item_code = $row[0];

$query="select lotnumber_id, lotnumber_qty, lotnumber_number, deliverydt_id from delivery_lotnumber
			where deliverydt_id = '$deliverydt_id'";
//echo $query;		
$result=mysql_query($query,$connection) or die(mysql_error());
 
$i=0; 
echo"<table onMouseOver=\"style.cursor='pointer'\"  border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:275px;'>";
echo"<tr height=25><td colspan=4><span class='style4'>
<font color=#1e87a5><b>Delivery Lot#</b>&nbsp;&nbsp;&nbsp;&nbsp;<img src='img/confirm.png' onmouseover=\"style.cursor='pointer'\" name='' title='Save' onclick=\"submitlot('$i','e')\"></span></td></tr>";
echo"
	<col width=35>
	<col width=200>
	<col width=100>
	<col width=35>
	";
	
	 
	echo"<th ><span class='style4'></span></th>
		<th ><span class='style4'>Lot #</span></th>
		<th ><span class='style4'>Qty</span></th>
		<th ><span class='style4'></span></th>";
		
		echo"<input type='hidden' name='selectedtr_lot' id='selectedtr_lot' value='$selectedtr_lot'>
		 <input type='hidden' name='selectedtrcolor_lot' id='selectedtrcolor_lot' value='$selectedtrcolor_lot'>";

while($row=mysql_fetch_row($result)){
	
	
		if ($i%2==0)
		$bgcolor='#F5FAFF';
	else	
		$bgcolor='#FFFFFF';
	 
	 
		$lotnumber_id[$i] = $row[0];
	 	$lotnumber_qty[$i] = $row[1];
		$lotnumber_number[$i] = $row[2];
	 
			 
	
			$rownum = $i+1;
			echo"<tr height='20px' id='lot$i' onclick=\"returncolor(this,'selectedtr_lot','selectedtrcolor_lot')\"  ondblclick=\"showwindow('$row[2]','$item_code')\"  style='background-color:$bgcolor'>";
			
			echo"<td>
			<img src='img/delete.png' onmouseover=\"style.cursor='pointer'\" name='' title='Delete' onclick=\"deletelot('$row[0]','$row[4]')\">";
			echo"</td>";
			
			echo"<td ><span class='style5'>
			<input type='text' style='width:199px' class='roundedtext_bonusdetail' style='width:70px' name='lotnumber_number[$i]'  id='lotnumber_number[$i]' value='".$lotnumber_number[$i]."'>
			 
			</span></td>";	
			
		
			echo"<td ><span class='style5'>
			<input type='text'  class='roundedtext_bonusdetail' style='width:100px' name='lotnumber_qty[$i]' id='lotnumber_qty[$i]' value='".$lotnumber_qty[$i]."'>
			 
			</span></td>";	
			
			
			//echo"<td><img src='img/confirm.png' onmouseover=\"style.cursor='pointer'\" name='' title='Save' onclick=\"submitlot('$i','e')\"></td>";//
			echo"</tr>";
			echo"<span class='style5'><input type='hidden'  name='lotnumber_id[$i]' id='lotnumber_id[$i]' value='".$lotnumber_id[$i]."' ></span>";
			
			$i+=1;
			
		}
		
			while($i<10 and ($deliverydt_id !='' and $deliverydt_id !='-1') ){
			echo"<tr height='20px' id='lot$i' onclick=\"returncolor(this,'selectedtr_lot','selectedtrcolor_lot')\"  style='background-color:$bgcolor'>";
			
			echo"<td><img src='img/undo.png' onmouseover=\"style.cursor='pointer'\" name='' title='Clear' onclick=\"cleartdmsemployee('$i')\"></td>";
	
			echo"<td ><span class='style5'>
			<input type='text'  style='width:199px'  class='roundedtext_bonusdetail' style='width:70px' name='lotnumber_number[$i]'   id='lotnumber_number[$i]' value='".$lotnumber_number[$i]."'>
			 
			</span></td>";	
			
		
			echo"<td ><span class='style5'>
			<input type='text'  class='roundedtext_bonusdetail' style='width:100px' name='lotnumber_qty[$i]' id='lotnumber_qty[$i]' value='".$lotnumber_qty[$i]."'>
			 
			</span></td>";	
			
		 		
			//echo"<td><img src='img/confirm.png' onmouseover=\"style.cursor='pointer'\" name='' title='Save' onclick=\"submitlot('$i','i')\"></td>";
			echo"<td></td>";
			echo"</tr>";
			echo"<span class='style5'><input type='hidden'  name='lotnumber_id[$i]' id='lotnumber_id[$i]' value='".$lotnumber_id[$i]."' ></span>";
			
			$numberofrecords=$i;
			if($numberofrecords<0)
				$numberofrecords=0;
			echo"<span class='style5'><input type='hidden'  name='numberofrecords' id='numberofrecords' value='".$numberofrecords."' ></span>";
			
			$i+=1;
			}
echo"</table>";	


}	


function GetReference($keyword,$vers,$statusfrom, $statusto,$connection,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	$Journal_DefaultCur = $arrayparams['Journal_DefaultCur'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	
	$txtJourDate = $arrayparams['txtJourDate'];
	
	
	$useraccount1 = $arrayparams['useraccount1'];
	$dbLabel = $arrayparams['dbLabel'];
	$Module_ProjectParent = $arrayparams['Module_ProjectParent'];
	$Module_CostCenterParent = $arrayparams['Module_CostCenterParent'];
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
		
	
$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$dbLabel.".loginstk on (($Module_ProjectParent=1 and project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%')) or ($Module_ProjectParent=0 and project.project_code = loginstk.project_code )) ".
			  " where user_account='".$useraccount1."' ".
			  " and (loginstk.project_code is not null and loginstk.project_code != '') ".
			  " order by project.project_code";
			  
$projectresult = mysql_query($queryproject,$connection) or die(mysql_error());
$projectexist  = mysql_num_rows($projectresult);		  

$querycostcent=" select distinct costcent.costcent_code, costcent_name ".
               " from costcent left join ".$dbLabel.".loginstk on (($Module_CostCenterParent=1 and costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%')) or ($Module_CostCenterParent=0 and costcent.costcent_code = loginstk.costcent_code )) ".
			   " where user_account='".$useraccount1."' ".
			   " and (loginstk.costcent_code is not null and loginstk.costcent_code != '') ".
			   " order by costcent.costcent_code";
//echo $querycostcent;
			   
$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
$costcentexist  = mysql_num_rows($costcentresult);		  

$queryledger= " select distinct ledger_number, ledger_include ".
              " from ".$dbLabel.".ledgerstk  ".
			  " where user_account='".$useraccount1."' ".
			  " and (ledger_number is not null and ledger_number != '') ".
			  " order by ledger_number";
$ledgerresult = mysql_query($queryledger,$connection) or die(mysql_error());
$ledgerexist  = mysql_num_rows($ledgerresult);	
$rows=mysql_fetch_array($ledgerresult) ;
$ledgerinclude = $rows[1];
if (empty($ledgerinclude)) $ledgerinclude = 0;

//Get Default Project
$querydflproject="select project_code as defaultproject from ".$dbLabel.".loginstk where user_account='".$useraccount1."' and default_project=1 ";
$resultdflproject=mysql_query($querydflproject,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflproject)){
	extract($rows);
}
	
//Get Default Coscenter
$querydflcostcent="select costcent_code as defaultcostcent from ".$dbLabel.".loginstk where user_account='".$useraccount1."' and default_costcenter=1 ";
$resultdflcostcent=mysql_query($querydflcostcent,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflcostcent)){
	extract($rows);
}

if ((!isset($PProjectCode)) && $Module_Project == 1)
	$PProjectCode = $defaultproject;
if ((!isset($PCostCentCode)) && $Module_CostCenter == 1)	
	$PCostCentCode = $defaultcostcent;	

	
	
	
	//Select Data From Delivery For Selected Record
	$query = " Select Distinct Deliveries.Delivery_ID, Delivery_Reference, Date_Format(Delivery_Date,'%d/%m/%Y') As Delivery_Date, ".
	         " Deliveries.Client_code As ClientCode, Clients.Ledger_name As ClientName,    ".
			 " Deliveries.Salesman_code As SalesmanCode, Salesmen.Ledger_name As SalesmanName, Delivery_Remark,Deliveries.USER_Account ".
			 " ,Delivery_Date as delivery_dateord ".
			 " From Deliveries As Deliveries ".
			 " Left Join Clients on Deliveries.Client_code = Clients.Ledger_Number ".
			 " Left Join ClientsDetail on Deliveries.Detail_Code = ClientsDetail.Detail_Code ".
			 " Left Join Salesmen on Deliveries.Salesman_code = Salesmen.Ledger_Number ".
			// " Left Join Journal on Deliveries.Journal_ID = Journal.Journal_ID ".
			 " Left Join delivery_deliverystatus on Deliveries.Delivery_id = delivery_deliverystatus.Delivery_id ";
if ($projectexist >	0)
  $query= $query." Inner join ( select distinct project_code from ".$dbLabel.".loginstk ".
							 "			   where loginstk.user_account='".$useraccount1."' ".
							 "             and (project_code is not null and project_code != '') ".
							 "			 ) Q1 on (($Module_ProjectParent=1 and Deliveries.project_code like concat(Q1.project_code,'%')) or ($Module_ProjectParent=0 and Deliveries.project_code = Q1.project_code ))  ";
							 
if ($costcentexist > 0)
  $query= $query." Inner join ( select distinct costcent_code from ".$dbLabel.".loginstk ".
							 "			   where loginstk.user_account='".$useraccount1."' ".
							 "             and (costcent_code is not null and costcent_code != '') ".
							 "			 ) Q2 on (($Module_CostCenterParent=1 and Deliveries.costcent_code like concat(Q2.costcent_code,'%')) or ($Module_CostCenterParent=0 and Deliveries.costcent_code = Q2.costcent_code )) ";							 
			


			$query=$query." Where 1=1    ";
	

if($ledgerinclude== 1 && $ledgerexist > 0) {
	$query = $query." And Exists ( select 1 from ".$dbLabel.".ledgerstk ".
								"              where ledgerstk.user_account='".$useraccount1."' ".
								"              and (ledger_number is not null and ledger_number != '') ".
								"              and Deliveries.Client_Code like concat(ledgerstk.ledger_number,'%') ".
								"            ) ";
}
if($ledgerinclude== 0 && $ledgerexist > 0){
	$query = $query." And Not Exists ( select 1 from ".$dbLabel.".ledgerstk ".
								"                  where ledgerstk.user_account='".$useraccount1."' ".
								"          	       and (ledger_number is not null and ledger_number != '') ".
								"                  and Deliveries.Client_Code like concat(ledgerstk.ledger_number,'%') ".
								"                ) ";	
}
	
	$query = $query." and Deliveries.Journal_Type = 'DI' ";
if($vers==1 and $keyword!='')
     $query = $query." and ( Deliveries.delivery_reference = '$keyword'  
							
							)";
if($statusfrom != '') $query = $query."and delivery_deliverystatus.status_id >= ".($statusfrom-1)." ";
if($statusto != '') $query = $query."and delivery_deliverystatus.status_id <= $statusto ";
		
	$query = $query ."order by delivery_dateord desc ";
	//echo $query; exit;
	$result=mysql_query($query,$connection) or die(mysql_error());
    $row=mysql_fetch_array($result);
	$deliveryid = $row[0];
	if($deliveryid !=''){
/* 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'>		
		
		 "; */
	return $deliveryid;
	
	} else return 0;
}

?>