<link type="text/css" href="includes/style1.css" rel="stylesheet" />
<link type="text/css" href="css/form1.css" rel="stylesheet" />

<?
 session_start();
if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); parent.document.location='ind.php' </script>";
  return;
}
 include("test.php");
include('includes/top.php'); 

 $df=$y."-".$m."-".$d;
 $dt=$y1."-".$m1."-".$d1;
?>

<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>
Day Book from <? print datetohtml($df).' to '.datetohtml($dt)?>

</center>

 
<tr bgcolor='silver'><td width=70><center><b>Date</td>  <td><center><b>Ledger</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($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'  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=1><font color=green>To Opening Balance (Cash)</td><td align=right><font color=green>".round($opbal,2)."</td><td></td></tr>";
}
else
{
print "<tr bgcolor=white><td></td><td colspan=1><font color=green>To Opening Balance (Cash)</td><td align=right><font color=red>".round($opbal,2)."</td><td></td></tr>";
}
$sq="select distinct jvno from journal2 where date='$row1[date]'  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' order by type desc");
          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[partic] - $fnl_row[narr]-$fnl_row[against]:$fnl_row[j_type]</td>";
                print "<td align=right>".nozero($fnl_row[dramt])."</td><td align=right>".nozero($fnl_row[cramt])."</td>";
		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 (Cash)</td><td></td><td align=right><font color=green>".round(abs($closebal),2)."</td>";
}
else
{
print "<tr bgcolor=silver><td colspan=2 align=center><font color=green><b>by Closing Balance (Cash)</td><td></td><td align=right><font color='red'>(".round(abs($closebal),2).")</td>";
}

print "<tr bgcolor=silver><td colspan=2><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;
	
}
?>

