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

session_start();

/*
if($HTTP_SESSION_VARS['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($HTTP_SESSION_VARS['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('".$HTTP_SESSION_VARS['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>';


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


$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 ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['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 ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%') ".
			  " where user_account='".$HTTP_SESSION_VARS['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 ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%') ".
			   " where user_account='".$HTTP_SESSION_VARS['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 ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
		 "                                          where user_account='".$HTTP_SESSION_VARS['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 ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['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'; 

print <<< HERE
<table width="100%" border="0" align="center">
<tr><td>
<table width='860' 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;
   

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

<tr><td>
<table class='params' width='860' 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';

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=100>
	  
	  <col width=110>";
		  
		  
 
	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=100>
			<tr>
				<th >&nbsp;</th>
				<th >Item Code</th>";

			echo"<th align='left'>Description</th>";
			echo"<th >Cost Price </th>";		 
			echo"<th colspan='2'>&nbsp; </th>";
			
	echo"	</tr>";
		
	echo"</table>";
	echo"</td></tr>";		

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

		 


$query = '';

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


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

$MasterItemCode = '';


$col=0;

$query="select item_code,item_description from items Where 1 = 1" ;

 
$query = $query.	
		" and ((ifNull(Items.Item_Pack,0) = 1 and ifNull(Items.Item_Promotion,0) = 0 and ifNull(Items.Item_Production,0) = 0 and ifNull(Items.Item_SemiFinished,0) = 0 and ifNull(Items.Item_NonStock,0) = 0) or ".
		"      ( ifNull(Items.Item_Pack,0) = 1 and ifNull(Items.Item_Promotion,0) = 1 and ifNull(Items.Item_Production,0) = 0 and ifNull(Items.Item_SemiFinished,0) = 0 and ifNull(Items.Item_NonStock,0) = 0) or ".
		"      ( ifNull(Items.Item_Pack,0) = 0 and (ifNull(Items.Item_Production,0) = 1 or ifNull(Items.Item_SemiFinished,0) = 1 or ifNull(Items.Item_ReverseProduction,0) = 1) and ifNull(Items.Item_NonStock,0) = 0) or  ".
		"      (ifNull(Items.Item_Pack,0) = 0 and (ifNull(Items.Item_Production,0) = 1 or ifNull(Items.Item_SemiFinished,0) = 1 or ifNull(Items.Item_ReverseProduction,0) = 1) )  ".
		"       ) ";
 
	/*
	$query = $query.	
		" and (('".$ItemType."' = 'Trading'      and ifNull(Items.Item_Pack,0) = 0 and ifNull(Items.Item_Promotion,0) = 0 and ifNull(Items.Item_Visible,0) = 1) or ".
		"      ('".$ItemType."' = 'Packed'       and ifNull(Items.Item_Pack,0) = 1 and ifNull(Items.Item_Promotion,0) = 0 and ifNull(Items.Item_Production,0) = 0 and ifNull(Items.Item_SemiFinished,0) = 0 and ifNull(Items.Item_NonStock,0) = 0) or ".
		"      ('".$ItemType."' = 'PromoPacked'  and ifNull(Items.Item_Pack,0) = 1 and ifNull(Items.Item_Promotion,0) = 1 and ifNull(Items.Item_Production,0) = 0 and ifNull(Items.Item_SemiFinished,0) = 0 and ifNull(Items.Item_NonStock,0) = 0) or ".
		"      ('".$ItemType."' = 'PromoTrading' and ifNull(Items.Item_Pack,0) = 0 and ifNull(Items.Item_Promotion,0) = 1 and ifNull(Items.Item_Visible,0) = 1) or ".
		"      ('".$ItemType."' = 'Production'   and ifNull(Items.Item_Pack,0) = 0 and (ifNull(Items.Item_Production,0) = 1 or ifNull(Items.Item_SemiFinished,0) = 1 or ifNull(Items.Item_ReverseProduction,0) = 1) and ifNull(Items.Item_NonStock,0) = 0) or  ".
		"      ('".$ItemType."' = 'NonStock'     and ifNull(Items.Item_Pack,0) = 0 and ifNull(Items.Item_Production,0) = 0 and ifNull(Items.Item_SemiFinished,0) = 0  and ifNull(Items.Item_NonStock,0) = 1) or ".
		"      ('".$ItemType."' = 'Assemble'     and ifNull(Items.Item_Pack,0) = 0 and (ifNull(Items.Item_Production,0) = 1 or ifNull(Items.Item_SemiFinished,0) = 1 or ifNull(Items.Item_ReverseProduction,0) = 1) ) or  ".
		"      ('".$ItemType."' = 'RawMaterial'  and  (ifNull(Items.Item_Production,0) = 0 and ifNull(Items.Item_SemiFinished,0) = 0 )) or  ".
		"      ('".$ItemType."' = 'All' and 1 = 1) ) ".
	*/	
		//echo $query;
$result = mysql_query($query,$connection);
while($rows=mysql_fetch_row($result)){

	if($col%2==0){
		$color = "style='background-color:".$report_color1."'";
	}else{
		$color = "style='background-color:".$report_color1."'";
	}
	
	 
		
			 
		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>";
			
			
			$querydt = " select  sum( MeanCostCur(Item_CodePackDt,now(),'1','".$dispcurr."','') * PackDt_Quantity )
				,date_format(PackDt_FromDate,'%d/%m/%Y')
				,date_format(PackDt_ToDate,'%d/%m/%Y'),Packdt_PromoQty,BARCODE, packdt.group_code ".
			   " from packdt ".
			   " left join items on items.item_code=packdt.Item_CodePackDt ".
			   " left join warehouse on warehouse.warehouse_code=packdt.Warehouse_Code ".
			   " where packdt.Item_Code='".$rows[0]."'
			   group by packdt.Item_Code
			   ";
			   
				$resultdt = mysql_query($querydt, $connection) or die(mysql_error());
				$rowsdt=mysql_fetch_row($resultdt);
		
		echo "	<td align='right' valign='top' style='border-right:1px solid black; padding-left:2px;'>".number_format($rowsdt[0], $DecimalBase)."</td>";		
		echo "	<td align='right' valign='top' ></td>";		
		echo "	<td align='right' valign='top' ></td>";		
		echo "	<td align='right' valign='top' ></td>";		
		echo "	<td align='right' valign='top' ></td>";		
		echo "	<td align='right' valign='top' ></td>";		
		echo "	<td align='right' valign='top' ></td>";		
		echo "	<td align='right' valign='top' ></td>";		
		echo "	<td align='right' valign='top' ></td>";		
		
		echo "</tr>";
		
		
		
		/////////////////////////////// packdt ////////////
		
			$querydt = " select Item_CodePackDt,item_description,packdt.Warehouse_Code,warehouse.warehouse_name
				,PackDt_Quantity, MeanCostCur(Item_CodePackDt,now(),'1','".$dispcurr."','') * PackDt_Quantity
				,date_format(PackDt_FromDate,'%d/%m/%Y')
				,date_format(PackDt_ToDate,'%d/%m/%Y'),Packdt_PromoQty,BARCODE, packdt.group_code ".
			   " from packdt ".
			   " left join items on items.item_code=packdt.Item_CodePackDt ".
			   " left join warehouse on warehouse.warehouse_code=packdt.Warehouse_Code ".
			   " where packdt.Item_Code='".$rows[0]."'";


	$resultdt = mysql_query($querydt, $connection) or die(mysql_error());
	$totaldtcost=0;
	while($rowsdt=mysql_fetch_row($resultdt)){
		
		$ct+=1;

		if($ct%2==0)
			$Color="style='background-color:".$grid_color1."'";
		else			
			$Color="style='background-color:".$grid_color2."'";	
							
		echo"<tr><td></td><td colspan=10><table width=100% border=0><col width=300><col width=150><col width=150><col width=150>";			
		echo "<tr ".$Color." >";
		echo "	<td align='left' ><span class='style15'>".$rowsdt[0]." - ".$rowsdt[1]."&nbsp;&nbsp;&nbsp;</span></td>";
		echo "	<td align='left'><span class='style15'>".$rowsdt[2]." - ".$rowsdt[3]."&nbsp;&nbsp;&nbsp;</span></td>";
		echo "	<td align='right'><span class='style15'>".$rowsdt[4]."</span></td>";
		echo "	<td align='right'><span class='style15'>".number_format($rowsdt[5], $DecimalBase)."</span></td>";
		
		
		echo "</tr>";
		
							
							
							
		 				
		echo"</table></td></tr>";					
	}		
 
		
	 
		
		echo "<tr><td colspan=10><hr></td></tr>";
		///////////////////////
		$col+=1;

	
	
	
}

 

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;

?>