<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<?php

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


print <<< HERE
<HTML>
<HEAD>
<TITLE> List of Production Request </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">

<style type="text/css">
table.params td { font-size: 12px; 
                  font-family:Times-New-Roman;
                 }

table.master th { font-size: 14px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                      }				 
table.master td { font-size: 13px; 
                  font-family:Times-New-Roman;
                 }
				 
table.master1 th { font-size: 14px; 
                   font-weight: bold;
                   font-family:Times-New-Roman;
                 }				 
table.master1 td { font-size: 13px; 
				   font-weight: bold;
                   font-family:Times-New-Roman;
                 }				 

table.detail th { font-size: 12px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                 }				 			   
table.detail td { font-size: 11px; 
				  font-weight: normal;
		          font-family:Times-New-Roman;
                 }			   
				 
.totals { font-size: 12px; 
          font-weight: bold;
          font-family:Times-New-Roman;
        }					  
				 
h4 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 2px;
   }
h5 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 1px;
   }   
   
</style>




</HEAD>

HERE;



include("config.inc.php");
include 'function.inc.php';

//Set Time Execution For the Report
ini_set('max_execution_time', $config['ReportTimeExecution']);

$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");

$querylog="SELECT print as printaccess from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')	";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_Grn,0) As Module_Grn, ifNull(Module_Delivery,0) As Module_Delivery
				, ifNull(Module_Project,0) As Module_Project, ifNull(Module_Costcenter,0) As Module_Costcenter ".
				" ,ifNull(Module_Heatno,0) As Module_Heatno, ifNull(Module_expiredate,0) As Module_expiredate,
				ifNull(Module_NewProduction,0) As Module_NewProduction	
				From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

if ($printaccess==0 or $printaccess=='')
echo'<BODY oncontextmenu="return false;">';
else
echo'<BODY>';


$querysetup="select grid_color1,grid_color2, DecimalBase, DecimalBase1, DecimalBase2, Base1, Base2,report_color1, report_color2, Company_Name As CompanyName from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultsetup)){
  extract($rows);
}

$queryPurchaseSetup="Select PurchaseTTC From PurchaseSetup";
$resultPurchaseSetup=mysql_query($queryPurchaseSetup,$connection) or die(mysql_error());
while($rowPurchaseSetup=mysql_fetch_array($resultPurchaseSetup)){
	extract($rowPurchaseSetup);
}
$querylog=" select ifNull(FI_RepRestrictProject,0) As FI_RepRestrictProject, ifNull(FI_RepRestrictCostcenter,0) As FI_RepRestrictCostcenter, ".
		  " ifNull(FI_RepRestrictWarehouse,0) As FI_RepRestrictWarehouse, ifNull(FI_ShowCostPrice,0) As FI_ShowCostPrice  ".
          " from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."') ";
$resultlog=mysql_query($querylog) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$_SESSION['dbLabel'].".loginstk on project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%') ".
			  " where user_account='".$_SESSION['useraccount']."' ".
			  " 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 ".$_SESSION['dbLabel'].".loginstk on costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%') ".
			   " where user_account='".$_SESSION['useraccount']."' ".
			   " and (loginstk.costcent_code is not null and loginstk.costcent_code != '') ".
			   " order by costcent.costcent_code";
$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
$costcentexist  = mysql_num_rows($costcentresult);		

$querywh=" select distinct warehouse.warehouse_code, warehouse_name ".
         " from warehouse where warehouse_code in ( select warehouse_code from ".$_SESSION['dbLabel'].".loginstk ".
		 "                                          where user_account='".$_SESSION['useraccount']."' ".
		 "                                          and (loginstk.warehouse_code is not null and loginstk.warehouse_code != '') ) ".
		 " order by warehouse.warehouse_code";
$resultwh = mysql_query($querywh,$connection) or die(mysql_error());
$whexist  = mysql_num_rows($resultwh);


$querygroup=" select distinct group_code,group_name ".
			" from grouping where group_code in (select group_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."') ".
			" order by group_code";
$resultgroup = mysql_query($querygroup,$connection) or die(mysql_error());
$groupexist  = mysql_num_rows($resultgroup);

$now=current_server_time($connection);
$dt=split(" ",$now[0]);
$printdate=split("-",$dt[0]);

print <<< HERE
<table width="100%" border="0" align="center">
<tr><td>
<table width='100%' border='0' align='left' style='table-layout:fixed'>
HERE;

if ($prtheader == 1){
	ShowReportHeader('List of Productions Request','portrait', $connection);
} else {
	ShowReportTitle('List of Productions Request','portrait', $connection);
}

echo $printdate[2]."/".$printdate[1]."/".$printdate[0];
//echo DATE("d"."/"."m"."/"."Y");
echo "<br>".$dt[1]." ".$dt[2];

if (empty($curr))  $curr = $Base1;


$dispfromdate = $fromdate;
$disptodate   = $todate;

$dispfromedate = $fromedate;
$disptoedate   = $toedate;

$dispcurr     = $curr;

  

if (empty($fromitem))  $fromitem = 'null';
else  $fromitem = "'".$fromitem."'";   
if (empty($toitem))  $toitem = 'null';
else  $toitem = "'".$toitem."'";      

if (empty($fromheatno))  $fromheatno = 'null';
else  $fromheatno = "'".$fromheatno."'";   
if (empty($toheatno))  $toheatno = 'null';
else  $toheatno = "'".$toheatno."'";      

if (empty($fromitemRM))  $fromitemRM = 'null';
else  $fromitemRM = "'".$fromitemRM."'";   
if (empty($toitemRM))  $toitemRM = 'null';
else  $toitemRM = "'".$toitemRM."'"; 

if (empty($fromproject))  $fromproject = 'null';
else  $fromproject = "'".$fromproject."'";    
if (empty($toproject))  $toproject = 'null';
else  $toproject = "'".$toproject."'";       

if (empty($fromcostcent))  $fromcostcent = 'null';
else  $fromcostcent = "'".$fromcostcent."'";    
if (empty($tocostcent))  $tocostcent = 'null';
else  $tocostcent = "'".$tocostcent."'";       

if (empty($fromgroup))  $fromgroup = 'null';
else  $fromgroup = "'".$fromgroup."'";    
if (empty($togroup))  $togroup = 'null';
else  $togroup = "'".$togroup."'";  

if (empty($fromsubgroup))  $fromsubgroup = 'null';
else  $fromsubgroup = "'".$fromsubgroup."'";    
if (empty($tosubgroup))  $tosubgroup = 'null';
else  $tosubgroup = "'".$tosubgroup."'";  

if (empty($fromcategory))  $fromcategory = 'null';
else  $fromcategory = "'".$fromcategory."'";    
if (empty($tocategory))  $tocategory = 'null';
else  $tocategory = "'".$tocategory."'";  

$fromwarehouse1=$fromwarehouse;
   




if (empty($fromwarehouse))  $fromwarehouse = 'null';
else  $fromwarehouse = "'".$fromwarehouse."'";      

$query=" select  warehouse_name ".
         " from warehouse where warehouse_code = $fromwarehouse" ;
		 
$result = mysql_query($query,$connection) or die(mysql_error());
$rows=mysql_fetch_row($result);
$warehousename = $rows[0];

if (!empty($fromdate))
   $fromdate = explodeword($fromdate);
if (!empty($todate))
   $todate = explodeword($todate);  

if (!empty($fromedate))
   $fromdate = explodeword($fromedate);
if (!empty($toedate))
   $todate = explodeword($toedate);  

if (empty($fromdate))  $fromdate = 'null';
else  $fromdate = "'".$fromdate."'"; 
if (empty($todate))  $todate = 'null';
else  $todate = "'".$todate."'";

if (empty($fromedate))  $fromedate = 'null';
else  $fromedate = "'".$fromedate."'"; 
if (empty($toedate))  $toedate = 'null';
else  $toedate = "'".$toedate."'";

if (empty($showdetail))  $showdetail = 0;
if (empty($showlastprice))  $showlastprice = 0;   

if (empty($showlastprice)) $showlastprice = 0;

$showlastprice = 0; 

print <<<HERE
</font></td>
</tr>
<tr height='4'><td colspan=5><h5></td></tr>
</table>
</td></tr>

<tr><td>
<table class='params' width='100%' border='0' align='left' style='table-layout:fixed' >
<tr>
  <td width="12%"><b>Date From/To:</b></td>
  <td width="18%">$dispfromdate - $disptodate </td>

HERE;
 
 if($Module_expiredate==1 && $edatechk == 1)
echo "  <td width='10%' valign='top'><b>Expire Date From/To:</b></td><td width='12%'>$dispfromedate - $disptoedate</td>";
  
 echo "  <td width='8%' valign='top'><b>Currency:</b></td>
  <td width='8%'>$dispcurr</td>";




  
  if($fromwarehouse1!='')
  echo"<td width='32%' valign='top'><b>Warehouse:</b> $fromwarehouse1 - $warehousename</td>";
  else  echo"<td width='32%' valign='top'></td>";
  
print <<<HERE

</tr>
<tr height='5'><td colspan=7><h4></td></tr>
</table>
</td></tr>

<tr><td>
HERE;

if ($showdetail == 1) $masterclass = 'master1';
else $masterclass = 'master';

echo "<table width='100%' border='0' align='left' cellpadding='0' cellspacing='0' class=$masterclass style='table-layout:fixed'>
	  <col width=70>
	  <col width=70>
	  <col width=100>
	  <col width=250>
	  <col width=60>";
if($Module_NewProduction == 1)  
echo "<col width=60>
	  <col width=60>
	  <col width=60>
	  <col width=60>
	  <col width=60>";	
echo "<col width=60>
	  <col width=60>
	  <col width=80>
	  <col width=100>";


echo"  <tr>
			<th >Date</th>
			<th >Reference</th>
			<th colspan='2' align='left'>Item Code / Description</th>
			<th >Qty</th>";

//if($Module_NewProduction == 1)  
//	echo "<th>Actual </th>";
if ($showlastprice == 1) {
	echo"  	<th >Currency</th>
			<th >Cost Per Unit </th>
			<th >Total Cost </th>";
} else {
	echo"  <th colspan='3'>&nbsp;</th>";

}		  
echo "</tr>";
if($Module_NewProduction == 1)
	$colspan = 13;	
else $colspan = 8;	
		  
if ($showdetail == 1) {
	echo"<tr height='5'><td colspan='$colspan'><h5></td></tr>";
	echo"<tr height='20'><td colspan='".($colspan+1)."'>";
	echo "<table width='100%' border='0' align='left' cellpadding='0' cellspacing='0' class='detail' style='table-layout:fixed'>
			<col width=20>
			<col width=100>
			<col width=360>
			<col width=80>
			<col width=80>
			<col width=60>
			<col width=80>
			<col width=100>
			<tr>
				<th >&nbsp;</th>
				<th >Item Code</th>
				<th align='left'>Description</th>";
				
			
			echo"<th >Warehouse</th>";
			
if($Module_Heatno==1 and $vShowHeatno==1)
			echo"<th >HeatNo</th>";	
			
				echo"<th >Qty</th>";


			
if ($showlastprice == 1) {
			echo"<th >Cost Price </th>";
			echo"<th >Total Cost</th>";
		}	
else if ($showlastprice == 1) {
			echo"<th >Last Price </th>";
			echo"<th >Total Last Price</th>";
		}		
		else
			echo"<th colspan='2'>&nbsp; </th>";
			
	echo"	</tr>";
		
	echo"</table>";
	echo"</td></tr>";		
}

echo"<tr height='5'><td colspan=$colspan><h4></td></tr>";

		 


$query = '';

$QueryRestrictPrj = " Inner join ( select distinct project_code from ".$_SESSION['dbLabel'].".loginstk ".
					" 			   where loginstk.user_account='".$_SESSION['useraccount']."' ".
					"              and (project_code is not null and project_code != '') ".
					"			 ) Q4  ";
					
$QueryRestrictCst = " Inner join ( select distinct costcent_code from ".$_SESSION['dbLabel'].".loginstk ".
					"			   where loginstk.user_account='".$_SESSION['useraccount']."' ".
					"              and (costcent_code is not null and costcent_code != '') ".
					"			 ) Q5  ";
	



$query = " Select Production.Production_ID as ProductionID, Production_Reference as ProductionReference, Date_Format(Production_Date,'%d/%m/%Y') As ProductionDate, ".
		 " Production.Item_code As ItemCode, Item_Description as ItemDescription, Production.Warehouse_Code as WarehouseCode, Warehouse_Name as WarehouseName, ".
		 " ifNull(Production_Quantity,1) as ProductionQuantity,  Production_IdealQty as ProductionIdealQty, Production.Currency_Code as CurrencyCode, ".
		 " Round(ifNull(Production_TotalBase1,0), ".$DecimalBase.") As ProductionTotalBase1, ".
		 " Round(ifNull(Production_TotalBase2,0), ".$DecimalBase.") As ProductionTotalBase2, ".
		 " Round(ifNull(Production_TotalBase1,0)/ifNull(Production_Quantity,1), ".$DecimalBase.") As CostPerUnitBase1, ".
		 " Round(ifNull(Production_TotalBase2,0)/ifNull(Production_Quantity,1), ".$DecimalBase.") As CostPerUnitBase2, ".
		 " 'PD' as ProdType, Null as ProductionReqFGId, Production_Date as OrderProductionDate, Null, Production.project_code, date_format(Production_ExpireDate,'%d-%m-%Y') as ExpireDate ".
		 " , round(ifnull(initialqty,0),2) as initialqty, 0 as damageqty, 0 as remixqty ".
		 " From Production ".
		 " Inner Join Items on Production.Item_code = Items.Item_code ".
		 " Left Join Warehouse on Production.Warehouse_Code = Warehouse.Warehouse_Code ".
		 " Left Join grouping on grouping.Group_Code = Items.Group_code ".
		 " Left Join subgroup on subgroup.Group_Code = Items.SubGroup_Code ".
		 " Left Join category on category.Category_Code = Items.Category_code ".
		 " left join (select production_id,sum(productiondt_initialQty) as initialqty
			from productiondt
			group by production_id ) as pdt on pdt.production_id =Production.production_id ";
		 
if ($projectexist >	0 && $FI_RepRestrictProject == 1)
	$query= $query.$QueryRestrictPrj." on Production.project_code like concat(Q4.project_code,'%') ";
							 
if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1)
	$query= $query.$QueryRestrictCst." on Production.Costcenter_Code like concat(Q5.costcent_code,'%') ";		 
		 

$query = $query.  " Where Items.Item_Code Between ifNull(".$fromitem.", Items.Item_Code) and ifNull(". $toitem.", Items.Item_Code) ".
				  " And ifNull(Production.Project_Code,'-1') between ifNull(".$fromproject.",ifNull(Production.Project_Code,'-1')) and ifNull(". $toproject.",ifNull(Production.Project_Code,'-1')) ".
				  " And ifNull(Production.Costcenter_Code,'-1') between ifNull(".$fromcostcent.",ifNull(Production.Costcenter_Code,'-1')) and ifNull(". $tocostcent.",ifNull(Production.Costcenter_Code,'-1')) ".
				  " And ifNull(Production.Warehouse_Code,'-1') = ifNull(". $fromwarehouse .", ifNull(Production.Warehouse_Code,'-1')) ".
				  " And Date_format(Production.Production_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(Production.Production_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(Production.Production_Date,'%Y-%m-%d'))  ".
				  " And ifNull(Items.Group_code,'-1') between ifNull(".$fromgroup.",ifNull(Items.Group_code,'-1')) and ifNull(". $togroup.",ifNull(Items.Group_code,'-1')) ".
				  " And ifNull(Items.SubGroup_Code,'-1') between ifNull(".$fromsubgroup.",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1')) ".
				  " And ifNull(Items.Category_code,'-1') between ifNull(".$fromcategory.",ifNull(Items.Category_code,'-1')) and ifNull(". $fromcategory.",ifNull(Items.Category_code,'-1')) ";
				  
if($Module_expiredate==1 && $edatechk == 1)
	$query=$query." and date_format(Production_ExpireDate,'%Y-%m-%d') between ifNull(". $fromedate.",date_format(Production_ExpireDate,'%Y-%m-%d')) and ifNull(". $toedate.",date_format(Production_ExpireDate,'%Y-%m-%d')) ";
				  
					  
if ($whexist > 0 && $FI_RepRestrictWarehouse == 1)
	$query = $query." And Production.warehouse_code In (select distinct warehouse_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."')  ";  
if ($groupexist > 0)
	$query = $query." And Items.group_code In (select distinct group_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."')  ";  
  
$query = $query." Union ";
$query = $query." Select productionRequest.ProductionReq_Id as ProductionID, ProductionReq_Reference as ProductionReference, Date_Format(productionRequest.ProductionReq_Date,'%d/%m/%Y') As ProductionDate, ".
				" productionReqFG.Item_Code As ItemCode, Item_Description as ItemDescription, productionReqFG.Warehouse_Code as WarehouseCode, Warehouse_Name as WarehouseName, ".
				" ifNull(ProductionReqFG_Quantity,1) as ProductionQuantity,  0 as ProductionIdealQty, '".$Base1."' as CurrencyCode, ".
				" Round(ifNull(0,0)*ifNull(ProductionReqFG_Quantity,1), ".$DecimalBase.") As ProductionTotalBase1, ".
				" Round(ifNull(0,0)*ifNull(ProductionReqFG_Quantity,1), ".$DecimalBase.") As ProductionTotalBase2, ".
				" Round(ifNull(0,0), ".$DecimalBase.") As CostPerUnitBase1, ".
				" Round(ifNull(0,0), ".$DecimalBase.") As CostPerUnitBase2, ".
				" 'PDN' as ProdType, productionReqFG.ProductionReqFG_Id as ProductionReqFGId, productionRequest.ProductionReq_Date as OrderProductionDate, '', '', date_format(productionRequest.ProductionReq_Date,'%d-%m-%Y') as ExpireDate, ".
				" ProductionReqFG_Quantity as initialqty, 0 as damageqty, 0 as remixqty ".
				" From productionReqFG ".
				" Inner Join productionRequest on productionReqFG.ProductionReq_Id = productionRequest.ProductionReq_Id ".
				" Inner Join Items on productionReqFG.Item_code = Items.Item_code ".
				" Left Join Warehouse on productionReqFG.Warehouse_Code = Warehouse.Warehouse_Code ".
				" Left Join grouping on grouping.Group_Code = Items.Group_code ".
				" Left Join subgroup on subgroup.Group_Code = Items.SubGroup_Code ".
				" Left Join category on category.Category_Code = Items.Category_code ";
		 
/*if ($projectexist >	0 && $FI_RepRestrictProject == 1)
	$query= $query.$QueryRestrictPrj." on ProductionHD.project_code like concat(Q4.project_code,'%') ";
							 
if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1)
	$query= $query.$QueryRestrictCst." on ProductionHD.Costcenter_Code like concat(Q5.costcent_code,'%') ";		 
*/		 

$query = $query.  " Where Items.Item_Code Between ifNull(".$fromitem.", Items.Item_Code) and ifNull(". $toitem.", Items.Item_Code) ".
				 //	And IfNull(ProductionFG.productionfg_heatno,'-1') Between ifNull(".$fromheatno.", IfNull(ProductionFG.productionfg_heatno,'-1')) and ifNull(". $toheatno.", IfNull(ProductionFG.productionfg_heatno,'-1'))".
				 // " And ifNull(ProductionHD.Project_Code,'-1') between ifNull(".$fromproject.",ifNull(ProductionHD.Project_Code,'-1')) and ifNull(". $toproject.",ifNull(ProductionHD.Project_Code,'-1')) ".
				 // " And ifNull(ProductionHD.Costcenter_Code,'-1') between ifNull(".$fromcostcent.",ifNull(ProductionHD.Costcenter_Code,'-1')) and ifNull(". $tocostcent.",ifNull(ProductionHD.Costcenter_Code,'-1')) ".
				  " And ifNull(ProductionReqFG.Warehouse_Code,'-1') = ifNull(". $fromwarehouse .", ifNull(ProductionReqFG.Warehouse_Code,'-1')) ".
				  " And Date_format(ProductionRequest.ProductionReq_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(ProductionRequest.ProductionReq_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(ProductionRequest.ProductionReq_Date,'%Y-%m-%d'))  ".
				  " And ifNull(Items.Group_code,'-1') between ifNull(".$fromgroup.",ifNull(Items.Group_code,'-1')) and ifNull(". $togroup.",ifNull(Items.Group_code,'-1')) ".
				  " And ifNull(Items.SubGroup_Code,'-1') between ifNull(".$fromsubgroup.",ifNull(Items.SubGroup_Code,'-1')) and ifNull(". $tosubgroup.",ifNull(Items.SubGroup_Code,'-1')) ".
				  " And ifNull(Items.Category_code,'-1') between ifNull(".$fromcategory.",ifNull(Items.Category_code,'-1')) and ifNull(". $fromcategory.",ifNull(Items.Category_code,'-1')) ";
				  
//if($Module_expiredate==1 && $edatechk == 1)
//	$query=$query." and date_format(ProductionFG_ExpireDate,'%Y-%m-%d') between ifNull(". $fromedate.",date_format(ProductionFG_ExpireDate,'%Y-%m-%d')) and ifNull(". $toedate.",date_format(ProductionFG_ExpireDate,'%Y-%m-%d')) ";
					  
	
if ($whexist > 0 && $FI_RepRestrictWarehouse == 1)
	$query = $query." And ProductionFG.warehouse_code In (select distinct warehouse_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."')  ";  
if ($groupexist > 0)
	$query = $query." And Items.group_code In (select distinct group_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."')  ";  
 
 if($Module_expiredate==1 && $edatechk == 1 && $grpedate == 1) 
	 $query = $query.  " Order By ExpireDate, OrderProductionDate, ProductionReference ";

 else $query = $query.  " Order By OrderProductionDate, ProductionReference ";
		 


//echo $query; 



$TotalAmount  = 0;
$ProductionUnitCost     = 0;
$ProductionTotalCost    = 0;
$ProductionDtUnitCost   = 0;
$ProductionDtTotalCost  = 0;

$prevExpdate = '';
$col=0;
$result=mysql_query($query,$connection) or die(mysql_error());
while($rows=mysql_fetch_row($result)){
	
		if($col%2==0){
			$color = "style='background-color:".$report_color1."'";
		}else{
			$color = "style='background-color:".$report_color2."'";
		}
	
		
	if($curr == '' or $curr == $Base1) {
		$ProductionUnitCost  = $rows[12];
		$ProductionTotalCost = $rows[10];
	} else {
		$ProductionUnitCost  = $rows[13];
		$ProductionTotalCost = $rows[11];
	}	
		
	if($Module_expiredate==1 && $grpedate == 1 && $prevExpdate != $rows[19]){
		echo "<tr height='20px'>";
		echo "	<td align='center' colspan='2' valign='top' ><b><u>Expire Date: ". $rows[19] ."</b></u></td>";
		echo "</tr>";
	}
	if(1==1){	 
		echo "<tr $color>"; 
		echo "	<td align='left' valign='top' style='border-right:1px solid black; '>". $rows[2] ."</td>";
		echo "	<td align='left' valign='top' style='border-right:1px solid black; padding-left:2px;'>". $rows[1] ."</td>";
		echo "	<td align='left' valign='top' style='border-right:1px solid black; padding-left:2px;'>". $rows[3] ."</td>";
		echo "	<td align='left' valign='top' style='border-right:1px solid black; padding-left:2px;'>". $rows[4] ."</td>";
		if($Module_Heatno==1 and $vShowHeatno==1)
		echo "	<td align='left' valign='top' >". $rows[17] ."</td>";
		echo "	<td align='right' valign='top' ";
		if($Module_NewProduction != 1) echo "style='border-right:1px solid black; padding-right:2px;'";
		echo ">". $rows[7] ."</td>";
	}else{
		echo "<tr>"; 
		echo "	<td align='left' valign='top' >". $rows[2] ."</td>";
	
	}

	//if($Module_NewProduction == 1) {
		
	//	$varDiff=$rows[7]-$rows[20];
		
	//	echo "	<td align='right' valign='top' >".$rows[7]."</td>";
	//	echo "	<td align='right' valign='top' >".$rows[20]."</td>";
	//	echo "	<td align='right' valign='top' >$varDiff</td>";
	//	echo "	<td align='right' valign='top' >".$rows[21]."</td>";
	//	echo "	<td align='right' valign='top' >".$rows[22]."</td>";
	//}
	if ($showlastprice == 1) {
		echo "	<td align='center' valign='top'>". $curr ."</td>";
		echo "	<td align='right' valign='top' >".number_format($ProductionUnitCost, $DecimalBase)."</td>";
		echo "	<td align='right' valign='top' >".number_format($ProductionTotalCost, $DecimalBase)."</td>";
	} else {
		echo "	<td align='center' valign='top' colspan='3'>&nbsp;</td>";
	}	
	
	echo "</tr>";
		
	if ($showdetail == 1) {
	
		if ($rows[14] == 'PDN') {
			$querydt = " Select ProductionReqRM.Item_code, Items.Item_Description, ProductionReqRM.Warehouse_Code, ".
					   " Round(ifNull(productionReqRM_Quantity,0), ".$DecimalBase.") As ProductionRM_Quantity, 0, ".
					   " if('$showlastprice' = '1', Round(ifNull(GetLastValuesFunc('$Base1','$Base1',ProductionReqRM.Item_code),0), ".$DecimalBase."), Round(ifNull(0,0), ".$DecimalBase.")) As ProductionDtCostBase1, ".
					   " if('$showlastprice' = '1', Round(ifNull(GetLastValuesFunc('$Base2','$Base2',ProductionReqRM.Item_code),0), ".$DecimalBase."), Round(ifNull(0,0), ".$DecimalBase.")) As ProductionDtCostBase2, ".
					   " Round((ifNull(productionReqRM_Quantity,0)*if('$showlastprice' = '1',ifNull(GetLastValuesFunc('$Base1','$Base1',ProductionReqRM.Item_code),0),ifNull(0,0))), ".$DecimalBase.") As TotalCostBase1, ".
					   " Round((ifNull(productionReqRM_Quantity,0)*if('$showlastprice' = '1',ifNull(GetLastValuesFunc('$Base2','$Base2',ProductionReqRM.Item_code),0),ifNull(0,0))), ".$DecimalBase.") As TotalCostBase2 ".
					   " From ProductionReqRM  ".
					   //" Inner Join ProductionReqRM on ProductionReqRM.ProductionReqRM_Id = ProductionReqRM.ProductionReqRM_Id ".
					   " Inner Join Items on ProductionReqRM.Item_code = Items.Item_code ".
					   " Where ProductionReqRM.ProductionReq_Id = '".$rows[0]."' ".
					   " And ProductionReqRM.ProductionReqFG_Id = '".$rows[15]."' ".
					   " And ProductionReqRM.Item_Code Between ifNull(".$fromitemRM.", ProductionReqRM.Item_Code) and ifNull(". $toitemRM.", ProductionReqRM.Item_Code) ".
					   " Order By ProductionReqRM.Item_code ";
					   
		} else {
			$querydt = " Select ProductionDt.Item_code, Items.Item_Description, ProductionDt.Warehouse_Code, ".
					   " Round(ifNull(ProductionDt_Quantity,0), ".$DecimalBase.") As ProductionDtQuantity, ProductionDt_IdealQty, ".
					   " if('$showlastprice' = '1', Round(ifNull(GetLastValuesFunc('$Base1','$Base1',Productiondt.Item_code),0), ".$DecimalBase."), Round(ifNull(ProductionDt_CostBase1,0), ".$DecimalBase.")) As ProductionDtCostBase1, ".
					   " if('$showlastprice' = '1', Round(ifNull(GetLastValuesFunc('$Base2','$Base2',Productiondt.Item_code),0), ".$DecimalBase."), Round(ifNull(ProductionDt_CostBase2,0), ".$DecimalBase.")) As ProductionDtCostBase2, ".
					   " Round((ifNull(ProductionDt_Quantity,0)*if('$showlastprice' = '1',ifNull(GetLastValuesFunc('$Base1','$Base1',Productiondt.Item_code),0),ifNull(ProductionDt_CostBase1,0))), ".$DecimalBase.") As TotalCostBase1, ".
					   " Round((ifNull(ProductionDt_Quantity,0)*if('$showlastprice' = '1',ifNull(GetLastValuesFunc('$Base2','$Base2',Productiondt.Item_code),0),ifNull(ProductionDt_CostBase2,0))), ".$DecimalBase.") As TotalCostBase2 ".
					   " From ProductionDt  ".
					   " Inner Join Items on ProductionDt.Item_code = Items.Item_code ".
					   " Where ProductionDt.Production_ID = '".$rows[0]."' ".
					   " And ProductionDt.Item_Code Between ifNull(".$fromitemRM.", ProductionDt.Item_Code) and ifNull(". $toitemRM.", ProductionDt.Item_Code) ".
					   " Order By ProductionDt.Item_code ";
		}
				  
		//echo $querydt;echo "<br><br>";
		
		echo "<tr><td colspan='$colspan'>"; 
		echo "<table width='100%' border='0' align='left' class='detail' cellpadding='0' cellspacing='0' style='table-layout:fixed'>
			  <col width=20>
			  <col width=100>
			  <col width=360>
			  <col width=80>
			  <col width=60>
			  <col width=80>
			  <col width=100>";
			
		$resultdt=mysql_query($querydt,$connection) or die(mysql_error());
		$col1=0;	
		while($rowsdt=mysql_fetch_row($resultdt)){
			
			
			if($col1%2==0){
				$color1 = "style='background-color:".$report_color1."'";
			}else{
				$color1 = "style='background-color:".$report_color2."'";
			}
			
			if($curr == '' or $curr == $Base1) {
				$ProductionDtUnitCost  = $rowsdt[5];
				$ProductionDtTotalCost = $rowsdt[7];
			} else {
				$ProductionDtUnitCost  = $rowsdt[6];
				$ProductionDtTotalCost = $rowsdt[8];
			}	
				

				
			echo "<tr $color1>"; 
			echo "	<td align='left' valign='top' >&nbsp;</td>";
			echo "	<td align='left' valign='top' style='border-right:1px solid black; '>". $rowsdt[0] ."</td>";
			echo "	<td align='left' valign='top' style='border-right:1px solid black; padding-left:2px;'>". $rowsdt[1] ."</td>";
			echo "	<td align='center' valign='top' style='border-right:1px solid black; '>". $rowsdt[2] ."</td>";
			echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>". $rowsdt[3] ."</td>";
			
			if ($showlastprice == 1 || $showlastprice == 1) {
				echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>".number_format($ProductionDtUnitCost, $DecimalBase)."</td>";
				echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>".number_format($ProductionDtTotalCost, $DecimalBase)."</td>";
			}	
			else 
				echo "	<td align='right' valign='top' colspan='2'>&nbsp;</td>";
				
			echo "</tr>";
				
			$col1+=1;			
		}

		echo "</table>";	
		echo "</td></tr>";
		
	}
		
	$TotalAmount = $TotalAmount +  $ProductionTotalCost;
	
	$prevExpdate = $rows[19];
	$col+=1;
	
}


// Report Totals
if($Module_NewProduction == 1)
	$colspan2 = 12;
else $colspan2 = 7;
if ($showlastprice == 1) {
	echo "<tr height='5'><td colspan='$colspan'><h4></td></tr>"; 
	echo "<tr height='10'><td colspan='8'>&nbsp;</td></tr>"; 
	echo "<tr class='totals'>"; 
	echo "  <td align='right' colspan='$colspan2'>Total &nbsp;</td>";
	echo "	<td align='right' valign='top' >". number_format($TotalAmount, $DecimalBase) ."</td>";
	echo "</tr>";	
}	

echo "</table></td></tr>";
echo "</table>";



function current_server_time($connection){

  $string="Select date_format(NOW(),'%Y-%m-%d %h:%i %p') ";
  $result=mysql_query($string,$connection) or die(mysql_error());
  $now=mysql_fetch_row($result);

  return $now;
}

function explodeword($date)
{

$trim = explode('/',$date);
$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";
return $trim;
}


if ($printaccess==1)
print <<< HERE

<script language=javascript>
if (typeof(window.print) != 'undefined')
{
    window.print();
}
</script>
</BODY>
</HTML>
HERE;

?>