<?
 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>
<p align=right>

</p>
<center>
<h2><?print $branch?> <br>AGE WISE STOCK REPORT </H2>
<form action='<?print $PHP_SELF?>' method=post>
<table bgcolor='silver'>
<tr><td> Date from </td><td>dd<input type=text name='d' size=2 value='1'>
				mm<input type=text name='m' size=2 value='<?print date('m',time())?>'>
				yy<input type=text name='y' size=4 value='<?print date('Y',time())?>'>
			</td></tr>
<tr><td> To </td><td>dd<input type=text name='d1' size=2 value='<?print date('d',time())?>'>
				mm<input type=text name='m1' size=2 value='<?print date('m',time())?>'>
				yy<input type=text name='y1' size=4 value='<?print date('Y',time())?>'>
			</td>
</tr>
<th colspan=2 align=center><input type=submit name='show' value='Show'>
</td></tr>
</table>
</form>
<?
$df=$y.'-'.$m.'-'.$d;
$dt=$y1.'-'.$m1.'-'.$d1;
print "<input type=button value='Print' onClick=window.open('prnagerepo.php?df=$df&dt=$dt')>";
print "<br>";

$tbl="tmp1".$user;
mysqli_query($db,"delete from $tbl");
$sql="create table $tbl (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' and transtype!='pur-ret' order by config");
while($row=mysqli_fetch_row($re))
{
 $sq="insert into $tbl 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,config,chasis,entrydate,amt2,loc from veh_purchase where brcode='$brcode'   and type !='pur-ret'  and sale_invoice='-' order by entrydate")or die(mysqli_error());$i=1;

while($row=mysqli_fetch_row($re))
{
 $sq="insert into $tbl 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' and da not in(select da from veh_salereturn where brcode='$brcode')");
$i=1;
while($row=mysqli_fetch_row($re))
{
 $sql="delete from $tbl where da='$row[0]'";
 mysqli_query($db,$sql);
}
/*
$re=mysqli_query($db,"select da,newconfig,chasis,entrydate,amt2,loc from veh_purchase where brcode='$brcode' and type='trans-in' and sale_invoice='-'  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(mysqli_error());
}
*/
$rcount=mysqli_query($db,"select count(*),sum(pvalue) from $tbl");
$amtrow=mysqli_fetch_row($rcount);

print "<font size=3>Total Vehicles in stock <b>".$amtrow[0];
//print "<br>Total Amount ".putComa($amtrow[1])."<hr>";
print "<br><b>Report from ".datetohtml($df).' to '.datetohtml($dt)."</b><br>";
$res=mysqli_query($db,"select *,to_days(curdate())-to_days(pdate) Age from $tbl where  pdate<='$dt' order by Age desc") or die(mysqli_error());
$k=1;
 print "<table cellspacing=1 cellpadding=2 bgcolor=gray width=100%>";
 print "<th width=10>No<th  width=90>DA  <th  width=210>Config <th>Ch No <th>Pur Date";
 print "<th>Age(Days)  <th>Loc";

while($row=mysqli_fetch_array($res))
{
  $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=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><br>&nbsp;";

?>
