<?
session_start();
$brcode = $_SESSION['brcode'];
$branch = $_SESSION['branch'];
if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); parent.document.location='ind.php' </script>";
  return;
}
 include("test.php");
 $d = $_REQUEST['d'];
 $m = $_REQUEST['m'];
 $y = $_REQUEST['y'];
 $d1 = $_REQUEST['d1'];
 $m1 = $_REQUEST['m1'];
 $y1 = $_REQUEST['y1'];
 $df=$y."-".$m."-".$d;
 $dt=$y1."-".$m1."-".$d1;
?>
<link href="css/printline.css" type="text/css" rel="stylesheet" media="print,screen"  />
<table width=100%" border="0" cellspacing="0" cellpadding="0" class="grid" >

<center>

<font face=arial narrow size=3>
<?print $branch?><br>
<?
$res=mysqli_query($db,"select braddr from branch where brcode = '$brcode'");
$r1=mysqli_fetch_row($res);
?>
<?print $r1[0]?><br>
Cash Book from <? print implode('-', array_reverse(explode('-',$df))).' to '.implode('-', array_reverse(explode('-',$dt)))?>

</center>

 
<tr bgcolor='silver'><td width=70><center><b>Date</td>  <td><center><b>Ledger</td><td><center><b>No :</td> <td width=100><center><b>Receipt</td><td width=100><center><b> Payment</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());
$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());

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

if($opbal>0)
{
print "<tr bgcolor=white><td></td><td colspan=1><font color=green>To Opening Balance</td><td></td><td align=right><font color=green>".putComa($opbal)."</td><td></td></tr>";
}
else
{
print "<tr bgcolor=white><td></td><td colspan=1><font color=green>To Opening Balance</td><td></td><td align=right><font color=red>".putComa($opbal)."</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[partic] <br>$fnl_row[narr]-$fnl_row[against]:$fnl_row[no]</td>";
                print "<td>$fnl_row[no]</td><td align=right>".nozero($fnl_row['cramt'])."</td><td align=right>".nozero($fnl_row['dramt'])."</d>";
		print "</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=2 align=center><font color=green><b>by Closing Balance</td><td></td><td></td><td align=right><font color=green>".putComa(abs($closebal))."</td>";
}
else
{
print "<tr bgcolor=silver><td colspan=2 align=center><font color=green><b>by Closing Balance</td><td></td><td></td><td align=right><font color='red'>(".putComa(abs($closebal)).")</td>";
}

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

