<?
include_once('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>Khadi</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><center><b>Ledger</th> <th width=100><center><b>Receipt</th><th width=100><center><b> Payment</th><th width=100>Closing</th>

<?
$bal=mysqli_query($db,"select op from ledger where led='Cash' and brcode='$brcode'");
$sql="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);
if($r2[0]>0)
{
    $opbal=($opbalance[0]+$r2[0]);
}
else
{
	$opbal=$opbalance[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;
$table="tempjournal".$brcode.$user;
mysqli_query($db,"delete from $table");
$qr="create table $table(invdt date,partic varchar(100),dbttot double(14,2),crtot double(14,2))";
mysqli_query($db,$qr);
while($row1=mysqli_fetch_array($result1))
{
	$tr=0;
	$tp=0;
    $sql1=mysqli_query($db,"select date,partic,dramt,cramt,jvno from journal2 where date='$row1[date]' and brcode='$brcode' and left(j_type,7)!='Counter' and partic='Cash'");
    while($row12=mysqli_fetch_array($sql1))
    {
        $sql12=mysqli_query($db,"select partic,j_type,narr from journal2 where brcode='$brcode' and jvno = $row12[jvno] and partic!='Cash'");
        $row13=mysqli_fetch_array($sql12);
        $sql="insert into $table(invdt,partic,dbttot,crtot) values ('$row12[date]','$row13[0] ($row13[1]-$row13[2])',$row12[dramt],$row12[cramt])";
        $rs=mysqli_query($db,$sql);	
    }
}
if($opbal>0)
{
print "<tr><td></td><td><font color=green>To Opening Balance</td><td align=right><font color=green>".number_format($opbal,2,'.',',')."</td><td></td></tr>";
}
else
{
print "<tr ><td></td><td><font color=green>To Opening Balance</td><td align=right><font color=red>".number_format($opbal,2,'.',',')."</td><td></td></tr>";
}

$quer="select invdate,gname,sum(total),sum(redct),sum(mba),sum(strbtnml),sum(strbtspl),sum(taxamt),sum(staffdisc),sum(nettot) from prod_invoice where invdate>='$df' and invdate<='$dt' and brcode='$brcode' group by invdate,gname";
//echo $quer;
$rsqr=mysqli_query($db,$quer);
while($row=mysqli_fetch_array($rsqr))
{
	if($row[2]>0)
		{
			$rqury="insert into $table values('$row[0]','$row[1].Sale','$row[2]',0)";
			mysqli_query($db,$rqury);
		}
		if($row[3]>0)
		{
			$rqury="insert into $table values('$row[0]','$row[1].Reduction',0,'$row[3]')";
			mysqli_query($db,$rqury);
		}
		if($row[4]>0)
		{
			$rqury="insert into $table values('$row[0]','$row[1].MDA',0,'$row[4]')";
			mysqli_query($db,$rqury);
		}
		if($row[5]>0)
		{
			$rqury="insert into $table values('$row[0]','$row[1].StateRebateNormal',0,'$row[5]')";
			mysqli_query($db,$rqury);
		}
		if($row[6]>0)
		{
			$rqury="insert into $table values('$row[0]','$row[1].StateRebateSpecial',0,'$row[6]')";
			mysqli_query($db,$rqury);
		}
		if($row[7]>0)
		{
			$rqury="insert into $table values('$row[0]','$row[1].Tax','$row[7]',0)";
			mysqli_query($db,$rqury);
		}
		if($row[8]>0)
		{
			$rqury="insert into $table values('$row[0]','$row[1].Staffdisc',0,$row[8])";
			mysqli_query($db,$rqury);
		}
		//if($row[9]>0)
		//{
		//	$rqury="insert into $table values('$row[0]','$row[1].Cash',$row[9],0)";
		//	mysqli_query($db,$rqury);
		//}
		//echo $rqury;
		//echo "<br>";
}
$fnl=mysqli_query($db,"select * from $table order by invdt,partic");
$fn2=mysqli_query($db,"select * from $table order by invdt,partic");
$row12=mysqli_fetch_row($fn2);
$mdate = $row12[0];
$mtotdr = $opbal;
$mtotcr = 0;
while($frow=mysqli_fetch_array($fnl))
{
	//echo "sdf".$mdate;
	
	if($mdate < $frow[0])
	{
		print "<tr><td colspan=2><td align=right><font color=green>".number_format($mtotdr,2,'.',',')."<br><hr></td><td align=right> <font color=green>".number_format($mtotcr,2,'.',',')."<br><hr></td><td></td></tr>";
		$closebal=$mtotdr-$mtotcr;
        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=red>" . number_format($closebal, 2, '.', ',') . "</td>";
            print "<tr><td></td><td align=left><font color=green><b>To Opening Balance</td><td align=right><font color=red>".number_format($closebal,2,'.',',')."</td><td></td></tr>";	
        }
        else
        {
            print "<tr ><td colspan=3 align=center><font color=green><b>by Closing Balance</td><td></td><td align=right><font color=green>" . number_format($closebal, 2, '.', ',') . "</td>";
            print "<tr><td></td><td align=left><font color=green><b>To Opening Balance</td><td align=right><font color=green>".number_format($closebal,2,'.',',')."</td><td></td><td></td></tr>";	
        }
		$mtotcr = 0;
		$mtotdr = $closebal;	
		$mdate  = $frow[0];
	}
	print "<tr ><td>".$frow[0]."</td><td>".$frow[1]."</td>";
	//echo $fnl_row[0];
    print "<td align=right>".$frow[2]."</td><td align=right>".$frow[3]."</td><td></td>";
	print "</tr>";
	$mtotdr+= $frow[2];	
	$mtotcr+= $frow[3];

//	$tp+=$fnl_row[2]; $tr+=$fnl_row[3];
//	$closebal=($tr+$opbal)-$tp;
}
print "<tr><td colspan=2><td align=right><font color=green>".number_format($mtotdr,2,'.',',')."<br><hr></td><td align=right> <font color=green>".number_format($mtotcr,2,'.',',')."<br><hr></td><td></td></tr>";
$closebal=$mtotdr-$mtotcr;
if ($closebal<0) {
    print "<tr ><td colspan=3 align=center><font color=green><b>by Closing Balance</td><td></td><td align=right><b><font color=red>" . number_format($closebal, 2, '.', ',') . "</td>";
}
else
{
    print "<tr ><td colspan=3 align=center><font color=green><b>by Closing Balance</td><td></td><td align=right><b><font color=green>" . number_format($closebal, 2, '.', ',') . "</td>";
}
?>
</table>

