<?php

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

if($_SESSION['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");
include("function.inc.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");

//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}


//Read From Module Table
$querymodule=" Select Module_AdditionalTaxes, ifNull(Module_NegativeQuantity,0) As Module_NegativeQuantity, ifNull(Module_rawmaterial,0) As Module_RawMaterial, ".
			 " ifNull(Module_DoubleUnit,0) As Module_DoubleUnit, ifNull(module_paymentterms,0) As Module_PaymentTerms, ".
             " ifNull(module_creditlimit,0) As Module_CreditLimit, ifNull(Module_Delivery,0) As Module_Delivery, ".
			 " ifNull(module_DeliveryWithSorder,0) As Module_DeliveryWithSorder, ifNull(Module_InvoiceCheckLimit,0) As Module_InvoiceCheckLimit, ".
			 " ifNull(Module_PostToJournal,0) As Module_PostToJournal, ifNull(Module_ReferenceByCostcenter,0) As Module_ReferenceByCostcenter, ".
			 " ifNull(Module_ReferenceByProject,0) As Module_ReferenceByProject,ifNull(Module_CreditLimitByProj,0) As Module_CreditLimitByProj,  ".
			 " ifNull(Module_SerialNo,0) As Module_SerialNo, ifNull(Module_Unit1Method,0) As Module_Unit1Method, ifNull(Module_ChangeInvoiceItems,0) As Module_ChangeInvoiceItems, ".
			 " ifNull(Module_ProjectDt,0) As Module_ProjectDt, ifNull(Module_CostCenterDt,0) As Module_CostCenterDt, ".
			 " ifNull(Module_ExpireDate,0) As Module_ExpireDate ".
             " From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

 

$Parameters = array();
$Parameters['FI_SalesCheckSalesman'] = $FI_SalesCheckSalesman;
$Parameters['FI_DeliverySalesPrice'] = $FI_DeliverySalesPrice;
$Parameters['Login_NegativeQty'] = $Login_NegativeQty;
$Parameters['Login_ModifySellingPrice'] = $Login_ModifySellingPrice;
$Parameters['Login_ModifyDiscount'] = $Login_ModifyDiscount;
$Parameters['Login_MinAllowCostPer'] = $Login_MinAllowCostPer;
$Parameters['Login_MaxAllowCostPer'] = $Login_MaxAllowCostPer;
$Parameters['Module_Delivery'] = $Module_Delivery;
$Parameters['Module_NegativeQuantity'] = $Module_NegativeQuantity;
$Parameters['Module_Unit1Method'] = $Module_Unit1Method;
$Parameters['Module_Pack'] = $Module_Pack;
$Parameters['Module_DoubleUnit'] = $Module_DoubleUnit;
$Parameters['Module_Project'] = $Module_Project;
$Parameters['Module_CostCenter'] = $Module_CostCenter;
$Parameters['Module_ProjectDt'] = $Module_ProjectDt;
$Parameters['Module_CostCenterDt'] = $Module_CostCenterDt;
$Parameters['Module_RestrictProjCost'] = $Module_RestrictProjCost;
$Parameters['Module_RestrictCost'] = $Module_RestrictCost;
$Parameters['ShowCostCentLayout'] = $ShowCostCentLayout;
$Parameters['Module_SerialNo'] = $Module_SerialNo;
$Parameters['Module_ExpireDate'] = $Module_ExpireDate;
$Parameters['Module_DeliveryClosingQty'] = $Module_DeliveryClosingQty;
$Parameters['Module_DeliveryWithSorder'] = $Module_DeliveryWithSorder;
$Parameters['Sale_MaxExceedQty'] = $Sale_MaxExceedQty;
$Parameters['SalesTTC'] = $SalesTTC;
$Parameters['DecimalBase'] = $DecimalBase;
$Parameters['Setup_VatPercentage'] = $Setup_VatPercentage;
$Parameters['Module_AdditionalTaxes'] = $Module_AdditionalTaxes;
$Parameters['ExportTransportToJV'] = $ExportTransportToJV;
$Parameters['ExportBonusToJV'] = $ExportBonusToJV;
$Parameters['VatApplied'] = $VatApplied;
$Parameters['AllowModifyVAT'] = $AllowModifyVAT;

$Parameters['SetupDftWareHouse'] = $SetupDftWareHouse;
$Parameters['defaultwarehouse']  = $defaultwarehouse;

$Parameters['txtDate'] 		   = $txtDate;
$Parameters['txtClientCode']   = $txtClientCode;
$Parameters['txtClientDetail'] = $txtClientDetail;
$Parameters['txtCurrCode']     = $txtCurr;
$Parameters['Base1']    	   = $base1;
$Parameters['Base2']    	   = $base2;
$Parameters['useraccount']    	   =  $_SESSION['username'];


$Parameters['dbLabel']    	   =  $HTTP_SESSION_VARS['dbLabel'];
$Parameters['useraccount1']    	   =  $HTTP_SESSION_VARS['useraccount'];
$Parameters['Module_CostCenterParent']    	   =  $Module_CostCenterParent;
$Parameters['Module_ProjectParent']    	   =  $Module_ProjectParent;



$_SESSION['gdl_parameters']  = $Parameters;



if($actionmode=='showrelation'){

$query = "select 1 from production where Item_Code = '$itemcode' ";

//$query=$query."and Production_BatchNo = '$lotnum'";


$result=mysql_query($query,$connection) or die(mysql_error());
$row=mysql_fetch_array($result);
$has_production = $row[0];


if($has_production==1) {
	echo "production";
	echo"[BRK]";
	ShowProductionDtGrid($itemcode,$lotnum,$connection,$_SESSION['gass_parameters'],$_SESSION['gass_arrayaccess']);
	
	}
else {
	echo "purchase";
	echo"[BRK]";
	ShowPurchasesDtGrid($itemcode,$lotnum,$connection,$_SESSION['gass_parameters'],$_SESSION['gass_arrayaccess']);
}	




exit;
}



function ShowProductionDtGrid($itemcode,$lotnum,$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 ProductionDt.ProductionDt_ID, ProductionDt.Item_CodePacked, ProductionDt.Item_code, Item_Description, ProductionDt.Warehouse_Code, ".
					 " ProductionDt_Remark, ProductionDt_Quantity, ProductionDt_IdealQty,ProductionDt_BatchNo, Round(ProductionDt_Cost, 2), ProductionDt_CostBase1, ProductionDt_CostBase2, ".
					 " ifNull(ProductionDt_IdealQty,0) - ifNull(ProductionDt_Quantity,0) As ProductionDtWasteCalc, ProductionDt_Waste, ".
					 " Round((ProductionDt_Quantity*ProductionDt_Cost), 2) As TotalActual, ".
					 " Round((ProductionDt_IdealQty*ProductionDt_Cost), 2) As TotalIdeal, ".
					 " ifNull(item_withserialno,0) As ItemWithSerialNo, 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,date_format(Production.Production_Date,'%d/%m/%Y'),Production_Reference ".
					 " From ProductionDt As ProductionDt Left Join Items on ProductionDt.Item_code = Items.Item_code ".
					 " Inner Join Production on Production.Production_ID = Productiondt.Production_ID ".
					 $query = $query . " WHERE 1=1 ";
					 $query = $query . " And Production.Production_BatchNo = '".$lotnum."' ";
					 $query = $query . " and Production.Item_Code = '$itemcode' ";
					 $query = $query . " Group By  ProductionDt.ProductionDt_ID ";
					 $query = $query . " Order By Production.Production_Date,Production_Reference,item_code  ";
					 			 
	        // 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:670px;'>";
		echo"<col width=90>";
		echo"<col width=90>";
		echo"<col width=90>";
		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>Date</span></th>";
		echo"  <th align='center' ><span>Reference</span></th>";
		echo"  <th align='center' ><span>Batch No.</span></th>";
		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]');showrelation('".$row[2]."','".$row[8]."')\" 
					 
					
					>";
	echo"<td>$row[23]</td>";
	echo"<td>$row[24]</td>";
	echo"<td>$row[8]</td>";
	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 ShowPurchasesDtGrid($itemcode,$lotnum,$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 PurchasesDt.PurchaseDt_ID, PurchasesDt.Item_CodePacked, PurchasesDt.Item_code, Item_Description, PurchasesDt.Warehouse_Code, ".
					 " Purchasedt_Remark, PurchaseDt_Quantity, PurchaseDt_Volume, Round(PurchaseDt_Price, 2), PurchaseDt_PriceBase1, PurchaseDt_PriceBase2, ".
					 " Round(PurchaseDt_DiscountPer, 2), PurchaseDt_ChargePer, PurchaseDt_ChargesAmnt, PurchaseDt_ChargesAmntB1, PurchaseDt_ChargesAmntB2,  ".
					 " Round(((PurchaseDt_Quantity*PurchaseDt_Price)-((PurchaseDt_Quantity*PurchaseDt_Price*ifNull(PurchaseDt_DiscountPer,0))/100)), 2) As Total, ".
					 " Round(PurchaseDt_PriceTTC, 2), ".
					 " Round(((PurchaseDt_Quantity*PurchaseDt_PriceTTC)-((PurchaseDt_Quantity*PurchaseDt_PriceTTC*ifNull(PurchaseDt_DiscountPer,0))/100)), 2) As TotalTTC, ".
					 " PurchasesDt.Item_Unit2Qty, Item_UnitPurchaseDesc, Item_UnitSalesDesc, PurchasesDt.Project_code, PurchasesDt.CostCent_Code,  ".
					 " PorderDt.PorderDt_Quantity, Porder.Porder_reference, PurchasesDt.RelatedDt_Id, ifNull(item_withserialno,0) As ItemWithSerialNo,   ".
					 " Date_Format(PurchaseDt_ExpireDate,'%d/%m/%Y') As PurchaseDt_ExpireDate, PurchaseDt_BatchNo,date_format(Purchases.Purchase_date,'%d/%m/%Y'),Purchase_Reference ".
					 " From PurchasesDt As PurchasesDt Left Join Items on PurchasesDt.Item_code = Items.Item_code ".			 
					 " Left Join PorderDt on PorderDt.PorderDt_ID = PurchasesDt.RelatedDt_Id  ".
					 " Left Join Porder on Porder.Porder_ID = PorderDt.Porder_ID  ".
					 " Left Join Purchases on Purchases.Purchase_ID = PurchasesDt.Purchase_ID  ".
					 " Where PurchasesDt.Item_Code = '".$itemcode."' ";
					$query = $query ."	 and PurchasesDt.PurchaseDt_BatchNo = '".$lotnum."'".
								" Order By PurchasesDt.item_Code ";
					 			 
	         
			// 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=90>";
		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>Date</span></th>";
		echo"  <th align='center' ><span>Reference</span></th>";
		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]');showrelation('".$row[2]."','".$lotnum."')\" 
					 
					
					>";
	echo"<td>$row[30]</td>";
	echo"<td>$row[31]</td>";
	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>";
	

	
	

}

?>