<?
session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}
?>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
	tr{background-color:white}
	th{background-color:steelblue;color:white}
</style>
<center>
<?
include("../opendb.php");
include("../test.php");
$sq="select distinct orderno,orderto  from purorder_spare where brcode='$brcode' and qty>sup_qty and closed='N' group by orderno order by orderto";
$res=mysqli_query($db,$sq) or die(mysqli_error());
while($rr=mysqli_fetch_array($res))
{
 $bname=getbr($rr[1]);
 $i=1;
 print "<fieldset width=90%><legend> Order No : $rr[0], Order to: $bname</legend>";
$re=mysqli_query($db,"select * from purorder_spare where brcode='$brcode' and orderno='$rr[0]' and qty>sup_qty");
print "<table border=1 width=90%> <th width=30> NO <th width=110>Date <th width=100>Part Code <th>Part Name  <th width=50>P Qty";
  while($row=mysqli_fetch_array($re))
  {
    print "<tr><td>".$i++."</td><td>$row[date]</td><td>$row[partcode]</td><td>$row[partname]</td>";
    print "<td align=right>".($row[qty]-$row[sup_qty])."</td>";
  }
 print "</table> </fieldset><br>";
}

function getbr($cde)
{
 $sq="select brname from branch where brcode='$cde'";
 $brres=mysqli_query($db,$sq)or die(mysqli_error());
 if(mysqli_num_rows($brres)<=0)
 {
  $sq="select supname from sup_master where code='$cde'";
  $re3=mysqli_query($db,$sq)or die(mysqli_error());
  $re9=mysqli_fetch_row($re3);
  $bname=$re9[0];
 }  
 else
 {
   $brrow=mysqli_fetch_row($brres);
   $bname=$brrow[0];
 }
 return $bname;
}
?>
