<?
 session_start();
if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); parent.document.location='ind.php' </script>";
  return;
}
 include("../test.php");
?>
<style>
 body,td{font-family:arial;font-size:13;color:black;}
 body{background-color:white;}
 select
  {border-style:ridge;
   border-color:black;
   height:25px;
   border-size=1;
   font-family:fixed;
   color:black;
  }
 input
  {border-style:ridge;
   border-color:#22437f;
   background-color:white;
   height:25px;
   font-weight:bold;
   border-size=1;
   font-family:sans-serif;
   color:BLACK;
  }
</style>
<center>
 
<font face=fixed size=4>
<? print $branch?><br>
Cash Book for the period of <?print datetoHtml($df)." To ".datetoHtml($dt)?>
<br>
<?print "Printed by $user";?><br>&nbsp;
<table bgcolor=ivory cellpadding=1 cellspacing=2 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>

<?
 include("../opendb.php");

$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>='$r1[0]' and date<'$df' and (partic='Cash')";

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

$opbal=($opbalance[0]+$r2[0]);
$result1=mysqli_query($db,"select distinct date from journal2 where brcode='$brcode' and date>='$df' and date<='$dt' and partic='Cash' order by date")or die(mysqli_error($db));

$head=1;
while($row1=mysqli_fetch_array($result1))
{

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($db));
$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>$fnl_row[partic]</td>";
                print "<td align=right>".nozero($fnl_row[cramt])."</td><td align=right>".nozero($fnl_row[dramt])."</d>";
		print "<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 ><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 ><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><b>".putComa($tp+$closebal)."<br><hr></td><td align=right><b>".putComa($tp+$closebal)."<br><hr></td><td></td></tr>";
$opbal=$closebal;
	
}
?>

