<?session_start();
include("../opendb.php");
include("../test.php");
?>
<link rel="stylesheet" href="style.css" type="text/css" />
<style>
body,td{font-family:'arial narrow';letter-spacing:1;font-size:12}
th{background-color:c1d799}
//tr{background-color:c1d799}
.mnu{margin-left:5}
.summ{font-size:13};
</style>
<center>

 BILLING SUMMARY
<form action='<?print $PHP_SELF?>' method=post>
<table bgcolor='silver'>
<tr><td> Date from </td><td>dd<input type=text name='d' size=2 value='<?print date('d',time())?>'>
				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;
?>
</center>
<table border=0 width=100%>
<th colspan=2 align=center>
<?print $branch?><br>
BILLING SUMMARY <BR>FOR THE PERIOD FROM <?PRINT datetohtml($df).' To '.datetohtml($dt)?></th>
<tr><td align=center VALIGN=TOP>
<table border=0 cellspacing=1 cellpadding=1 bgcolor=silver style={font-size:13}>
<?
 $res=mysqli_query($db,"select count(*),sum(amt2) from veh_invoice where invdate >='$df' and invdate<='$dt' and brcode='$brcode' and type='-'");
$r=mysqli_fetch_row($res);
print "<tr bgcolor=white><td class=summ>To Customers </td><td class=summ align=center>$r[0]</td>";
print "<td>".putcoma($r[1])."</td></tr>";

 $res=mysqli_query($db,"select count(*),sum(amt2) from veh_invoice where invdate >='$df' and invdate<='$dt' and brcode='$brcode' and type='d'");
$r=mysqli_fetch_row($res);
print "<tr  bgcolor=white><td class=summ>To Dealers </td><td class=summ align=center>$r[0]</td>";
print "<td>".putComa($r[1])."</td></tr>";


$res=mysqli_query($db,"select fin,count(fin) from veh_invoice where brcode='$brcode' and type='-' AND invdate >='$df' and invdate<='$dt' group by fin order by 2 desc");

print "<th colspan=3 align=center>FINANCIERS</th>";
$t=0;
while($r=mysqli_fetch_row($res))
 {print "<tr  bgcolor=white><td class=summ>$r[0] </td><td colspan=2 class=summ align=center>$r[1]</td></tr>";
  $t+=$r[1];
 }
print "<tr  bgcolor=white><td class=summ><center><b>Total </td><td colspan=2 class=summ align=center><b>$t</td></tr></tr>";
print "</table></td>";
print "<td align=center valign=top>";
//******************************************************
print "<table cellspacing=1 cellpadding=1 bgcolor=silver><th Colspan=2>LOCATION</TH>";
 $res=mysqli_query($db,"select area,count(area)  from veh_invoice where invdate >='$df' and invdate<='$dt' and brcode='$brcode' and type='-' group by area order by 2 desc");
$t=0;
while($r=mysqli_fetch_row($res))
 {print "<tr  bgcolor=white><td class=summ>$r[0] </td><td class=summ align=center>$r[1]</td></tr>";
  $t+=$r[1];
 }
print "<tr  bgcolor=white><td class=summ><center><b>Total </td><td class=summ align=center><b>$t</td></tr></tr>";

//************************
print "<th Colspan=2>REPRESENTATIVE</TH>";
 $res=mysqli_query($db,"select rep,count(rep)  from veh_invoice where invdate >='$df' and invdate<='$dt' and brcode='$brcode' and type='-' group by rep order by 2 desc");
$t=0;
while($r=mysqli_fetch_row($res))
 {
  $repr=mysqli_query($db,"select repname from rep_master where code='$r[0]'");
   $reprow=mysqli_fetch_row($repr);
  print "<tr  bgcolor=white><td class=summ>$reprow[0] </td><td class=summ align=center>$r[1]</td></tr>";
  $t+=$r[1];
 }
print "<tr  bgcolor=white><td class=summ><center><b>Total </td><td class=summ align=center><b>$t</td></tr></tr>";
PRINT "</table>";


print "</td></tr><tr><td align=center>";
print "<table cellspacing=1 cellpadding=1 bgcolor=silver ><th Colspan=2>CONFIGURATION</TH>";
 $res=mysqli_query($db,"select config,count(config)  from veh_invoice where invdate >='$df' and invdate<='$dt' and brcode='$brcode' and type='-' group by config order by 2 desc");
$t=0;
while($r=mysqli_fetch_row($res))
 {print "<tr  bgcolor=white><td class=summ>$r[0] </td><td class=summ align=center>$r[1]</td></tr>";
  $t+=$r[1];
 }
print "<tr  bgcolor=white><td class=summ><center><b>Total </td><td class=summ align=center><b>$t</td></tr></tr></TABLE><td valign=top align=center>";
//****************************************************
print "<table cellspacing=1 cellpadding=1 bgcolor=silver><th Colspan=2>SEGMENT</TH>";
 $res=mysqli_query($db,"select segment,count(segment)  from veh_invoice where invdate >='$df' and invdate<='$dt' and brcode='$brcode' and type='-' group by segment order by 2 desc");
$t=0;
while($r=mysqli_fetch_row($res))
 {print "<tr  bgcolor=white><td class=summ>$r[0] </td><td class=summ align=center>$r[1]</td></tr>";
  $t+=$r[1];
 }
print "<tr  bgcolor=white><td class=summ><center><b>Total </td><td class=summ align=center><b>$t</td></tr></tr></TABLE>";


print "</td></table>";


//**************************************************8
print "<br><h3>DETAILED BILLING REGISTER<table>";

 $res=mysqli_query($db,"select * from veh_invoice where invdate >='$df' and invdate<='$dt' and brcode='$brcode' and type!='trans-out' order by invno");
$tm=0;
 if(mysqli_num_rows($res)>0)
 {
  print "<table cellspacing=1 cellpadding=1 bgcolor=silver>";
print "<th>Inv. No <th>Inv Date<th>Customer <th>Config <th>Segment <th>Rep. <th>Area <th> Financer<th>Before Tax";
  while($row=mysqli_fetch_array($res))
  {
    print "<tr bgcolor=white><td>$row[invno]</td><td>".datetohtml($row[invdate])."</td>";
  if($row[type]=='-')
    {
      $acres=mysqli_query($db,"select acname from cust_master where code='$row[cust]'");
      $acrow=mysqli_fetch_array($acres);
      print "<td><a href='../vmledger.php?cd=$acrow[acname]'>$acrow[acname]</a></td>";
   }
 else
 {
  print "<td><font color=green>$row[cust]</td>";
 }
      $repres=mysqli_query($db,"select repname from rep_master where code='$row[rep]'");
      $reprow=mysqli_fetch_array($repres);

 print "<td>$row[config]</td><td>$row[segment]</td><td>$reprow[repname]</td><td>$row[area]</td>";
    print "<td>$row[fin]</td><td align=right>$row[amt2]</td>";
 $tm+=$row[amt2];
  }

 }
}
?>
