<?
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");
?>
<form action='<?print $PHP_SELF?>'>
<table>
<tr><td>Date </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>
<td><input type=submit name='submit' value='Show'> 
<input type=submit name='print' value='Print'></td></tr>
</table>
<?
$print = $_REQUEST['print'];
$m = $_REQUEST['m'];
$d = $_REQUEST['d'];
$y = $_REQUEST['y'];
$submit = $_REQUEST['submit'];
if($print)
{
  $dt=$y.'-'.$m.'-'.$d;
  print "<script> window.open('prndstate.php?dt=$dt'); </script>";
}

if($submit)
{
 $dt=$y.'-'.$m.'-'.$d;
PRINT "<CENTER><H3>DAILY STATEMENT FOR THE DATE".implode('-', array_reverse(explode('-', $dt)));
 $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 "Cash Opening: ".$bal_row[0];
?>
</center>
<h3>CASH</H3>
<table bgcolor=ivory cellpadding=1 cellspacing=1 width=100%>
<tr bgcolor='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>
<?
$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());
//print 'opbal='.$opbal;
$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>";
}

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></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 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>".implode('-', array_reverse(explode('-', $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=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><td></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><td></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><td></td><td></td></tr>";
$opbal=$closebal;
$head++;	
}

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


<?
 include("opendb.php");
$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><H3>BANK :<font color=red>".$bnk_row[0]."</H3>";
$bnk=$bnk_row[0];
?>
<table bgcolor=ivory cellpadding=1 cellspacing=1 width=100%>
<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><td><center><b>Ref.</td><td><center><b>Ref. NO.</td>
<?
$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>".implode('-', array_reverse(explode('-', $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><td></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><td></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><td></td><td></td></tr>";
$opbal=$closebal;
 }	
print "</table><br>";
}
?>
</FONT>

<!-- //********************************     -->
<h3>PURCHASE</h3>
<table bgcolor=ivory cellpadding=1 cellspacing=1 width=100%>
<th>PNo.<th>Inv NO. <th>Inv Date <th>From <th>DA <th>MCode <th>Config
<th>Net Amt. <th>Tax% <th>Tax <th>Cess<th>Before Tax 
<?
$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[pur_no]</td><td>$row[invno]</td><td>".implode('-', array_reverse(explode('-', $row['invdate'])))."</td><td>$row[ledger]</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>";
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>

<!-- //********************************     -->
<h3>SALES</h3>
<table bgcolor=ivory cellpadding=1 cellspacing=1 width=100%>
<th>Inv NO. <th>Inv Date <th>To <th>DA <th>MCode <th>Config
<th>Net Amt. <th>Tax% <th>Tax <th>cess<th>Before Tax 
<?
$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>".implode('-', array_reverse(explode('-', $row['invdate'])))."</td><td>$cus_row[0]</td>";
 print "<td>$row[da]</td><td>$row[model_code]</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>

<!-- //********************************     -->
<h3>PURCHASE RETURN</h3>
<table bgcolor=ivory cellpadding=1 cellspacing=1 width=100%>
<th>Ret NO. <th>Date <th>To <th>DA <th>MCode <th>Config
<th>Net Amt. <th>Tax% <th>Tax <th>Before Tax 
<?
$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>".implode('-', array_reverse(explode('-', $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=silver><td colspan=6 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>

<!-- //********************************     -->
<h3>SALES RETURN</h3>
<table bgcolor=ivory cellpadding=1 cellspacing=1 width=100%>
<th>Ret NO. <th>Date <th>From <th>DA <th>MCode <th>Config
<th>Net Amt. <th>Tax% <th>Tax <th>Before Tax 
<?
$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>".implode('-', array_reverse(explode('-', $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=silver><td colspan=6 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>

<!-- //********************************     -->
<h3>DEBIT NOTE</h3>
<table bgcolor=ivory cellpadding=1 cellspacing=1 width=100%>
<th>DB. No  <th>Particulars <th>Debit <th>Credit <th>Narration <th>Ref <th>No<?
$tnet=0;
$ttax=0;
$tamt2=0;

$re=mysqli_query($db,"select distinct dbno from dbnote where date='$dt' and brcode='$brcode'")or die(mysqli_error());
while($row=mysqli_fetch_array($re))
{
   print "\n<tr bgcolor='silver'><td>$row[dbno]</td><td></td>";
  print "<td></td><td></td><td></td><td></td><td></td></tr>";
  $re2=mysqli_query($db,"select * from dbnote where dbno='$row[dbno]'") or die(mysqli_error());
    while($jvrow=mysqli_fetch_array($re2))
    {
        print "\n<tr bgcolor=white><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><td>$jvrow[against]</td><td>$jvrow[no]</td></tr>";
  }
     print "<tr><td colspan=9 bgcolor=silver></td></tr>";

}
?>
</table>
<!-- //********************************     -->


<!-- //********************************     -->
<h3>CREDIT NOTE</h3>
<table bgcolor=ivory cellpadding=1 cellspacing=1 width=100%>
<th>No.<th>D.A <th>MCode <th>Config<th>Sale_Inv.<th>Cust<th>Net Amt<th>Tax <th>Tax% <th>Before<br> Tax
<?
$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]' and brcode='$brcode'");
  $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=silver><td colspan=6 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>
<!-- //********************************     -->
<h3>JOURNAL ENTRIES</h3>

<?
$re=mysqli_query($db,"select distinct jvno,date from journal2 where brcode='$brcode' and date='$dt' and j_type='JV'");
print "<table bgcolor='ivory' border=0 cellspacing=3 cellpadding=1 width=100%> <th>Tr. No <th>Date <th>Particulars <th>Debit <th>Credit <th>Narration <th>Ref <th>No";
while($row=mysqli_fetch_array($re))
{
  print "\n<tr bgcolor='silver'><td>$row[jvno]</td><td>".implode('-', array_reverse(explode('-', $row['date'])))."</td><td></td>";
  print "<td></td><td></td><td></td><td></td><td></td></tr>";
  $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><td>$jvrow[against]</td><td>$jvrow[no]</td></tr>";
  }
     print "<tr><td colspan=9 bgcolor=silver></td></tr>";

}
?>
</table>

<style>
 body,td{font-family:arial narrow;font-size:14;color:black;}
 th{font-family:arial narrow;font-size:14;color:black;background-color:silver}
 body{background-color:ivory;}
</style>

