<?
 session_start();
if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); parent.document.location='ind.php' </script>";
  return;
}
include("../opendb.php");
include("../test.php");
?>
<style>
body,td{font-family:'arial narrow';letter-spacing:1;font-size:13}
tr{background-color:white} 
th{background-color:c1d799;font-size:13}
.mnu{margin-left:5}
body{BACKGROUND-COLOR:fcfff1}
</style>
<center>
<font size=4>
<b><?print $branch?></b> <br>
AGE WISE STOCK REPORT </font> <br>
[printed by <?print $user.' on '.date('d-m-Y').' at '.date("h:i:s A");?>]
<br>&nbsp;<br>&nbsp;

<?
mysqli_query($db,"delete from tmp1");
$sql="create table tmp1 (da varchar(100),config varchar(250),";
$sql.="chno varchar(100),pdate date, pvalue double(14,2),loc varchar(200))";
mysqli_query($db,$sql);

$re=mysqli_query($db,"select dano,config,chno,pdate,pval,loc from vehopstock where brcode='$brcode' order by config");
while($row=mysqli_fetch_row($re))
{
 $sq="insert into tmp1 values('$row[0]','$row[1]','$row[2]','$row[3]','$row[4]','$row[5]')";
  mysqli_query($db,$sq) or die('err1'.mysqli_error());
}

$re=mysqli_query($db,"select da,newconfig,chasis,entrydate,amt2,loc from veh_purchase where brcode='$brcode'  order by entrydate")or die(mysqli_error());$i=1;
while($row=mysqli_fetch_row($re))
{
 $sq="insert into tmp1 values('$row[0]','$row[1]','$row[2]','$row[3]','$row[4]','$row[5]')";
// print $i++.$sq."<br>";
 mysqli_query($db,$sq) or die('err2'.mysqli_error());
}

$re=mysqli_query($db,"select da from veh_invoice where brcode='$brcode'");
$i=1;
while($row=mysqli_fetch_row($re))
{
 $sql="delete from tmp1 where da='$row[0]'";
 mysqli_query($db,$sql);
}
$rcount=mysqli_query($db,"select count(*),sum(pvalue) from tmp1");
$amtrow=mysqli_fetch_row($rcount);

print "<font size=3>Total Vehicles in stock <b>".$amtrow[0];
print "<br>Total Amount Rs.".putComa($amtrow[1])."<hr>";


//***********************************
$sumres=mysqli_query($db,"select config,count(config) from tmp1 group by config order by config");
print "<table border=1><th>No.<th>Config <th>Count";
$i=1;
while($sumrow=mysqli_fetch_row($sumres))
{
print "<tr><td align=center>".$i++."</td><td>$sumrow[0]</td><td align=right>$sumrow[1]</td></tr>";
}
print "</table>";
//***********************************

$res=mysqli_query($db,"select distinct config from tmp1") or die(mysqli_error());
$k=1;
while($row1=mysqli_fetch_array($res))
{
  print "<center> Vehicle # <b>".$k++;
 print "<table cellspacing=1 cellpadding=2 bgcolor=gray width=100%>";
 print "<th width=10>No<th  width=90>DA  <th  width=250>Config <th width=100>Ch No <th>Pur Date";
 print "<th width=65>Age(Days) <th width=100> P.Value <th>Loc";
 $veres=mysqli_query($db,"select *,to_days(curdate())-to_days(pdate) Age from tmp1 where config='$row1[0]' order by pdate")or die(mysqli_error());
   $i=1;
   $t=0;
  while($row=mysqli_fetch_array($veres))
  {
  $lres=mysqli_query($db,"select lname from loc_master where code='$row[5]'");
  $lorow=mysqli_fetch_row($lres);
  print "<tr><td align=center>".$i++."</td><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td>";
  print "<td align=center>".datetohtml($row[3])."</td>";
  print "<td align=center>$row[Age]</td><td align=right>$row[4]</td></td><td align=center> $lorow[0]</td>";
 $t+=$row[4];
 }
 print "<tr><td colspan=6 align=right><b>Total </td><td align=right>".putComa($t)."</td><td>&nbsp;</td></tr>";
 print "</table>";
}

?>
