<?
ob_start();
session_start();
if(isset($_SESSION['ses_name']))
{
$user=$_SESSION['ses_name'];
}else{
header("location:index.php");
}
$parent_name='';
$ledger_name='';
$veh_num='';
$sdate_12 ='';
$page='1';
include("include/config.php");
require_once "include/Database_functions.php";
$sqlObj=new sqlclass();
$msg = '';
$cumulative=0;

if(isset($_REQUEST['from_date']) && $_REQUEST['from_date']!=''){
$from_date=date('d-m-Y',strtotime($_REQUEST['from_date']));
}
else
{
$from_date=date('d-m-Y');
}
if(isset($_REQUEST['to_date']) && $_REQUEST['to_date']!=''){
$to_date=date('d-m-Y',strtotime($_REQUEST['to_date']));
}
else
{
$to_date=date('d-m-Y');
}

function print1($selectype,$from_date,$to_date)
			{
			
				//$userids = $_SESSION['admin_id'];
				
					$dates = date('Y-m-d');
					$names = $selectype.$dates;
					$outstr = NULL;
					header("Content-type: application/vnd.ms-word");
					header("Content-Disposition: attachment;Filename=".$names.".xls");
			
					//set the directory to get image			
					// Query database to get data
					$i = 1;
					'<div id="header" >';
					
										
					$outstr.='
						 <table style="font-family:Arial, Helvetica, sans-serif; font-size:12px;width:500px;border:1px solid #000000; " cellspacing=0 cellpadding=0 ">';
						 
						 $open_date='';$presentdate=date('Y-m-d',strtotime($from_date));

					//echo "first";
					$sql="select * from tbl_openingbal where open_date<='".$presentdate."' and cashorbank = '1'";
					
					$row=mysql_query($sql);
					if($row && mysql_num_rows($row)>0){
					
					while($rows=mysql_fetch_array($row))
					{
					$open_date=$rows['open_date'];
					$open_amt=$rows['open_amt'];
					}
					
					}
					
					if($open_date!='')
					{
					
					 $balancesum="select sum(amount) as totalreciept from cf_reciept where cashed=1 and pay_date<'".$presentdate."' and pay_date >='".$open_date."' ";
					}
					else
					{
					
					
					
					 $balancesum="select sum(amount) as totalreciept from cf_reciept where cashed=1 and pay_date<'".$presentdate."' ";
					}
					 $balancesum;
					$balancesum_row=mysql_query($balancesum);
					if($balancesum_row && mysql_num_rows($balancesum_row)>0){
					
					while($balancesum_rows=mysql_fetch_array($balancesum_row))
					{
					
					$totalreciept=$balancesum_rows['totalreciept'];
					
					}
					
					}
					
					if($open_date!='')
					{
					
					 $balancesumvoucher="select sum(amount) as totalpayment from cf_voucher where cashed=1 and pay_date<'".$presentdate."' and pay_date >='".$open_date."' ";
					}
					else
					{
					
					
					 $balancesumvoucher="select sum(amount) as totalpayment from cf_voucher where cashed=1 and pay_date<'".$presentdate."' ";
					}
					
					$balancesumvoucher_row=mysql_query($balancesumvoucher);
					if($balancesumvoucher_row && mysql_num_rows($balancesumvoucher_row)>0){
					
					while($balancesumvoucher_rows=mysql_fetch_array($balancesumvoucher_row))
					{
					
					
					 $totalpayment=$balancesumvoucher_rows['totalpayment'];
					
					}
					
					}
					
					//---------------------------total contra calculation ---------------------------------------------//
					if($open_date!='')
					{
					
					$getotal_contra = 0;
					$balancesumcontra=mysql_query("select * from cf_contra where type_book=1 and pay_date<'".$presentdate."' and pay_date >='".$open_date."' ");
					while($rescontra=mysql_fetch_array($balancesumcontra))
					{  
						if($rescontra['account_type'] == '1')
						{
							$getotal_contra = $getotal_contra - $rescontra['amount'];
							//echo "<br";
						}
						else if($rescontra['account_type'] == '2')
						{
							$getotal_contra = $getotal_contra + $rescontra['amount'];
						}	
					}
					}
					else
					{
					$balancesumcontra="select sum(amount) as totalcontra from cf_contra where type_book=1 and pay_date<'".$presentdate."' ";
					}
					
					$cumulative=$opening_balance=(($open_amt)+($totalreciept-$totalpayment+$getotal_contra));
//---------------------------End-------------------------------------------------------------------//
					
					$outstr.='
					<tr>
					     	<td colspan="8" style="font-size:14px; min-width:100px; text-align:center; padding:5px; min-width:200px;border-right:1px solid #000000;"><strong>Cash Book</strong></td>
    					</tr>
					<tr >
					  <td colspan="8" align="right"><strong> Opening Balance : '.$opening_balance.'</td>
						</tr>
						 
						 
						<tr>
					     	<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000;"><strong>No</strong></td>
    						<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000;"><strong>Date</strong></td>
    						<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;"><strong>Ref No</strong></td>
							<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;"><strong>Ledger Name</strong></td>
							
    						<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;"><strong>Description</strong></td>
							<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;"><strong>Debit</strong></td>
							<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;"><strong>Credit</strong></td>
							<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;"><strong>Running Balance</strong></td>
							
  					</tr>
					';
					
					
					
					$debit_total=0;
					$credit_total=0;
					
					
					$today = date('d-m-Y');
					$condprint='';
					//if(isset($_REQUEST['searchcashbook']) && $_REQUEST['searchcashbook']!='')
					if($from_date == $today && $from_date == $today)
					{
					
					if(!empty($from_date)){
					$from_date=date('Y-m-d',strtotime($from_date));
					$condprint.=" and   a.pay_date >= '$from_date'";
					}
					else
					{
					$from_date=date('Y-m-d');
					$condprint.=" and   a.pay_date >= '$from_date'";
					}
					if(!empty($to_date)){
					$to_date=date('Y-m-d',strtotime($to_date));
					$condprint.=" and   a.pay_date <= '$to_date'";
					}
					else
					{
					$to_date=date('Y-m-d');
					$condprint.=" and   a.pay_date <= '$to_date'";
					}
					
					}
					else
					{
					
					if(!empty($from_date)){
					$from_date=date('Y-m-d',strtotime($from_date));
					$condprint.=" and   a.pay_date >= '$from_date'";
					}
					else
					{
					$from_date=date('Y-m-d');
					$condprint.=" and   a.pay_date >= '$from_date'";
					}
					if(!empty($to_date)){
					$to_date=date('Y-m-d',strtotime($to_date));
					$condprint.=" and   a.pay_date <= '$to_date'";
					}
					else
					{
					$to_date=date('Y-m-d');
					$condprint.=" and   a.pay_date <= '$to_date'";
					}
					}
					
				
				$sq_arr_print="select * from (SELECT 0 as acctype,a.pay_date as pay_date,a.amount as amount,a.reciept_id as reciept_id,a.purpose as purpose,b.ledgername as ledgername, 1 as type 
    FROM cf_reciept a
        LEFT JOIN cf_ledger b
            ON b.ledgerid = a.from where cashed=1 ".$condprint."
UNION SELECT 0 as acctype,a.pay_date as pay_date,a.amount as amount,a.voucher_id as reciept_id,a.purpose as purpose,b.ledgername as ledgername, 2 as type 
    FROM cf_voucher a
        LEFT JOIN cf_ledger b
            ON b.ledgerid = a.to where cashed=1 ".$condprint."
			UNION SELECT a.account_type as acctype,a.pay_date as pay_date,a.amount as amount,a.ref_no as reciept_id,a.contra_reference as purpose,b.ledgername as ledgername, 3 as type FROM cf_contra a LEFT JOIN cf_ledger b ON b.ledgerid = a.crd_dbt where a.type_book=1  ".$condprint." ) k order by k.pay_date,type ASC";

				
			
				$resultclientsql=mysql_query($sq_arr_print);	
					
				
				while($row = mysql_fetch_array($resultclientsql))
				{												  
				 
					
					
				
	
					
					 
	 		if($row['type'] !=3 )
			{
				$outstr.= '<tr>
				<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000; border-top:1px solid #000000;">'.$i.'</td>
				<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000; border-top:1px solid #000000;">'.date('d-m-Y',strtotime($row['pay_date'])).'</td>
				<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000; border-top:1px solid #000000;">'.$row['reciept_id'].'</td>
				<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000;border-top:1px solid #000000;">'.$row['ledgername'].'</td>
				<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000;border-top:1px solid #000000;">'.$row['purpose'].'</td>';
				
				 if($row['type'] ==1 )
					{
					$cumulative=$cumulative+$row['amount'];
					$debit_total= $debit_total+$row['amount'];
				$outstr.= '<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000;border-top:1px solid #000000;">'.$row['amount'].'</td>
				<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;border-top:1px solid #000000;">0</td>';
				}
				
				if($row['type'] ==2 )
					{
					$cumulative=$cumulative-$row['amount'];
					$credit_total = $credit_total+$row['amount']; 
				$outstr.= '<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;border-top:1px solid #000000;">0</td>	
				<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;border-top:1px solid #000000;">'.$row['amount'].'</td>';	
				}
				
				$outstr.= '<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;border-top:1px solid #000000;">'.$cumulative.'</td>';
				$outstr.= '</tr>';
				}else
				
				{
					$outstr.= '<tr>
				<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000; border-top:1px solid #000000;">'.$i.'</td>
				<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000; border-top:1px solid #000000;">'.date('d-m-Y',strtotime($row['pay_date'])).'</td>
				<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000; border-top:1px solid #000000;">'.$row['reciept_id'].'</td>';
				
				$sqledger = mysql_query("select cf_ledger.ledgername as ledger_name from cf_contra join cf_ledger on cf_contra.crd_dbt = cf_ledger.ledgerid where contra_reference = '".$row['purpose']."' and type_book = '2'");
		$rw = mysql_fetch_array($sqledger);					
				
				$outstr.= '<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000;border-top:1px solid #000000;">'.$rw['ledger_name'].'</td>
				<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000;border-top:1px solid #000000;">Contra</td>';
				
				if($row['type'] ==3 )
					{
					if($row['acctype'] ==1){
					$cumulative=$cumulative-$row['amount'];
					$credit_total = $credit_total+$row['amount'];
				$outstr.= '<td style="font-size:14px; min-width:100px; text-align:left; padding:5px; min-width:200px;border-right:1px solid #000000;border-top:1px solid #000000;">0</td>
				<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;border-top:1px solid #000000;">'.$row['amount'].'</td>'; 
				}else{
				$cumulative=$cumulative+$row['amount'];
				$debit_total= $debit_total+$row['amount'];
				$outstr.= '<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;border-top:1px solid #000000;">'.$row['amount'].'</td>
				<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;border-top:1px solid #000000;">0</td>';	
				}}
				$outstr.= '<td style="font-size:14px; min-width:100px; text-align:left;padding:5px; min-width:200px;border-right:1px solid #000000;border-top:1px solid #000000;">'.$cumulative.'</td>
				</tr>';
				}
				 
				 $i++;}
				 
				 $outstr.= '<tr class="heading">
  <td colspan="8" align="right"><strong> Closing Balance : '.$cumulative.'</strong></td>
  	</tr>';
			
				$outstr.= ' </table>
		</div>'; 
				echo $outstr;
				exit;
				
			}
			
			
				
			if(isset($_POST['download']))
			{
				$selectype = "Cash-Book";
				$from_date = $_POST['from_date'];
				$to_date = $_POST['to_date'];
								
				print1($selectype,$from_date,$to_date);
			}

?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>OIL FILL</title>
<link href="css/styles.css" rel="stylesheet"  />

<link href="css/menu.css" rel="stylesheet" type="text/css" />
<style>
.newcategoryProfile{ background:#FFFFFF; position:absolute; width:400px; height:250px; z-index:5;
					-webkit-box-shadow: 0px 0px 35px 1px rgba(0,0,0,0.75);
					-moz-box-shadow: 0px 0px 35px 1px rgba(0,0,0,0.75);
					box-shadow: 0px 0px 35px 1px rgba(0,0,0,0.75);}


</style>
<link type="text/css" href="css_date/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
<script type='text/javascript' src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="js/trippopup.js" type="text/javascript"></script>
<!----------------------------css for auto complete-------------------------------->
<link rel="stylesheet" href="css/chosen.css">

<!--end-->

<script type='text/javascript' src="js/jquery.autocomplete.js"></script>
<link rel="stylesheet" type="text/css" href="js/jquery.autocomplete.css" />

<script type="text/javascript" src="scripts_date/jquery-calendar-1.7.2.js"></script>
<script language="javascript">

$(document).ready(function(){
$('#pay_date').datepicker({
showOn: 'both',
buttonImage: 'icons_date/calendar.gif',
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
dateFormat: 'dd-mm-yy'
});
});

$(document).ready(function(){
$('#add_date').datepicker({
showOn: 'both',
buttonImage: 'icons_date/calendar.gif',
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
dateFormat: 'dd-mm-yy'
});
});

$(document).ready(function(){
$('#cheque_date').datepicker({
showOn: 'both',
buttonImage: 'icons_date/calendar.gif',
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
dateFormat: 'dd-mm-yy'
});
});

$(document).ready(function(){
$('#from_date').datepicker({
showOn: 'both',
buttonImage: 'icons_date/calendar.gif',
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
dateFormat: 'dd-mm-yy'
});
});

$(document).ready(function(){
$('#to_date').datepicker({
showOn: 'both',
buttonImage: 'icons_date/calendar.gif',
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
dateFormat: 'dd-mm-yy'
});
});


</script>
</head>

<body>
<!------------------popup area (view, edit)--------------->



<!---------------popup area ends------------------>
<div class="viewEmployeePopup login-popup"  id="login-box" style="height: 220px;width:500px;
overflow-x: scroll;">
    <a href="#" class="close">
    <img src="" class="btn_close" title="Close Window" alt="Close" />
    </a>
    <div id="view_emp"></div>
</div>
<div class="wrapper">
  <div>
<?php 
	include("header.php");
?>
 
</div>
 
 <div class="tabbedArea">
   
  <div class="tabBody">
   <h1>Cash Book</h1>
   <form name="cashbook_frm" id="cashbook_frm" method="post"  >
   <div class="searchBox" style="width:93%; margin-right:0px;  padding:0px; ">
   <div align="left">
   <div style="padding:20px;">
    
    
    <div class="searchField">
     <p class="input1">From Date</p>
    		<input type="text" name="from_date" id="from_date"  class="slabinput input1" style="width:70px;" value="<?=$from_date?>" required="required"/></td>

    </div>
   
    <div class="searchField">
     <p class="input1">To Date</p>
    		<input type="text" name="to_date" id="to_date"  class="slabinput input1" style="width:70px;" value="<?=$to_date?>" required="required"/></td>

    </div>

    <div class="searchField">
    <input name="searchcashbook" type="submit" class="input1" value="search" />
    </div>
	
	<div class="searchField" style="padding-left:60px;margin-right: 20px;">
    <input name="download" type="submit" class="input1"  value="download" />
    </div>
  </div>
   </div>
   </div>
   </form>
      <div id="popup" align="center"  style="margin-left: 400px;"></div>
   <div id="list">
   
   
   <table  border="0" cellspacing="0" cellpadding="0" class="tableList">
   
     <?php
$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";




$cond='';
 $query=$sq_arr;
if(isset($_REQUEST['searchcashbook']) && $_REQUEST['searchcashbook']!='')
{
if(isset($_REQUEST['ledger_name']) && $_REQUEST['ledger_name']!=''){
$ledger_name=$_REQUEST['ledger_name'];
$cond.=" and   b.ledgername like '%".$ledger_name."%'";
}
if(isset($_REQUEST['from_date']) && $_REQUEST['from_date']!=''){
$from_date=date('Y-m-d',strtotime($_REQUEST['from_date']));
$cond.=" and   a.pay_date >= '$from_date'";
}
else
{
$from_date=date('Y-m-d');
$cond.=" and   a.pay_date >= '$from_date'";
}
if(isset($_REQUEST['to_date']) && $_REQUEST['to_date']!=''){
$to_date=date('Y-m-d',strtotime($_REQUEST['to_date']));
$cond.=" and   a.pay_date <= '$to_date'";
}
else
{
$to_date=date('Y-m-d');
$cond.=" and   a.pay_date <= '$to_date'";
}

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

$open_date='';$presentdate=date('Y-m-d',strtotime($from_date));

//echo "first";
 $sql="select * from tbl_openingbal where open_date<='".$presentdate."' and cashorbank = '1'";

$row=mysql_query($sql);
if($row && mysql_num_rows($row)>0){

while($rows=mysql_fetch_array($row))
{
$open_date=$rows['open_date'];
$open_amt=$rows['open_amt'];
}

}

if($open_date!='')
{

 $balancesum="select sum(amount) as totalreciept from cf_reciept where cashed=1 and pay_date<'".$presentdate."' and pay_date >='".$open_date."' ";
}
else
{



 $balancesum="select sum(amount) as totalreciept from cf_reciept where cashed=1 and pay_date<'".$presentdate."' ";
}
 $balancesum;
$balancesum_row=mysql_query($balancesum);
if($balancesum_row && mysql_num_rows($balancesum_row)>0){

while($balancesum_rows=mysql_fetch_array($balancesum_row))
{

$totalreciept=$balancesum_rows['totalreciept'];

}

}

if($open_date!='')
{

 $balancesumvoucher="select sum(amount) as totalpayment from cf_voucher where cashed=1 and pay_date<'".$presentdate."' and pay_date >='".$open_date."' ";
}
else
{


 $balancesumvoucher="select sum(amount) as totalpayment from cf_voucher where cashed=1 and pay_date<'".$presentdate."' ";
}

$balancesumvoucher_row=mysql_query($balancesumvoucher);
if($balancesumvoucher_row && mysql_num_rows($balancesumvoucher_row)>0){

while($balancesumvoucher_rows=mysql_fetch_array($balancesumvoucher_row))
{


 $totalpayment=$balancesumvoucher_rows['totalpayment'];

}

}

//---------------------------total contra calculation ---------------------------------------------//
if($open_date!='')
{

$getotal_contra = 0;
$balancesumcontra=mysql_query("select * from cf_contra where type_book=1 and pay_date<'".$presentdate."' and pay_date >='".$open_date."' ");
while($rescontra=mysql_fetch_array($balancesumcontra))
{  
	if($rescontra['account_type'] == '1')
	{
		$getotal_contra = $getotal_contra - $rescontra['amount'];
		//echo "<br";
	}
	else if($rescontra['account_type'] == '2')
	{
		$getotal_contra = $getotal_contra + $rescontra['amount'];
	}	
}
}
else
{
$balancesumcontra="select sum(amount) as totalcontra from cf_contra where type_book=1 and pay_date<'".$presentdate."' ";
}
//---------------------------End-------------------------------------------------------------------//


/*echo "opening Balance : ".$open_amt;
echo "<br/>";
echo "Receipt Total : ".$totalreciept;
echo "<br/>";
echo "Voucher Total :".$totalpayment;
echo "<br/>";
echo "Contra Total : ".$getotal_contra;*/

$cumulative=$opening_balance=(($open_amt)+($totalreciept-$totalpayment+$getotal_contra));
?>
   <tr class="heading">
  <td colspan="8" align="right"> Opening Balance : <?=$opening_balance?></td>
  	</tr>

  <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

 $sq_arr="select * from (SELECT 0 as acctype,a.pay_date as pay_date,a.amount as amount,a.reciept_id as reciept_id,a.purpose as purpose,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 0 as acctype,a.pay_date as pay_date,a.amount as amount,a.voucher_id as reciept_id,a.purpose as purpose,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."
			UNION SELECT a.account_type as acctype,a.pay_date as pay_date,a.amount as amount,a.ref_no as reciept_id,a.contra_reference as purpose,b.ledgername as ledgername, 3 as type FROM cf_contra a LEFT JOIN cf_ledger b ON b.ledgerid = a.credit where a.type_book=1  ".$cond." ) k order by k.pay_date,type ASC
			";
			//echo $sq_arr;a.debit ='0'
$function="Searchdate";
include("pagingserch.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++;

 if($res['type'] !=3 )
	{
  ?> 
  <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 
   } else {
   		?>
		<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>
	<?php 
		$sqledger = mysql_query("select cf_ledger.ledgername as ledger_name from cf_contra join cf_ledger on cf_contra.crd_dbt = cf_ledger.ledgerid where contra_reference = '".$res['purpose']."' and type_book = '2'");
		$rw = mysql_fetch_array($sqledger);
	?>
	
	<td><?php echo $rw['ledger_name'];?></td>
	<td><?php echo "Contra";?></td>
	<?php if($res['type'] ==3 )
	{
	//$cumulative=$cumulative+$res['amount'];
	?>
	<?php 
	if($res['acctype'] ==1){
	$cumulative=$cumulative-$res['amount'];
	?>
		<td>0</td>
		<td><?php echo $res['amount'];?></td>
	<?php } else {
	$cumulative=$cumulative+$res['amount'];
	?>
	 	<td><?php echo $res['amount'];?></td>
	 	<td>0</td>
	<?php } ?>
		
   <?php }
  ?>
   <td><?php echo $cumulative;?></td>
  <?php 
    }}}
  ?>
    <tr class="heading">
  <td colspan="8" align="right"> Closing Balance : <?=$cumulative?></td>
  	</tr>

</table>

  <div align="right" style="padding-top:10px;"><?=$pagination?></div> 
  </div>
  </div>
 </div>
 <div class="footer"><p>Powered by <a href="http://www.aadhocc.co.uk/" target="_blank">Aadhocc</a></p></div>
 
</div>
<?php /*?><div class="quicLInksCover">
 <?php 
	include("leftlink.php");
?>

</div><?php */?>
</body>
<!----------------------- script for autocomplete------------------------------>
		<script src="js/chosen.jquery.min.js" type="text/javascript">
		</script>
		<script type="text/javascript"> 
			$(".chzn-select").chosen({search_contains: true}); 
		</script>

<!----------------end of script -------------------------------->

</html>
