<?
include('top.php');
//include_once('../opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); parent.document.location='../inde.php' </script>";
  return;
}
$y=$_REQUEST['y'];
$m=$_REQUEST['m'];
$d=$_REQUEST['d'];
$y1=$_REQUEST['y1'];
$m1=$_REQUEST['m1'];
$d1=$_REQUEST['d1'];
// include("../test.php");

?>
<style>
/* td{font-family:arial narrow;font-size:14;color:black;}

 select
  {border-style:ridge;
   border-color:black;
   height:25px;
   border-size=1;
   font-family:verdana;
   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>
<?
 $df=$y."-".$m."-".$d;
 $dt=$y1."-".$m1."-".$d1;
?>
<script>
function show()
{
//window.open("prncbook.php?df=<?print$df?>&dt=<?print$dt?>");
}
</script>
<title>Xpress</title>
    <script type="text/javascript" src="../scripts/jquery-latest.js"></script>
	<link type="text/css" href="../includes/style1.css" rel="stylesheet" />
	<link type="text/css" href="../css/form.css" rel="stylesheet" />
    <script type="text/javascript">
        $(function() {
          if ($.browser.msie && $.browser.version.substr(0,1)<7)
          {
			$('li').has('ul').mouseover(function(){
				$(this).children('ul').show();
				}).mouseout(function(){
				$(this).children('ul').hide();
				})
          }
        });        
    </script>

<body>


<table class="container"><tr><td>

 <center>
<font face=verdana size=3>

Cash Book from <? print $df.' to '.$dt?>
<table cellpadding=1 cellspacing=1 class='report'>
<tr><th width=90><center><b>Date</th> <th width=80><center><b>Ref </th> <th><center><b>Ledger</th> <th width=100><center><b>Receipt</th><th width=100><center><b> Payment</th>

<?


$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><td></td><td colspan=2><font color=green>To Opening Balance</td><td align=right><font color=green>".$opbal."</td><td></td></tr>";
}
else
{
print "<tr ><td></td><td colspan=2><font color=green>To Opening Balance</td><td align=right><font color=red>".$opbal."</td><td></td></tr>";
}
//echo $opbal;
//exit;
$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))
    {
	   $jvnos=$prt_row[0];
	}
	//echo $jvnos;

  	//$fnl=mysqli_query($db,"select * from journal2 where jvno='$prt_row[0]' and brcode='$brcode' order by no");
	$fnl=mysqli_query($db,"select sum(dramt),sum(cramt),date,against,no,jvno,partic from journal2 where date='$row1[date]' and brcode='$brcode' and partic!='%Counter Sale' group by partic,date");
          while($fnl_row=mysqli_fetch_array($fnl))
          {
               if($fnl_row[partic]!='Cash') // && $fnl_row[type]=='Dr'
		{
                 print "<tr ><td>".$fnl_row[date]."</td><td>$fnl_row[3]:$fnl_row[no]</td><td>$fnl_row[partic]</td>";
                print "<td align=right>".$fnl_row[1]."</td><td align=right>".$fnl_row[0]."</td>";
		print "</tr>";
			if($fnl_row[type]=='Dr') $tp+=$fnl_row[0]; else $tr+=$fnl_row[1];

$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>".abs($closebal)."</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'>(".abs($closebal).")</td>";
}

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

