<?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;
                }				 
				 
table.detail td { font-size: 12px; 
                  font-family:Times-New-Roman;
                 }	

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;
                       }					  
table.totals td.borderclassR {
								font-size: 14px; 
								font-family:Times-New-Roman;
								border-width: 1px;
								border-style: none none solid solid;
								border-color: #666666;
							}
table.totals td.borderclassL {
								font-size: 14px; 
								font-family:Times-New-Roman;
								border-width: 1px;
								border-style: none solid solid solid;
								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]);



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);
	}
	
	
	//Read From Project Table
	$queryProject="Select Project_Memo As ProjectMemo From Project Where Project_Code = '".$ProjectCode."'";
	$resultProject=mysql_query($queryProject,$connection) or die(mysql_error());
	while($rowProject=mysql_fetch_array($resultProject)){
		extract($rowProject);
	}
	
	
	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>Raw Materials</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></table>";
	
	echo "<table border=0 width='635' cellspacing=2 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=110>
		  <col width=340>
		  <col width=90>
		  <col width=100>";

    
	echo "<tr height='5'><td colspan=4><h4></td></tr>";
	echo "<tr>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Department</th>";
	echo "  <td align='left'>&nbsp;</td>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Date</th>";
	echo "  <td align='left'>$ProductionReq_Date</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Store Keeper</th>";
	echo "  <td align='left' >&nbsp;</td>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Issue To</th>";
	echo "  <td align='left'>&nbsp;</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Remark</th>";
	echo "  <td align='left' colspan='3'>$ProductionHD_Remark</td>";
	echo "</tr>";
	
	
	
	echo "<tr height='5'><td colspan=4><h4></td></tr>";
	echo "</table></td></tr>";

	$sub_query1 = " Select ProductionReqRM.ProductionReqRM_ID, ProductionReqRM.Item_CodePacked, ProductionReqRM.Item_Code, Item_Description as ItemDescription, ".
				  " ProductionReqRM.Warehouse_Code, Sum(ifNull(ProductionReqRM_Quantity,0)) As ProductionReqRM_Quantity, Items.Item_UnitSalesDesc As ItemUnit ".
				  " From ProductionReqRM Left Join Items on ProductionReqRM.Item_Code = Items.Item_code ".
				  " Where ProductionReqRM.ProductionReq_ID = '".$ProductionReqId."' ".
				  " Group By ProductionReqRM.Item_Code, ProductionReqRM.Warehouse_Code ".
				  " Order By ProductionReqRM.Item_Code, ProductionReqRM.Warehouse_Code ";
				  
	
	//echo $sub_query1;
	echo "<tr><td align='left'>";
	echo "<table border=0 width='635' cellspacing=2 class='detail' style='table-layout:fixed;' >
		  <col width=355>
		  <col width=100>
		  <col width=100>
		  <col width=80>";
		  
	echo "<tr bgcolor=#D8D8D8>";
	echo "   <th align='left'>Raw Material</th>";
	echo "	 <th align='center'>Warehouse</th>";
	echo "	 <th align='center'>Quantity</th>";
	echo "	 <th align='center'>Unit</th>";
	echo "</tr>";			
		  
	echo "<tr height='5'><td colspan=4><h4></td></tr>";
	
	$TotalQty = 0;
	
	$sub_result1=mysql_query($sub_query1,$connection) or die(mysql_error());
	while($sub_rows1=mysql_fetch_array($sub_result1)){
		extract($sub_rows1);
		
		$TotalQty = $TotalQty + $ProductionReqRM_Quantity;
		
		echo "<tr>";
		echo "  <td align='left' valign='top'>$ItemDescription</td>";
		echo "  <td align='left' valign='top'>$Warehouse_Code</td>";
		echo "	<td align='right' valign='top'>".number_format($ProductionReqRM_Quantity, $DecimalBase)."</td>";
		echo "	<td align='left' valign='top'>$ItemUnit</td>";
		echo "</tr>";
		
	}
	
	echo "<tr height='5'><td colspan=4><h4></td></tr>";
	echo "</table></td></tr>";
	
	
	echo "<tr><td align='left'>";
	echo "<table border=0 width='635' cellspacing=0 class='totals' style='table-layout:fixed;' >
		  <col width=100>
		  <col width=217>
		  <col width=100>
		  <col width=218>";
		  
	echo "<tr height='80'>";
	echo "	 <td align='center' colspan='4'>&nbsp;</td>";
	echo "</tr>";	
	
	echo "<tr>";
	echo "	 <td align='center' colspan='2' class='labels' style='border-right:0px;'>Production Manager</td>";
	echo "	 <td align='center' colspan='2' class='labels'>Branch Manager</td>";
	echo "</tr>";	
	
	echo "<tr>";
	echo "  <td align='left' valign='top' class='borderclassR'>Name</td>";
	echo "  <td align='left' valign='bottom' class='borderclassR'>&nbsp;</td>";
	echo "  <td align='left' valign='top' class='borderclassR'>Name</td>";
	echo "  <td align='left' valign='bottom' class='borderclassL'>&nbsp;</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "	<td align='left' valign='top' class='borderclassR'>Signature</td>";
	echo "  <td align='left' valign='bottom' class='borderclassR'>&nbsp;</td>";
	echo "	<td align='left' valign='top' class='borderclassR'>Signature</td>";
	echo "  <td align='left' valign='bottom' class='borderclassL'>&nbsp;</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;
?>

