<?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.master th { font-size: 14px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                }				 
table.master td { font-size: 14px; 
                  font-family:Times-New-Roman;
                 }
table.detail th { font-size: 13px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
				  padding-left:5px;
				  padding-right:5px;
                }				 
				 
table.detail td { font-size: 12px; 
                  font-family:Times-New-Roman;
				  padding-left:5px;
				  padding-right:5px;
                 }	

table.totals td.labels { font-size: 14px; 
                         font-weight: bold;
                         font-family:Times-New-Roman;
						 border-width: 1px;
						 border-style: solid solid solid solid;
						 border-color: #666666;
                       }					  
.borderclassL {
				font-size: 14px; 
				font-family:Times-New-Roman;
				border-width: 1px;
				border-style: none solid solid solid;
				border-color: #666666;
			}
.borderclassR {
				font-size: 14px; 
				font-family:Times-New-Roman;
				border-width: 1px;
				border-style: none solid solid none;
				border-color: #666666;
			}
				 
h4 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 1px;
   }
   
h3 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 2px;
	 width: 150px;
   }   
   
   
   
</style>


</HEAD>

<BODY>
HERE;

include("config.inc.php");


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



//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, Company_memo As CompanyMemo, ".
             " Company_Name As CompanyName from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}



//Read From Module Table
$querymodule=" Select ifNull(Module_ShowLayoutProj,0) As Module_ShowLayoutProj, ifNull(Module_ShowLayoutCost,0) As Module_ShowLayoutCost From Module";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}
	
$now=current_server_time($connection);
$dt=split(" ",$now[0]);
$printdate=split("-",$dt[0]);


$ArrayItemFG     = Array();
$ArrayItemDescFG = Array();
$ArrayItemRM     = Array();
$ArrayItemDescRM = Array();

$ArrayDataFG    = Array();
$ArrayDataRM    = Array();

if($ProductionReqId != '')
{
	$query = " Select ProductionReq_ID, ProductionReq_Reference, Date_Format(ProductionReq_Date,'%d/%m/%Y') As ProductionReq_Date, ".
	         " ProductionReq_Remark, ProductionReq_Manager, ProductionReq_Shift, ifNull(ProductionReq_Confirmed,0) ".
			 " From ProductionRequest ".
			 " Where ProductionReq_ID = ".$ProductionReqId;
			 
	$result=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
	}
	
	

	
	echo "<table border=0 width='100%' cellspacing=0 cellpadding=0><tr><td align='left'>";
	echo "<table border=0 width='635' cellspacing=2 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=165>
		  <col width=270>
		  <col width=95>
		  <col width=110>";
		
	echo "<tr><td height='60'><img src='LogoFile.php' width='115' height='50' /></td>
	          <td align='center'><b><font size=5>Production Request</font></td><td align='right' colspan=2 ><font size='2'><b>Printed&nbsp;Date&nbsp;&amp;&nbsp;Time:</b>&nbsp;&nbsp;";
	echo $printdate[2]."/".$printdate[1]."/".$printdate[0];
	echo "<br>".$dt[1]." ".$dt[2];
	echo "</font></td></tr>";
	
	echo "<tr height='5'><td colspan=4><h4></td></tr>";
	echo "</table></td></tr>";
	


	$sub_query1 = " Select ProductionReqFG.ProductionReqFG_ID, ProductionReqFG.Item_CodePacked, ProductionReqFG.Item_Code As ItemCodeFG, Item_Description as ItemDescriptionFG, ".
				  " ProductionReqFG.Warehouse_Code As WarehouseCodeFG, ifNull(ProductionReqFG_Quantity,0) As ProductionReqFG_Quantity, Items.Item_UnitSalesDesc As ItemUnitFG ".
				  " From ProductionReqFG Left Join Items on ProductionReqFG.Item_Code = Items.Item_code ".
				  " Where ProductionReqFG.ProductionReq_ID = '".$ProductionReqId."' ".
				  " Order By ProductionReqFG.Item_Code ";
	
	//echo $sub_query1;
	$sub_result1=mysql_query($sub_query1,$connection) or die(mysql_error());
	while($sub_rows1=mysql_fetch_array($sub_result1)){
		extract($sub_rows1);
		
		//Check If ItemFG Exists In Array
		$ItemFGFound = 0;
		$count = count ($ArrayItemFG); 
		for ($j=0; $j<$count; $j++){
			if ($ArrayItemFG[$j] == $ItemCodeFG)
				$ItemFGFound = 1;
		}
					
		if ($ItemFGFound == 0){
			$ArrayItemFG[$count] = $ItemCodeFG;
			$ArrayItemDescFG[$count] = $ItemDescriptionFG;
		}	
			
		$ArrayDataFG[$ItemCodeFG]['Qty'] = $ProductionReqFG_Quantity;
		
		$sub_query2 = " Select ProductionReqRM.ProductionReqRM_ID, ProductionReqRM.Item_CodePacked, ProductionReqRM.Item_Code As ItemCodeRM, Item_Description As ItemDescriptionRM, ".
					  " ProductionReqRM.Warehouse_Code As WarehouseCodeRM, ifNull(ProductionReqRM_Quantity,0) As ProductionReqRM_Quantity, Items.Item_UnitSalesDesc As ItemUnitRM ".
					  " From ProductionReqRM Left Join Items on ProductionReqRM.Item_Code = Items.Item_code ".
					  " Where ProductionReqRM.ProductionReq_ID = '".$ProductionReqId."' ".
					  " And ProductionReqFG_Id = '".$ProductionReqFG_ID."' ".
					  " Order By ProductionReqRM.Item_Code ";
			
		$sub_result2=mysql_query($sub_query2,$connection) or die(mysql_error());
		while($sub_rows2=mysql_fetch_array($sub_result2)){
			extract($sub_rows2);
				
			//Check If ItemRM Exists In Array
			$ItemRMFound = 0;
			$count = count ($ArrayItemRM); 
			for ($j=0; $j<$count; $j++){
				if ($ArrayItemRM[$j] == $ItemCodeRM)
					$ItemRMFound = 1;
			}
						
			if ($ItemRMFound == 0) { 
				$ArrayItemRM[$count] = $ItemCodeRM;	
				$ArrayItemDescRM[$count] = $ItemDescriptionRM;	
			}	
				
			
			$ArrayDataRM[$ItemCodeRM][$ItemCodeFG]['Qty'] += $ProductionReqRM_Quantity;
			
		}
			
	}
	
	
	echo "<tr><td align='left'>";
	echo "<table border=0 cellspacing=0 class='detail' style='table-layout:fixed; margin-top:30px;' >
			<col width=180>";
			
	$CountItemFG = count($ArrayItemFG);
	for ($i=0; $i<$CountItemFG; $i++){
		echo"<col width=120>";
	}
	
	//sorting data in array
	array_multisort($ArrayItemFG);
	array_multisort($ArrayItemRM);
	
	//Display Header
	echo"<tr height='20'>";
	echo"	<th align='left' class='borderclassL' style='border-top:1px solid black;'>FG/RM</th>";
	
	$CountItemFG = count($ArrayItemFG);
	for ($i=0; $i<$CountItemFG; $i++){
		echo"<th align='center' class='borderclassR' style='border-top:1px solid black;'>".$ArrayItemDescFG[$i]."</th>";
	}
	
	echo"</tr>";
		
	// Displaying Data
	$CountItemRM = count($ArrayItemRM); 
	for ($i=0; $i<$CountItemRM; $i++){
		echo"	<tr height='20'>";
		echo"		<th align='left' class='borderclassL'>".$ArrayItemDescRM[$i]."</th>";
	
		$CountItemFG = count($ArrayItemFG);
		for ($j=0; $j < $CountItemFG; $j++){
			$QtyRM = $ArrayDataRM[$ArrayItemRM[$i]][$ArrayItemFG[$j]]['Qty'];
			echo"	<td align='right' class='borderclassR'>";
			if (!empty($QtyRM)) echo number_format($QtyRM, $DecimalBase); else echo '&nbsp;';
			echo"</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 format_currency($amount, $DecimalBase) { 

    if($amount < 0) 
        return "(".number_format(abs($amount),$DecimalBase).")"; 
 
    else return number_format($amount, $DecimalBase); 
} 

print <<< HERE

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

