<?php

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

print <<< HERE
<HTML>
<HEAD>
<TITLE> Quantity Budget Report </TITLE>

<style type="text/css">
	.header1 {
		font-size: 26px;
		font-weight: bold;
		font-family: serif;
	}
	.header2 {
		font-size: 20px;
		font-weight: bold;
		font-family: serif;
	}
	.header3 {
		font-size: 14px;
		font-family: serif;
	}
	.header4 {
		font-size: 14px;
		font-weight: bold;
		font-family: 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");

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

//Read From Setup Table							
$querysetup= " select Company_Name from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}


if ($todate == '') $todate = $fromdate;

$dispfromdate = $fromdate;
$disptodate = $todate;


if ($fromdate != ''){
	$ndate = explode('/', $fromdate);
	$fromdate = $ndate[2]."-".$ndate[1]."-".$ndate[0];
}
if ($todate != ''){
	$ndate = explode('/', $todate);
	$todate = $ndate[2]."-".$ndate[1]."-".$ndate[0];
}
$project_code = "".$project_code;
$costcent_code = "".$costcent_code;

echo "<table border = '0' cellspacing=0 cellpadding=0 width='1070px'><col width='285px'><col width='500px'><col width='285px'>";
echo "<tr><td colspan='3'><span class='header2'>$Company_Name</span></td></tr>";
echo "<tr><td colspan='3' align='center'><span class='header1'>Monthly Distribution of Quantities</span></td></tr>";
echo "<tr><td colspan='3' align='right'><span class='header3'><b>Printed Date:</b>".date("d/m/Y")."<br><b>Time:</b>".date("h:i:sa")."</span></td></tr>";
echo "<tr height='25px'><td colspan='3'><hr></td></tr>";
echo "<tr height='25px'><td colspan='3'><b>Filter By:</b></td></tr>";

if ($fromdate != '')
echo "<tr height='25px'><td colspan='3'><span class='header3'><b>&#8594;Transaction From/To Date: $dispfromdate - $disptodate</b></span></td></tr>";
if ($project_code != '')
echo "<tr><td colspan='3'><span class='header3'><b>&#8594;Project: $project_code</b></span></td></tr>";
if ($costcent_code != '')
echo "<tr><td colspan='3'><span class='header3'><b>&#8594;Costcenter: $costcent_code</b></span></td></tr>";

echo "<tr height='25px'><td colspan='3'></td></tr>";
echo "</table>";	

$btop = "border-top: 2px solid black";
$bright = "border-right: 1px solid black";
$bleft = "border-left: 1px solid black";
$bbottom = "border-bottom: 1px solid black";


echo "<table border = '0' cellspacing=0 cellpadding=0 >";
	echo "<tr height='30px'  bgcolor='#d3d3d3'>
			<td style='$bbottom;' colspan='14'></td><td style='$bbottom;'></td></tr>";

echo "<tr height='30px'>
		<td width='150px' align='center' style='$btop;$bbottom;'><span class='header4'>Item</span></td>
		<td width='200px' align='center' style='$btop;$bbottom;'><span class='header4'>Description</span></td>
		<td width='70px' align='center' style='$btop;$bbottom;'><span class='header4'>Quantity</span></td>
		<td width='60px' align='center' style='$btop;$bbottom;'><span class='header4'>Unit</span></td>
		<td width='80px' align='center' style='$btop;$bbottom;'><span class='header4'>Previous Received</span></td>
		<td width='80px' align='center' style='$btop;$bbottom;'><span class='header4'>Monthly Received</span></td>
		<td width='80px' align='center' style='$btop;$bbottom;'><span class='header4'>Total Received</span></td>
		<td width='80px' align='center' style='$btop;$bbottom;'><span class='header4'>Previous issued on the project</span></td>
		<td width='80px' align='center' style='$btop;$bbottom;'><span class='header4'>Previous issued to other project</span></td>
		<td width='80px' align='center' style='$btop;$bbottom;'><span class='header4'>Monthly issued on the project</span></td>
		<td width='80px' align='center' style='$btop;$bbottom;'><span class='header4'>Monthly issued to other project</span></td>
		<td width='80px' align='center' style='$btop;$bbottom;'><span class='header4'>Total issued on project</span></td>
		<td width='80px' align='center' style='$btop;$bbottom;'><span class='header4'>Total issued to other project</span></td>
		<td width='80px' align='center' style='$btop;$bbottom;'><span class='header4'>Total issued</span></td>
		<td width='80px' align='center' style='$btop;$bbottom;'><span class='header4'>Monthly Balance</span></td>		
	</tr>";

$query = "select budget_qtydt.item_code as item_code, item_description, Item_UnitSalesDesc, budget_qtyhd.project_code as project_code, qty
			 from budget_qtydt 
			 inner join items on items.item_code = budget_qtydt.item_code
			 inner join budget_qtyhd on budget_qtyhd.BQ_Id = budget_qtydt.BQ_Id
			 where budget_qtyhd.project_code = '$project_code'
			 order by item_code ";//echo $query;
$result = mysql_query($query, $connection);
while($row = mysql_fetch_array($result)){
	extract($row);
	if (!($ct%2))
		$bgcolor = "bgcolor=#ABD4F1";
	else 
		$bgcolor = "bgcolor=#EFF1F3";
	
	$queryPrevRecvd = "select ifnull(sum(QuantityEntered),0) from stktransaction where item_code = '$item_code' and project_code = '$project_code' and date_format(Transaction_Date,'%Y-%m-%d') < '$fromdate' 
						and Transaction_Type in ('TRT', 'AI', 'PI');"; //echo $queryPrevRecvd;
	$resPrevRcvd  	= mysql_query($queryPrevRecvd, $connection);
	$rowPrevRecvd 	= mysql_fetch_row($resPrevRcvd);
	$PrevRecvdQty 	= $rowPrevRecvd[0];
	
	$queryMonRecvd 	 = "select ifnull(sum(QuantityEntered),0) from stktransaction where item_code = '$item_code' and if('$project_code'='',1=1, project_code = '$project_code') 
						and Transaction_Type in ('TRT', 'AI', 'PI') and date_format(Transaction_Date,'%Y-%m-%d') between '$fromdate' and '$todate' ;";
	$resMonRcvd  	 = mysql_query($queryMonRecvd, $connection);
	$rowMonRecvd 	 = mysql_fetch_row($resMonRcvd);
	$MonthlyRecvdQty = $rowMonRecvd[0];
	
	$totalRecvd = $PrevRecvdQty + $MonthlyRecvdQty;
	
	$queryPrevIssued = "select ifnull(sum(abs(QuantityEntered)),0), ifnull(sum(QuantityDelivered),0) from stktransaction 
						where item_code = '$item_code' and project_code = '$project_code' and date_format(Transaction_Date,'%Y-%m-%d') < '$fromdate' and Transaction_Type in ('AR', 'PR');";
	$resPrevIssued  	= mysql_query($queryPrevIssued, $connection);
	$rowPrevIssued 	= mysql_fetch_row($resPrevIssued);
	$PrevIssuedQty 	= $rowPrevIssued[0]+ $rowPrevIssued[1];
	
	$queryPrevIssOth = "select ifnull(sum(abs(QuantityEntered)),0), ifnull(sum(QuantityDelivered),0) from stktransaction 
						where item_code = '$item_code' and project_code = '$project_code' and date_format(Transaction_Date,'%Y-%m-%d') < '$fromdate' and Transaction_Type in ('TR');";
	$resPrevIssOth  	= mysql_query($queryPrevIssOth, $connection);
	$rowPrevIssOth 	= mysql_fetch_row($resPrevIssOth);
	$PrevIssOthQty 	= $rowPrevIssOth[0]+ $rowPrevIssOth[1];
	
	$queryMonIssued 	 = "select ifnull(sum(abs(QuantityEntered)),0), ifnull(sum(QuantityDelivered),0)
						from stktransaction where item_code = '$item_code' and project_code = '$project_code' and Transaction_Type in ('AR', 'PR')
						and date_format(Transaction_Date,'%Y-%m-%d') between '$fromdate' and '$todate' ;";
	$resMonIssued  	 = mysql_query($queryMonIssued, $connection);
	$rowMonIssued 	 = mysql_fetch_row($resMonIssued);
	$MonthlyIssuedQty = $rowMonIssued[0] + $rowMonIssued[1];
	
	$queryMonIssOth	 = "select ifnull(sum(abs(QuantityEntered)),0), ifnull(sum(QuantityDelivered),0)
						from stktransaction where item_code = '$item_code' and project_code = '$project_code' and Transaction_Type in ('TR')
						and date_format(Transaction_Date,'%Y-%m-%d') between '$fromdate' and '$todate' ;";
	$resMonIssOth  	 = mysql_query($queryMonIssOth, $connection);
	$rowMonIssOth 	 = mysql_fetch_row($resMonIssOth);
	$MonthlyIssOthQty = $rowMonIssOth[0] + $rowMonIssOth[1];
	
	$totalIssuedProject = $MonthlyIssuedQty + $PrevIssuedQty;
	$totalIssuedOthers  = $MonthlyIssOthQty + $PrevIssOthQty;
	
	$totalIssued = $totalIssuedProject + $totalIssuedOthers;
	
	$MonthlyBalance = $MonthlyRecvdQty- $totalIssuedProject;
		
	echo "<tr height='20px' $bgcolor>
			<td width='180px' align='left'><span class='header3'>$item_code</span></td>
			<td width='200px' align='left'><span class='header3'>$item_description</span></td>
			<td width='70px' align='center'><span class='header3'>$qty</span></td>
			<td width='60px' align='center'><span class='header3'>$Item_UnitSalesDesc</span></td>
			<td width='80px' align='center'><span class='header3'>".number_format($PrevRecvdQty,2)."</span></td>
			<td width='80px' align='center'><span class='header3'>".number_format($MonthlyRecvdQty,2)."</span></td>
			<td width='80px' align='center'><span class='header3'>".number_format($totalRecvd,2)."</span></td>
			<td width='80px' align='center'><span class='header3'>".number_format($PrevIssuedQty,2)."</span></td>
			<td width='80px' align='center'><span class='header3'>".number_format($PrevIssOthQty,2)."</span></td>
			<td width='80px' align='center'><span class='header3'>".number_format($MonthlyIssuedQty,2)."</span></td>
			<td width='80px' align='center'><span class='header3'>".number_format($MonthlyIssOthQty,2)."</span></td>
			<td width='80px' align='center'><span class='header3'>".number_format($totalIssuedProject,2)."</span></td>
			<td width='80px' align='center'><span class='header3'>".number_format($totalIssuedOthers,2)."</span></td>
			<td width='80px' align='center'><span class='header3'>".number_format($totalIssued,2)."</span></td>
			<td width='80px' align='center'><span class='header3'>".number_format($MonthlyBalance,2)."</span></td>		
		</tr>";
	
	
		
	
	$ct++;
}

echo "<tr height='10px'><td colspan='16'></td></tr>";

echo "</table>";



print <<< HERE

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

	?>