<?php
include("../include/config.php");
require_once "../include/Database_functions.php";
?>

   <table  border="0" cellspacing="0" cellpadding="0" class="tableList">
  <tr class="heading">
    <td>No</td>
	<td>Date</td>
	<td>Ref No.</td>   
    <td>Ledger Name</td>
	<td>Description</td>   
    <td>Debit</td>
	<td>Credit</td>
	<td>Running Balance</td>
	
  	</tr>
<?php

$ledger_name ='';
$fromdate='';
$todate='';
$cond = '';
$cumulative=0;
if(isset($_REQUEST['ledger_name'])){
$ledger_name=$_REQUEST['ledger_name'];
}

if(isset($_REQUEST['fromdate'])){
$fromdate=$_REQUEST['fromdate'];
$fromdate1 = date('Y-m-d', strtotime($fromdate));
}

if(isset($_REQUEST['todate'])){
$todate=$_REQUEST['todate'];
$todate1 = date('Y-m-d', strtotime($todate));
}
$i=1;
	   $sq_arr="SELECT a.*,b.ledgername as ledgername, 1 as type 
    FROM cf_reciept a
        LEFT JOIN cf_ledger b
            ON b.ledgerid = a.from where cashed=1
UNION SELECT a.*,b.ledgername as ledgername, 2 as type 
    FROM cf_voucher a
        LEFT JOIN cf_ledger b
            ON b.ledgerid = a.to where cashed=1";
			
if($ledger_name!=''){
$cond.=" and   b.ledgername like '%".$ledger_name."%'";
}



if(isset($fromdate) && $fromdate!=''){
$from_date=date('Y-m-d',strtotime($fromdate));
$cond.=" and   a.pay_date >= '$from_date'";
}
if(isset($todate) && $todate!=''){
$todate=date('Y-m-d',strtotime($todate));
$cond.=" and   a.pay_date <= '$todate'";
}

/*if($sdate_12!=''){
$sq_arr.=" and date='".$sdate."' ";
}*/
$sq_arr="SELECT a.*,b.ledgername as ledgername, 1 as type 
    FROM cf_reciept a
        LEFT JOIN cf_ledger b
            ON b.ledgerid = a.from where cashed=1 ".$cond."
UNION SELECT a.*,b.ledgername as ledgername, 2 as type 
    FROM cf_voucher a
        LEFT JOIN cf_ledger b
            ON b.ledgerid = a.to where cashed=1 ".$cond."";
			
$query=$sq_arr;
//echo $query;
$function="Searchdate";
include("../paging.php");

if(isset($_REQUEST['page'])){
$page=$_REQUEST['page'];
}else{
$page=10;
}

$array=mysql_query($sq_arr);
if($array && mysql_num_rows($array)>0){
$pagecnt=$start;
while($res=mysql_fetch_array($array))
{

$pagecnt++;


  ?> 
  <tr>
    <td><?php echo $pagecnt;?></td>
    <td><?php echo date('d-m-Y',strtotime($res['pay_date']));?></td>
	 
    <td><?php echo $res['reciept_id'];?></td>
	<td><?php echo $res['ledgername'];?></td>
	<td><?php echo $res['purpose'];?></td>
	<?php if($res['type'] ==1 )
	{
	$cumulative=$cumulative+$res['amount'];
	?>
    <td><?php echo $res['amount'];?></td>
	<td>0</td>
	<?php
	
	}
	 ?>
	 <?php if($res['type'] ==2 )
	{
	$cumulative=$cumulative-$res['amount'];
	?>
	<td>0</td>
    <td><?php echo $res['amount'];?></td>
	 
	<?php
	}
	 ?>
	  <td><?php echo $cumulative;?></td>

   		
  </tr>
  <?php 
    }}
  ?>
 

   
</table>  
   <div align="right" style="padding-top:10px;"><?=$pagination?></div>