<?php include('fun-Trial.php');
if($_SESSION['APRfrom']!='' && $_SESSION['APRto']!='')
		{
			 $_REQUEST['from_date']=$_SESSION['APRfrom'];
			 $_REQUEST['to_date']=$_SESSION['APRto'];
		}
?>
<form name="trial" method="post">
<table width="100%" border="1" cellpadding="2" cellspacing="1">
<tr>
	<th colspan="7" style="text-align:center;">SEARCH</th>
</tr>
<tr>
	<th>From :</th>
	<td><input type="text" name="from_date" value="<?php echo $_REQUEST['from_date']; ?>" class="datepicker" /></td>
	<th>To :</th>
	<td><input type="text" name="to_date" value="<?php echo $_REQUEST['to_date']; ?>" class="datepicker" /></td>
	<th>Ledger :</th>
	<td>
	<?php 
		$sql_trial=DB_query("SELECT * FROM accountledger WHERE openingBalance!='' ORDER BY AccountName ASC",$db);
	?>
	<select name="ledger_name" style="width:107px;">
		<option value="0">Select</option>
		<?php while($sql_trialRow=DB_fetch_array($sql_trial)) {?>
		<option value="<?php echo $sql_trialRow['AccountsID']; ?>"<?php echo($sql_trialRow['AccountsID']==$_REQUEST['ledger_name']?'selected':''); ?>><?php echo $sql_trialRow['AccountName']; ?></option>
		<?php }?>
	</select>
	</td>
	<th><input type="submit" name="search" value="Search" /></th>
</tr>
</table>
</form>
<?php 
if($_REQUEST['search'])
{
//echo '<pre>';
//print_r($_REQUEST);

 	$from_date=ConvertSQLDate($_REQUEST['from_date']);
	$to_date=ConvertSQLDate($_REQUEST['to_date']);
	$ledger_name=$_REQUEST['ledger_name'];
	if($_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	{
		$filter[]="B.Dates between '".$from_date."' and '".$to_date."'";
	}
	if($_REQUEST['ledger_name']!='0')
	{
		$filter[]="A.AccountsID='".$ledger_name."'";
	}
	if($filter!='')
	{
		$filter1=implode(" and ",$filter);
		$_SESSION['filter']=$filter1;
		$filter2=$_SESSION['filter'];
		//$GetLedger="select * from accountledger where openingBalance!='' and $filter1 order by AccountName ASC";
echo $GetLedger="SELECT A.* ,B.Dates as SerDate FROM accountledger A, account_book B,account_book_details C 
WHERE C.ledgerid=A.AccountsID  AND B.id=C.account_bookid and $filter1  group by C.ledgerid";	}
}else{ 	
		if($_SESSION['APRfrom']!='' && $_SESSION['APRto']!='')
		{
			  $from_date=$_SESSION['APRfrom'];
			  $to_date=$_SESSION['APRto'];
		}
 echo $GetLedger="select * from accountledger where UnderGroup= '".$_REQUEST['acc_group']."' order by  AccountName ASC  ";
 	}


?>
<p><a href="APRLedgerWise-TrialBalance-print.php?action1=<?php echo $filter2; ?>&from=<?php echo $_REQUEST['from_date'] ?>&to=<?php echo $_REQUEST['to_date'] ?>">Print</a></p>
<table width="100%" border="1" cellpadding="5"  cellspacing="1">
  <tr>
    <th width="18%">Ledger Name </th>
    <th width="18%">Account Type </th>
    <th width="15%">Total Debit </th>
    <th width="13%">Total Credit </th>
    <th width="9%">Closing Balance </th>  
  </tr>  
  <?php 
  	 //$GetLedger=DB_query("select * from accountledger where 1=1  ",$db);
  	// echo "select * from accountledger where openingBalance!=''   order by AccountName ASC";
	 $GetLedger=DB_query($GetLedger,$db);
 	//echo DB_num_rows($GetLedger);
	if(DB_num_rows($GetLedger)>0)
	{
	$add_opening=array();
	$add_TD=array();
	$add_TC=array();
	$add_CL=array();
	 while($RowGetLedger=DB_fetch_array($GetLedger))
	 {
	 	   $TDR= Get_AccountByLedger($RowGetLedger['AccountsID'],'DR',$from_date,$to_date);
	  	   $TCR= Get_AccountByLedger($RowGetLedger['AccountsID'],'CR',$from_date,$to_date);

	
	   if($TDR!='' || $TCR!='')
	   {
 	     $link="index.php?route=Account/AccountingReport&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."&tabid=".$_REQUEST['tabid']."&page=ledger_monthly_transaction&acc_ledger=".$_REQUEST['acc_group']."";
	 ?>
  <tr>
    <td><a href="<?php echo  $link;?>" target="_blank"><?php echo $RowGetLedger['AccountName'].'&nbsp;'  .$RowGetLedger['AccountsID'];?></a></td>
    <td><?php
	 
	echo GetAccountGroupmaster($RowGetLedger['UnderGroup']);?>&nbsp;</td>
    <td><?php
	if($TDR!='')
	$add_TD[]=$TDR;
	echo $TDR ; ?>&nbsp;</td>
    <td><?php 
	if($TCR!='')
	$add_TC[]=$TCR;
	echo $TCR ;?>&nbsp;</td>
    <td><?php
	 if($_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	 {
	 	   $tx= Get_AccountByLedger($RowGetLedger['AccountsID'],'DR',$from_date,$to_date);
	  	   $tx1= Get_AccountByLedger($RowGetLedger['AccountsID'],'CR',$from_date,$to_date);

	 }else{
	  $tx= Get_AccountByLedger($RowGetLedger['AccountsID'],'DR','','');
	 $tx1=Get_AccountByLedger($RowGetLedger['AccountsID'],'CR','','');
	 }
 		if($RowGetLedger['openingtype']=='DR')
		{
		  // echo $OB.'__'.$TDR.'__'.$TCR.'<br>';
			 $t1=(integer)$TDR+(integer)$OB-(integer)$TCR ;
			 if($t1>0)
			 $type="DR";
			 else
			 $type="CR";
			 echo $t1=abs($t1);
			 
 		}
		elseif($RowGetLedger['openingtype']=='CR'){
		  	//	echo $OB.'__'.$TDR.'__'.$TCR.'<br>';
				  $t1=(integer)$TCR+$OB-(integer)$TDR ;
				if($t1>0)
				 $type="CR";
				 else
				 $type="DR";
				 echo  $t1=abs($t1);
 		 } else{
				// echo $OB.'__'.$TDR.'__'.$TCR.'<br>';
				
				if($TCR>$TDR){
				 $type="CR";
				   $t1=(integer)$TCR-(integer)$TDR ;
				 }
				 else{
				 $type="DR";
				   $t1=(integer)$TCR-(integer)$TDR ;
				 }
				 echo  $t1=abs($t1);
		 }
		  
	  
	 
	?></td>
  </tr>
  <?php 
  }
  } ?>
  <tr>
  <td>Total</td>
  <td>&nbsp;</td>
  <td>Total: <?php 
// echo Get_TotalAccountByLedger('DR' ,$from_date,$to_date);
 //echo "<br>";
 echo array_sum($add_TD); ?></td>
  <td>Total: <?php 
  // echo Get_TotalAccountByLedger('CR',$from_date,$to_date);
 //echo "<br>";
    echo array_sum($add_TC); ?></td>
  <td style="white-space:nowrap;"> <?php //Total:echo array_sum($add_CL); ?></td>
  </tr>
<?php   } ?>
</table>
