<?
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>
<?
if($show)
{
 $dfrom=$y.'-'.$m.'-'.$d;
 $dto=$y1.'-'.$m1.'-'.$d1;
print "<center>Invoice Summary from $dfrom to $dto";
$tbl="invsum1".$user;
mysqli_query($db,"create table $tbl (partcode varchar(100),partname varchar(100),qty double(14,2),brcode varchar(10))");
mysqli_query($db,"delete from $tbl");

$sql="select partcode,partname,sum(qty),brcode from spareinvoice where invdate>='$dfrom' and invdate<='$dto' group by brcode,partcode";

$re=mysqli_query($db,$sql);
while($row=mysqli_fetch_array($re))
{
 $sql="insert into $tbl values('$row[0]','$row[1]','$row[2]','$row[3]')";
 mysqli_query($db,$sql) or die(mysqli_error());
}
//**************************
$sql="select partcode,partname,sum(qty),brcode from jcardparts where date>='$dfrom' and date<='$dto' group by brcode,partcode";
$re1=mysqli_query($db,$sql);
while($row=mysqli_fetch_array($re1))
{
 $sql="insert into $tbl values('$row[0]','$row[1]','$row[2]','$row[3]')";
 mysqli_query($db,$sql) or die(mysqli_error());
}
print "<table border=1><th>Partcode<th>Part Name";
$i=0;
$re3=mysqli_query($db,"select distinct brcode from $tbl order by brcode");
while($row=mysqli_fetch_array($re3))
{
 print "<th>".getbrname($row[0])."</th>";
 $ar[$i]=$row[0];
 $i++;
}
print "<th>Total";
$re4=mysqli_query($db,"select distinct partcode,partname from $tbl order by brcode");
while($row=mysqli_fetch_array($re4))
{
 $t=0;
 print "<tr><td>$row[0]</td><td>$row[1]</td>";
 for($k=0;$k<$i;$k++)
 {
 $sql="select sum(qty) from $tbl where partcode='$row[partcode]' and brcode='$ar[$k]' group by partcode";

  $sres=mysqli_query($db,$sql)or die("Err".mysqli_error());
  $srow=mysqli_fetch_array($sres);
  print "<td align=right>&nbsp;$srow[0]</td>";
 $t+=$srow[0];
 }
 print "<td align=right>".putComa($t)."</td>";
}

return;
} // end if show
?>

<form action='<?print $PHP_SELF?>' name='f1'>
<table>
<tr><td bgcolor='steelblue' colspan=2><center><b><font color=white>SPARE INVOICE SUMMARY</td></tr>
<tr><td>Date From </td><td>dd<input type=text name='d' size=2 value='<?print date("d");?>'>
			mm<input type=text name='m' size=2 value='<?print date("m");?>'>
			yy<input type=text name='y' size=4 value='<?print date("Y");?>'>
</td></tr>
<tr><td>Date To </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>
<th colspan=2><INPUT TYPE=SUBMIT VALUE='SHOW' name='show'></td></tr>
</table>
</form>
