<? session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}
include("../opendb.php");
include("../test.php");
$sqldel="delete from vehstkgw where brcode='$brcode'"; 
 $rrdel=mysqli_query($db,$sqldel)or die (mysqli_error());

?>
<style>
body,td{font-family:'arial narrow';letter-spacing:1;font-size:13}
th{background-color:green;color:white;font-size:13}
.mnu{margin-left:5}
</style>
<body bgcolor='ivory'>
<h3>VEHICLE STOCK REPORT<hr></H3>


<?
$i=1;
$tot=0;
$res=mysqli_query($db,"select * from vehopstock where sold='-' and brcode='$brcode' order by pdate");
while($row=mysqli_fetch_array($res))
{
$ag=(strtotime(date("Y-m-d"))-strtotime($row[pdate]))/86400;
//$ag=date("Y-m-d");

 $sq="select * from blockda where da='$row[dano]' and brcode='$brcode'"; 
 $rr=mysqli_query($db,$sq)or die (mysqli_error());
  if(mysqli_num_rows($rr)>0)
    $cnt=1;
  else
    $cnt=0;
 if($cnt==1)
   print "\n<tr bgcolor=bfcaf3>";
 else
   print "\n<tr>";

 //print "<td>".$i++."</td><td>$row[dano]</td><td>$row[config]</td><td>$row[mcode]</td><td>$row[engno]</td>";;
 //print "<td>$row[chno]</td><td>".datetohtml($row[pdate])."</td>";
//*********
$rrcat=mysqli_query($db,"select cat from veh_master where config='$row[config]' and brcode='$brcode'");
$rowcat=mysqli_fetch_row($rrcat);
//print "<td>$rowcat[0]</td>";
//**********
if($level>=3)
 //print "<td align=right>".putComa($row[pval])."</td>";

 //print "<td>$row[pinv]</td><td><font color='brown'>Op</td>";

  $sq="select lname from loc_master where code='$row[loc]' and brcode='$brcode'";  
 $res1=mysqli_query($db,$sq)or die(mysqli_error());
 $loc_row=mysqli_fetch_row($res1);
  //print "<td>$loc_row[0]</td><td>$ag</td><td align=center>";

if($level>=2)
{
  if($cnt==1)
  {
   //print "<input type=button value='C' onClick=blk('$row[dano]','clr') style={width:19}></td></tr>";
  }
  else
  {
   //print "<input type=button value='B' onClick=blk('$row[dano]') style={width:19}></td></tr>";
  }
}
$j=$i-1;

$sqlins="insert into  vehstkgw values('$j','$row[dano]','$row[config]','$row[mcode]','$row[engno]','$row[chno]','$row[entrydate]','$rowcat[0]','$row[amt2]','$row[invno]','Op','$loc_row[0]','$ag','','$brcode')"; 
 $rrdel=mysqli_query($db,$sqlins)or die (mysqli_error());
 $tot+=$row[pval];
}
//**********************************************************
$res=mysqli_query($db,"select * from veh_purchase where sale_invoice='-' and type !='pur-ret' and brcode='$brcode' order by config,entrydate")or die(mysqli_error());
while($row=mysqli_fetch_array($res))
{

$ag=(strtotime(date("Y-m-d"))-strtotime($row[entrydate]))/86400;
//$ag=$row[invdate];

 $sq="select * from blockda where da='$row[da]' and brcode='$brcode'"; 
 $rr=mysqli_query($db,$sq)or die (mysqli_error());
  if(mysqli_num_rows($rr)>0)
    $cnt=1;
  else
    $cnt=0;
if($cnt>0)
 print "\n<tr bgcolor=bfcaf3>";
else
 //print "\n<tr>";

//print "<td>".$i++."</td><td>$row[da]</td><td>$row[config]</td><td>$row[mcode]</td><td>$row[engno]</td>";;
 //print "<td>$row[chasis]</td><td>".datetohtml($row[entrydate])."</td>";
//*********
$rrcat=mysqli_query($db,"select cat from veh_master where config='$row[config]' and brcode='$brcode'");
$rowcat=mysqli_fetch_row($rrcat);
//print "<td>$rowcat[0]</td>";
//********

if($level>=3)
 //print "<td align=right>".putComa($row[amt2])."</td>";
//print "<td>$row[invno]</td>";
 if($row[type]=='trans-in')
  {
   //print "<td><font color='brown'> Transfer</td>";
 $res2=mysqli_query($db,"select lname from loc_master where code='$row[loc]'");
 $loc_row=mysqli_fetch_row($res2);
  //print "<td>$loc_row[0]</td><td>$ag</td></tr>";
}
else
 {
  //print "<td><font color='brown'>&nbsp;</td>";
 $res2=mysqli_query($db,"select lname from loc_master where code='$row[loc]'");
 $loc_row=mysqli_fetch_row($res2);
  //print "<td>$loc_row[0]</td><td>$ag</td><td align=center>";
}
 $rr=mysqli_query($db,"select * from blockda where da='$row[da]' and brcode='$brcode'");
if($level>=2)
{
  if(mysqli_num_rows($rr)>0)
  {
   //print "<input type=button value='C' onClick=blk('$row[da]','clr') style={width:19}></td>";
  }
  else
  {
   //print "<input type=button value='B' onClick=blk('$row[da]') style={width:19}></td>";
  }
}

 $tot+=$row[amt2];
$j=$i-1;

$sqlins="insert into  vehstkgw values('$j','$row[da]','$row[config]','$row[mcode]','$row[engno]','$row[chasis]','$row[entrydate]','$rowcat[0]','$row[amt2]','$row[invno]','','$loc_row[0]','$ag','','$brcode')"; 
 $rrdel=mysqli_query($db,$sqlins)or die (mysqli_error());
}
if($level>=3)
//print "\n<tr bgcolor=green style={color:white}><td colspan=8 align=center><b>Total </td><td align=right>".putComa($tot)."</td><td colspan=4></td></tr>";

?>
</table>

<script>
 function blk(x,y)
 {
  if(y)
  {
   if(confirm('Do you want to Clear the DA '+x+' ?'))
    document.location='blockda.php?da='+x+'&type='+y;
  }
else
   {
    if(confirm('Do you want to block the DA '+x+' ?'))
      document.location='blockda.php?da='+x;
  }
 }
</script>




<table border=1>
<th>No<th>DA  <th>Config<th>M.Code <th>Eng NO <th>Ch No <th>Category<th>Pur Date
<?
if($level>=3)
 print "<th>P.Value";
?>
<th>Pur Inv.
<th>Rem<th>Loc <th>age<th>Blk

<?
$resultstk=mysqli_query($db,"select * from vehstkgw where  brcode='$brcode' order by cat")or die(mysqli_error());
$k=1;
while($rowstk=mysqli_fetch_array($resultstk))
{

print "<tr><td>$k</td><td>$rowstk[da]</td><td>$rowstk[config]</td><td>$rowstk[mcode]</td><td>$rowstk[engno]</td><td>$rowstk[chno]</td><td>$rowstk[cat]</td><td>$rowstk[purdate]</td><td>$rowstk[pvalue]</td><td>$rowstk[purinv]</td><td>$rowstk[rem]</td><td>$rowstk[loc]</td><td>$rowstk[age]</td><td>$rowstk[blk]</td></tr>";
$k++;

}
print "\n<tr bgcolor=green style={color:white}><td colspan=8 align=center><b>Total </td><td align=right>".putComa($tot)."</td><td colspan=4></td></tr>";
