
<?php

$i=1;
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>Ledger Name</td>    
    <td>Group Name</td>
	<!--<td>Opening Balance</td>-->
    <td>Amount</td>
    <!--<td>Delete</td>-->  
  </tr>
  
<?php


$ledger_name ='';
$fromdate='';
$todate='';


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.category as parent FROM cf_ledger a LEFT JOIN cf_account_groups b ON b.id = a.groupid WHERE a.status = '1' ";

if(isset($_REQUEST['ledger_name']) && $_REQUEST['ledger_name']!=''){
$ledger_name=$_REQUEST['ledger_name'];
$sq_arr.="and   a.ledgerid = '$ledger_name'";
}
//if(isset($_REQUEST['parent_name']) && $_REQUEST['parent_name']!=''){
//$parent_name=$_REQUEST['parent_name'];
$sq_arr.="and   a.groupid='22'";
//}
$sq_arr.=" order by a.ledgerid asc ";
$query=$sq_arr;
//echo $query;
$function="Searchdate";
include("../paging.php");
$sq_arr.=" LIMIT $start, $limit";

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++;

$cumulative= '';
	$ledgerid = $res['ledgerid'];
$cond = "and b.ledgerid = '$ledgerid' "; 


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


	$sq_arr11="select * from (SELECT a.transaction_bank  as toid,0 as acctype,a.pay_date as pay_date,a.amount as amount,a.reciept_id as reciept_id,a.purpose as purpose,0 as ref,b.ledgername as ledgername, 1 as type 
    	  FROM cf_reciept a
          LEFT JOIN cf_ledger b ON b.ledgerid = a.from where 1 ".$cond."
          UNION SELECT a.transaction_bank  as toid,0 as acctype,a.pay_date as pay_date,a.amount as amount,a.voucher_id as reciept_id,a.purpose as purpose,0 as ref,b.ledgername as ledgername, 2 as type 
          FROM cf_voucher a
          LEFT JOIN cf_ledger b ON b.ledgerid = a.to where 1 ".$cond."
		  
	UNION SELECT a.journal_id as toid,a.account_type as acctype,a.pay_date as pay_date,a.amount as amount,a.ref_no as reciept_id,a.purpose as purpose,a.journal_reference as ref,b.ledgername as ledgername, 3 as type 
		  FROM cf_journal a 
		  LEFT JOIN cf_ledger b ON b.ledgerid = a.crd_dbt where 1  ".$cond."
		  
		  UNION SELECT  a.pur_acc_id as toid,a.account_type as acctype,a.pay_date as pay_date ,a.acc_amount as amount,a.account_reference as reciept_id,b.ledgername as purpose,0 as ref,b.ledgername as ledgername, 4 as type FROM cf_purchase_account a 
			LEFT JOIN cf_ledger b ON b.ledgerid = a.ven_client_id where 1 ".$cond. " )  k order by k.pay_date ASC";
	
	if($res['cr_dt'] == 1)
	{
		$cumulative= 0-$res['opening_balance'];
	}
	else
	{
		$cumulative= $res['opening_balance'];
	}
	//$cumulative= $res['opening_balance'];
	$sq_arr12=mysql_query($sq_arr11);
	while($rws = mysql_fetch_array($sq_arr12))
	{
	if($rws['type'] ==1 ){
	$cumulative=$cumulative-$rws['amount'];
	?>
	
	<?php } ?>
	<!--------for voucher---------------->
	<?php if($rws['type'] ==2 ){
	$cumulative=$cumulative+$rws['amount'];
	?>
	
	
	<?php } ?>
	<!--------for journal---------------->
	<?php if($rws['type'] ==3 )
	{ 
	if($rws['acctype'] ==1){
	$cumulative=$cumulative-$rws['amount'];
	?>
					

	<?php } else {
	$cumulative=$cumulative+$rws['amount'];
	?>
	 	
	<?php } 
	}?>
	
	<!--------for Purchase---------------->
	<?php if($rws['type'] ==4 )
	{ 
	if($rws['acctype'] ==1){
	$cumulative=$cumulative-$rws['amount'];
	?>
		
				

	<?php } else {
	$cumulative=$cumulative+$rws['amount'];
	?>
	 	
	<?php } 
	}

}


  ?> 
  <tr>
    <td><?php echo $pagecnt;?></td>
    <td><?php echo $res['ledgername'];?></td>
    <td><?php echo $res['parent'];?></td>
	<td><?php echo $cumulative;?></td>
    <?php /*?><td><?php echo $res['opening_balance'];?></td>
   <td class="tr_td_b"> <a href="#login-box" class="login-update" style="font-weight:bold;" data-id=<?php echo $res['ledgerid']?>><img src="images/edit.png" title="edit"><input type="hidden" name="acc_id" id="acc_id<?php echo $res['ledgerid'];?>" value="<?php echo $res['ledgerid'];?>" /></a></td><?php */?>
   
   <?php /*?><td class="tr_td_r" ><a href="account-ledger.php?del=delete&id=<?php echo $res['ledgerid']; ?>" onClick="return confirm('Do you really want to delete this field?');"><img src="images/delete.png" title="delete"></a></td><?php */?>
   
  </tr>
  <?php 
    }}
  ?>
 
  
   
</table>
  
   <div align="right" style="padding-top:10px;"><?=$pagination?></div>