<? session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}


?>

<?



$adr='vehtmpcls';




$sq="delete from ".$adr;
mysqli_query($db,$sq);
$sql="create table ".$adr." (da varchar(100),config varchar(250),mcode varchar(100), engno varchar(200),";
$sql.="chno varchar(100),pdate date, pvalue double(14,2), pinv varchar(100),rem varchar(20), loc varchar(200))";

mysqli_query($db,$sql);

$re=mysqli_query($db,"select dano,config,mcode,engno,chno,pdate,pval,pinv,loc from vehopstock where brcode='$brcode' order by pdate");
while($row=mysqli_fetch_row($re))
{
 $sq="insert into ".$adr." values('$row[0]','$row[1]','$row[2]','$row[3]','$row[4]','$row[5]','$row[6]','$row[7]','OP','$row[8]')";
 //print $sq;
 mysqli_query($db,$sq) or die(mysqli_error());
}

$re=mysqli_query($db,"select da,config,mcode,engno,chasis,entrydate,amt2,invno,loc from veh_purchase
 where  brcode='$brcode' and oldornew='-' and entrydate <='$dt'  order by entrydate")or die(mysqli_error());
$i=1;
while($row=mysqli_fetch_row($re))
{
 $sq="insert into ".$adr." values('$row[0]','$row[1]','$row[2]','$row[3]','$row[4]','$row[5]','$row[6]','$row[7]',' ','$row[8]')";
 //print $i++.$sq."<br>";
 mysqli_query($db,$sq) or die(mysqli_error());
}

//*****************  sales return 
$sql="select * from veh_salereturn where date<='$dt' and brcode='$brcode' ";
    $result=mysqli_query($db,$sql)or die(mysqli_error());
 while($row=mysqli_fetch_array($result))
 {
   $chres=mysqli_query($db,"select engno,chno,loc,pinv,pval,pdate from vehopstock where dano='$row[da]'");
  if(mysqli_num_rows($chres)>0)
  { 
  $chrow=mysqli_fetch_array($chres);
 mysqli_query($db,"insert into ".$adr." values('$row[da]','$row[config]','$row[mcode]','$chrow[0]','$chrow[1]','$chrow[pdate]','$chrow[pval]','$chrow[pinv]','','$chrow[2]')")or die('err:'.mysqli_error());
 }
  else
{
$chres1=mysqli_query($db,"select engno,chasis,loc from veh_purchase where da='$row[da]'");
  if(mysqli_num_rows($chres1)>0)
  { 
  $chrow1=mysqli_fetch_array($chres1);
 mysqli_query($db,"insert into ".$adr." values('$row[da]','$row[config]','$row[mcode]','$chrow1[0]','$chrow1[1]','$row[date]','$row[netamt]','$row[ret_num]','','$chrow1[2]')")or die('err:'.mysqli_error());
 }
}
 }

//*****************  purchase return 
$sql="select * from veh_purchreturn where date<='$dt' and brcode='$brcode' ";
    $result=mysqli_query($db,$sql)or die(mysqli_error());
 while($row=mysqli_fetch_array($result))
 {
  mysqli_query($db,"delete from ".$adr." where da='$row[da]' ")or die(mysqli_error());
 }

//**********
$re=mysqli_query($db,"select da from veh_invoice where brcode='$brcode' and invdate<='$dt'  order by invdate");
$i=1;
while($row=mysqli_fetch_row($re))
{
 $sql="delete from ".$adr." where da='$row[0]' limit 1";
 mysqli_query($db,$sql);
}

return;


//***********88888888888888888*****************************



?>
