<?php


session_start();
if($HTTP_SESSION_VARS['dbName']==""){
header("location:../index.php");
}

print <<< HERE
<HTML>
<HEAD>
<TITLE> Production Template 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.divisions th { font-size: 12px; 
                     font-weight: bold;
                     font-family:Times-New-Roman;
                   }				 
table.divisions td { font-size: 12px; 
                     font-family:Times-New-Roman;
                   }				 
				 
table.detail td.sr  { font-size: 10px; 
					  font-style: italic; 
                      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: none solid solid solid;
  						 border-color: #666666;
                       }					  
table.totals td.amounts { font-size: 14px; 
                          font-family:Times-New-Roman;
						  border-width: 1px;
						  border-style: none none solid none;
  						  border-color: #666666;
				        }
table.totals td.totalborder {
								font-size: 14px; 
								font-family:Times-New-Roman;
								border-width: 1px;
								border-style: none solid solid none;
								border-color: #666666;
							}						
				 
h4 { border-top-style: double; 
	 border-top-color: black; 
	 border-top-width: 1px;
   }
   
h3 { border-top-style: double; 
	 border-top-color: black; 
	 border-top-width: 2px;
	 width: 150px;
   }   
   
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}   
   
   
</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($ProductionId != '')
{
	$query = " Select ProdRecipe_ID, ProdRecipe_Reference, Date_Format(ProdRecipe_Date,'%d/%m/%Y') As ProdRecipe_Date, ".
	         " ProdRecipe_Remark, if (ifNull(ProdRecipe_Active,0)=1, 'Yes', 'No') As ProdRecipe_Active ".
			 " From ProdRecipe ".
	         " Where ProdRecipe_ID = ".$ProductionId;
			 
	$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>";
		  //<img src='img/imgheader1.jpg' width='115' height='50' />
	echo "<tr><td height='60'><img src='LogoFile.php' width='115' height='50' /></td>
	          <td align='center'><b><font size=5>Production Template</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='10'><td colspan=4><hr></td></tr>";
	echo "<tr height='5'><td colspan=4><h4></td></tr>";
	echo "<tr>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Reference</th>";
	echo "  <td align='left'>$ProdRecipe_Reference</td>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Date</th>";
	echo "  <td align='left'>$ProdRecipe_Date</td>";
	echo "</tr>";
	
	
	echo "<tr>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Remark</th>";
	echo "  <td align='left' colspan='3'>$ProdRecipe_Remark</td>";
	echo "</tr>";
	
	
	
	//echo "<tr height='10'><td colspan=4><hr></td></tr>";
	echo "<tr height='5'><td colspan=4><h4></td></tr>";
	echo "</table></td></tr>";

				  
	$sub_query1 = " Select ProdRecipeFG.ProdRecipeFG_ID As ProdRecipeFGID, ProdRecipeFG.Item_CodePacked, ProdRecipeFG.Item_Code As ItemCode, Item_Description As ItemDescription, ".
				  " ProdRecipeFG.Warehouse_Code As WarehouseCode, ProdRecipeFG_Quantity, ProdRecipeFG_Remark, ".
				  " if(ProdRecipeFG_DistOption=1, 'Percentage', if(ProdRecipeFG_DistOption=2, 'Quantity', 'Fixed')) As DistOption  ".
				  " From ProdRecipeFG Left Join Items on ProdRecipeFG.Item_Code = Items.Item_code ".
				  " Where ProdRecipeFG.ProdRecipe_ID = ".$ProductionId;
				  
	
	//echo $sub_query1;
	echo "<tr><td align='left'>";
	echo "<table border=0 width='635' cellspacing=2 class='detail' style='table-layout:fixed;' >
		  <col width=100>
		  <col width=270>
		  <col width=85>
		  <col width=80>
		  <col width=100>";
		  
	echo"<tr bgcolor='#D8D8D8' height='20'>";
	echo"  <td align='center' colspan='5'><span class='style16'>Finished Goods</span></td>";
	echo"</tr>";
	  
	echo "<tr bgcolor=#D8D8D8>";
	echo "   <th align='left'>Code</th>";
	echo "   <th align='left'>Description</th>";
	echo "	 <th align='center'>Warehouse</th>";
	echo "	 <th align='center'>Qty.</th>";
	echo "	 <th align='center'>Cost Option</th>";
	echo "</tr>";			
		  
	
	echo "<tr height='5'><td colspan=5><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 + $ProdRecipeFG_Quantity;
		
		echo "<tr>";
		echo "  <td align='left' valign='top'>$ItemCode</td>";
		echo "  <td align='left' valign='top'>$ItemDescription</td>";
		echo "  <td align='left' valign='top'>$WarehouseCode</td>";
		echo "	<td align='right' valign='top'>$ProdRecipeFG_Quantity</td>";
		echo "	<td align='center' valign='top'>$DistOption</td>";
		echo "</tr>";
		
		if ($ProdRecipeFG_Remark != '') {
			echo "<tr>";
			echo "  <td align='left' valign='top'>&nbsp;</td>";
			echo "  <td align='left' valign='top' colspan='4'>$ProdRecipeFG_Remark	</td>";
			echo "</tr>";
		}	
		
	}
	
	
	echo "<tr height='5'><td colspan=5><h4></td></tr>";
	
	echo "<tr>";
	echo "   <td align='left' colspan='2'>&nbsp;</td>";
	echo "	 <th align='left' class='labels'>Total Qty.</th>";
	echo "	 <th align='right' class='amounts'>$TotalQty</th>";
	echo "   <td align='left'>&nbsp;</td>";
	echo "</tr>";
	
	echo "</table></td></tr>";
	
	$sub_query1 = " Select ProdRecipeRM.ProdRecipeRM_ID As ProdRecipeRMID, ProdRecipeRM.Item_CodePacked, ProdRecipeRM.Item_Code As ItemCode, Item_Description As ItemDescription, ".
				  " ProdRecipeRM.Warehouse_Code As WarehouseCode, ProdRecipeRM_Quantity, ProdRecipeRM_Remark, ".
				  " if(ProdRecipeRM_DistOption=1, 'Percentage', if(ProdRecipeRM_DistOption=2, 'Quantity', 'Fixed')) As DistOption  ".
				  " From ProdRecipeRM Left Join Items on ProdRecipeRM.Item_Code = Items.Item_code ".
				  " Where ProdRecipeRM.ProdRecipe_ID = ".$ProductionId;
				  
	
	//echo $sub_query1;
	echo "<tr><td align='left'>";
	echo "<table border=0 width='635' cellspacing=2 class='detail' style='table-layout:fixed;' >
		  <col width=100>
		  <col width=270>
		  <col width=85>
		  <col width=80>
		  <col width=100>";
	
	echo"<tr height='5'><td colspan=5><h4></td></tr>";	
	
	echo"<tr bgcolor='#D8D8D8' height='20'>";
	echo"  <td align='center' colspan='5'><span class='style16'>Raw Materials</span></td>";
	echo"</tr>";
	  
	echo "<tr bgcolor=#D8D8D8>";
	echo "   <th align='left'>Code</th>";
	echo "   <th align='left'>Description</th>";
	echo "	 <th align='center'>Warehouse</th>";
	echo "	 <th align='center'>Qty.</th>";
	echo "	 <th align='center'>Cost Option</th>";
	echo "</tr>";			
		  
	
	echo "<tr height='5'><td colspan=5><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 + $ProdRecipeRM_Quantity;
		
		echo "<tr>";
		echo "  <td align='left' valign='top'>$ItemCode</td>";
		echo "  <td align='left' valign='top'>$ItemDescription</td>";
		echo "  <td align='left' valign='top'>$WarehouseCode</td>";
		echo "	<td align='right' valign='top'>$ProdRecipeRM_Quantity</td>";
		echo "	<td align='center' valign='top'>$DistOption</td>";
		echo "</tr>";
		
		if ($ProdRecipeRM_Remark != '') {
			echo "<tr>";
			echo "  <td align='left' valign='top'>&nbsp;</td>";
			echo "  <td align='left' valign='top' colspan='4'>$ProdRecipeRM_Remark	</td>";
			echo "</tr>";
		}	
		
	}
	
	
	echo "<tr height='5'><td colspan=5><h4></td></tr>";
	
	echo "<tr>";
	echo "   <td align='left' colspan='2'>&nbsp;</td>";
	echo "	 <th align='left' class='labels'>Total Qty.</th>";
	echo "	 <th align='right' class='amounts'>$TotalQty</th>";
	echo "   <td align='left'>&nbsp;</td>";
	echo "</tr>";
	
	echo "</table></td></tr>";
	
	
		
	
	
	/*echo "<tr><td align='left'>";
	echo "<table border=0 width='635' cellspacing=0 cellpadding=1  class='master' style='table-layout:fixed;' >
		  <col width=235>
		  <col width=175>
		  <col width=235>";
	
	echo "<tr height='100'>";
	echo "  <td align='left' colspan='3'>&nbsp;</td>";
	echo "</tr>";	
	
	echo "<tr height='5'>";
	echo "  <td align='center'><h3></td>";
	echo "  <td align='center'><h3></td>";
	echo "  <td align='center'>&nbsp;</td>";
	echo "</tr>";	
	
	echo "<tr height='40'>";
	echo "  <td align='center' valign='top'>PREPARED BY</td>";
	echo "  <td align='center' valign='top'>AUTHORIZED BY</td>";
	echo "  <td align='center' valign='top'>&nbsp;</td>";
	echo "</tr>";	
	
	echo "<tr height='5'><td colspan=3><h4></td></tr>";
	
	echo "<tr>";
	echo "  <td align='center'  valign='top'><img src='img\Samsung.JPG' width='170' height='80'></td>";
	echo "  <td align='center' style='font-size:12px; font-family:Times-New-Roman;'>$ProjectMemo</td>";
	echo "  <td align='center' valign='top'><img src='img\chigo.JPG' width='160' height='70'></td>";
	echo "</tr>";	
	echo "</table></td></tr>";
	
	echo "</table>";*/
	echo "<br>";
	

}


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;
?>

