<?
session_start();
$brcode = $_SESSION['brcode'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); 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}
th{background-color:white}
.mnu{margin-left:5}
body{BACKGROUND-COLOR:white}
</style>
<center>
<?
$df = $_REQUEST['df'];
$dt = $_REQUEST['dt'];
    $result=mysqli_query($db,"select count(*) from vehopstock where  brcode='$brcode'")or die(mysqli_error());
    $r=mysqli_fetch_row($result);
    $tcount=$r[0];

 $sql="select count(*) from veh_purchase where entrydate<'$df' and brcode='$brcode' and oldornew='-'";
    $result=mysqli_query($db,$sql)or die(mysqli_error());

      $r=mysqli_fetch_row($result);
      $tcount+=$r[0];

    $sql="select count(*) from veh_invoice where invdate<'$df' and brcode='$brcode'";
    $result=mysqli_query($db,$sql)or die(mysqli_error());
     $r=mysqli_fetch_row($result);
      $tcount-=$r[0];

    $result=mysqli_query($db,"select count(*) from veh_purchreturn where date<'$df' and brcode='$brcode'")or die(mysqli_error());
     $r=mysqli_fetch_row($result);
      $tcount-=$r[0];

    $result=mysqli_query($db,"select count(*) from veh_salereturn where date<'$df' and brcode='$brcode'")or die(mysqli_error());
     $r=mysqli_fetch_row($result);
      $tcount+=$r[0];




 $sql="select * from veh_purchase where entrydate>='$df' and entrydate<='$dt' and brcode='$brcode'
and oldornew='-' ";
    $result=mysqli_query($db,$sql)or die(mysqli_error());
print "<h3>$branch<br>STOCK LEDGER FROM ".implode('-', array_reverse(explode('-', $df))) ." TO ".implode('-', array_reverse(explode('-', $dt)));    
?>
<table bgcolor=black cellspacing=1 cellpadding=5>
<th>Date <th> Particulars <th> Pur<br>Qty <th>Sale <br> Qty <th>Balance
<?
$tbl= $user.str_replace('.','',$REMOTE_ADDR).'led' ;
mysqli_query($db,"create table $tbl (date date,parti varchar(250),pqty int(3),sqty int (3))");

 while($row=mysqli_fetch_array($result))
 {
  mysqli_query($db,"insert into $tbl values('$row[entrydate]','Inv NO.: $row[invno] - DA: $row[da] - Config: $row[config] - Chasis: $row[chasis]','1','0')")or die(mysqli_error());
 }

 $sql="select * from veh_invoice where invdate>='$df' and invdate<='$dt' and brcode='$brcode' ";
//and type!='trans-out'";
    $result=mysqli_query($db,$sql)or die(mysqli_error());
 while($row=mysqli_fetch_array($result))
 {
  mysqli_query($db,"insert into $tbl values('$row[invdate]','Inv No: $row[invno] - DA: $row[da] - Config: $row[config] - Chasis: $row[chasis]','0','1')")or die(mysqli_error());
 }

//*****************  purchase return 
$sql="select * from veh_purchreturn where date>='$df' and date<='$dt' and brcode='$brcode' ";
    $result=mysqli_query($db,$sql)or die(mysqli_error());
 while($row=mysqli_fetch_array($result))
 {
  mysqli_query($db,"insert into $tbl values('$row[date]','<b>P Ret No: $row[ret_num] - DA: $row[da] - Config: $row[config] - ','0','1')")or die(mysqli_error());
 }

//*****************  sales return 
$sql="select * from veh_salereturn where date>='$df' and date<='$dt' and brcode='$brcode' ";
    $result=mysqli_query($db,$sql)or die(mysqli_error());
 while($row=mysqli_fetch_array($result))
 {
  mysqli_query($db,"insert into $tbl values('$row[date]','<b>S Ret No: $row[ret_num] - DA: $row[da] - Config: $row[config] - ','1','0')")or die(mysqli_error());
 }



 $sql="select * from $tbl order by date";
    $result=mysqli_query($db,$sql)or die(mysqli_error());
$cnt=$tcount;
print "<tr bgcolor=silver><td></td><td> Opening </td><td></td><td></td><td align=right>$tcount</td></tr>";
 while($row=mysqli_fetch_array($result))
 {
  if($row['pqty']>0)
  {
    $cnt++;
    
  }
    
  else if($row['sqty']>0)
  {
      
    $cnt--;
  }
     $t1+=$row[2];
   $t2+=$row[3];

   print "<tr bgcolor='white'><td>".implode('-', array_reverse(explode('-', $row[0])))."</td><td>$row[1]</td>";
   print "<td align=right>$row[2]</td><td align=right>$row[3]</td><td align=right>$cnt</td></tr>";
 }
 print "<tr bgcolor=white><td colspan=2><center><b>Total</td><td align=right>$t1</td><td align=right>$t2</td>";
 print "<td></td></tr>";

mysqli_query($db,"delete from $tbl");
//} // end if
?>

