<?
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");
?>
<style>
body,td,th,pre{font-family:'arial narrow';letter-spacing:1;font-size:13}
//tr{background-color:c1d7bb}
.mnu{margin-left:5}
</style>
<body bgcolor='ivory'>
<center>
<h3>ORDER FORM REPORT </H3>
<?
if($show)
{
$df=$y.'-'.$m.'-'.$d;
$dto=$y1.'-'.$m1.'-'.$d1;
print "<table><tr><td valign=top>";
$repcntres=mysqli_query($db,"select rname,count(rname) from orderform where brcode='$brcode' and dt>='$df' and dt<='$dto' group by rname order by rname")or die(mysqli_error());
print "<table border=1><th>Representative <th>Count";
while($repcntrow=mysqli_fetch_row($repcntres))
{
 print "<tr><td>".getRep($repcntrow[0])."</td><td><center>$repcntrow[1]</td></tr>";
 $t1+=$repcntrow[1];
}
print "<tr><td><b>Total</td><td><b><center>$t1</td></tr>";
print "</table></td><td valign=top>";

//
$repcntres1=mysqli_query($db,"select location,count(location) from orderform where brcode='$brcode' and dt>='$df' and dt<='$dto' group by location order by location")or die(mysqli_error());
print "<table border=1><th>Location <th>Count";
while($repcntrow=mysqli_fetch_row($repcntres1))
{
 print "<tr><td>".($repcntrow[0])."</td><td><center>$repcntrow[1]</td></tr>";
 $t2+=$repcntrow[1];
}
print "<tr><td><b>Total</td><td><center><b>$t2</td></tr>";
print "</table></td><td>";



//***********************************************************
$repcntres2=mysqli_query($db,"select config,count(config) from orderform where brcode='$brcode' and dt>='$df' and dt<='$dto' group by config order by config")or die(mysqli_error());
print "<table border=1><th>Config <th>Count";
while($repcntrow2=mysqli_fetch_row($repcntres2))
{
 print "<tr><td>".($repcntrow2[0])."</td><td><center>$repcntrow2[1]</td></tr>";
 $t3+=$repcntrow2[1];
}
print "<tr><td><b>Total</td><td><center><b>$t3</td></tr>";
print "</table></td><td>";


print "</tr></table>";

//***********************************************************
$sql="select a.formno,a.cust,a.dt,a.addr,a.phone,b.repname,b.dist,a.model,a.config,a.netamt,a.invalue,a.op,a.sale_invno,a.bodydetails from orderform a, rep_master b where a.brcode='$brcode' and a.rname=b.code and a.dt>='$df' and a.dt<='$dto' order by formno";
$re=mysqli_query($db,$sql) or die(mysqli_error());

print "<p align=right><input type=button value='Print' onClick=window.open('prnoformrepo.php?df=$df&dto=$dto')></p>";
?>
<table border=1 width=100%>
<th>No<th>Inv No<th>Form<br> NO <th>Customer <th>Date   <th>Model <th>Config <th>Body <th>Amt@HO <th>Amt@Br<th>Inv Amt <th>Net Amt<th>OP. Bal<th>Balance<th>Rep <th>Dist<th>Address <th>Phone
<?
$sno=1;
while($form_row=mysqli_fetch_array($re))
{
$t=0;
 print "\n<tr onMouseOver=bgColor='c1d7bb'; onMouseOut=bgColor='ivory'><td>".$sno++."</td>";
 $invres=mysqli_query($db,"select invno from veh_invoice where formno='$form_row[formno]'");
 if(mysqli_num_rows($invres)>0)
 {
  $invrow=mysqli_fetch_row($invres);
  print "<td>$invrow[0]</td>";
 }
 else
  print "<td>-</td>";
  //print "<td>$form_row[sale_invno]</td>";
 print "<td><a href='editoform.php?code=$form_row[formno]'>$form_row[formno]</a></td><td>$form_row[cust]</td><td>".datetohtml($form_row[dt])."</td>";

 print "<td>$form_row[model]</td><td>$form_row[config]</td><td>$form_row[bodydetails]</td>";

 $sql="select sum(amount) from advcollection where formno='$form_row[0]' and brcode='$brcode' and clearing='y' group by clearing order by clearing desc";
 $res=mysqli_query($db,$sql)or die(mysqli_error());

 $r1=mysqli_fetch_row($res);
  if($r1[0]>0)
   { print "<td align=right>".putComa($r1[0])."</td>";$t=$r1[0];}
  else
    print "<td align=right>0.0</td>";
		//8***********************
 $sql="select sum(amount) from advcollection where formno='$form_row[0]' and brcode='$brcode' and clearing='n' group by clearing order by clearing desc";
 $res=mysqli_query($db,$sql)or die(mysqli_error());

 $r2=mysqli_fetch_row($res);
  if($r2[0]>0)
   { print "<td align=right><a href='svadv.php?formno=$form_row[0]'>$r2[0]</a></td>"; $t+=$r2[0];}
  else
    print "<td align=right>0.0</td>";


$bal=($form_row[netamt]-$form_row[op])-$t;
 print "<td>".putComa($form_row[invalue])."</td><td>".putComa($form_row[netamt])."</td>";
 print "<td align=right>".putcoma($form_row[op])."</td><td align=right>".putComa($bal)."</td>";
print "<td>$form_row[repname]</td><td>$form_row[dist]</td>";
 print "<td width=120>".str_replace(',',', ',$form_row[addr])."</td><td>$form_row[phone]</td></tr>";

}
?>
</table>
<?
return;
}
?>

<form action='<?print $PHP_SELF?>'>
<table>
  <tr><td>Date from </td>
      <td>dd<input type=text name='d' size=2 value='1'>
	  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>
      <td><input type=submit value='Show' name='show'><input type=submit value='Print' name='prn' ></td>
   </tr>
</table>
</form>
<?
 function getRep($code)
 {
   $reep=mysqli_query($db,"select repname from rep_master where code='$code'");
   $reeprow=mysqli_fetch_row($reep);
   return $reeprow[0];
}
?>

