<?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.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 ProductionHD_ID, ProductionHD_Reference, Date_Format(ProductionHD_Date,'%d/%m/%Y') As ProductionHD_Date, ".
	         " Date_Format(ProductionHD_ExpireDate,'%d/%m/%Y') As ExpireDate, ProductionHD.ProductionHD_BatchNo, ".
			 " ProductionHD_Remark, ProductionHD_Type, ProductionHD.Currency_Code, ".
			 " Round(ProductionHD_Total, ".$DecimalBase.") As ProductionTotal, ".
			 " Round(ProductionHD_IdealTotal, ".$DecimalBase.") As ProductionIdealTotal, ".
			 " Round(ProductionHD_WasteTotal, ".$DecimalBase.") As ProductionWasteTotal ".
			 " From ProductionHD ".
	         " Where ProductionHD_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);
	}
	
	//Read From Divisions Table
	$sub_query = " Select ProductionHDDivisions_ID, ProductionHDDivisions.Divisions_Id, DivisionTypes_Name, Divisions_Code, Divisions_Name  ".
				 " From ProductionHDDivisions ".
				 " Inner Join Divisions on ProductionHDDivisions.Divisions_Id = Divisions.Divisions_Id ".
				 " Inner Join DivisionTypes on Divisions.DivisionTypes_Id = DivisionTypes.DivisionTypes_Id ".
				 " Where ProductionHDDivisions.ProductionHD_Id = '".$ProductionId."' ";
	$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());	
	$sub_totalrows = mysql_num_rows($sub_result);
	
	
	echo "<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td align='left'>";
	echo "<table border=0 width='735' cellspacing=2 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=200>
		  <col width=270>
		  <col width=135x>
		  <col width=130>";
		  //<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</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='735' cellspacing=2 cellpadding=0 class='master' style='table-layout:fixed;' >
		  <col width=120>
		  <col width=400>
		  <col width=95>
		  <col width=120>";

    //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'>$ProductionHD_Reference</td>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Date</th>";
	echo "  <td align='left'>$ProductionHD_Date</td>";
	echo "</tr>";
	
	
	echo "<tr>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Remark</th>";
	echo "  <td align='left' >$ProductionHD_Remark</td>";
	echo "  <th align='left' bgcolor='#D8D8D8'>Currency</th>";
	echo "  <td align='left'>$Currency_Code</td>";
	echo "</tr>";
	
	
	if ($sub_totalrows > 0) {
		
		echo"<tr>";
		echo "  <th align='left' valign='top' bgcolor='#D8D8D8'>Divisions</th>";
		echo"	<td align='left' colspan='6'>";
		echo"	<table border='0' class='divisions' style='table-layout:fixed'>
				  <col width=100>
				  <col width=100>
				  <col width=225>";
		echo"		<tr>";
		echo"  			<th align='center' bgcolor='#D8D8D8'>Type</th>";
		echo"  			<th align='center' bgcolor='#D8D8D8'>Code</th>";
		echo"  			<th align='center' bgcolor='#D8D8D8'>Name</th>";
		echo"		</tr>";
		
		while($sub_rows=mysql_fetch_array($sub_result)){
			echo "<tr>";
			echo "	<td align='left'>".$sub_rows[2]."</td>";
			echo "	<td align='left'>".$sub_rows[3]."</td>";
			echo "	<td align='left'>".$sub_rows[4]."</td>";
			echo "</tr>";
		}
		
		echo"	</table></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 ProductionFG.ProductionFG_ID As ProductionFGID, ProductionFG.Item_CodePacked, ProductionFG.Item_Code As ItemCode, Item_Description As ItemDescription, ".
				  " ProductionFG.Warehouse_Code As WarehouseCode, ProductionFG_Quantity, ProductionFG_IdealQty,ProductionFG_DamageQty,ProductionFG_RemixQty, ProductionFG_Remark, ".
				  " Round(ProductionFG_Cost, ".$DecimalBase.") As ProductionFG_Cost, ProductionFG_CostBase1, ProductionFG_CostBase2, ".
				  " ifNull(ProductionFG_IdealQty,0) - ifNull(ProductionFG_Quantity,0) As ProductionFG_Waste, ".
				  " Round((ProductionFG_Quantity*ProductionFG_Cost), ".$DecimalBase.") As TotalActual, ".
				  " Round((ProductionFG_IdealQty*ProductionFG_Cost), ".$DecimalBase.") As TotalIdeal, ".
				  " Date_Format(ProductionFG_ExpireDate,'%d-%m-%Y') As ProductionFG_ExpireDate, ProductionFG.ProductionFG_BatchNo,(ProductionFG_IdealQty-ProductionFG_Quantity) as TotalQty2 ".
				  " From ProductionFG Left Join Items on ProductionFG.Item_Code = Items.Item_code ".
				  " Where ProductionFG.ProductionHD_ID = ".$ProductionId;
				  
	
	//echo $sub_query1;
	echo "<tr><td align='left'>";
	echo "<table border=0 width='735' cellspacing=2 class='detail' style='table-layout:fixed;' >
		  <col width=90>
		  <col width=185>
		  <col width=85>
		  <col width=50>
		  <col width=50>
		  <col width=50>
		  <col width=50>
		  <col width=50>
		  <col width=70>
		  <col width=70>";
		  
	echo"<tr bgcolor='#D8D8D8' height='20'>";
	echo"  <td align='center' colspan='10'><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'>Ideal</th>";
	echo "   <th align='center'>Damage Qty</th>";
	echo "   <th align='center'>Remix Qty</th>";
	echo "	 <th align='center'>Qty</th>";
	echo "	 <th align='center'>Diff</th>";
	echo "	 <th align='center'>Cost Price</th>";
	echo "	 <th align='center'>Total Price</th>";
	echo "</tr>";			
		  
	//echo "<tr height='10'><td colspan=7><hr></td></tr>";
	echo "<tr height='5'><td colspan=10><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 + $ProductionFG_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'>$ProductionFG_IdealQty</td>";
		echo "  <td align='right' valign='top'>$ProductionFG_DamageQty</td>";
		echo "  <td align='right' valign='top'>$ProductionFG_RemixQty</td>";
		
		echo "	<td align='right' valign='top'>$ProductionFG_Quantity</td>";
		echo "	<td align='right' valign='top'>$TotalQty2</td>";
		
		echo "	<td align='right' valign='top'>".number_format($ProductionFG_Cost, $DecimalBase)."</td>";
		echo "	<td align='right' valign='top'>".number_format($TotalActual, $DecimalBase)."</td>";
		echo "</tr>";
		

		
		if ($ProductionFG_Remark != '') {
			echo "<tr>";
			echo "  <td align='left' valign='top'>&nbsp;</td>";
			echo "  <td align='left' valign='top' colspan='6'>$ProductionFG_Remark	</td>";
			echo "</tr>";
		}	
		
		if ($PrtSer == 1) {
			$querysr = " SELECT SerialNo_Code ".
					   " FROM Purchasesserialno ".
					   " WHERE Purchasesserialno.ProductionFG_ID = '".$ProductionFGID."' ".
					   " AND Purchasesserialno.Journal_Type = 'PD' ";
			
			$resultsr=mysql_query($querysr,$connection) or die(mysql_error());
			$num=mysql_num_rows($resultsr);
			$SerialCode='';
			if ($num != 0)
			{
				while($rowsr=mysql_fetch_array($resultsr)){
					$SerialCode = $SerialCode." , ".$rowsr[0];
				}
				
				$SerialCode = substr($SerialCode,2);
				echo "<tr><td align=center valign=top class='sr'>Serial No :</td><td colspan='6' valign=top class='sr'>";
				echo $SerialCode;
				echo "</td></tr>";
			}
		}
	}
	
	//echo "<tr height='10'><td colspan=7><hr></td></tr>";
	echo "<tr height='5'><td colspan=10><h4></td></tr>";
	
	echo "<tr>";
	echo "   <td align='left' colspan='5'>&nbsp;</td>";
	echo "	 <th align='left' class='labels'>Total Qty.</th>";
	echo "	 <th align='right' class='amounts'>$TotalQty</th>";
	echo "   <td align='left' colspan='2'>&nbsp;</td>";
	echo "</tr>";
	
	echo "</table></td></tr>";
	
	
	$sub_query1 = " Select ProductionRM.ProductionRM_ID As ProductionRMID, ProductionRM.Item_CodePacked, ProductionRM.Item_Code As ItemCode, Item_Description As ItemDescription, ".
				  " ProductionRM.Warehouse_Code As WarehouseCode, ProductionRM_Quantity, ProductionRM_IdealQty, ProductionRM_Remark, ".
				  " Round(ProductionRM_Cost, ".$DecimalBase.") As ProductionRM_Cost, ProductionRM_CostBase1, ProductionRM_CostBase2, ".
				  " ifNull(ProductionRM_IdealQty,0) - ifNull(ProductionRM_Quantity,0) As ProductionRM_Waste, ".
				  " Round((ProductionRM_Quantity*ProductionRM_Cost), ".$DecimalBase.") As TotalActual, ".
				  " Round((ProductionRM_IdealQty*ProductionRM_Cost), ".$DecimalBase.") As TotalIdeal, ".
				  " Date_Format(ProductionRM_ExpireDate,'%d-%m-%Y') As ProductionRM_ExpireDate, ProductionRM.ProductionRM_BatchNo ".
				  " From ProductionRM Left Join Items on ProductionRM.Item_Code = Items.Item_code ".
				  " Where ProductionRM.ProductionHD_ID = ".$ProductionId;
				  
	
	//echo $sub_query1;
	echo "<tr><td align='left'>";
	echo "<table border=0 width='735' cellspacing=2 class='detail' style='table-layout:fixed;' >
		  <col width=100>
		  <col width=185>
		  <col width=85>
		  <col width=60>
		  <col width=60>
		  <col width=60>
		  <col width=100>
		  <col width=100>";
	
	echo"<tr height='5'><td colspan=8><h4></td></tr>";	
	
	echo"<tr bgcolor='#D8D8D8' height='20'>";
	echo"  <td align='center' colspan='8'><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='left'>Ideal</th>";	
	echo "	 <th align='center'>Qty</th>";
	echo "	 <th align='center'>Diff</th>";
	echo "	 <th align='center'>Cost </th>";
	echo "	 <th align='center'>Total </th>";
	echo "</tr>";			
		  
	//echo "<tr height='10'><td colspan=7><hr></td></tr>";
	echo "<tr height='5'><td colspan=8><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 + $ProductionRM_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'>".number_format($ProductionRM_IdealQty, $DecimalBase)."</td>";
		echo "	<td align='right' valign='top'>".number_format($ProductionRM_Quantity, $DecimalBase)."</td>";
		echo "	<td align='right' valign='top'>".number_format($ProductionRM_IdealQty-$ProductionRM_Quantity, $DecimalBase)."</td>";
		echo "	<td align='right' valign='top'>".number_format($ProductionRM_Cost, $DecimalBase)."</td>";
		echo "	<td align='right' valign='top'>".number_format($TotalActual, $DecimalBase)."</td>";
		echo "</tr>";
		
	
		
		if ($ProductionRM_Remark != '') {
			echo "<tr>";
			echo "  <td align='left' valign='top'>&nbsp;</td>";
			echo "  <td align='left' valign='top' colspan='6'>$ProductionRM_Remark	</td>";
			echo "</tr>";
		}	
		
		if ($PrtSer == 1) {
			$querysr = " SELECT SerialNo_Code ".
					   " FROM Purchasesserialno ".
					   " WHERE Purchasesserialno.ProductionRM_ID = '".$ProductionRMID."' ".
					   " AND Purchasesserialno.Journal_Type In ('PDT','PDO') ";
			
			$resultsr=mysql_query($querysr,$connection) or die(mysql_error());
			$num=mysql_num_rows($resultsr);
			$SerialCode='';
			if ($num != 0)
			{
				while($rowsr=mysql_fetch_array($resultsr)){
					$SerialCode = $SerialCode." , ".$rowsr[0];
				}
				
				$SerialCode = substr($SerialCode,2);
				echo "<tr><td align=center valign=top class='sr'>Serial No :</td><td colspan='6' valign=top class='sr'>";
				echo $SerialCode;
				echo "</td></tr>";
			}
		}
	}
	
	//echo "<tr height='10'><td colspan=7><hr></td></tr>";
	echo "<tr height='5'><td colspan= 9><h4></td></tr>";
	
	echo "<tr>";
	echo "   <td align='left' $bright;$brleft;$brtop;$brbottom; colspan='5'>&nbsp;</td>";
	echo "	 <th align='left' $bright;$brleft;$brtop;$brbottom; class='labels'>Total Qty.</th>";
	echo "	 <th align='right'$bright;$brleft;$brtop;$brbottom; class='amounts'>".number_format($TotalQty, $DecimalBase)."</th>";
	echo "   <td align='left' colspan='2'>&nbsp;</td>";
	echo "</tr>";
	
	echo "</table></td></tr>";
	
	
	$queryalpha  = " SELECT Alpha_Num ('ENGLISH', '".$Currency_Code."', ".$ProductionTotal.") As AlphaAmount FROM Dual ";
	$resultalpha = mysql_query($queryalpha,$connection) or die(mysql_error());
	$rowalpha=mysql_fetch_array($resultalpha);
		
	echo "<tr height='30'><td colspan=6 valign='top' style='font-size:12px; font-family:Times-New-Roman;'>".$rowalpha[0]."</td></tr>";
		
	echo "<tr><td align='left'>";
	echo "<table border=1 width='735' cellspacing=0 cellpadding=1  class='totals' style='table-layout:fixed;' >
		  <col width=400>
		  <col width=150>
		  <col width=130>
		  <col width=55>";
		
	echo "<tr>";
	echo "   <td align='left'$bright;$brleft;$brtop;$brbottom; >&nbsp;</td>";
	echo "	 <td align='left' class='labels'$bright;$brleft;$brtop;$brbottom;>Total $Currency_Code</td>";
	echo "	 <td align='right' class='amounts'$bright;$brleft;$brtop;$brbottom;>".number_format($ProductionTotal, $DecimalBase)."</td>";
	echo "	 <td align='center' class='totalborder'$bright;$brleft;$brtop;$brbottom;>&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;
?>

