<?
 session_start();
if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); parent.document.location='ind.php' </script>";
  return;
}
include("../opendb.php");
include("../test.php");
mysqli_query($db,"drop table pwstat");
$sql="create table pwstat (fno int(10),date date,cust varchar (200), rep varchar(200), config varchar(100),location varchar(200), vehcost double(14,2), advance double(14,2), balance double(14,2),invalue double(14,2),exvalue double(14,2),disamt double(14,2),netamt double(14,2))";
mysqli_query($db,$sql);

?>
<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}
a{color:black}
</style>
<center>
<h2>
<?print $branch?> <br>PAYMENT WISE STATUS AT H.O</h2>
[Printed by <?print $user?> on <?print date("r");?>] <br>&nbsp;


<?
if($go||$dto)
{
if(!$df)
{
$df=$y1.'-'.$m1.'-'.$d1;
$dto=$y2.'-'.$m2.'-'.$d2;
}
$sq="select formno from veh_invoice where formno!=0 and brcode='$brcode'";
$rr=mysqli_query($db,$sq);
while($vrow=mysqli_fetch_array($rr))
{
 $s.=$vrow[0].",";
}
$s=substr($s,0,strlen($s)-1);
//print $s;
$sq="select * from orderform where brcode='$brcode' and  formno not in($s) and stat!='In-Active' and  dt>='$df' && dt<'$dto'";

$res=mysqli_query($db,$sq)or die('Err:'.mysqli_error());
$i=1;
while($row=mysqli_fetch_array($res))
{
  $repres=mysqli_query($db,"select repname from rep_master where code='$row[rname]'");
  $reprow=mysqli_fetch_row($repres);
 
   $advres=mysqli_query($db,"select sum(amount) from advcollection where formno='$row[formno]' and clearing='y' and brcode='$brcode'");
    $advrow=mysqli_fetch_array($advres)or die(mysqli_error());
      if($advrow[0]>0)
       {
$bal=($row[netamt]-$advrow[0]);
//invalue double(14,2),exvalue double(14,2),disamt double(14,2))";
$sq="insert into pwstat values('$row[formno]','$row[dt]','$row[cust]','$reprow[0]','$row[config]', '$row[location]','$row[netamt]','$advrow[0]','$bal','$row[invalue]','$row[exvalue]','$row[disamt]','$row[netamt]')";
mysqli_query($db,$sq) or die(mysqli_error());
       }
    }
?>      
<table border=0 bgcolor=gray cellspacing=1 cellpadding=2>
<th>NO.<th> <a href='pwstat.php?or=1'>Form NO </a>
<?if ($or==1)
  print "<img src='../img/uparrowgrnlrg.gif'>";
?>

<th><a href='pwstat.php?or=2&dto=<?print $dto?>&df=<?print $df?>'>Date</a>
<?if ($or==2)
  print "<img src='../img/uparrowgrnlrg.gif'>";
?>
<th><a href='pwstat.php?or=3&dto=<?print $dto?>&df=<?print $df?>'>Customer </a>
<?if ($or==3)
  print "<img src='../img/uparrowgrnlrg.gif'>";
?>
<th><a href='pwstat.php?or=4&dto=<?print $dto?>&df=<?print $df?>'>Rep.</a>
<?if ($or==4)
  print "<img src='../img/uparrowgrnlrg.gif'>";
?>

 <th><a href='pwstat.php?or=5&dto=<?print $dto?>&df=<?print $df?>'>Config </a>
<?if ($or==5)
  print "<img src='../img/uparrowgrnlrg.gif'>";
?>

<th><a href='pwstat.php?or=6&dto=<?print $dto?>&df=<?print $df?>'>Location</a>
<?if ($or==6)
  print "<img src='../img/uparrowgrnlrg.gif'>";
//invalue double(14,2),exvalue double(14,2),disamt double(14,2))";
?>

<th>INV Value <th>Exg. Value <th>Disc <th>Net Amt
 <th><a href='pwstat.php?or=7&dto=<?print $dto?>&df=<?print $df?>'>Veh. Cost</a>
<?if ($or==7)
  print "<img src='../img/uparrowgrnlrg.gif'>";
?>

<th><a href='pwstat.php?or=8&dto=<?print $dto?>&df=<?print $df?>'>Advance </a>
<?if ($or==8)
  print "<img src='../img/uparrowgrnlrg.gif'>";
?>

<th> <a href='pwstat.php?or=9&dto=<?print $dto?>&df=<?print $df?>'>Balance</a>
<?if ($or==9)
  print "<img src='../img/uparrowgrnlrg.gif'>";
?>

<?
if(strlen($or)<=0)
 $or=1;
$sq="select * from pwstat order by $or";
$res=mysqli_query($db,$sq)or die(mysqli_error());
$i=1;
while($row=mysqli_fetch_array($res))
{
 print "<tr bgcolor=white><td align=center>".$i++."</td><td align=center>$row[0]</td><td>".datetohtml($row[1])."</td><td>".strtoupper($row[2])."</td><td align=center>$row[3]</td><td>$row[4]</td>";
 print "<td>$row[5]</td><td align=right>$row[invalue]</td>";
 print "<td align=right>$row[exvalue]</td>";
 print "<td align=right>$row[disamt]</td>";
 print "<td align=right>$row[netamt]</td>";

 print "<td align=right>$row[6]</td><td align=right>$row[7]</td>";
 print "<td align=right>$row[8]</td>";
}
return;
}// end if 

?>
<center>
<form action='<?print $PHP_SELF?>'>
<table cellspacing=1 cellpadding=1 bgcolor=black>
<tr><td bgcolor='ivory'><center>
<table>
<tr><td>From </td><td>dd<input type=text name='d1' value='1' size=2>
mm<input type=text name='m1' value='<?print date('m')?>' size=2>
yy<input type=text name='y1' value='<?print date('Y')?>' size=4>
</td></tr>
<tr><td>To </td><td>dd<input type=text name='d2' value='<?print date('d')?>' size=2>
mm<input type=text name='m2' value='<?print date('m')?>' size=2>
yy<input type=text name='y2' value='<?print date('Y')?>' size=4>
</td></tr>
<tr><td colspan=2 align=center><input type=submit value='Show' name='go'></td></tr>
</table>
</td></tr>
</table>

</form>
