<?php
session_start();

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

include("config.inc.php");

if($exp ==1){
	$filename = "stocktake_" . date('Ymd') . ".xls";
	header("Content-Disposition: attachment; filename=\"$filename\"");
	header("Content-Type: application/vnd.ms-excel");
} 

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

$query_setup  = "select company_name, company_memo, DecimalBase, DecimalBase1, DecimalBase2, Setup_VatPercentage, base1, base2, setup_invoiceremarks from setup";
$result_setup = mysql_query($query_setup, $connection); 
$row_setup    = mysql_fetch_array($result_setup);
extract($row_setup);
print <<<HERE
<html>
<head>
<title>Stock Take REPORT</title>
</head>
<body>
HERE;

$query = " Select stocktake_ID, stocktake_Reference, Date_Format(stocktake_Date,'%d/%m/%Y') As stocktakeDate, ".
	         " stocktake.Currency_Code As CurrencyCode, stocktake_Remak, stocktake_Type, stocktake_EntryType As AdjustType, ".
			 " stocktake.Project_Code As ProjectCode, Project_Name, ".
             " stocktake.Costcent_code As Costcentcode, Costcent_name,  ".
			 " Round(stocktake_Amount, ".$DecimalBase.") As stocktakeAmount, ".
			 " Round(stocktake_AmountBase1, ".$DecimalBase1.") As stocktakeAmountBase1, ".
			 " Round(stocktake_AmountBase2, ".$DecimalBase2.") As stocktakeAmountBase2 ".
			 " , user_account as PreparedBy".
			 " From stocktake ".
			 " Left Join Project on stocktake.Project_Code = Project.Project_Code ".
			 " Left Join CostCent on stocktake.Costcent_code = CostCent.CostCent_Code ".
	         " Where stocktake_ID = ".$stocktakeId;
			 
			 
	$result=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result)){
		extract($rows);
	}
	if($AdjustType == 'O') $Title = "Return Outward";
	if($AdjustType == 'I') $Title = "Return Inward";
echo "<table width=800>
<tr>
<td width=50%><img src='LogoFile.php' width=180 /></td>
<td width=50% style='text-align: center; font-family: Arial; font-size: 40px;  color: #a6a6a6;'>&nbsp;&nbsp;&nbsp;  $Title</td>
</tr>
</table>
<table width=800>
<tr style='height: 20px'>
<td></td>
</tr>
</table>
<table width=800>
<tr>
<td width=500 style='font-family: Arial; font-size: 14px'><i><b><u>$company_name</u></b></i>
<br><b>
<pre>$company_memo</pre>
</td>
<td style='width: 300px; vertical-align: top;'>
<table width=100%>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>DATE:</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 13px'>$stocktakeDate</td>
</tr>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>Invoice #:</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 13px'>$stocktake_Reference</td>
</tr>
<tr>
<td style='text-align: right; font-weight: bold; font-size: 12px; font-family: Arial; width:120px'>Prepared By:</td>
<td style='width: 20px'></td>
<td style='width: 160px; font-family: Arial; font-size: 13px'>$PreparedBy</td>
</tr>
</table>
</td>
</tr>
<tr style='height: 15px'>
<td></td>
</tr>
</table>
<table width=800>

</table>
<table width=800>";

echo "<tr style='height: 15px'>
<td colspan=2></td>
</tr>";
if($stocktake_Remak != ''){
	echo "<tr style='height: 15px'>
	<td colspan=2><b>Remark : $stocktake_Remak </b></td>
	</tr>";
}
echo "<tr>
<td colspan=2 style='font-size: 13px; font-family: Arial; font-weight: bold'>Comments or special instructions:</td>
</tr>";
echo "<tr style='height: 15px'>
<td colspan=2></td>
</tr>";
echo"</table>";
echo "<table width=800 style='border-collapse:collapse'>";
echo "<tr >";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; background: #d0d0d0'>Code</th>";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 285px; background: #d0d0d0 '>Description</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 100px; background: #d0d0d0 '>Warehouse</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 50px; background: #d0d0d0 '>Existing Qty</th>";
    echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 50px; background: #d0d0d0 '>Reading Qty</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 50px; background: #d0d0d0 '> Diff Qty</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; background: #d0d0d0 '>Cost</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 100px; background: #d0d0d0 '>Total Cost</th>";
	echo "</tr>";
	$querydt = " Select stocktakeDt.stocktakeDt_ID As stocktakeDtID, stocktakeDt.Item_CodePacked, stocktakeDt.Item_code As ItemCode, ".
			   " Item_Description As ItemDescription, stocktakeDt.Warehouse_Code As WarehouseCode, stocktakeDt_Quantity, ".
			   " Round(stocktakeDt_Price, ".$DecimalBase.") As stocktakeDtPrice, stocktakeDt_PriceBase1, stocktakeDt_PriceBase2, ".
			   " Round((ifNull(stocktakeDt_Quantity,0)*ifNull(stocktakeDt_Price,0)), ".$DecimalBase.") As TotalAmount, stocktakeDt.Journal_Type As JournalType, ".
			   " floor(ifnull(Item_Unit2Qty,1)/ifNull(Items.Item_Unit,1)) as itemwhole,ifnull(Item_Unit2Qty,1)%ifNull(Items.Item_Unit,1) as itempiece,StockTakeDt_ExistingQty,StockTakeDt_PhysicalQty ".
			   " From stocktakeDt ".
			   " Left Join Items on stocktakeDt.Item_code = Items.Item_code ".
			   " Where stocktakeDt.stocktake_ID = ".$stocktakeId;
	$resultdt = mysql_query($querydt, $connection);
	$TotalQty = 0;
	while($rowdt = mysql_fetch_array($resultdt)){
		extract($rowdt);
		$TotalQty = $TotalQty + $StockTakeDt_PhysicalQty;
		
		echo "<tr >";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; vertical-align: top; '>$ItemCode</th>";
	echo "   <th align='left' style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 285px; vertical-align: top;'>$ItemDescription</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 100px; vertical-align: middle; '>$WarehouseCode</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 50px; vertical-align: middle; text-align: right;'>$StockTakeDt_ExistingQty</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 50px; vertical-align: middle; text-align: right;'>$StockTakeDt_PhysicalQty</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 50px; vertical-align: middle; text-align: right;'>$stocktakeDt_Quantity</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; vertical-align: middle; text-align: right;'>".number_format($stocktakeDtPrice, $DecimalBase)."</th>";
	echo "	 <th align='center'  style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 100px; vertical-align: middle; text-align: right;'>(".number_format(abs($TotalAmount), $DecimalBase).")</th>";
	echo "</tr>";
	}
	//echo $total_discountamount_oninvoice;
	$total_afterdiscount=$total_beforediscount;
	echo "<tr>
	<td colspan=7 style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; background: #d0d0d0; text-align: right; border-bottom: none; font-weight: bold'>Total Qty</td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 100px; vertical-align: middle; text-align: right; border-bottom: none'>$TotalQty</td>
	</tr>
	<tr>
	<td colspan=7 style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 76px; background: #d0d0d0; text-align: right; border-top: none; font-weight: bold'>Total $CurrencyCode</td>
	<td style='border: solid 1px #000000; font-family: Arial; font-size: 13px; padding: 5px; width: 100px; vertical-align: middle; text-align: right; border-top: none; '>(".number_format(abs($stocktakeAmount), $DecimalBase).")</td>
	</tr>
	<tr style='height: 35px'>
	<th colspan=6></th>
	</tr>
	<tr>
	<td colspan=6 style='font-size: 13px; font-weight: none; font-family: Arial'>$setup_invoiceremarks</td>
	</tr>
	<tr style='height: 45px'>
	<th colspan=6></th>
	</tr>
	<tr>
	<th colspan=6 style='font-size: 14px; font-family: Arial'>THANK YOU FOR YOUR BUSINESS!</th>
	</tr>
	";
echo"</table>";
print <<<HERE
<script>
window.print();
</script>
</body>
</html>
HERE;
?>