<?
 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> <input type=button value='View / Print All' onClick=window.open('prnconrepo.php')> </p>
<center>
<h2><?print $branch?> <br>CONFIGURATION WISE STOCK REPORT </H2>
<form action='<?print $PHP_SELF?>' method=post>
<table bgcolor='silver'>
<th colspan=2>PURCHASE PERIOD</TH>
<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>

<?
if($show)
{
$df=$y.'-'.$m.'-'.$d;
$dt=$y1.'-'.$m1.'-'.$d1;
print "<b>Report from ".datetohtml($df).' to '.datetohtml($dt)."</b><br>";
$tmp1="tmp1".$user;
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'   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 $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 ".putComa($amtrow[1])."<hr>";
//***********************************
$tc=0;
$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";
while($sumrow=mysqli_fetch_row($sumres))
{
$tc=$tc+=$sumrow[1];
print "<tr><td align=center>".$i++."</td><td>$sumrow[0]</td><td align=right>$sumrow[1]</td></tr>";
}
print "<tr><td></td><td>Total</td><td align=right>$tc</td></tr>";

print "</table><br>";
//***********************************

$res=mysqli_query($db,"select distinct config from $tmp1 order by config") or die(mysqli_error());
$k=1;
while($row1=mysqli_fetch_array($res))
{
  print "<center> Congifuration # <b>".$k++;
 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> 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);

  $dares=mysqli_query($db,"select * from blockda where da='$row[0]'");
   $dabrow=mysqli_fetch_array($dares);
  if(mysqli_num_rows($dares)>0)
   { print "<tr><td align=center><font color=red>*</font>".$i++."</td><td><a href='#' title='Blocked for $dabrow[rep]'>$row[0]</a></td>";}
  else
   { print "<tr><td align=center>".$i++;
    print "<td>$row[0]</td>";
   }
 print "<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><br>&nbsp;";
}
} // end if
?>
