<?
session_start();
include("../opendb.php");
include("../test.php");

?>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
	th{background-color:lightsteelblue;color:black}
	td{font-size:12}
.right{text-align:right};
</style>

<center>
<h3>SPARE ADJUSTMENT REPORT </H3>
<?
if(isset($_POST['show']))
{
$y2=$_REQUEST['y2'];    
$m2=$_REQUEST['m2'];
$d2=$_REQUEST['d2'];
$y1=$_REQUEST['y1'];    
$m1=$_REQUEST['m1'];
$d1=$_REQUEST['d1'];
$dt = $y2."-".$m2."-".$d2;
$df = $y1."-".$m1."-".$d1;
$re=mysqli_query($db,"select partcode,partname, sum(qty) qty,sum(outqty) outqty,brcode from adjstock where entrydate >= '$df' and entrydate <= '$dt' group by partcode,brcode order by brcode");
print "<table bgcolor='steelblue' cellspacing=1 cellpadding=2>";
print "<th>Ref No<th>Date <th>Part Code <th>Part Name <th>Rate <th>In QTY <th>Out QTY";
print "<th>Amount <th>Note";
$i=1;
$tinqty=0;
$toutqty=0;
$tamt=0;
while($row=mysqli_fetch_array($re))
{ 
    if($i%2==0)
        print "\n<tr bgcolor='aliceblue'>";
    else
        print "\n<tr bgcolor='white'>";
    print "\n<td>$row[refno]</td><td>".datetohtml($row[entrydate])."</td>";
    print "<td>$row[partcode]</td><td>$row[partname]</td>";
    $purres=mysqli_query($db,"select price,tot,supqty,taxtype from sparepurchase where partcode='$row[partcode]' order by entrydate desc limit 1");
	$purrow=mysqli_fetch_row($purres);
  	if($purrow[0]>0)
  	{
		if($purrow[3]=='CST')
		{
			$mndp=$purrow[1]/$purrow[2];
		}
		else
		{
			$mndp=$purrow[0];
		}
	}
	else
	{
		$prres=mysqli_query($db,"select ndpcost from spareprmast where partcode='$row[partcode]' order by effectfrom desc");
		$prrow=mysqli_fetch_row($prres);
		$mndp=$prrow[0];
	}
    print "<td align=right>$mndp</td><td align=right>$row[qty]</td>";
    $mtot = $mndp * $row[qty];
    if ($row[outqty]>0)
        $mtot = -1*($mndp * $row[outqty]);
    print "<td align=right>$row[outqty]</td><td align=right>".putComa($mtot)."</td>";
    print "<td>$row[brcode]</td></tr>";
    $i++;
    $tinqty+=$row[qty];
    $toutqty+=$row[outqty];
    $tamt+=$mtot;
}
?>
<tr bgcolor='lightsteelblue'><td colspan=5 align=center><b>Total</b></td>
<td align=right><?print number_format($tinqty,2,'.',',')?></td>
<td align=right><?print $toutqty?></td>
<td align=right><?print putComa($tamt)?></td>
<td>&nbsp;</td></tr>
</table>
<?}?>
<form action='<?Print $PHP_SELF?>' method='post'>
<table class=noprint>
<tr><td>Date from </td><td>dd <input type=text name='d1' size=2 value='<?print date("d")?>'>
mm <input type=text name='m1' size=2 value='<?print date("m")?>'>
yy <input type=text name='y1' size=4 value='<?print date("Y")?>'></td></tr>

<tr><td align=right>to </td><td>dd <input type=text name='d2' size=2 value='<?print date("d")?>'>
mm <input type=text name='m2' size=2 value='<?print date("m")?>'>
yy <input type=text name='y2' size=4 value='<?print date("Y")?>'>
</td></tr>
<tr><td colspan=2 align=center bgcolor='lightsteelblue'><input type=submit value='Show' name='show'></td></tr>
</table>



