<!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> Production Report </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");

//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);
}


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

$querymodule=" Select 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);
}

$querysetup="select grid_color1,grid_color2, DecimalBase, DecimalBase1, DecimalBase2, Base1, Base2, report_color1, report_color2, Company_Name As CompanyName, ifNull(ListOfProductionMaskQty,2) as ListOfProductionMaskQty 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]);

if ($listby == 1) $ReportTitle = 'List of Finished Goods';
else $ReportTitle = 'List of Raw Materials'; 

$height = 1150;	


print <<< HERE
<table width="100%" border="0" align="center">
<tr><td>
<table width='$height' border='0' align='left' style='table-layout:fixed'>
<tr>
  <td width='35%'><b>$CompanyName</b></td>
  <td width='45%' align='center'><font size=4><b>$ReportTitle</b></font></td>
  <td width='10%'><font size=1><b> Printed Date: </b> <br><b>Time: </b>
  <td width='10%'><font size=1>
HERE;

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;
$dispcurr     = $curr;


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

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($fromwarehouse))  $fromwarehouse = 'null';
else  $fromwarehouse = "'".$fromwarehouse."'";      


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

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

if (empty($showdetail))   $showdetail = 0;
if (empty($showcost))     $showcost = 0;
if (empty($showunit2qty)) $showunit2qty = 0;
if (empty($showlastprice)) $showlastprice = 0;
if($showlastprice == 1) $showcost=1;


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

<tr><td>
<table class='params' width='$height' border='0' align='left' style='table-layout:fixed' >
<tr>
  <td width="12%"><b>Date From/To:</b></td>
  <td width="18%">$dispfromdate - $disptodate </td>
  <td width="8%" valign='top'><b>Currency:</b></td>
  <td width="12%">$dispcurr</td>
  <td width="50%" valign='top'>&nbsp;</td>
</tr>
<tr height='5'><td colspan=5><h4></td></tr>
</table>
</td></tr>

<tr><td>
HERE;

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


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


if ($listby == 1){
$query = $query." Select distinct Production.warehouse_code as warehouse_code ".
				" From Production ".
				" Inner Join Items on Production.Item_code = Items.Item_code ";
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'))  ";
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.  " group by Production.warehouse_code ";

$query = $query." union Select ProductionFG.warehouse_code As warehouse_code ".
				" From ProductionFG ".
				" Inner Join Items on ProductionFG.Item_code = Items.Item_code ".
				" Where Items.Item_Code Between ifNull(".$fromitem.", Items.Item_Code) and ifNull(". $toitem.", Items.Item_Code) ".
				" And ifNull(ProductionFG.Warehouse_Code,'-1') = ifNull(". $fromwarehouse .", ifNull(ProductionFG.Warehouse_Code,'-1')) ".
				" And Date_format(ProductionFG.ProductionHD_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(ProductionFG.ProductionHD_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(ProductionFG.ProductionHD_Date,'%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']."')  ";  
$query = $query.  " group by ProductionFG.warehouse_code ";
} 
else if ($listby == 2){
		 
	$query = " Select ProductionDt.Warehouse_Code as Warehouse_Code
				From ProductionDt ".
			  " Inner Join Items on ProductionDt.Item_code = Items.Item_code ";
		 
	if ($projectexist >	0 && $FI_RepRestrictProject == 1)
		$query= $query.$QueryRestrictPrj." on ProductionDt.project_code like concat(Q4.project_code,'%') ";
								 
	if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1)
		$query= $query.$QueryRestrictCst." on ProductionDt.Costcenter_Code like concat(Q5.costcent_code,'%') ";		 
			 

	$query = $query.  " Where ProductionDt.Item_Code Between ifNull(".$fromitemRM.", ProductionDt.Item_Code) and ifNull(". $toitemRM.", ProductionDt.Item_Code) ".
						" And ifNull(ProductionDt.Project_Code,'-1') between ifNull(".$fromproject.",ifNull(ProductionDt.Project_Code,'-1')) and ifNull(". $toproject.",ifNull(ProductionDt.Project_Code,'-1')) ".
						" And ifNull(ProductionDt.Costcenter_Code,'-1') between ifNull(".$fromcostcent.",ifNull(ProductionDt.Costcenter_Code,'-1')) and ifNull(". $tocostcent.",ifNull(ProductionDt.Costcenter_Code,'-1')) ".
						" And ifNull(ProductionDt.Warehouse_Code,'-1') = ifNull(". $fromwarehouse .", ifNull(ProductionDt.Warehouse_Code,'-1')) ".
						" And Date_format(ProductionDt.Production_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(ProductionDt.Production_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(ProductionDt.Production_Date,'%Y-%m-%d'))  ";

	$query = $query." Group By ProductionDt.Warehouse_Code ";
	
	$query = $query." Union ";  
	$query = $query." Select ProductionRM.Warehouse_Code as Warehouse_Code
						From ProductionRM ".
					  " Inner Join Items on ProductionRM.Item_code = Items.Item_code ";
		 
	$query = $query.  " Where ProductionRM.Item_Code Between ifNull(".$fromitemRM.", ProductionRM.Item_Code) and ifNull(". $toitemRM.", ProductionRM.Item_Code) ".
						" And Date_format(ProductionRM.ProductionHD_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(ProductionRM.ProductionHD_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(ProductionRM.ProductionHD_Date,'%Y-%m-%d'))  ".
						" And ifNull(ProductionRM.Warehouse_Code,'-1') = ifNull(". $fromwarehouse .", ifNull(ProductionRM.Warehouse_Code,'-1')) ";
	 
	$query = $query." Group By ProductionRM.Warehouse_Code ";

} 
$queryWh = "select distinct warehouse_code from ( ".$query.") as QQ order by warehouse_code";
$resWh = mysql_query($queryWh,$connection);
$numWh = mysql_num_rows($resWh);

$colw = 700 + (100 * $numWh);	
echo "<table width='$colw' border='0' align='left' cellpadding='0' cellspacing='0' class=$masterclass style='table-layout:fixed'>
	  <col width=120>
	  <col width=200>
	  <col width=60>";
	  
for($t=0;$t<$numWh;$t++)
echo " <col width=100> ";	
echo " <col width=90>
	  <col width=60>
	  <col width=100>
	  <col width=100>";



	echo"  <tr>";
	echo"	<th colspan='2' align='left'>Item Code / Description</th>";
	echo"	<th >Unit1</th>";
	echo"	<th colspan='$numWh' >Qty</th>";
	echo"	<th >Total Qty</th>";
	echo"	<th >Currency</th>";
	echo"	<th >Cost Per Unit </th>";
	echo"	<th >Total Cost </th>";
	echo"  </tr>";
	echo"  <tr><td colspan='2'></td><td></td>";
while($rWh = mysql_fetch_row($resWh)){
	echo "<td align='center'><b>$rWh[0]</b></td>";
}
 	echo"	<td ></td><td ></td><td ></td><td ></td>";
	echo"  </tr>";	  

 $colspan = (7+$numWh);	

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

		 
$query = "";


$query = " Select Q1.ItemCode, ItemDescription, Unit1FG, ";
$i=0;
$resWh = mysql_query($queryWh,$connection);
while($rWh = mysql_fetch_row($resWh)){
	$query = $query. " ProductionQuantity$i,  ";
	$i++;
}

 $query = $query." ProductionQuantity, Currency_Code,  CostPerUnitBase1, CostPerUnitBase2, ProductionTotalBase1, ProductionTotalBase2  ";		

$query = $query." From ( ";
$query = $query." Select Production.Item_code As ItemCode, Items.Item_Description As ItemDescription, ";

$i=0;
$resWh = mysql_query($queryWh,$connection);
while($rWh = mysql_fetch_row($resWh)){
	$query = $query. " if(Production.warehouse_code='$rWh[0]',Sum(ifNull(Production_Quantity,0)),0) As ProductionQuantity$i,  ";
$i++;
}
$query = $query." Sum(ifNull(Production_Quantity,0)) As ProductionQuantity,  ".
				" Production.Currency_Code, Round(Sum(ifNull(Production_TotalBase1,0)), ".$DecimalBase.") As ProductionTotalBase1, ".
				" Round(Sum(ifNull(Production_TotalBase2,0)), ".$DecimalBase.") As ProductionTotalBase2, ".
				" Round(Sum(ifNull(Production_TotalBase1,0))/Sum(ifNull(Production_Quantity,0)), ".$DecimalBase.") As CostPerUnitBase1, ".
				" Round(Sum(ifNull(Production_TotalBase2,0))/Sum(ifNull(Production_Quantity,0)), ".$DecimalBase.") As CostPerUnitBase2, ".
				" Item_UnitSalesDesc As Unit1FG ".
				" From Production ".
				" Inner Join Items on Production.Item_code = Items.Item_code ";
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'))  ";
					  
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." Group By ItemCode, ItemDescription  ";	
$query = $query." ) as Q1  ";



$query = $query." Union "; 

$query = $query." Select Q1.ItemCode, ItemDescription, Unit1FG, ";
$i=0;
$resWh = mysql_query($queryWh,$connection);
while($rWh = mysql_fetch_row($resWh)){
	$query = $query. " ProductionQuantity$i,  ";
	$i++;
}

 $query = $query. "ProductionQuantity, Currency_Code, CostPerUnitBase1, CostPerUnitBase2, ProductionTotalBase1, ProductionTotalBase2  ";

$query = $query." From ( ";
$query = $query." Select ProductionFG.Item_code As ItemCode, Items.Item_Description As ItemDescription, ";

$i=0;
$resWh = mysql_query($queryWh,$connection);
while($rWh = mysql_fetch_row($resWh)){
	$query = $query. " if(ProductionFG.warehouse_code='$rWh[0]',Sum(ifNull(ProductionFG_Quantity,0)),0) As ProductionQuantity$i,  ";
$i++;
}
$query = $query." Sum(ifNull(ProductionFG_Quantity,0)) As ProductionQuantity, ProductionFG.Currency_Code, Round(Sum(ifNull(ProductionFG_CostBase1,0)*ifNull(ProductionFG_Quantity,1)), ".$DecimalBase.") As ProductionTotalBase1, ".
				" Round(Sum(ifNull(ProductionFG_CostBase2,0)*ifNull(ProductionFG_Quantity,1)), ".$DecimalBase.") As ProductionTotalBase2, ".
				" Round(Sum(ifNull(ProductionFG_CostBase1,0)*ifNull(ProductionFG_Quantity,0))/Sum(ifNull(ProductionFG_Quantity,0)), ".$DecimalBase.") As CostPerUnitBase1, ".
				" Round(Sum(ifNull(ProductionFG_CostBase2,0)*ifNull(ProductionFG_Quantity,0))/Sum(ifNull(ProductionFG_Quantity,0)), ".$DecimalBase.") As CostPerUnitBase2, ".
				" Item_UnitSalesDesc As Unit1FG ".
				" From ProductionFG ".
				" Inner Join Items on ProductionFG.Item_code = Items.Item_code ".
				" Where Items.Item_Code Between ifNull(".$fromitem.", Items.Item_Code) and ifNull(". $toitem.", Items.Item_Code) ".
				" And ifNull(ProductionFG.Warehouse_Code,'-1') = ifNull(". $fromwarehouse .", ifNull(ProductionFG.Warehouse_Code,'-1')) ".
				" And Date_format(ProductionFG.ProductionHD_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(ProductionFG.ProductionHD_Date,'%Y-%m-%d')) 
				and ifNull(". $todate.",date_format(ProductionFG.ProductionHD_Date,'%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']."')  ";  

$query = $query." Group By ItemCode, ItemDescription  ";	
$query = $query." ) as Q1  ";

$query = $query." Order By ItemCode ";
	 

//echo $query;

//echo $query;
//exit;


		 
$query1 = " Select ProductionDt.Item_code As ItemCode, Items.Item_Description As ItemDescription,  Item_UnitSalesDesc As Unit1RM,  ";
$i=0;
$resWh = mysql_query($queryWh,$connection);
while($rWh = mysql_fetch_row($resWh)){
	$query1 = $query1. " if(ProductionDt.warehouse_code='$rWh[0]',Round(Sum(ifNull(ProductionDt_Quantity,0)), ".$DecimalBase."),0) As ProductionDtQuantity$i,  ";
$i++;
}
$query1 = $query1." Round(Sum(ifNull(ProductionDt_Quantity,0)), ".$DecimalBase.") As ProductionDtQuantity, ProductionDt.Currency_Code,  ".
		  " Round(Sum(ifNull(ProductionDt_Quantity,0)*ifNull(ProductionDt_CostBase1,0))/Sum(ifNull(ProductionDt_Quantity,0)), ".$DecimalBase.")  As ProductionDtCostBase1, ".
      	  " Round(Sum(ifNull(ProductionDt_Quantity,0)*ifNull(ProductionDt_CostBase2,0))/Sum(ifNull(ProductionDt_Quantity,0)), ".$DecimalBase.")  As ProductionDtCostBase2, ".
		  " Round((Sum(ifNull(ProductionDt_Quantity,0)* ifNull(ProductionDt_CostBase1,0))), ".$DecimalBase.") As ProductionDtTotalCostBase1, ".
		  " Round((Sum(ifNull(ProductionDt_Quantity,0)* ifNull(ProductionDt_CostBase2,0) )), ".$DecimalBase.") As ProductionDtTotalCostBase2 ";
		 

$query1 = $query1." From ProductionDt ".
			      " Inner Join Items on ProductionDt.Item_code = Items.Item_code ";
	 
if ($projectexist >	0 && $FI_RepRestrictProject == 1)
	$query1= $query1.$QueryRestrictPrj." on ProductionDt.project_code like concat(Q4.project_code,'%') ";
							 
if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1)
	$query1= $query1.$QueryRestrictCst." on ProductionDt.Costcenter_Code like concat(Q5.costcent_code,'%') ";		 
		 

$query1 = $query1.  " Where ProductionDt.Item_Code Between ifNull(".$fromitemRM.", ProductionDt.Item_Code) and ifNull(". $toitemRM.", ProductionDt.Item_Code) ".
					" And ifNull(ProductionDt.Project_Code,'-1') between ifNull(".$fromproject.",ifNull(ProductionDt.Project_Code,'-1')) and ifNull(". $toproject.",ifNull(ProductionDt.Project_Code,'-1')) ".
				    " And ifNull(ProductionDt.Costcenter_Code,'-1') between ifNull(".$fromcostcent.",ifNull(ProductionDt.Costcenter_Code,'-1')) and ifNull(". $tocostcent.",ifNull(ProductionDt.Costcenter_Code,'-1')) ".
					" And ifNull(ProductionDt.Warehouse_Code,'-1') = ifNull(". $fromwarehouse .", ifNull(ProductionDt.Warehouse_Code,'-1')) ".
				    " And Date_format(ProductionDt.Production_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(ProductionDt.Production_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(ProductionDt.Production_Date,'%Y-%m-%d'))  ";

$query1 = $query1." Group By ItemCode, ItemDescription ";

$query1 = $query1." Union ";  
$query1 = $query1." Select ProductionRM.Item_code As ItemCode, Items.Item_Description As ItemDescription, Item_UnitSalesDesc As Unit1RM, ";
$i=0;
$resWh = mysql_query($queryWh,$connection);
while($rWh = mysql_fetch_row($resWh)){
	$query1 = $query1. " if(ProductionRM.warehouse_code='$rWh[0]',Round(Sum(ifNull(ProductionRM_Quantity,0)), ".$DecimalBase."),0) As ProductionDtQuantity$i,  ";
$i++;
}
$query1 = $query1." Round(Sum(ifNull(ProductionRM_Quantity,0)), ".$DecimalBase.") As ProductionDtQuantity, ProductionRM.Currency_Code,  ".
				  " Round(Sum(ifNull(ProductionRM_Quantity,0)*ifNull(ProductionRM_CostBase1,0))/Sum(ifNull(ProductionRM_Quantity,0)), ".$DecimalBase.")  As ProductionDtCostBase1, ".
				  " Round(Sum(ifNull(ProductionRM_Quantity,0)*ifNull(ProductionRM_CostBase2,0))/Sum(ifNull(ProductionRM_Quantity,0)), ".$DecimalBase.")  As ProductionDtCostBase2, ".
				  " Round((Sum(ifNull(ProductionRM_Quantity,0)*ifNull(ProductionRM_CostBase1,0))), ".$DecimalBase.")  As ProductionDtTotalCostBase1, ".
				  " Round((Sum(ifNull(ProductionRM_Quantity,0)*ifNull(ProductionRM_CostBase2,0))), ".$DecimalBase.")  As ProductionDtTotalCostBase2 ";

$query1 = $query1." From ProductionRM ".
			      " Inner Join Items on ProductionRM.Item_code = Items.Item_code ";

$query1 = $query1.  " Where ProductionRM.Item_Code Between ifNull(".$fromitemRM.", ProductionRM.Item_Code) and ifNull(". $toitemRM.", ProductionRM.Item_Code) ".
					" And Date_format(ProductionRM.ProductionHD_Date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(ProductionRM.ProductionHD_Date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(ProductionRM.ProductionHD_Date,'%Y-%m-%d'))  ".
					" And ifNull(ProductionRM.Warehouse_Code,'-1') = ifNull(". $fromwarehouse .", ifNull(ProductionRM.Warehouse_Code,'-1')) ";
 
$query1 = $query1." Group By ItemCode, ItemDescription ";
  
$query1 = $query1." Order By ItemCode ";


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

$MasterItemCode = '';

if ($listby == 1)
	$result=mysql_query($query,$connection) or die(mysql_error());
else 
	$result=mysql_query($query1,$connection) or die(mysql_error());
	
$col=0;	
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[$numWh+5];
		$ProductionTotalCost = $rows[$numWh+7];
	} else {
		$ProductionUnitCost  = $rows[$numWh+6];
		$ProductionTotalCost = $rows[$numWh+8];
	}	
		
		 
	echo "<tr $color>"; 
	echo "	<td align='left' valign='top' style='border-right:1px solid black;'>". $rows[0] ."</td>";
	

	echo "	<td align='left' valign='top' style='border-right:1px solid black; padding-left:2px;'>". $rows[1] ."</td>";
	echo "	<td align='center' valign='top' style='border-right:1px solid black; '>". $rows[2] ."</td>";
	
	for($t=0;$t<$numWh;$t++)
	echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>". number_format($rows[$t+3], $ListOfProductionMaskQty) ."</td>";
	
	
	echo "	<td align='center' valign='top' style='border-right:1px solid black;'>". number_format($rows[$numWh+3], $ListOfProductionMaskQty) ."</td>";
	echo "	<td align='center' valign='top' style='border-right:1px solid black;'>". $curr ."</td>";
	echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>". number_format($ProductionUnitCost, $ListOfProductionMaskQty) ."</td>";
	echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>". number_format($ProductionTotalCost, $ListOfProductionMaskQty) ."</td>";
	
	echo "</tr>";
	
	$TotalAmount = $TotalAmount +  $ProductionTotalCost;
	$col+=1;

		
	
	
}


// Report Totals
if($Module_NewProduction == 1)
	$colspan2 = 14;
else $colspan2 = 10;
if ($showcost == 1) {
	echo "<tr height='5'><td colspan='$colspan'><h4></td></tr>"; 
	echo "<tr height='10'><td colspan='11'>&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;

?>