<!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'; 

if($Module_NewProduction == 1)
	$height = 1150;	
else $height = 860;	

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='4'><td colspan=6><h4></td></tr>
</table>
</td></tr>

<tr><td>
HERE;

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

echo "<table width='900' border='0' align='left' cellpadding='0' cellspacing='0' class=$masterclass style='table-layout:fixed'>
	  <col width=120>
	  <col width=150>
	  <col width=60>
	  <col width=60>
	  <col width=60>
	  <col width=60>
	  <col width=60>";
if($Module_NewProduction == 1)  
echo "<col width=60>
	  <col width=60>
	  <col width=60>
	  <col width=60>";		  
echo "<col width=60>
	  <col width=60>
	  <col width=100>
	  <col width=110>";


if ($showcost == 1) {
	if ($listby == 2 && $showidealqty == 1) {
		echo"  <tr height='18'>";
		echo"	<th colspan='5' align='left'>&nbsp;</th>";
		echo"	<th colspan='3' align='center' valign='top'>Qty</th>";
		echo"	<th colspan='3' align='center'>&nbsp;</th>";
		echo"  </tr>";
		
		echo"  <tr>";
		if ($showunit2qty == 1) {	
			echo"	<th colspan='2' align='left'>Item Code / Description</th>";
			echo"	<th >Unit2</th>";
			echo"	<th >Unit2Qty</th>";			
		} else {
			echo"	<th colspan='4' align='left'>Item Code / Description</th>";
		}
		echo"	<th >Unit1</th>";
		echo"	<th >Ideal</th>";
		echo"	<th >Actual</th>";
		echo"	<th >Diff</th>";
		if($Module_NewProduction == 1) 
			echo "<th>Actual Qty</th>".	
				"<th>Initial Qty</th>".
				"<th>Damage Qty</th>".
				"<th>Remix Qty</th>";
		echo"	<th >Currency</th>";
		if($showlastprice == 1 && $listby == 2) echo"	<th >Last Price </th>";
		else echo"	<th >Cost Per Unit </th>";
		echo"	<th >Total Cost </th>";
		echo"  </tr>";
	} else {
		echo"  <tr>";
		
		if ($showunit2qty == 1) {
			echo"	<th colspan='4' align='left'>Item Code / Description</th>";
			echo"	<th >Unit2</th>";
			echo"	<th >Unit2Qty</th>";
			echo"	<th >Unit1</th>";
			echo"	<th >Qty</th>";
		} else {
			echo"	<th colspan='6' align='left'>Item Code / Description</th>";
			echo"	<th >Unit1</th>";
			echo"	<th >Qty</th>";
		}
		if($Module_NewProduction == 1) 
			echo "<th>Actual Qty</th>".	
				"<th>Initial Qty</th>".
				"<th>Damage Qty</th>".
				"<th>Remix Qty</th>";
		echo"	<th >Currency</th>";
		if($showlastprice == 1 && $listby == 2) echo"	<th >Last Price </th>";
		else echo"	<th >Cost Per Unit </th>";
		echo"	<th >Total Cost </th>";
		echo"  </tr>";
	}	
} else {
	if ($listby == 2 && $showidealqty == 1) {
		echo"  <tr height='18'>";
		echo"	<th colspan='5' align='left'>&nbsp;</th>";
		echo"	<th colspan='3' align='center' valign='top'>Qty</th>";
		echo"	<th colspan='3' align='center'>&nbsp;</th>";
		echo"  </tr>";
		
		echo"  <tr>";
		if ($showunit2qty == 1) {	
			echo"	<th colspan='2' align='left'>Item Code / Description</th>";
			echo"	<th >Unit2</th>";
			echo"	<th >Unit2Qty</th>";			
		} else {	
			echo"	<th colspan='4' align='left'>Item Code / Description</th>";
		}	
		
		echo"	<th >Unit1</th>";
		echo"	<th >Ideal Qty</th>";
		echo"	<th >Qty</th>";
		echo"	<th >Diff</th>";
		if($Module_NewProduction == 1) 
			echo "<th>Actual Qty</th>".	
				"<th>Initial Qty</th>".
				"<th>Damage Qty</th>".
				"<th>Remix Qty</th>";
		echo"	<th colspan='3'>&nbsp;</th>";
		echo"  </tr>";
	} else {
		echo"  <tr>";
		if ($showunit2qty == 1) {
			echo"	<th colspan='4' align='left'>Item Code / Description</th>";
			echo"	<th >Unit2</th>";
			echo"	<th >Unit2Qty</th>";
			echo"	<th >Unit1</th>";
			echo"	<th >Qty</th>";
		} else {
			echo"	<th colspan='6' align='left'>Item Code / Description</th>";
			echo"	<th >Unit1</th>";
			echo"	<th >Qty</th>";
		}
		if($Module_NewProduction == 1) 
			echo "<th>Actual Qty</th>".	
				"<th>Initial Qty</th>".
				"<th>Damage Qty</th>".
				"<th>Remix Qty</th>";
				
		echo"	<th colspan='3'>&nbsp;</th>";
		echo"  </tr>";
	}	

}		  

if($Module_NewProduction == 1)
	$colspan = 15;	
else $colspan = 11;	
		  
if ($showdetail == 1) {
	echo"<tr height='5'><td colspan='11'><h5></td></tr>";
	echo"<tr height='20'><td colspan='11'>";
	echo "<table width='830' border='0' align='left' class='detail' style='table-layout:fixed'>
			<col width=20>
			<col width=110>
			<col width=110>
			<col width=60>
			<col width=60>
			<col width=60>
			<col width=60>
			<col width=60>
			<col width=60>
			<col width=80>
			<col width=100>
			<tr>
				<th >&nbsp;</th>
				<th >Item Code</th>";
				
		if ($showidealqty == 1) {		
			if ($showunit2qty == 1) {
				echo"<th align='left'>Description</th>";
				echo"<th >Unit2</th>";
				echo"<th >Unit2Qty</th>";
				echo"<th >Unit1</th>";
				echo"<th >Ideal</th>";
				echo"<th >Actual</th>";
				echo"<th >Diff</th>";
			} else {
				echo"<th align='left' colspan='3'>Description</th>";
				echo"<th >Unit1</th>";
				echo"<th >Ideal</th>";
				echo"<th >Actual</th>";
				echo"<th >Diff</th>";
			}
		} else {
			if ($showunit2qty == 1) {	
				echo"<th align='left' colspan='3'>Description</th>";
				echo"<th >Unit2</th>";
				echo"<th >Unit2Qty</th>";
				echo"<th >Unit1</th>";
				echo"<th >Qty</th>";
			} else {
				echo"<th align='left' colspan='5'>Description</th>
					 <th >Unit1</th>
					 <th >Qty</th>";
			}
		}
				
		if ($showcost == 1) {
			if($showlastprice == 1 && $listby == 1) echo"	<th >Last Price </th>";
			else echo"<th >Cost Price </th>";
			echo"<th >Total Cost</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 Q1.ItemCode, ItemDescription, ProductionQuantity, Currency_Code, ProductionTotalBase1, ProductionTotalBase2,  ".
		 " CostPerUnitBase1, CostPerUnitBase2, ProdType, Unit1FG, Unit2FG, Unit2QtyFG  ";
		 
if ($showdetail	== 1) {
	$query = $query.", ItemCodeDt, ItemDescriptionDt, ProductionDtQuantity, ProductionDtIdealQty, ProductionDtCostBase1, ProductionDtCostBase2, ".
					"  ProductionDtTotalCostBase1, ProductionDtTotalCostBase2, Unit1RM, Unit2RM, Unit2QtyRM ";
} else $query = $query.", null as ItemCodeDt, null as ItemDescriptionDt, null as ProductionDtQuantity, null as ProductionDtIdealQty, null as ProductionDtCostBase1, null as ProductionDtCostBase2, ".
					"  null as ProductionDtTotalCostBase1, null as ProductionDtTotalCostBase2, null as Unit1RM, null as Unit2RM, null as Unit2QtyRM ";
$query = $query." ,initialqty, damageqty, remixqty ";

$query = $query." From ( ";
$query = $query." Select Production.Item_code As ItemCode, Items.Item_Description As ItemDescription, 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, ".
				" 'PD' as ProdType, Item_UnitSalesDesc As Unit1FG, Item_UnitPurchaseDesc As Unit2FG, Sum(ifNull(Production.Item_Unit2Qty,0)) As Unit2QtyFG ".
				" , 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 (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'))  ";
					  
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  ";
			 
if ($showdetail	== 1) {
	$query = $query." INNER JOIN (  ";
	$query = $query." Select Production.Item_code As ItemCode, ProductionDt.Item_code As ItemCodeDt, Items1.Item_Description As ItemDescriptionDt, ".
					" Round(Sum(ifNull(ProductionDt_Quantity,0)), ".$DecimalBase.") As ProductionDtQuantity, ".
					" Round(Sum(ifNull(ProductionDt_IdealQty,0)), ".$DecimalBase.") As ProductionDtIdealQty, ".
					" 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, ".
					" Item_UnitSalesDesc As Unit1RM, Item_UnitPurchaseDesc As Unit2RM, Round(Sum(ifNull(ProductionDt.Item_Unit2Qty,0)), ".$DecimalBase.") As Unit2QtyRM ".
					" From ProductionDt ".
					" Inner Join Production on ProductionDt.Production_ID = Production.Production_ID ".
					" Inner Join Items as Items1 on ProductionDt.Item_code = Items1.Item_code ".
					" Where ProductionDt.Item_Code Between ifNull(".$fromitemRM.", ProductionDt.Item_Code) and ifNull(". $toitemRM.", ProductionDt.Item_Code) ".
					" 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 ItemCode, ItemCodeDt, ItemDescriptionDt  ";
	$query = $query." ) as Q2 on Q2.ItemCode = Q1.ItemCode  ";

}



$query = $query." Union "; 
$query = $query." Select Q1.ItemCode, ItemDescription, ProductionQuantity, Currency_Code, ProductionTotalBase1, ProductionTotalBase2,  ".
				" CostPerUnitBase1, CostPerUnitBase2, ProdType, Unit1FG, Unit2FG, Unit2QtyFG  ";
		 
if ($showdetail	== 1) {
	$query = $query.", ItemCodeDt, ItemDescriptionDt, ProductionDtQuantity, ProductionDtIdealQty, ProductionDtCostBase1, ProductionDtCostBase2, ".
					"  ProductionDtTotalCostBase1, ProductionDtTotalCostBase2, Unit1RM, Unit2RM, Unit2QtyRM ";
} else $query = $query.", null as ItemCodeDt, null as ItemDescriptionDt, null as ProductionDtQuantity, null as ProductionDtIdealQty, null as ProductionDtCostBase1, null as ProductionDtCostBase2, ".
					"  null as ProductionDtTotalCostBase1, null as ProductionDtTotalCostBase2, null as Unit1RM, null as Unit2RM, null as Unit2QtyRM ";
$query = $query." ,initialqty, damageqty, remixqty ";
$query = $query." From ( ";
$query = $query." Select ProductionFG.Item_code As ItemCode, Items.Item_Description As ItemDescription, 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, ".
				" 'PDN' as ProdType, Item_UnitSalesDesc As Unit1FG, Item_UnitPurchaseDesc As Unit2FG, Sum(ifNull(ProductionFG.Item_Unit2Qty,0)) As Unit2QtyFG, ".
				" ProductionFG_InitialQty as initialqty, ProductionFG_DamageQty as damageqty, ProductionFG_RemixQty as remixqty ".
				" 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.Project_Code,'-1') between ifNull(".$fromproject.",ifNull(ProductionFG.Project_Code,'-1')) and ifNull(". $toproject.",ifNull(ProductionFG.Project_Code,'-1')) ".
				//" And ifNull(ProductionFG.Costcenter_Code,'-1') between ifNull(".$fromcostcent.",ifNull(ProductionFG.Costcenter_Code,'-1')) and ifNull(". $tocostcent.",ifNull(ProductionFG.Costcenter_Code,'-1')) ".
				" 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  ";
			 
if ($showdetail	== 1) {
	$query = $query." INNER JOIN (  ";
	$query = $query." Select ProductionFG.Item_code As ItemCode, ProductionRM.Item_code As ItemCodeDt, Items1.Item_Description As ItemDescriptionDt, ".
					" Round(Sum(ifNull(ProductionRM_Quantity,0)), ".$DecimalBase.") As ProductionDtQuantity, ".
					" Round(Sum(ifNull(ProductionRM_IdealQty,0)), ".$DecimalBase.") As ProductionDtIdealQty, ".
					" 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, ".
					" Item_UnitSalesDesc As Unit1RM, Item_UnitPurchaseDesc As Unit2RM, Round(Sum(ifNull(ProductionRM.Item_Unit2Qty,0)), ".$DecimalBase.") As Unit2QtyRM ".
					" From ProductionRM ".
					" Inner Join ProductionFG on ProductionRM.ProductionHD_ID = ProductionFG.ProductionHD_ID ".
					" Inner Join ProductionReqRM on ProductionReqRM.ProductionReqRM_Id = ProductionRM.ProductionReqRM_Id And ProductionReqRM.ProductionReqFG_Id = ProductionFG.ProductionReqFG_Id ".
					" Inner Join Items as Items1 on ProductionRM.Item_code = Items1.Item_code ".
					" Where ProductionRM.Item_Code Between ifNull(".$fromitemRM.", ProductionRM.Item_Code) and ifNull(". $toitemRM.", ProductionRM.Item_Code) ";
					
	$query = $query." Group By ItemCode, ItemCodeDt, ItemDescriptionDt  ";
	$query = $query." ) as Q2 on Q2.ItemCode = Q1.ItemCode  ";

}

if ($showdetail	== 1) {  
	$query = $query." Order By ItemCode, ItemCodeDt ";
} else {
	$query = $query." Order By ItemCode ";
}		 

//echo $query;
		 
$query1 = " Select ProductionDt.Item_code As ItemCode, Items.Item_Description As ItemDescription, ".
		  " Round(Sum(ifNull(ProductionDt_Quantity,0)), ".$DecimalBase.") As ProductionDtQuantity, ProductionDt.Currency_Code,  ".
		  " 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, ".
		  " 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, ".
		  " 'PD' as ProdType, Item_UnitSalesDesc As Unit1RM, Item_UnitPurchaseDesc As Unit2RM, Round(Sum(ifNull(ProductionDt.Item_Unit2Qty,0)), ".$DecimalBase.") As Unit2QtyRM ";	
			 
if ($showdetail	== 1) {
	$query1 = $query1.",ItemCodeDt, ItemDescriptionDt, ProductionQuantity, Round(Sum(ifNull(ProductionDt_IdealQty,0)), ".$DecimalBase.") As ProductionDtIdealQty, ".
				      " CostPerUnitBase1, CostPerUnitBase2, ProductionTotalBase1, ProductionTotalBase2, Unit1FG, Unit2FG, Unit2QtyFG ";
} else {
	$query1 = $query1.",Null As ItemCodeDt, Null As ItemDescriptionDt, Null As ProductionQuantity, Round(Sum(ifNull(ProductionDt_IdealQty,0)), ".$DecimalBase.") As ProductionDtIdealQty, ".
				      " Null As CostPerUnitBase1, Null As CostPerUnitBase2, Null As ProductionTotalBase1, Null As ProductionTotalBase2, Null As Unit1FG, Null As Unit2FG, Null As Unit2QtyFG ";
}


$query1 = $query1." From ProductionDt ".
			      " Inner Join Items on ProductionDt.Item_code = Items.Item_code ";
if ($showdetail	== 1) {
	$query1 = $query1." Inner Join  ".
	                  " ( Select Production.Item_code As ItemCodeDt, Items.Item_Description As ItemDescriptionDt, ".
			    	  "   ProductionDt.Item_code As QItemCode, Sum(ifNull(Production_Quantity,0)) As ProductionQuantity,  ".
				      "   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, ".
				      "   Round(Sum(ifNull(Production_TotalBase1,0)), ".$DecimalBase.") As ProductionTotalBase1, ".
				      "   Round(Sum(ifNull(Production_TotalBase2,0)), ".$DecimalBase.") As ProductionTotalBase2, ".
					  "   Item_UnitSalesDesc As Unit1FG, Item_UnitPurchaseDesc As Unit2FG, Sum(ifNull(Production.Item_Unit2Qty,0)) As Unit2QtyFG ".
					  "   From Production ".
			          "   Inner Join Items on Production.Item_code = Items.Item_code ".
					  "   Inner Join ProductionDt on ProductionDt.Production_ID = Production.Production_ID ";
					  
	if ($projectexist >	0 && $FI_RepRestrictProject == 1)
		$query1= $query1.$QueryRestrictPrj." on Production.project_code like concat(Q4.project_code,'%') ";
								 
	if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1)
		$query1= $query1.$QueryRestrictCst." on Production.Costcenter_Code like concat(Q5.costcent_code,'%') ";		 
			 

	$query1 = $query1.  " 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)
		$query1 = $query1." And Production.warehouse_code In (select distinct warehouse_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."')  ";  
	if ($groupexist > 0)
		$query1 = $query1." And Items.group_code In (select distinct group_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."')  ";  
					  
					  
    $query1 = $query1."   Group By ItemCodeDt, ItemDescriptionDt, QItemCode  ".
	                  "   Order By QItemCode, ItemCodeDt ".
					  " ) Q1 on Q1.QItemCode = ProductionDt.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'))  ";
  
if ($showdetail	== 1) {  
	$query1 = $query1." Group By ItemCode, ItemDescription, ItemCodeDt ";
} else {
	$query1 = $query1." Group By ItemCode, ItemDescription ";
}  
  
$query1 = $query1." Union ";  
$query1 = $query1." Select ProductionRM.Item_code As ItemCode, Items.Item_Description As ItemDescription, ".
				  " Round(Sum(ifNull(ProductionRM_Quantity,0)), ".$DecimalBase.") As ProductionDtQuantity, ProductionRM.Currency_Code,  ".
				  " 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, ".
				  " 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, ".
				  " 'PDN' as ProdType, Item_UnitSalesDesc As Unit1RM, Item_UnitPurchaseDesc As Unit2RM, Round(Sum(ifNull(ProductionRM.Item_Unit2Qty,0)), ".$DecimalBase.") As Unit2QtyRM ";
			 
if ($showdetail	== 1) {
	$query1 = $query1.",ItemCodeDt, ItemDescriptionDt, ProductionQuantity, Round(Sum(ifNull(ProductionRM_IdealQty,0)), ".$DecimalBase.") As ProductionDtIdealQty, ".
				      " CostPerUnitBase1, CostPerUnitBase2, ProductionTotalBase1, ProductionTotalBase2, Unit1FG, Unit2FG, Unit2QtyFG ";
} else {
	$query1 = $query1.",Null As ItemCodeDt, Null As ItemDescriptionDt, Null As ProductionQuantity, Round(Sum(ifNull(ProductionRM_IdealQty,0)), ".$DecimalBase.") As ProductionDtIdealQty, ".
				      " Null As CostPerUnitBase1, Null As CostPerUnitBase2, Null As ProductionTotalBase1, Null As ProductionTotalBase2, Null As Unit1FG, Null As Unit2FG, Null As Unit2QtyFG ";
}


$query1 = $query1." From ProductionRM ".
			      " Inner Join Items on ProductionRM.Item_code = Items.Item_code ";
if ($showdetail	== 1) {
	$query1 = $query1." Inner Join  ".
	                  " ( Select ProductionFG.Item_code As ItemCodeDt, Items.Item_Description As ItemDescriptionDt, ".
			    	  "   ProductionRM.Item_code As QItemCode, Sum(ifNull(ProductionFG_Quantity,0)) As ProductionQuantity,  ".
				      "   Round(Sum(ifNull(ProductionFG_CostBase1,0)*ifNull(ProductionFG_Quantity,1))/Sum(ifNull(ProductionFG_Quantity,0)), ".$DecimalBase.") As CostPerUnitBase1, ".
				      "   Round(Sum(ifNull(ProductionFG_CostBase1,0)*ifNull(ProductionFG_Quantity,1))/Sum(ifNull(ProductionFG_Quantity,0)), ".$DecimalBase.") As CostPerUnitBase2, ".
				      "   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, ".
					  "   Item_UnitSalesDesc As Unit1FG, Item_UnitPurchaseDesc As Unit2FG, Sum(ifNull(ProductionFG.Item_Unit2Qty,0)) As Unit2QtyFG ".
					  "   From ProductionFG ".
			          "   Inner Join Items on ProductionFG.Item_code = Items.Item_code ".
					  "	  Inner Join ProductionRM on ProductionRM.ProductionHD_ID = ProductionFG.ProductionHD_ID ".
					  "   Inner Join ProductionReqRM on ProductionReqRM.ProductionReqRM_Id = ProductionRM.ProductionReqRM_Id And ProductionReqRM.ProductionReqFG_Id = ProductionFG.ProductionReqFG_Id ";
					  
		 

	$query1 = $query1.  " Where Items.Item_Code Between ifNull(".$fromitem.", Items.Item_Code) and ifNull(". $toitem.", Items.Item_Code) ".
					    //" And ifNull(ProductionFG.Project_Code,'-1') between ifNull(".$fromproject.",ifNull(ProductionFG.Project_Code,'-1')) and ifNull(". $toproject.",ifNull(ProductionFG.Project_Code,'-1')) ".
					    //" And ifNull(ProductionFG.Costcenter_Code,'-1') between ifNull(".$fromcostcent.",ifNull(ProductionFG.Costcenter_Code,'-1')) and ifNull(". $tocostcent.",ifNull(ProductionFG.Costcenter_Code,'-1')) ".
					    //" 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)
		$query1 = $query1." And ProductionFG.warehouse_code In (select distinct warehouse_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."')  ";  
	if ($groupexist > 0)
		$query1 = $query1." And Items.group_code In (select distinct group_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."')  ";  
					  
					  
    $query1 = $query1."   Group By ItemCodeDt, ItemDescriptionDt, QItemCode  ".
	                  "   Order By QItemCode, ItemCodeDt ".
					  " ) Q1 on Q1.QItemCode = ProductionRM.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')) ";
  
if ($showdetail	== 1) {  
	$query1 = $query1." Group By ItemCode, ItemDescription, ItemCodeDt ";
} else {
	$query1 = $query1." Group By ItemCode, ItemDescription ";
}   
 
  
if ($showdetail	== 1) {  
	$query1 = $query1." Order By ItemCode, ItemCodeDt ";
} else {
	$query1 = $query1." Order By ItemCode ";
}
//echo $query1;
//exit;



$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[6];
		$ProductionTotalCost = $rows[4];
		$base = 1;
	} else {
		$ProductionUnitCost  = $rows[7];
		$ProductionTotalCost = $rows[5];
		$base = 2;
	}	
		
	if ($showdetail == 0) {	
			 
		echo "<tr $color>"; 
		echo "	<td align='left' valign='top' style='border-right:1px solid black;'>". $rows[0] ."</td>";
		
		if ($listby == 2 && $showidealqty == 1) {
			if ($showunit2qty == 1) {
				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[10] ."</td>";
				echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>". number_format($rows[11], $ListOfProductionMaskQty) ."</td>";
			} else {
				echo "	<td align='left' valign='top' colspan='3' style='border-right:1px solid black; '>". $rows[1] ."</td>";
			}	
			
			echo "	<td align='center' valign='top' style='border-right:1px solid black;'>". $rows[9] ."</td>";
			echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>". number_format($rows[15], $ListOfProductionMaskQty) ."</td>";
			echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>". number_format($rows[2], $ListOfProductionMaskQty) ."</td>";
			echo "	<td align='right' valign='top'style='border-right:1px solid black; padding-right:2px;'>". number_format(($rows[15] - $rows[2]), $ListOfProductionMaskQty)."</td>";
		}
		else {
			if ($showunit2qty == 1 && $listby == 1) {
				echo "	<td align='left' valign='top' colspan='3' 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[10] ."</td>";
				echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>". number_format($rows[11], $ListOfProductionMaskQty) ."</td>";
				echo "	<td align='center' valign='top' style='border-right:1px solid black; '>". $rows[9] ."</td>";
			}	
			else if ($showunit2qty == 1 && $listby == 2) {
				echo "	<td align='left' valign='top' colspan='3' 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[10] ."</td>";
				echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>". number_format($rows[11], $ListOfProductionMaskQty) ."</td>";	
				echo "	<td align='center' valign='top' style='border-right:1px solid black; '>". $rows[9] ."</td>";
			} else {
				echo "	<td align='left' valign='top' colspan='5' 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[9] ."</td>";
			}	
			echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>". number_format($rows[2], $ListOfProductionMaskQty) ."</td>";
		}
		
		if($Module_NewProduction == 1) 
			echo "<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>".number_format($rows[2], $ListOfProductionMaskQty)."</td>".	
				 "<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>".number_format($rows[23], $ListOfProductionMaskQty)."</td>".
				 "<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>".number_format($rows[24], $ListOfProductionMaskQty)."</td>".
				 "<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>".number_format($rows[25], $ListOfProductionMaskQty)."</td>";
				
		if ($showcost == 1) {
			echo "	<td align='center' valign='top' style='border-right:1px solid black; '>". $curr ."</td>";
			if($showlastprice == 1){
				$lastprice = getLastPrice($rows[0], $base, $PurchaseTTC, $connection);
				echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>".number_format($lastprice, $DecimalBase)."</td>";
				echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>".number_format($lastprice*$rows[2], $DecimalBase)."</td>";
			}
			else {
				echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>".number_format($ProductionUnitCost, $DecimalBase)."</td>";
				echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>".number_format($ProductionTotalCost, $DecimalBase)."</td>";
			}
		} else {
			echo "	<td align='center' valign='top' colspan='3' style='border-right:1px solid black; '>&nbsp;</td>";
		}	
		
		echo "</tr>";
		
		$TotalAmount = $TotalAmount +  $ProductionTotalCost;
		$col+=1;
	} else {

		
		if ($MasterItemCode == '' || $MasterItemCode != $rows[0]) {
			
			echo "<tr >"; 
			echo "	<td align='left' valign='top' >". $rows[0] ."</td>";
			
			if ($listby == 2 && $showidealqty == 1) {
				if ($showunit2qty == 1) {
					echo "	<td align='left' valign='top'>". $rows[1] ."</td>";
					echo "	<td align='center' valign='top' >". $rows[10] ."</td>";
					echo "	<td align='right' valign='top' >". number_format($rows[11], $ListOfProductionMaskQty) ."</td>";
				} else {
					echo "	<td align='left' valign='top' colspan='3'>". $rows[1] ."</td>";
				}	
				echo "	<td align='center' valign='top' >". $rows[9] ."</td>";
				echo "	<td align='right' valign='top'>". number_format($rows[15], $ListOfProductionMaskQty) ."</td>";
				echo "	<td align='right' valign='top' >". number_format($rows[2], $ListOfProductionMaskQty) ."</td>";
				echo "	<td align='right' valign='top'>". number_format(($rows[15] - $rows[2]), $ListOfProductionMaskQty)."</td>";
			}
			else {
				if ($showunit2qty == 1) {
					echo "	<td align='left' valign='top' colspan='3'>". $rows[1] ."</td>";
					echo "	<td align='center' valign='top' >". $rows[10] ."</td>";
					echo "	<td align='right' valign='top' >". number_format($rows[11], $ListOfProductionMaskQty) ."</td>";
				} else {
					echo "	<td align='left' valign='top' colspan='5'>". $rows[1] ."</td>";
				}
				echo "	<td align='center' valign='top' >". $rows[9] ."</td>";
				echo "	<td align='right' valign='top' >". number_format($rows[2], $ListOfProductionMaskQty) ."</td>";
			}
			
			if ($showcost == 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>";

			$MasterItemCode = $rows[0];
			
			$TotalAmount = $TotalAmount +  $ProductionTotalCost;
		
		}

		if($curr == '' or $curr == $Base1) {
			$ProductionDtUnitCost  = $rows[16];
			$ProductionDtTotalCost = $rows[18];
		} else {
			$ProductionDtUnitCost  = $rows[17];
			$ProductionDtTotalCost = $rows[19];
		}	
			
		echo "<tr><td colspan='8'>"; 
		echo "<table width='830' border='0' cellpadding='0' cellspacing='0' align='left' class='detail' style='table-layout:fixed'>
			  <col width=20>
			  <col width=110>
			  <col width=110>
			  <col width=60>
			  <col width=60>
			  <col width=60>
			  <col width=60>
			  <col width=60>
			  <col width=80>
			  <col width=100>";
							
		echo "<tr $color>"; 
		echo "	<td align='left' valign='top' >&nbsp;</td>";
		echo "	<td align='left' valign='top' style='border-right:1px solid black; '>". $rows[12] ."</td>";
		
		if ($showidealqty == 1) {
			echo "	<td align='left' valign='top' style='border-right:1px solid black; padding-left:2px;'>". $rows[13] ."</td>";
			echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>". number_format($rows[15], $ListOfProductionMaskQty) ."</td>";
			echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>". number_format($rows[14], $ListOfProductionMaskQty) ."</td>";
			echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>". number_format(($rows[15] - $rows[14]), $ListOfProductionMaskQty) ."</td>";
		} else {
			if ($showunit2qty == 1) {
				echo "	<td align='left' valign='top' colspan='3' style='border-right:1px solid black; padding-left:2px;'>". $rows[13] ."</td>";
				echo "	<td align='center' valign='top' style='border-right:1px solid black;'>". $rows[21] ."</td>";
				echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>". number_format($rows[22], $ListOfProductionMaskQty) ."</td>";
				echo "	<td align='center' valign='top' style='border-right:1px solid black;'>". $rows[20] ."</td>";
				echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>". number_format($rows[14], $ListOfProductionMaskQty) ."</td>";
			} else {
				echo "	<td align='left' valign='top' colspan='5' style='border-right:1px solid black; padding-left:2px;'>". $rows[13] ."</td>";
				echo "	<td align='center' valign='top' style='border-right:1px solid black;'>". $rows[20] ."</td>";
				echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-right:2px;'>". number_format($rows[14], $ListOfProductionMaskQty) ."</td>";
			}	
		}
			
		if ($showcost == 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' style='border-right:1px solid black; padding-right:2px;'>&nbsp;</td>";
				
		echo "</tr>";
		
		echo "</table>";	
		echo "</td></tr>";
		$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 getLastPrice($itemcode, $base, $ttc, $connection){
	$query = "SELECT if('$ttc' = '1', PriceTTCBase1, PriceBase1) as lastpriceb1, if('$ttc' = '1', PriceTTCBase2, PriceBase2) as lastpriceb2
			  FROM stktransaction WHERE if('$ttc' = '1', PriceTTC, Price) > 0	and transaction_type in ('PI', 'AI', 'PD') and item_code='$itemcode'
			  Order By transaction_date Desc Limit 1;";
	$res = mysql_query($query, $connection);
	$row = mysql_fetch_row($res);
	$lastpriceb1 = $row[0];
	$lastpriceb2 = $row[1];
	if ($lastpriceb1 == '') $lastpriceb1 = 0;
	if ($lastpriceb2 == '') $lastpriceb2 = 0;
	
	if ($base == 1) return $lastpriceb1;
	else return $lastpriceb2;
	
}

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;

?>