<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); parent.document.location='ind.php' </script>";
  return;
}
 include("test.php");
$y=$_REQUEST['y'];
$m=$_REQUEST['m'];
$d=$_REQUEST['d'];
$y1=$_REQUEST['y1'];
$m1=$_REQUEST['m1'];
$d1=$_REQUEST['d1'];
?>
<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>
<body onload='window.print();'>
<center>
 
<font face=fixed size=4>
<? print $company?><br>
<?

 $df=$y.'-'.$m.'-'.$d;
 $dt=$y1.'-'.$m1.'-'.$d1;
?>
Cash Book for the period of <?print datetoHtml($df)." To ".datetoHtml($dt)?>
<br>
<?print "Printed by $user";?><br>&nbsp;
<table bgcolor=ivory  border='1' width=100% style='border-collapse:collapse;'>
<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 IN HAND' 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 IN HAND')";
$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 IN HAND' 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 IN HAND' 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 IN HAND') // && $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;
	
}

print "<tr style='border:0px solid white'><td colspan=5 height='30' width=50%> prepared by:</td><td colspan=1 height='30' width=70% > approved by:</td></tr></table>";
?>

