<?php 
ob_start();
$PageSecurity = 1;
include('config.php');
include('includes/ConnectDB.inc');
include('includes/function_lib.php');
include('includes/LanguageSetup.php');
include('includes/DateFunctions.inc');
include('Account/fun-Trial.php');?>
<?php 
?>
<script type="text/javascript" language="javascript">
  function toggle_visibility(id) {
	
	 
       var e = document.getElementById(id);
	       e.style.display = 'none';
		       //alert(e.style.display);
			   window.print();
    }

</script>
<?php $Comp=DB_query("select CompanyName from companies",$db); 
	  $CompRow=DB_fetch_array($Comp);
?> 
<table width="100%" border="0" >
<tr>
<td colspan="2" style="text-align:center;"><center><strong><?php echo $CompRow['CompanyName']; ?> <br  />Trial Balance-Ledger Wise</strong></center></td>
</tr>
<tr>

<td><?php if(!empty($_REQUEST['from'])) { ?>FROM DATE: <?php echo $_REQUEST['from'];  }?></td>
<td style="text-align:right;"><?php if(!empty($_REQUEST['to'])) { ?>TO DATE: <?php echo $_REQUEST['to'];  }?></td>
</tr>
</table>
<table width="100%" border="1" cellpadding="1"  cellspacing="1" style="border-collapse: collapse; border: #000000 solid 1px">
  <tr>
    <th width="18%">Ledger Name </th>
    <th width="18%">Account Type </th>
    <th width="18%">Opening Balance </th>
    <th width="15%">Total Debit </th>
    <th width="13%">Total Credit </th>
    <th width="9%">Closing Balance </th>  
    <th width="9%">DR/CR</th>
  </tr>  
  <?php
   

  	 //$GetLedger=DB_query("select * from accountledger where 1=1  ",$db);
  	// echo "select * from accountledger where openingBalance!=''   order by AccountName ASC";
	if(!empty($_REQUEST['action1']))
	{
	 $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 ".$_REQUEST['action1']."";
	}
else{
	$GetLedger="select * from accountledger where openingBalance!='' order by AccountName ASC";
	}
	 $GetLedger1=DB_query($GetLedger,$db);
	 
 	//echo DB_num_rows($GetLedger);
	if(DB_num_rows($GetLedger1)>0)
	{
	$add_opening=array();
	$add_TD=array();
	$add_TC=array();
	$add_CL=array();
	 while($RowGetLedger=DB_fetch_array($GetLedger1))
	 {?>
  <tr>
    <td><?php echo $RowGetLedger['AccountName'];?></td>
    <td><?php
	 
	echo GetAccountGroupmaster($RowGetLedger['UnderGroup']);?>&nbsp;</td>
    <td><?php  
	$OB= $RowGetLedger['openingBalance'];
	if($OB=='' || $OB=='0.00')
	{
	$add_opening[]=$OB;
	echo '0.00';
	}
	else
	{
	$add_opening[]=$RowGetLedger['openingBalance'];
		echo $OB= $RowGetLedger['openingBalance'];
		echo '('.$RowGetLedger['openingtype'].')';
	}
	   ?>&nbsp;</td>
    <td><?php  $TDR= Get_AccountByLedger($RowGetLedger['AccountsID'],'DR');
	$add_TD[]=$TDR;
	echo $TDR= Get_AccountByLedger($RowGetLedger['AccountsID'],'DR'); ?>&nbsp;</td>
    <td><?php 
	$TCR= Get_AccountByLedger($RowGetLedger['AccountsID'],'CR');
	$add_TC[]=$TCR;
	echo $TCR= Get_AccountByLedger($RowGetLedger['AccountsID'],'CR');?>&nbsp;</td>
    <td><?php
	  $tx= Get_AccountByLedger($RowGetLedger['AccountsID'],'DR');
	 $tx1=Get_AccountByLedger($RowGetLedger['AccountsID'],'CR');
 		if($RowGetLedger['openingtype']=='DR')
		{
			$t1=abs((integer)$tx+(integer)$OB-(integer)$tx1);
			$add_CL[]=$t1;
			echo  $t1=abs((integer)$tx+(integer)$OB-(integer)$tx1);
 		}
		else{
				$t2=abs((integer)$tx1+$OB-(integer)$tx);
				$add_CL[]=$t2;
			  echo $t2=abs((integer)$tx1+$OB-(integer)$tx);
 		 }

		  
	  
	 
	?></td>
	<td><?php  echo ($t1>$t2?'DR':'CR');?></td>
  </tr>
  <?php } ?>
  <tr>
  <td>Total</td>
  <td>&nbsp;</td>
  <td>Total: <?php echo array_sum($add_opening); ?></td>
  <td>Total: <?php echo array_sum($add_TD); ?></td>
  <td>Total: <?php echo array_sum($add_TC); ?></td>
  <td style="white-space:nowrap;">Total: <?php echo array_sum($add_CL); ?></td>
  <td>&nbsp;</td>

  </tr>
<?php   } ?>
</table>
<div id="foo"><p align="center">
<a href="#" onclick="javascript:toggle_visibility('foo');" >Print</a>  
</div><!-- Close 3 Para -->
