<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}

print <<< HERE
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<TITLE> Items Summary </TITLE>
<script type="text/javascript" language="javascript">

</script>
<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;
				  border-top:1px solid black;
				  border-bottom:1px solid black;
				  border-left:1px solid black;
                }				 
				 
table.detail td { font-size: 12px; 
                  font-family:Times-New-Roman;
				  border-left:1px solid black;
                 }			
h1.breakhere { page-break-after: auto;}		
.style88 {
	font-size: 16px;
    font-weight: bold;
}

.style84 {
	font-size: 14px;
    font-family: sans-serif;
}		 
</style>
</HEAD>
<BODY>
HERE;

include("config.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536)	or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

$connection1 = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536)	or die("Could not connect to DB");
mysql_select_db($config['dbName_log'],$connection1) or die("Could not find DB");

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

//mysql_set_charset('utf8',$connection);

//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, Setup_vatno, ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage, ".
			 " Company_Name, Company_NameF, Company_Memo, Company_MemoF, Company_Capital, Company_CapitalF ".
			 " from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

$style="style='border:1px solid black; text-align:right;'";
echo"<table border='0' width='1100px' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
		<col width=200>
		<col width=300>
		<col width=300>";
		//T-header
echo"	<tr>
			<td align=' left' valign='top' style='font-size:14px; font-style:italic; font-weight:bold;' colspan=3>
			<table>
			<tr>
			<td rowspan=2><img src='LogoFile.php' ></td>
			<td style='vertical-align: top;'><br><span style='font-size: 20px; font-weight: bold'>$Company_Name</span><br>
			<span style='font-size: 11px;'><pre>".str_replace( '\n', '<br>', $Company_Memo)."</pre></span></td>
			</tr>
			
			</table>
			<td>
			
		<tr>";
		
		//2nd row
		
echo "	<tr>
			<td align='right' colspan='2' valign='top' style='font-size:20px; font-weight:bold;'>Items Summary<td>
			<td align='left' valign='top' style='font-size:14px; font-style:italic; font-weight:bold;'>
				<td colspan='2'><b>Printed Date:</b>";
			echo date("d/m/Y")."<br>";
				echo "<span><b>Time:</b></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".date("h:i:sa");
				echo "</td></tr>";
				
echo "			</table>
			</td>
		</tr>
	</table>";
	
	
echo "<table border='0' style='table-layout:fixed' width='600px'>
		  <col width=30><col width=380><col width=120><col width=100>";
echo" <tr height='30'><td></td>";	  
echo"   <td><span class='style88'>Finished Item Code / Description</span></td><td><span class='style88'>Quantity</span></td><td></td>";
echo" </tr>";	
$query = "select Calculation_id, item_codefg,item_description, qty
		from RM_Calculation
		inner join items on items.item_code = item_codefg
		where  user_code = '".$_SESSION['useraccount']."'
		order by Calculation_id";
$res = mysql_query($query,$connection);
$numrow = mysql_num_rows($res);

while($rw = mysql_fetch_row($res)){
	
	echo "<tr>";
	echo "<td></td>";
	echo "<td><span class='style84'>$rw[1] - $rw[2]</span></td>";
	echo "<td><span class='style84'>$rw[3]</span></td>";
	echo "<td></td>";
	echo "</tr>";
}	

echo"<tr height='30px'><td></td></tr>";
echo "</table>";

echo"<table border='0' style='table-layout:fixed' width='670px'>
		  <col width=30><col width=350><col width=90><col width=100><col width=100>";	
echo "<tr><td colspan='5'><hr></td></tr>";
echo" <tr height='30'><td></td>";	  
echo"   <td><span class='style88'>Raw Material Code / Description</span></td><td><span class='style88'>Quantity</span></td><td><span class='style88'>Cost</span></td><td><span class='style88'>Total</span></td>";
echo" </tr>";

$ndate = explode('/',$fromdate); 	  
$date = $ndate[2]."-".$ndate[1]."-".$ndate[0];
$query = "select Calculationdt_id, RM_Calculationdt.Calculation_id, item_coderm,items.item_description, RM_Calculationdt.qty, 
			if('$cur'='$base1',MeanCostCur(item_coderm,'$date', 1, '$cur','$cur') , MeanCostCur(item_coderm,'$date', 2, '$cur','$cur') ) 
			from RM_Calculationdt
			inner join items on items.item_code = item_coderm
			inner join RM_Calculation on RM_Calculation.Calculation_id = RM_Calculationdt.Calculation_id
			where user_code = '".$_SESSION['useraccount']."'
			order by Calculationdt_id, Calculation_id ;"; 
	$res = mysql_query($query,$connection);

while($rw = mysql_fetch_row($res)){
	$totalitem  = 0;
	echo" <tr height='30'><td></td>";	  
	echo" <td><span class='style84'>$rw[2] - $rw[3]</td>";
	echo" <td><span class='style84'>$rw[4]</span></td>";
	echo" <td><span class='style84'>".number_format($rw[5],2)."</span></td>";
	$totalitem = $rw[4]*$rw[5];
	echo" <td><span class='style84'>".number_format($totalitem,2)."</span></td>";
	echo"</tr>";
	$totallitem = $totallitem + $totalitem; 
}
echo "<tr><td colspan='5'><hr></td></tr>";
echo" <tr height='30'><td></td>";	  
echo" <td><span class='style84'></td>";
echo" <td><span class='style84'></td>";
echo" <td><span class='style84'><b>Total: </b></span></td>";
echo" <td><span class='style84'>".number_format($totallitem,2)."</span></td>";
echo"</tr>";
echo"   </table>";

print <<< HERE

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