<?
session_start();
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$branch=$_SESSION['branch'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Invalid User'); parent.document.location='../index.html' </script>";
 return;
}
include("../opendb.php");
include("../test.php");
$d=$_REQUEST['d'];
$m=$_REQUEST['m'];
$y=$_REQUEST['y'];
$dt=$y.'-'.$m.'-'.$d;
$sql="select sum(dramt)-sum(cramt) from journal2 where partic='Cash' and date<'$dt' and brcode='$brcode'";

$re=mysqli_query($db,$sql) or die(mysqli_error());
$bal_row=mysqli_fetch_row($re);
print "<center><font size=3><b>$branch <br>DAILY STATEMENT FOR THE DAY ".parseDate($dt)."</font></b> [Printed by ".strtoupper($user)."]</center><br>";
// print "<br>Cash Opening: ".$bal_row[0];
?>
<link href="../css/form.css" type="text/css" rel="stylesheet" media="screen"  />
<html>
<body>

<table align=center bgcolor=silver border=2 cellpadding=1 cellspacing=1 width=90%>

<?
print "</center><h4><font color=red>Cash</h4>";
?>
<trbgcolor=silver><td width=90><center><b>Date</td> <td width=80><center><b>Ref </td> <td><center><b>Ledger</td> <td width=100><center><b>Receipt</td><td width=100><center><b> Payment</td> <td><center><b>Narration</td><td><center><b>Ref.</td><td><center><b>Ref. NO.</td></tr>
<?
$bal=mysqli_query($db,"select op from ledger where led='Cash' and brcode='$brcode'");
$opbalance=mysqli_fetch_row($bal);
$res=mysqli_query($db,"select acfrom from acperiod");
$r1=mysqli_fetch_row($res);
$ssq="select sum(dramt)-sum(cramt) from journal2 where brcode='$brcode' and date<'$dt' and partic='Cash'";

$res2=mysqli_query($db,$ssq)or die('jour err:'.mysqli_error());
$r2=mysqli_fetch_row($res2);

$opbal=($opbalance[0]+$r2[0]);
$result1=mysqli_query($db,"select distinct date from journal2 where brcode='$brcode' and date='$dt'and partic='Cash' order by date")or die(mysqli_error());
if($opbal>=0)
{
print "<tr bgcolor=white><td></td><td colspan=2><font color=green>To Opening Balance</td><td align=right><font color=green>".putComa($opbal)."</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>";
}
else
{
print "<tr bgcolor=white><td></td><td colspan=2><font color=green>To Opening Balance</td><td align=right><font color=red>".putComa($opbal)."</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>";
}
$head=1;
while($row1=mysqli_fetch_array($result1))
{
if($head>1)
{
if($opbal>0)
{
print "<tr bgcolor=white><td></td><td colspan=2><font color=green>To Opening Balance</td><td align=right><font color=green>".putComa($opbal)."</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>";
}
else
{
print "<tr bgcolor=white><td></td><td colspan=2><font color=green>To Opening Balance</td><td align=right><font color=red>".putComa($opbal)."</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>";
}
}
$sq="select distinct jvno from journal2 where date='$row1[date]' and partic='Cash' and brcode='$brcode'";
     $se=mysqli_query($db,$sq)or die(mysqli_error());
$tr=0;
$tp=0;
   while($prt_row=mysqli_fetch_array($se))
    {

  	$fnl=mysqli_query($db,"select * from journal2 where jvno='$prt_row[0]' and brcode='$brcode'");
          while($fnl_row=mysqli_fetch_array($fnl))
          {
               if($fnl_row[partic]!='Cash') // && $fnl_row[type]=='Dr'
		{
                 print "<tr bgcolor=white><td>".datetohtml($fnl_row[date])."</td><td>$fnl_row[against]:$fnl_row[no]</td><td><a href='multientry/viewtrans.php?code=$fnl_row[jvno]'>$fnl_row[partic]</a></td>";
                print "<td align=right>".nozero($fnl_row[cramt])."</td><td align=right>".nozero($fnl_row[dramt])."</d>";
		print "<td>$fnl_row[narr]</td><td>$fnl_row[against]</td><td>$fnl_row[no]</td></tr>";
			if($fnl_row[type]=='Dr') $tp+=$fnl_row[dramt]; else $tr+=$fnl_row[cramt];

$closebal=($tr+$opbal)-$tp;
                 }
          }

    }

if($closebal>0)
{
print "<tr bgcolor=white><td colspan=3 align=center><font color=green><b>by Closing Balance</td><td></td><td align=right><font color=green>".putComa(abs($closebal))."</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>";
}
else
{
print "<tr bgcolor=white><td colspan=3 align=center><font color=green><b>by Closing Balance</td><td></td><td align=right><font color='red'>(".putComa(abs($closebal)).")</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>";
}

print "<tr bgcolor=white><td colspan=3><td align=right><font color=green>".putComa($tp+$closebal)."<br><hr></td><td align=right> <font color=green>".putComa($tp+$closebal)."<br><hr></td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>";
$opbal=$closebal;
$head++;
	
}


?>
</table>
<br>
<!--           bank .................................... -->


<?
$res222=mysqli_query($db,"select * from ledger where brcode='$brcode' and(grp='Bank Accounts' or grp='Bank O/d')")or die(mysqli_error());

while($bnk_row=mysqli_fetch_array($res222))
{
print "</center><h4><font color=red>BANK :".$bnk_row[0]."</h4>";
$bnk=$bnk_row[0];
?>
<table align=center bgcolor=ivory border=1 cellpadding=1 cellspacing=1 width=90%>
<tr bgcolor='silver'><td width=90><center><b>Date</td> <td width=80><center><b>Voucher </td> <td><center><b>Ledger</td> <td width=100><center><b>Receipt</td><td width=100><center><b> Payment</td> <td><center><b>Narration</td></tr>
<?
$sq="select op,drcr from ledger where brcode='$brcode' and led='$bnk_row[0]'";
$re=mysqli_query($db,$sq) or die(mysqli_error());
$opb=mysqli_fetch_row($re) or die(mysqli_error());
$opbval=$opb[0];
$drorcr=$opb[1];
$res=mysqli_query($db,"select acfrom from acperiod");
$r1=mysqli_fetch_row($res);

$ssq="select sum(dramt)-sum(cramt) from journal2 where brcode='$brcode' and date<'$dt' and partic='$bnk'";

$res2=mysqli_query($db,$ssq)or die('sum err:'.mysqli_error());
$r2=mysqli_fetch_row($res2);
$opbal=($opbval+$r2[0]);


$head=1;
if($opbal>0)
{
print "<tr bgcolor=white><td></td><td colspan=2><font color=green>To Opening Balance</td><td align=right><font color=green>".putComa($opbal)."</td><td></td><td></td></tr>";
}
else
{
print "<tr bgcolor=white><td></td><td colspan=2><font color=green>To Opening Balance</td><td align=right><font color=red>".putComa($opbal)."</td><td></td><td></td></tr>";
}

$sq="select distinct date from journal2 where brcode='$brcode' and date='$dt' and partic='$bnk' order by date";
$result1=mysqli_query($db,$sq)or die(mysqli_error());

while($row1=mysqli_fetch_array($result1))
{

$sq="select distinct jvno from journal2 where date='$row1[date]' and partic='$bnk' and brcode='$brcode'";

     $se=mysqli_query($db,$sq)or die(mysqli_error());
$tr=0;
$tp=0;
   while($prt_row=mysqli_fetch_array($se))
    {

  	$fnl=mysqli_query($db,"select * from journal2 where jvno='$prt_row[0]' and brcode='$brcode'")or die(mysqli_error());
          while($fnl_row=mysqli_fetch_array($fnl))
          {
               if(strtoupper($fnl_row[partic])!=strtoupper($bnk)) // && $fnl_row[type]=='Dr'
		{
                 print "\n<tr bgcolor=white><td>".datetohtml($fnl_row[date])."</td><td>$fnl_row[against]:$fnl_row[no]</td><td><a href='multientry/viewtrans.php?code=$fnl_row[jvno]'>$fnl_row[partic]</a></td>";
                print "\n<td align=right>".nozero($fnl_row[cramt])."</td><td align=right>".nozero($fnl_row[dramt])."</d>";
		print "\n<td>$fnl_row[narr]</td></tr>";
			if($fnl_row[type]=='Dr') $tp+=$fnl_row[dramt]; else $tr+=$fnl_row[cramt];

$closebal=($tr+$opbal)-$tp;
                 }
          }

    }

if($closebal>0)
{
print "<tr bgcolor=silver><td colspan=3 align=center><font color=green><b>by Closing Balance</td><td></td><td align=right><font color=green>".putComa(abs($closebal))."</td><td></td>";
}
else
{
print "<tr bgcolor=silver><td colspan=3 align=center><font color=green><b>by Closing Balance</td><td></td><td align=right><font color='red'>(".putComa(abs($closebal)).")</td><td></td>";
}

print "<tr bgcolor=silver><td colspan=3><td align=right><font color=green>".putComa($tp+$closebal)."<br><hr></td><td align=right> <font color=green>".putComa($tp+$closebal)."<br><hr></td><td></td></tr>";
$opbal=$closebal;
 }	
print "</table><br>";
}

?>
<!-- //********************************     -->

</table>
<h4>Veh.PURCHASE</h4>
<table align=center bgcolor=silver border=1 cellpadding=1 cellspacing=1 width=90%>

<tr bgcolor='silver'><td><center><b>Pno</td> <td><center><b>Invno </td> 
<td><center><b>InvDate</td> <td ><center><b>From</td><td ><center><b> DA</td> 
<td><center><b>Mcode</td><td><center><b>Config</td><td><center><b>Net</td>

<td><center><b>Tax%</td> <td ><center><b>Tax</td><td><center><b>Cess</td> 
<td><center><b>BeforeTax</td></tr>

<?
$cesstot=0;
$re=mysqli_query($db,"select * from veh_purchase where entrydate='$dt' and brcode='$brcode'");
while($row=mysqli_fetch_array($re))
{
 print "<tr bgcolor='white'><td>$row[purno]</td><td>$row[invno]</td><td>".datetohtml($row[pinvdate])."</td><td>$row[sup]</td>";
 print "<td>$row[msno]</td><td>$row[model]</td><td>$row[config]</td><td align=right>$row[netamt]</td>";
 print "<td align=right>$row[taxp]</td><td align=right>$row[tax]</td>";
print "<td align=right>$row[cessamt]</td><td align=right>$row[amt2]</td></tr>";
$tnet+=$row[netamt];
$ttax+=$row[tax];
$tamt2+=$row[amt2];
$cesstot+=$row[cessamt];
}
print "<tr bgcolor=silver><td colspan=7 align=center><b>Total</td><td align=right>".putComa($tnet)."</td>";
print "<td></td><td align=right>".putComa($ttax)."</td>";
print " <td align=right>".putComa($cesstot)."</td> <td align=right>".putComa($tamt2)."</td></tr>";
?>
</table>

<!-- //********************************     -->
<h4>Veh.SALES</h4>
<table align=center bgcolor=ivory border=1 cellpadding=1 cellspacing=1 width=90%>


<tr bgcolor='silver'> <td><center><b>Invno </td> 
<td><center><b>InvDate</td> <td ><center><b>To</td><td ><center><b> DA</td> 
<td><center><b>Mcode</td><td><center><b>Config</td><td><center><b>Net</td>

<td><center><b>Tax%</td> <td ><center><b>Tax</td><td><center><b>Cess</td> 
<td><center><b>BeforeTax</td></tr>
<?
$tnet=0;
$ttax=0;
$tamt2=0;

$re=mysqli_query($db,"select * from veh_invoice where invdate='$dt'  and brcode='$brcode'");
while($row=mysqli_fetch_array($re))
{
  $res=mysqli_query($db,"select custname from cust_master where code='$row[cust]' and brcode='$brcode'");
  $cus_row=mysqli_fetch_row($res);
 print "<tr bgcolor='white'><td>$row[invno]</td><td>".datetohtml($row[invdate])."</td><td>$row[cust]</td>";
 print "<td>$row[msno]</td><td>$row[model]</td><td>$row[config]</td><td align=right>$row[netamt]</td>";
 print "<td align=right>$row[taxp]</td><td align=right>$row[tax]</td>";
 print "<td align=right>$row[cessamt]</td><td align=right>$row[amt2]</td></tr>";
$tnet+=$row[netamt];
$ttax+=$row[tax];
$tamt2+=$row[amt2];
$tcess+=$row[cessamt];
}
print "<tr bgcolor=silver><td colspan=6 align=center><b>Total</td><td align=right>".putComa($tnet)."</td>";
print "<td></td><td align=right>".putComa($ttax)."</td>";
print " <td align=right>".putComa($tcess)."</td> <td align=right>".putComa($tamt2)."</td></tr>";

?>
</table>


<!-- //********************************     -->
<h4>PURCHASE RETURN</h4>
<table align=center bgcolor=black border=1 cellpadding=1 cellspacing=1 width=90%>

<tr bgcolor='silver'> <td><center><b>RetNo </td> 
<td><center><b>Ret.Date</td> <td ><center><b>To</td><td ><center><b> DA</td> 
<td><center><b>Mcode</td><td><center><b>Config</td><td><center><b>Net</td>

<td><center><b>Tax%</td> <td ><center><b>Tax</td><td><center><b>Cess</td> 
<td><center><b>BeforeTax</td></tr>

<?
$tnet=0;
$ttax=0;
$tamt2=0;

$re=mysqli_query($db,"select * from veh_purchreturn where date='$dt' and brcode='$brcode'")or die(mysqli_error());
while($row=mysqli_fetch_array($re))
{
 print "<tr bgcolor='white'><td>$row[ret_num]</td><td>".datetohtml($row[date])."</td><td>$row[retto]</td>";
 print "<td>$row[da]</td><td>$row[mcode]</td><td>$row[config]</td><td align=right>$row[netamt]</td>";
 print "<td align=right>$row[taxp]</td><td align=right>$row[tax]</td><td align=right>$row[amt2]</td></tr>";
$tnet+=$row[netamt];
$ttax+=$row[tax];
$tamt2+=$row[amt2];
}
print "<tr bgcolor=white><td colspan=7 align=center><b>Total</td><td align=right>".putComa($tnet)."</td>";
print "<td></td><td align=right>".putComa($ttax)."</td> <td align=right>".putComa($tamt2)."</td></tr>";

?>
</table>

<!-- //********************************     -->
<h4>SALES RETURN</h4>
<table align=center bgcolor=black border=1 cellpadding=1 cellspacing=1 width=90%>

<tr bgcolor='silver'> <td><center><b>RetNo </td> 
<td><center><b>Ret.Date</td> <td ><center><b>From</td><td ><center><b> DA</td> 
<td><center><b>Mcode</td><td><center><b>Config</td><td><center><b>Net</td>

<td><center><b>Tax%</td> <td ><center><b>Tax</td><td><center><b>Cess</td> 
<td><center><b>BeforeTax</td></tr>




<?
$tnet=0;
$ttax=0;
$tamt2=0;

$re=mysqli_query($db,"select * from veh_salereturn where date='$dt' and brcode='$brcode'")or die(mysqli_error());
while($row=mysqli_fetch_array($re))
{
  $res=mysqli_query($db,"select custname from cust_master where code='$row[retfrom]' and brcode='$brcode'");
  $cus_row=mysqli_fetch_row($res);

 print "<tr bgcolor='white'><td>$row[ret_num]</td><td>".datetohtml($row[date])."</td><td>$cus_row[0]</td>";
 print "<td>$row[da]</td><td>$row[mcode]</td><td>$row[config]</td><td align=right>$row[netamt]</td>";
 print "<td align=right>$row[taxp]</td><td align=right>$row[tax]</td><td align=right>$row[amt2]</td></tr>";
$tnet+=$row[netamt];
$ttax+=$row[tax];
$tamt2+=$row[amt2];
}
print "<tr bgcolor=white><td colspan=7 align=center><b>Total</td><td align=right>".putComa($tnet)."</td>";
print "<td></td><td align=right>".putComa($ttax)."</td> <td align=right>".putComa($tamt2)."</td></tr>";

?>
</table>

<!-- //********************************     -->
<h4>CREDIT NOTE</h4>
<table align=center bgcolor=black border=1 cellpadding=1 cellspacing=1 width=90%>

<tr bgcolor='silver'> <td><center><b>No </td> 
<td ><center><b> DA</td> 
<td><center><b>Mcode</td><td><center><b>Config</td><td><center><b>Sale_inv</td>
<td><center><b>Cust</td><td><center><b>Net Amt</td><td><center><b>Tax</td> <td ><center><b>Tax%</td><td><center><b>Beforetax</td> 
<td><center><b>tax</td></tr>




<?
$tnet=0;
$ttax=0;
$tamt2=0;

$re=mysqli_query($db,"select * from crnote where date='$dt' and brcode='$brcode'")or die(mysqli_error());
while($row=mysqli_fetch_array($re))
{
   $res=mysqli_query($db,"select custname from cust_master where code='$row[retfrom]'");
  $cus_row=mysqli_fetch_row($res);
 print "<tr bgcolor=white><td>$row[ret_num]</td><td>$row[da]</td><td>$row[mcode]</td>";
 print "<td>$row[config]</td><td>$row[sale_invoice]</td><td>$cus_row[0]</td><td align=right>$row[netamt]</td>";
 print "<td align=right>$row[taxp]</td><td align=right>$row[tax]</td><td align=right>$row[amt2]</td>";
$tnet+=$row[netamt];
$ttax+=$row[tax];
$tamt2+=$row[amt2];
}
print "<tr bgcolor=white><td colspan=7 align=center><b>Total</td><td align=right>".putComa($tnet)."</td>";
print "<td>&nbsp;</td><td align=right>".putComa($ttax)."</td> <td align=right>".putComa($tamt2)."</td></tr>";
?>
</table>
<!-- //********************************     -->
<h4>JOURNAL ENTRIES</h4>
<table align=center bgcolor=black border=1 cellpadding=1 cellspacing=1 width=90%>

<tr bgcolor='silver'> <td><center><b>TrNo </td> 
<td ><center><b> Date</td> 
<td><center><b>Particulars</td><td><center><b>Debit</td><td><center><b>Credit</td>
<td><center><b>Narration</td></tr>

<?
$re=mysqli_query($db,"select distinct jvno,date from journal2 where brcode='$brcode' and date='$dt'and j_type not in('CP','CR','BP','BR')");

while($row=mysqli_fetch_array($re))
{

  print "\n<tr bgcolor='white'><td>$row[jvno]</td><td>".datetohtml($row[date])."</td><td></td>";
  print "<td></td><td></td><td></td>";
  $re2=mysqli_query($db,"select * from journal2 where jvno='$row[jvno]' and brcode='$brcode'") or die(mysqli_error());
    while($jvrow=mysqli_fetch_array($re2))
    {
        print "\n<tr bgcolor=white><td>&nbsp;</td><td>&nbsp;</td><td>$jvrow[partic]</td>";
        print "<td align=right>".nozero($jvrow[dramt])."</td>";
        print "<td align=right>".nozero($jvrow[cramt])."</td><td>$jvrow[narr]</td></tr>";
  }
     print "<tr><td colspan=9 bgcolor=silver></td></tr>";


}


?>





</table>

<!-- //********************************     -->
<h4>Stock Transfer Out</h4>
<table align=center bgcolor=black border=1 cellpadding=1 cellspacing=1 width=90%>

<tr bgcolor='silver'> <td><center><b>No </td> 
<td><center><b>Date</td> <td ><center><b>To</td><td ><center><b> Amount</td> 
</tr>




<?
$tnet=0;
$ttax=0;
$tamt2=0;

$re=mysqli_query($db,"select distinct(tr_no),to_branch,entrydate from sparetransout where entrydate='$dt' and brcode='$brcode'")or die(mysqli_error());
while($row=mysqli_fetch_array($re))
{
  $res=mysqli_query($db,"select brname from branch where brcode='$row[to_branch]' ");
  $cus_row=mysqli_fetch_row($res);

$rs=mysqli_query($db,"select sum(qty*amount) from sparetransout where brcode='$brcode' and tr_no=$row[tr_no]");
  $c=mysqli_fetch_row($rs);

 print "<tr bgcolor='white'><td>$row[tr_no]</td><td>".datetohtml($row[entrydate])."</td><td>$cus_row[0]</td>";
 print "<td align=right>".putComa($c[0])."</td>";
$tamt2+=$c[0];
}
print "<tr bgcolor=white><td colspan=3 align=center><b>Total</td>";
print "<td align=right>".putComa($tamt2)."</td></tr>";

?>
</table>

<!-- //********************************     -->


<!-- //********************************     -->
<h4>Stock Transfer In</h4>
<table align=center bgcolor=black border=1 cellpadding=1 cellspacing=1 width=90%>

<tr bgcolor='silver'> <td><center><b>No </td> 
<td><center><b>Date</td> <td ><center><b>From</td><td ><center><b> Amount</td> 
</tr>

<?
$tnet=0;
$ttax=0;
$tamt2=0;

$re=mysqli_query($db,"select distinct(tr_no),from_branch,entrydate from sparetransin where entrydate='$dt' and brcode='$brcode'")or die(mysqli_error());
while($row=mysqli_fetch_array($re))
{
  $res=mysqli_query($db,"select brname from branch where brcode='$row[from_branch]' ");
  $cus_row=mysqli_fetch_row($res);

$rs=mysqli_query($db,"select sum(qty*amount) from sparetransin where brcode='$brcode' and tr_no=$row[tr_no]");
  $c=mysqli_fetch_row($rs);

 print "<tr bgcolor='white'><td>$row[tr_no]</td><td>".datetohtml($row[entrydate])."</td><td>$cus_row[0]</td>";
 print "<td align=right>".putComa($c[0])."</td>";
$tamt2+=$c[0];
}
print "<tr bgcolor=white><td colspan=3 align=center><b>Total</td>";
print "<td align=right>".putComa($tamt2)."</td></tr>";

?>
</table>

<!-- //********************************     -->

<!-- //********************************     -->
<h4>Spare Sales</h4>
<table align=center bgcolor=black border=1 cellpadding=1 cellspacing=1 width=90%>

<tr bgcolor='silver'> <td>No </td> 
<td>Date</td> <td >Customer</td><td align=right> Amount</td> 
</tr>

<?
$tnet=0;
$ttax=0;
$tamt2=0;

$re=mysqli_query($db,"select distinct(invno),entrydate,customer from spareinvoice where entrydate='$dt' and brcode='$brcode'")or die(mysqli_error());
while($row=mysqli_fetch_array($re))
{
  $res=mysqli_query($db,"select acname from customer where acname='$row[customer]' ");
  $cus_row=mysqli_fetch_row($res);

$rs=mysqli_query($db,"select round(sum(tot)) from spareinvoice where brcode='$brcode' and invno=$row[invno]");
  $c=mysqli_fetch_row($rs);

 print "<tr bgcolor='white'><td>$row[invno]</td><td>".datetohtml($row[entrydate])."</td><td>$cus_row[0]</td>";
 print "<td align=right>".putComa($c[0])."</td>";
$tamt2+=$c[0];
}
print "<tr bgcolor=white><td colspan=3 align=center><b>Total</td>";
print "<td align=right>".putComa($tamt2)."</td></tr>";

?>
</table>

<!-- //********************************     -->

<!-- //********************************     -->
<h4>Return -Spare Sales</h4>
<table align=center bgcolor=black border=1 cellpadding=1 cellspacing=1 width=90%>

<tr bgcolor='silver'> <td><center><b>No </td> 
<td><center><b>Date</td> <td ><center><b>Customer</td><td ><center><b> Amount</td> 
</tr>

<?
$tnet=0;
$ttax=0;
$tamt2=0;

$re=mysqli_query($db,"select distinct(retno),entrydate,customer from salesreturn where entrydate='$dt' and brcode='$brcode'")or die(mysqli_error());
while($row=mysqli_fetch_array($re))
{
  $res=mysqli_query($db,"select acname from customer where acname='$row[customer]' ");
  $cus_row=mysqli_fetch_row($res);

$rs=mysqli_query($db,"select round(sum(tot)) from salesreturn where brcode='$brcode' and retno=$row[retno]");
  $c=mysqli_fetch_row($rs);

 print "<tr bgcolor='white'><td>$row[retno]</td><td>".datetohtml($row[entrydate])."</td><td>$cus_row[0]</td>";
 print "<td align=right>".putComa($c[0])."</td>";
$tamt2+=$c[0];
}
print "<tr bgcolor=white><td colspan=3 align=center><b>Total</td>";
print "<td align=right>".putComa($tamt2)."</td></tr>";

?>
</table>

<h4>JobCard</h4>
<table align=center bgcolor=black border=1 cellpadding=1 cellspacing=1 width=90%>

<tr bgcolor='silver'> <td><center><b>No </td><td><center><b>InvNo </td> 
<td><center><b>Date</td> <td ><center><b>Customer</td><td ><center><b> invtype</td> 

<td><center><b>BeforeTax</td> <td ><center><b>Tax%</td><td ><center><b> Tax</td> 
<td><center><b>Serv Amt</td> <td ><center><b>Tax%</td><td ><center><b> STax</td> 
<td><center><b>Total</td> <td ><center><b>Rounded</td><td ><center><b> GTotal</td> 

</tr>



<?

$sq1="select jcno,cname,entrydate,invno,invtype from jobcard where entrydate='$dt'  and jvno>0 and brcode='$brcode' order by invno";
//print $sq1.'<br>';
    $invre66=mysqli_query($db,$sq1) or die('Err11:'.mysqli_error());
  if(mysqli_num_rows($invre66)>0)
{

//print " <th>Sno<th>Inv No <th>Invoice Date ";

//print "<th>Customer<th>Invtype<th>Before Tax <th>Tax%<th>Tax <th>Serv Amt<th>Tax% <th>S Tax <th>Total <th>Rounded <th>G Total";
$a=1;

$jres=mysqli_query($db,$sq1)or die('Err'.mysqli_error());
while($jrow=mysqli_fetch_array($jres))
{
    $sql="select sum(amt)sparice,sum(taxamt)stx,sum(cessamt)ces,sum(gtot)tt,vat from jcardparts where jcno='$jrow[0]' and brcode='$brcode' group by jcno";
//print $sql.'<br>';
    $invre=mysqli_query($db,$sql) or die('Err:'.mysqli_error());
  if(mysqli_num_rows($invre)>0)
  {
    while($invrow=mysqli_fetch_array($invre))
    {

   print "<tr bgcolor='white'><td>".$a++."</b></td><td>WS-$jrow[invno]</td><td>".datetohtml($jrow[entrydate])."</td><td>$jrow[cname]</td>";
   print "<td align=right>VAT-$jrow[4]</td><td align=right>".putComa($invrow[0])."</td><td align=right>$invrow[4]</td><td align=right>$invrow[1]</td>";
  //  print "<td align=right> -- $invrow[3]</td>"; j tot
   //printf("<td align=right>%.2f</td>",$rnd);
  // print "<td align=right>".putcoma(round($invrow[3]))."</td>";

    $sql="select sum(servamt)sparice,sum(servamt*servtx/100)stx,sum(servamt*edutx/100),sum(servamt*hredutx/100),sum(tot)tt,servtx from jcardserv where jcno='$jrow[0]' and brcode='$r[brcode]' group by jcno";

$jres2=mysqli_query($db,$sql)or die('Err'.mysqli_error());
$jrow2=mysqli_fetch_array($jres2);

print "<td align=right>".putComa($jrow2[0])."</td>";
  printf("<td align=right>%.2f</td>",$jrow2[5]);
 
  printf("<td align=right>%.2f</td>",$jrow2[1]);
   
//   print "<td align=right>$invrow[2]</td><td align=right>$invrow[3]</td>";
   $rnd=round($jrow2[4]+$invrow[3]);
   $dif=$rnd-($jrow2[4]+$invrow[3]);
   print "<td align=right>".putComa($jrow2[4]+$invrow[3])."</td>";
   printf("<td align=right>%.2f</td>",$dif);
   print "<td align=right>".putComa(round($jrow2[4]+$invrow[3]))."</td>";
  
   $t1+=$invrow[0];
   $t2+=$invrow[1];
   $t3+=$invrow[2];
   //$t4+=$invrow[3];

   $t5+=$jrow2[0];
   $t6+=$jrow2[1];
   $t7+=$jrow2[2];
   $t8+=$jrow2[3];
   $t9+=($jrow2[4]+$invrow[3]);
   $tdiff+=$dif;
   $tgtot+=round($jrow2[4]+$invrow[3]);
    }
  } // end if numrow
 else
{
    $sql="select sum(servamt)sparice,sum(servamt*servtx/100)stx,sum(servamt*edutx/100),sum(servamt*hredutx/100),sum(tot)tt,servtx from jcardserv where jcno='$jrow[0]' and brcode='$brcode' group by jcno";

$jres21=mysqli_query($db,$sql)or die('Err'.mysqli_error());
$jrow2=mysqli_fetch_array($jres21);
   print "<tr bgcolor='white'><td>".$a++."</b></td><td>WS-$jrow[invno]</td><td>".datetohtml($jrow[entrydate])."</td><td>$jrow[cname]</td>";
print "<td align=right>VAT-$jrow[4]</td><td></td><td></td><td></td>";
print "<td align=right>".putComa($jrow2[0])."</td>";
  printf("<td align=right>%.2f</td>",$jrow2[5]);

   printf("<td align=right>%.2f</td>",$jrow2[1]);
 
//   print "<td align=right>$invrow[2]</td><td align=right>$invrow[3]</td>";
   $rnd=round($jrow2[4]);
   $dif=$rnd-($jrow2[4]);
   print "<td align=right>".putComa($jrow2[4])."</td>";
   printf("<td align=right>%.2f</td>",$dif);
   print "<td align=right>".putComa(round($jrow2[4]))."</td>";
  
/*   $t1+=$invrow[0];
   $t2+=$invrow[1];
   $t3+=$invrow[2];*/
   //$t4+=$invrow[3];

   $t5+=$jrow2[0];
   $t6+=$jrow2[1];
   $t7+=$jrow2[2];
   $t8+=$jrow2[3];
   $t9+=($jrow2[4]); //+$invrow[3]);
   $tdiff+=$dif;
   $tgtot+=round($jrow2[4]); //+$invrow[3]);

} // end else


}// end if jrow
	print "<tr bgcolor='white' ><td colspan=5 align=center> Total </td><td align=right><b>".round($t1,2)."</b></td><td></td><td align=right><b>".round($t2,2)."</b></td><td align=right><b>".round($t5,2)."</b></td><td align=right></td><td align=right><b>".round($t6,2)."</b></td><td align=right><b>".round($t9,2)."</b></td><td align=right><b>".round($tdiff,2)."</b></td><td align=right><b>$tgtot</b></td></tr>";  

}

?>
</table>


<h4>Warranty Spare Sales</h4>
<table align=center bgcolor=black border=1 cellpadding=1 cellspacing=1 width=90%>

<tr bgcolor='silver'> <td><b>No </td> <td><b>Date</td> <td>Customer</td><td> Amount</td> 
</tr>

<?
$tnet=0;
$ttax=0;
$tamt2=0;

$re=mysqli_query($db,"select distinct(invno),entrydate,customer from warspareinvoice where entrydate='$dt' and brcode='$brcode'")or die(mysqli_error());
while($row=mysqli_fetch_array($re))
{
  $res=mysqli_query($db,"select acname from customer where acname='$row[customer]' ");
  $cus_row=mysqli_fetch_row($res);

$rs=mysqli_query($db,"select round(sum(tot)) from warspareinvoice where brcode='$brcode' and invno=$row[invno]");
  $c=mysqli_fetch_row($rs);

 print "<tr bgcolor='white'><td>$row[invno]</td><td>".datetohtml($row[entrydate])."</td><td>$cus_row[0]</td>";
 print "<td align=right>".putComa($c[0])."</td>";
$tamt2+=$c[0];
}
print "<tr bgcolor=white><td colspan=3 align=center><b>Total</td>";
print "<td align=right>".putComa($tamt2)."</td></tr>";

?>
</table>

<!-- //********************************     -->

<h4>Return -Warranty Spare Sales</h4>
<table align=center bgcolor=black border=1 cellpadding=1 cellspacing=1 width=90%>

<tr bgcolor='silver'> <td><center><b>No </td> 
<td><center><b>Date</td> <td ><center><b>Customer</td><td ><center><b> Amount</td> 
</tr>

<?
$tnet=0;
$ttax=0;
$tamt2=0;

$re=mysqli_query($db,"select distinct(retno),entrydate,customer from wsalesreturn where entrydate='$dt' and brcode='$brcode'")or die(mysqli_error());
while($row=mysqli_fetch_array($re))
{
  $res=mysqli_query($db,"select acname from customer where acname='$row[customer]' ");
  $cus_row=mysqli_fetch_row($res);

$rs=mysqli_query($db,"select round(sum(tot)) from wsalesreturn where brcode='$brcode' and retno=$row[retno]");
  $c=mysqli_fetch_row($rs);

 print "<tr bgcolor='white'><td>$row[retno]</td><td>".datetohtml($row[entrydate])."</td><td>$cus_row[0]</td>";
 print "<td align=right>".putComa($c[0])."</td>";
$tamt2+=$c[0];
}
print "<tr bgcolor=white><td colspan=3 align=center><b>Total</td>";
print "<td align=right>".putComa($tamt2)."</td></tr>";

?>
</table>
<?
$i=1;
$sql="select * from branch where brcode='$brcode'";
$br=mysqli_query($db,$sql);
while($r=mysqli_fetch_array($br))
{


$sql="select * from branch where brcode='$r[brcode]'";
$brres=mysqli_query($db,$sql);
$brow=mysqli_fetch_array($brres);

$brc=$r[brcode];

$sql="select * from serinquiry where brcode ='$r[brcode]' and jvno>0 and entrydate='$dt' order by invno";

$r=mysqli_query($db,$sql) or die('Err:'.mysqli_error());


print "<tr><td  align=center><b>".strtoupper($brow[brname])." - ServiceCall </td></tr>";


$btax=0;
$tax=0;
$round=0;
$tot=0;

$a=1;

 
?>
<table align=center bgcolor=silver border=1 cellpadding=1 cellspacing=1 width=90%>

<tr ><th align=left>Sno<th>InvNo<th>Date<th>InquiryNo<th>Service<th>Msno<th>Customer<th>BeforeTax<th>ServiceTax 14.00%<th>Roundoff<th>Total<th>Engineer<th></tr>
<?

while($row=mysqli_fetch_array($r))
{

 


 print "\n<tr bgColor='white' onMouseOver=bgColor='aliceblue' onMouseOut=bgColor='#e7e7e7'>";
 print "\n<form action='updserreco.php' method='post'>";
 print "\n<input type=hidden name='inqno' value='$row[inqno]'>";
 print "\n<input type=hidden name='msno' value='$row[msno]'>";
 print "\n<input type=hidden name='brc' value='$brc'>";
print "\n<input type=hidden name='fdat' value='$row[fdate]'>";


 print "\n<input type=hidden name='dfrom' value='$dfrom'>";
 print "\n<input type=hidden name='dto' value='$dto'>";
 print "\n<td>".$a++."</b></td><td><b>$row[invno]</b></td><td>".datetohtml($row[entrydate])."</td><td>$row[inqno]</td><td>$row[lastserv]</td><td>$row[msno]</td><td>$row[prospect]</td><td>$row[rate]</td><td>$row[tax]</td><td>$row[roundoff]</td><td>$row[total]

</td><td>$row[exec]</td>";




$btax+=$row[rate];
$tax+=$row[tax];
$round+=$row[roundoff];
$tot+=$row[total];
?>
</form>
</tr>

<?
}

print "<tr ><td colspan=7 align=center> Total </td><td ><b>$btax</b></td><td><b>$tax</b></td><td>$round</td><td>$tot</td></tr>";  
?>
</table>
<?
//============asc start

$sql="select * from ascdetails where brcode ='$brc' and jvno>0 and entrydate='$dt'order by invno";


$r=mysqli_query($db,$sql) or die('Err:'.mysqli_error());


print "<tr><td   align=center><b>".strtoupper($brow[brname])." - ASC </td></tr>";


$btax=0;
$tax=0;
$round=0;
$tot=0;

$a=1;

 
?>

<table align=center bgcolor=silver border=1 cellpadding=1 cellspacing=1 width=90%>

<tr><th align=left>Sno<th>InvNo<th>Date<th>RefNo<th>Service<th>Msno<th>Customer<th>BeforeTax<th>ServiceTax 14.00%<th>Roundoff<th>Total<th></tr>
<?

while($row=mysqli_fetch_array($r))
{

 


print "<tr bgcolor='white' ><td><b>".$a++."</b></td><td><b>".abs($row[invno])."</b></td><td>".datetohtml($row[entrydate])."</td><td>$row[sno]</td><td>$row[lastser]</td><td>$row[mser]</td><td>$row[customer]</td><td>$row[amt]</td><td>$row[tax]</td><td>$row[roundoff]</td><td>$row[total]

</td>";

if($row[brcode]==$brcode)
{
print "<td><input type=button value='Print' onClick=javascript:prnasc('$row[invno]','$row[brcode]')></td></tr>";  
}
$btax+=$row[amt];
$tax+=$row[tax];
$round+=$row[roundoff];
$tot+=$row[total];

}
print "<tr bgcolor='white' ><td colspan=7 align=center> Total </td><td ><b>$btax</b></td><td><b>$tax</b></td><td>$round</td><td>$tot</td></tr>";  


//===============asc end




}


?>
</table>

<?

