<style type="text/css">
	#tabs div{min-height: auto; padding: 0;}
</style>

<?php include('fun-Trial.php');$url="modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."&tabid=".$_REQUEST['tabid']."&page=".$_REQUEST['page'];
if($_REQUEST['Reset']!=''){unset($_SESSION['FDATE']);unset($_SESSION['TDATE']);}
if($_REQUEST['Show']!='')
{
	$from_date=FormatDateForSQL($_REQUEST['from_date']);
	$to_date=FormatDateForSQL($_REQUEST['to_date']);	
	$_SESSION['FDATE']=$from_date;
	$_SESSION['TDATE']=$to_date;
}
if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!=''){$period=ConvertSQLDate($_SESSION['FDATE']).' to '.ConvertSQLDate($_SESSION['TDATE']);}else{$period=ConvertSQLDate($_SESSION['financialYrStart']).' to '.ConvertSQLDate($_SESSION['financialYrEnd']);}
if($_REQUEST['gid']=='' && $_REQUEST['lid']=='')
{?>
<form action="" method="post">
<table width="100%" border="0" celpadding="3" cellspacing="0">
<tr class="row1">
	<td width="14%"></td>
	<td width="13%" style="text-align:right;">From Date</td>
	<td width="20%"><input type="text" name="from_date" value="<?php if(isset($_SESSION['FDATE']))echo ConvertSQLDate($_SESSION['FDATE']);?>" class="datepicker"  /></td>
	<td width="13%" style="text-align:right;">To Date</td>
	<td width="20%"><input type="text" name="to_date" value="<?php if(isset($_SESSION['TDATE']))echo ConvertSQLDate($_SESSION['TDATE']);?>" class="datepicker"  /></td>
	<td></td>
</tr>
<tr class="row1">
	<td colspan="6" align="center" valign="middle"><input type="submit" name="Show" value="Show" class="search" /> <input type="submit" name="Reset" value="Reset" class="search" /></td>
</tr>
</table>
</form>
  <p align="center">
  <span><strong><?php echo $period;?></strong></span>
  <span style="float:right;"><a href="APRGroupWise-PandL_Print.php" target="_blank" class="search">Print</a> <a href="APRGroupWise-PandL_Exl.php" class="search">Export</a></span>
  </p>

<!-- <h2><?php //echo $period;?></h2> -->
<table width="100%" cellspacing="0" cellpadding="0" border="0" class="thborder">
  <tr>
    <th>EXPENSE</th>
    <th width="50%">INCOME</th>
  </tr>
  <tr>
    <td valign="top" width="50%">
    <div id="amc-tab"> 
	<table width="100%" border="0" cellspacing="0" cellpadding="3"><?php 
 	  $table='accountgroupmaster';
	  $getExpenseId=$s->getField($table,'GroupName','EXPENSE','GroupId');
	  //$getUnderGroups=DB_query("select * from $table where NatureOfGroup=$getExpenseId and MainGroupId=0",$db);
	$getUnderGroups=DB_query("select * from $table where NatureOfGroup=$getExpenseId and MainGroupId=0 AND GroupName like'%opening%' union select * from $table where NatureOfGroup=$getExpenseId and MainGroupId=0 AND GroupName NOT like'%opening%'",$db);			

		while($underGroups=DB_fetch_array($getUnderGroups))
		{
		$sql_check=DB_query("select GroupId from accountgroupmaster where GroupId='".$underGroups['GroupId']."'
        and (GroupName like'%opening%' or GroupName like'%closing%')",$db);
       $row_check=DB_fetch_array($sql_check);
       if($row_check['GroupId']!='')
       {
       $id1=1; 
       }
       else
        $id1=0;
			if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
			{$x=getGroupBalanceSheet($underGroups['GroupId'],$_SESSION['FDATE'],$_SESSION['TDATE']);}
			else{$x=getGroupBalanceSheet($underGroups['GroupId'],'','');}			
			//echo "--".$underGroups['GroupId'];
						
			if($x[0]!='' || $x[1]!='' || $x[2]!='' || $x[3]!='')
			{
 	    	?>
		<tr onMouseOver="this.style.backgroundColor='#fffeb3'; this.style.color='#00f';" onMouseOut="this.style.backgroundColor='#FFF'; this.style.color='#000000';">
		 <td width="50%">
              <?php if($id1==0) { ?>
            <a href="index.php?route=<?php echo $_REQUEST['route'];?>&<?php echo $url;?>&gid=<?php echo $underGroups['GroupId'];?>" title="<?php echo ucwords($underGroups['GroupName']);?>"><strong><?php echo ucwords($underGroups['GroupName']);?></strong></a>
            <?php } else { ?>
            <strong><?php echo ucwords($underGroups['GroupName']);?></strong>
            <?php } ?>
			  </td>
			  <td style="text-align:right;"><strong>
			    <?php 
	 unset($_SESSION['down_acc_group']);
$acc_down_grp=getallundergroups($underGroups['GroupId']);

if(empty($acc_down_grp))
	$acc_down_grp[]=$underGroups['GroupId'];
else
	array_push($acc_down_grp, $underGroups['GroupId']);
$opening_expense=getgroupopening($acc_down_grp);
//echo "<pre>";
//print_r($opening_expense);

if($opening_expense[1]=='DR')
	{ $x[2]=$opening_expense[0];$x[3]='0'; }
else if($opening_expense[1]=='CR')
	{ $x[3]=$opening_expense[0]; $x[2]='0'; }
else
{
	$x[2]=0;$x[3]=0;
}
//echo "<pre>";
//			print_r($x);
$tot_deb_expense=$x[0]+$x[2];
$tot_cre_expense=$x[1]+$x[3];
if($tot_deb_expense>$tot_cre_expense)
{
	echo $LIABDR=$tot_deb_expense-$tot_cre_expense;
	echo " DR"; 
	$LIABDR_ARR[]=$LIABDR;
}
else if($tot_deb_expense<$tot_cre_expense)
{		
	echo $LIABCR=$tot_cre_expense-$tot_deb_expense; 
	echo " CR"; 
	$LIABCR_ARR[]=$LIABCR;
}
else if($tot_deb_expense==$tot_cre_expense)
{
	echo "";
}
			   /* if($x[0]>$x[1])
			   {
			   	echo $LIABDR=$x[0]-$x[1]; 
			   	echo " DR"; 
			   	$LIABDR_ARR[]=$LIABDR;
			   }
			elseif($x[1]>$x[0])
				{
					echo $LIABCR=$x[1]-$x[0]; 
					echo " CR"; 
					$LIABCR_ARR[]=$LIABCR;
				}*/
					?></strong></td>
			  </tr>	<?php } } ?>	  
		  
  </table></div></td>
  <td valign="top">
  <div id="amc-tab"> 
  <table width="100%" border="0" cellspacing="0" cellpadding="3"><?php 
	  $getIncomeId=$s->getField($table,'GroupName','INCOME','GroupId');		  
	  $getUnderGroups=DB_query("select * from $table where NatureOfGroup=$getIncomeId and MainGroupId=0 or GroupName like'closing%'",$db);
		while($underGroups=DB_fetch_array($getUnderGroups))
		{     
        $sql_check=DB_query("select GroupId from accountgroupmaster where GroupId='".$underGroups['GroupId']."'
        and (GroupName like'%closing%' or GroupName like'%Closing%')",$db);
        $row_check=DB_fetch_array($sql_check);
       if($row_check['GroupId']!='')
       {  $id1=1;  }  else $id1=0;

			if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
			{
				$y=getGroupBalanceSheet($underGroups['GroupId'],$_SESSION['FDATE'],$_SESSION['TDATE']);}
			else{
			$y=getGroupBalanceSheet($underGroups['GroupId']);}
			//echo "--".$underGroups['GroupId'];
			//echo "<pre>";
			//print_r($y);			
			if($y[0]!='' || $y[1]!='' || $y[2]!='' || $y[3]!=''){			
		 ?> 		  	 
			  <tr onMouseOver="this.style.backgroundColor='#fffeb3'; this.style.color='#00f';" onMouseOut="this.style.backgroundColor='#FFF'; this.style.color='#000000';">
			  <td width="50%">
             <?php if($id1==0) { ?>
            <a href="index.php?route=<?php echo $_REQUEST['route'];?>&<?php echo $url;?>&gid=<?php echo $underGroups['GroupId'];?>" title="<?php echo ucwords($underGroups['GroupName']);?>"><strong><?php echo ucwords($underGroups['GroupName']);?></strong></a>
            <?php } else { ?>
            <strong><?php echo ucwords($underGroups['GroupName']) ;?></strong>            
            <?php } ?></td>
			  <td style="text-align:right;"> 
			 <strong><?php 
			  unset($_SESSION['down_acc_group']);
$acc_down_grp=getallundergroups($underGroups['GroupId']);
if(empty($acc_down_grp))
	$acc_down_grp[]=$underGroups['GroupId'];
else
	array_push($acc_down_grp, $underGroups['GroupId']);
$opening_income=getgroupopening($acc_down_grp);

if($opening_income[1]=='DR')
	{ $y[2]=$opening_income[0];$y[3]='0'; }
else if($opening_income[1]=='CR')
	{ $y[3]=$opening_income[0]; $y[2]='0'; }
else
{
	$y[2]=0;$y[3]=0;
}
//echo "<pre>";
//			print_r($y);
$tot_deb_income=$y[0]+$y[2];
$tot_cre_income=$y[1]+$y[3];
if($tot_deb_income>$tot_cre_income)
{
	echo $ASSDR=$tot_deb_income-$tot_cre_income;
	echo " DR"; 
	$ASSDR_ARR[]=$ASSDR;
}
else if($tot_deb_income<$tot_cre_income)
{		
	echo $ASSCR=$tot_cre_income-$tot_deb_income; 
	echo " CR"; 
	$ASSCR_ARR[]=$ASSCR;
}
else if($tot_deb_income==$tot_cre_income)
{
	echo "";
}
			 /*if($y[0]>$y[1]){
			 	echo $ASSDR=$y[0]-$y[1]; 
			 	echo " DR"; 
			 	$ASSDR_ARR[]=$ASSDR;}
					elseif($y[1]>$y[0]){
						echo $ASSCR=$y[1]-$y[0];
						 echo " CR";
						  $ASSCR_ARR[]=$ASSCR;
						}*/
					?></strong></td>
			  </tr>
	<?php 
		}} ?>			
  </table>
  </div>
  </td>
  </tr>
  <?php   
  //echo $expense_type."---".$income_type."<br />";
  
  if($expense_type==$income_type)
  {
     if($expense_type=='CR')
	 {
	 	$proffit=$expense+$income;
		$proffit_type=$expense_type;
	 }
	 elseif($expense_type=='DR')
	 {
	 	$loss=$expense+$income;
		$loss_type=$expense_type;
	 }
  }
  elseif($expense_type!=$income_type)
  {
  		if($expense>$income)
		{
			$value=$expense-$income;
			$value_type=$expense_type;
		}
		elseif($income>$expense)
		{
			$value=$income-$expense;
			$value_type=$income_type;
		}
		
		if($value_type=='CR')
		{$proffit=$value;$proffit_type=$value_type;}
		elseif($value_type=='DR')
		{$loss=$value;$loss_type=$value_type;}
  }
  
  /*if($expense>$income)
  {
  	$loss=$expense-$income;
	$loss_type=$expense_type;
  }
  elseif($income>$expense)
  {
  	$proffit=$income-$expense;
	$proffit_type=$income_type;
  }
*/  else{$loss=''; $loss_type=''; $proffit=''; $proffit_type='';}
 // echo "<pre>"; print_r($ASSCR_ARR);
  //print_r($LIABDR_ARR);
  ?>
  <tr>
  	<td colspan="2" valign="top">
  	<div id="amc-tab"> 
  		<table width="100%" border="0" cellspacing="0" cellpadding="3">	
  			<tr onMouseOver="this.style.backgroundColor='#fffeb3'; this.style.color='#00f';" onMouseOut="this.style.backgroundColor='#FFF'; this.style.color='#000000';"><?php 
						if($LIABDR_ARR!=''){ $sum1=array_sum($LIABDR_ARR);}
   						 if($LIABCR_ARR!=''){ $sum2=array_sum($LIABCR_ARR);} 
   						 if($sum1>$sum2){ $expense=($sum1-$sum2); $expense_type='DR'; }
   						 else{ $expense=($sum2-$sum1); $expense_type='CR';} 

   						 if($ASSDR_ARR!=''){$sumA1=array_sum($ASSDR_ARR);}
   						 if($ASSCR_ARR!=''){$sumA2=array_sum($ASSCR_ARR);}
  						 if($sumA1>$sumA2){ $income=($sumA1-$sumA2); $income_type='DR'; }
  						 else{ $income=($sumA2-$sumA1); $income_type='CR';}	
  						 if($expense<$income){
  			?>
  				<td width="25%"><strong>Excess of Income Over Expenditure</strong></td>
  				<td width="25%" style="text-align:right;"><strong><?php 
  						echo $pro_val=$income-$expense."&nbsp$expense_type";
  					 ?></strong>
   				</td>
   			    <td width="25%"></td>
  				<td></td>
   			<?php } else if($expense>$income) {		 ?>
   			    <td width="25%"></td>
  				<td width="25%"></td>
  				<td width="25%"><strong>Excess of Expenditure Over Income</strong></td>
  				<td style="text-align:right;"><strong><?php echo $loss_val=$expense-$income."&nbsp$income_type"; ?></strong></td>
  				<?php } ?>
  			</tr>
  			<tr bgcolor="#ffffff" onMouseOver="this.style.backgroundColor='#fffeb3'; this.style.color='#00f';" onMouseOut="this.style.backgroundColor='#e6e6e6'; this.style.color='#000000';">
  				<td><strong>Total</strong></td>
  				<td style="text-align:right;"><strong><?php echo $expense+$pro_val. "&nbsp$expense_type"; ?></strong></td>
  				<td><strong>Total</strong></td>
  				<td style="text-align:right;"><strong><?php echo $income+$loss_val. "&nbsp$income_type"; ?></strong></td>
  			</tr>
  		</table>
  	</div>
  	</td>
  </tr> 
  <!--<tr>
  <td valign="top" colspan="2">
  <table width="100%" border="0" cellspacing="3" cellpadding="3" style="border:#999999 solid 1px">
	<tr>
		<td width="25%" style="border-bottom: #666666 solid 2px;border-left: #FFFFFF solid 2px;border-top: #FFFFFF solid 2px;border-right: #666666 solid 2px; width:25%;"><strong>Loss</strong></td>
		<td width="25%" style="border-bottom: #666666 solid 2px;border-left: #FFFFFF solid 2px;border-top: #FFFFFF solid 2px;border-right: #666666 solid 2px; width:25%; text-align:right"><strong><?php echo $loss."&nbsp;".$loss_type;?></strong></td>
		<td width="25%" style="border-bottom: #666666 solid 2px;border-left: #FFFFFF solid 2px;border-top: #FFFFFF solid 2px;border-right: #666666 solid 2px; width:25%;"><strong>Proffit</strong></td>
		<td width="25%" style="border-bottom: #666666 solid 2px;border-left: #FFFFFF solid 2px;border-top: #FFFFFF solid 2px;border-right: #666666 solid 2px; width:25%; text-align:right"><strong><?php echo $proffit."&nbsp;".$proffit_type;?></strong></td>		
	</tr>		
  </table>		
  </td>
  </tr> -->
  
</table>
<?php }
if($_REQUEST['gid']!='')
{
	$gid=$_REQUEST['gid'];
	$_SESSION['gid']=$gid;
?>
<br>
<h3><?php echo $s->getField('accountgroupmaster','GroupId',$gid,'GroupName')?></h3>
<!-- <h2><?php //echo $period;?></h2> -->
<!--<p align="right"><a href="index.php?route=<?php //echo $_REQUEST['route'];?>&<?php //echo $url;?>"><strong><font color="#000000">Back</font></strong></a></p>
-->
<!--<p align="right"><a href="javascript:history.go(-1);"><strong><font color="#000000">Back</font></strong></a></p>-->
 <p align="center">
 <span><strong><?php echo $period;?></strong></span>
  <span style="float:right;"><a href="APRGroupWise-PandL_Libilities_Print.php?gid=<?php echo $_REQUEST['gid'] ?>" target="_blank" class="search">Print</a> <a href="APRGroupWise-PandL_Libilities_Exl.php?gid=<?php echo $_REQUEST['gid'] ?>" class="search">Export</a></span>
  </p>
<table width="100%" border="0" cellpadding="3" cellspacing="0" class="thborder">
<tr>
    <th>Particulars</th>
	<th width="10%" style="text-align:center;">Opening</th>
    <th width="10%" style="text-align:center;">Debit</th>
	<th width="10%" style="text-align:center;">Credit</th>
	<th width="10%" style="text-align:center;">Closing</th>
  </tr>
  <?php 
 // echo "Select * from accountgroupmaster where MainGroupId=$gid";
  $getSubGroups=DB_query("Select * from accountgroupmaster where MainGroupId=$gid",$db);
  $openingCR_array=''; $openingDR_array='';
  while($subGroups=DB_fetch_array($getSubGroups))
  {
  		if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!=''){
  		$res=getGroupBalanceSheet($subGroups['GroupId'],$_SESSION['FDATE'],$_SESSION['TDATE']);}
		else
		{$res=getGroupBalanceSheet($subGroups['GroupId'],'','');}
		//print_r($res);
		$TD=$res[0];
		$TC=$res[1];
  ?>
  <tr onMouseOver="this.style.backgroundColor='#fffeb3'; this.style.color='#00f';" onMouseOut="this.style.backgroundColor='#FFF'; this.style.color='#000000';">
  <td><a href="index.php?route=<?php echo $_REQUEST['route'];?>&<?php echo $url;?>&gid=<?php echo $subGroups['GroupId'];?>" title="<?php echo ucwords($subGroups['GroupName']);?>"><?php echo $subGroups['GroupName'];
  //echo "<br />CR -- " ; echo $tot_CR_open;
  //echo "<br />DR---"; echo $tot_DR_open;
     ?></a></td>
     <?php unset($_SESSION['down_acc_group']);
$acc_down_grp=getallundergroups($subGroups['GroupId']);
if(empty($acc_down_grp))
  $acc_down_grp[]=$subGroups['GroupId'];
else
  array_push($acc_down_grp, $subGroups['GroupId']);
//echo "<pre>";

$opening=getgroupopening($acc_down_grp);
//print_r($opening);
 ?>
	 <td style="text-align:right;"><?php 
	/* $DROPEN=$res[2];
	 $CROPEN=$res[3];
	 if($DROPEN>$CROPEN)
	 {echo $TOTALOPDR=$DROPEN-$CROPEN; echo "&nbsp;DR"; $openingDR_array[]=$TOTALOPDR;}
	 elseif($CROPEN>$DROPEN)
	 {echo $TOTALOPCR=$CROPEN-$DROPEN; echo "&nbsp;CR"; $openingCR_array[]=$TOTALOPCR;}	 */
	 if($opening[1]=='DR')
   {
      $TOTALOPDR=$opening[0];
      echo $TOTALOPDR.' '.' DR';
     $openingDR_array[]=$TOTALOPDR; $TOTALOPCR='0';
   }
   elseif($opening[1]=='CR')
   {
      $TOTALOPCR=$opening[0];
      echo $TOTALOPCR.' '.' CR';
      $openingCR_array[]=$TOTALOPCR; $TOTALOPDR='0';
   }
	 ?>
	 </td>
  <td style="text-align:right;"><?php if($TD==0){echo "";}else echo $TD; $TDA[]=$TD;?></td>
  <td style="text-align:right;"><?php if($TC==0){echo "";}else echo $TC; $TCA[]=$TC;?></td>
  <td style="text-align:right;">
  	<?php 
		$tot_deb_sub_group=$TOTALOPDR+$TD;
		$tot_cre_sub_group=$TOTALOPCR+$TC;
		if($tot_cre_sub_group>$tot_deb_sub_group)
			{
				echo $tot_cre_sub_group-$tot_deb_sub_group.' CR';
				$close_cre_sub_grp[]=$tot_cre_sub_group-$tot_deb_sub_group;
			}
		else if($tot_cre_sub_group<$tot_deb_sub_group)
			{
				echo $tot_deb_sub_group-$tot_cre_sub_group.' DR';
				$close_deb_sub_grp[]=$tot_deb_sub_group-$tot_cre_sub_group;
			}
		else if($tot_cre_sub_group==$tot_deb_sub_group)
			echo '';
  	?>
  </td>
  </tr><?php } ?>
 
  <?php //echo $_SESSION['FDATE']; echo "------------";echo $_SESSION['TDATE'];
  //	echo "Select * from accountledger where CompanyId='".$_SESSION['ACGCompanyId']."' and UnderGroup=$gid order by AccountName ASC";

  $getLedgers=DB_query("Select * from accountledger where CompanyId='".$_SESSION['ACGCompanyId']."' and UnderGroup=$gid order by AccountName ASC",$db);
  while($Ledgers=DB_fetch_array($getLedgers))
  {
  	$ledger_id=$Ledgers['AccountsID'];
	
	if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
	{$opening_bal=$s->GetFieldValue('accountledger',"AccountsID=$ledger_id and openingdate<='".$_SESSION['TDATE']."'",'openingBalance');}
	else{$opening_bal=$s->getField('accountledger','AccountsID',$ledger_id,'openingBalance');}
	$opening_type=$s->getField('accountledger','AccountsID',$ledger_id,'openingtype');

  	if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
	{$totVoucherDR=Get_AccountByLedger($Ledgers['AccountsID'],'DR',$_SESSION['FDATE'],$_SESSION['TDATE']);
  	$totVoucherCR=Get_AccountByLedger($Ledgers['AccountsID'],'CR',$_SESSION['FDATE'],$_SESSION['TDATE']);}
	else{
	$totVoucherDR=Get_AccountByLedger($Ledgers['AccountsID'],'DR','','');
  	$totVoucherCR=Get_AccountByLedger($Ledgers['AccountsID'],'CR','','');}	
	//echo $ledgertype; 	
  ?>
  <tr onMouseOver="this.style.backgroundColor='#B7CFD9'; this.style.color='#00f';" onMouseOut="this.style.backgroundColor='#FFF'; this.style.color='#000000';">
  <td><a href="index.php?route=<?php echo $_REQUEST['route'];?>&<?php echo $url;?>&lid=<?php echo $Ledgers['AccountsID'];?>" title="<?php echo ucwords($Ledgers['AccountName']);?>"><strong><?php  echo $Ledgers['AccountName']; //echo "----".$openingCR."-------".$openingDR;//print_r($ledgerPosting);
  ?></strong></a></td>  
  <td style="text-align:right;"><?php /* if($opening_bal!=''){echo $opening_bal."&nbsp;".$opening_type; }
  if($opening_type=='CR'){
  $openingCR_array[]=$opening_bal;}
  else{$openingDR_array[]=$opening_bal;}*/
$led_opening=getledgeropeningyearwise($Ledgers['AccountsID']);

echo $led_opening[0].' '.$led_opening[1];
if($led_opening[1]=='CR'){
  $openingCR_array[]=$led_opening[0]; $OPCR=$led_opening[0]; $OPDR='0';}
  else{$openingDR_array[]=$led_opening[0]; $OPDR=$led_opening[0]; $OPCR='0';}
  ?></td>  
  <td style="text-align:right;">
	<?php
	 $total_DR=$totVoucherDR;
	if($total_DR==0) {echo " ";}else{ echo $total_DR;}
	$ARRDR[]=$total_DR;
	?></td>
  <td style="text-align:right;"><?php 
  $total_CR=$totVoucherCR; if($total_CR==0) echo " "; else echo $total_CR; $ARRCR[]=$total_CR;?></td>
  <td style="text-align:right;">
<?php 
$led_tot_deb=$OPDR+$total_DR;
$led_tot_cre=$OPCR+$total_CR;
if($led_tot_deb>$led_tot_cre)
{	
echo $led_tot_deb-$led_tot_cre.' DR';
$close_deb_sub_grp[]=$led_tot_deb-$led_tot_cre;
}
else if($led_tot_deb<$led_tot_cre)
{	
echo $led_tot_cre-$led_tot_deb.' CR';
$close_cre_sub_grp[]=$led_tot_cre-$led_tot_deb;
}
else if($led_tot_deb==$led_tot_cre)
echo '';
?>
  </td>
  </tr><?php } ?>
 <tr>
    <td style="text-align:right"><strong>Total</strong></td>	
	<td style="text-align:right"><strong><?php 	 
	 if($openingDR_array!='')$opDR=array_sum($openingDR_array);
	 if($openingCR_array!='')$opCR=array_sum($openingCR_array);
	 if($opDR>$opCR){echo $opDR-$opCR; echo "&nbsp;DR";}else{echo $opCR-$opDR; echo "&nbsp;CR";}	 
	 ?></strong></td>	
    <td style="text-align:right"><strong><?php 
	if($TDA!=''){$xx=array_sum($TDA);}else{$xx=0;}
	if($ARRDR!=''){$yy=array_sum($ARRDR);}else{$yy=0;}
	echo $xx+$yy;
	?></strong></td>
	<td style="text-align:right"><strong><?php 
	if($TCA!=''){$mm=array_sum($TCA);}else{$mm=0;}
	if($ARRCR!=''){$nn=array_sum($ARRCR);}else{$nn=0;}
	echo $mm+$nn;?></strong></td>
	<td style="text-align:right"><strong>
		<?php 
		if(count($close_deb_sub_grp)==0)
			$total_debit_closing=0;
		else		
			$total_debit_closing=array_sum($close_deb_sub_grp);

		if(count($close_cre_sub_grp)==0)
			$total_credit_closing=0;
		else		
			$total_credit_closing=array_sum($close_cre_sub_grp);

		if($total_credit_closing>$total_debit_closing)
			echo $total_credit_closing-$total_debit_closing.' CR';
		else if($total_credit_closing<$total_debit_closing)
			echo $total_debit_closing-$total_credit_closing.' DR';
		else if($total_credit_closing==$total_debit_closing)
			echo '0';

		?></strong>
	</td>
  </tr>
</table>	
<?php 	
}
if($_REQUEST['lid']!='')
{
	$month_array=array('04'=>'April','05'=>'May','06'=>'June','07'=>'July','08'=>'August','09'=>'September','10'=>'October','11'=>'November','12'=>'December','01'=>'January','02'=>'February','03'=>'March');
	$lid=$_REQUEST['lid'];
	$_SESSION['lid']=$lid;
	//echo $_SESSION['financialYrStart'];
	//echo $_SESSION['financialYrEnd'];
	
	if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
	{
		$opening_bal=$s->GetFieldValue('accountledger',"AccountsID=$lid and openingdate<='".$_SESSION['TDATE']."'",'openingBalance');
	}
	else{$opening_bal=$s->getField('accountledger','AccountsID',$lid,'openingBalance');}
	
	$opening_type=$s->getField('accountledger','AccountsID',$lid,'openingtype');
	if($opening_type!=''){if($opening_type=='CR'){$openingCR=$opening_bal; $openingDR='';}else{$openingDR=$opening_bal; $openingCR='';}}
	
	/*echo $_SESSION['FDATE']; echo "---";
	echo $_SESSION['TDATE'];*/
	
?>
<br>
<h3><?php echo $s->getField('accountledger','AccountsID',$lid,'AccountName');?></h3>
<!-- <h2><?php //echo $period;?></h2> -->
<!--<p align="right"><a href="javascript:history.go(-1);"><strong><font color="#000000">Back</font></strong></a></p>-->
<p align="center">
<span><strong><?php echo $period;?></strong></span>
  <span style="float:right;"><a href="ProfitLoss-BalanceSheet-Lid-Print.php?lid=<?php echo $_REQUEST['lid'] ?>" target="_blank" class="search">Print</a> <a href="Profit-LossA-c-Lid-Exl.php?lid=<?php echo $_REQUEST['lid'] ?>" class="search">Export</a></span>
  </p>
<table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
<tr>
<th>Months</th>
<th width="12%" style="text-align:center;">Debit</th>
<th width="12%" style="text-align:center;">Credit</th>
<th width="12%" style="text-align:center;">Closing</th>
  </tr>
<?php 
	foreach($month_array as $key=>$val)
	{

		$a=explode('-',$_SESSION['financialYrStart']);
		$b=explode('-',$_SESSION['financialYrEnd']);
		//echo $a[0];
		//echo $b[0];
		if($key>='04' && $key<='12'){$yr=$a[0];}elseif($key>='01' && $key<'03'){$yr=$b[0];}
		$flag='';
		if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
		{
			$FDATEBRK=explode('/',$_SESSION['FDATE']);
			$TDATEBRK=explode('/',$_SESSION['TDATE']);
			$fmnth=$FDATEBRK[1];
			$tmnth=$TDATEBRK[1];
			$flag=0;
			if($fmnth==$tmnth)
			{
			
				if($key==$fmnth){$flag=1;}
			}
			elseif($fmnth!=$tmnth)
			{
			
				if($fmnth==$key){$flag=2;}
				else
				if($tmnth==$key){$flag=3;}
				else 
				$flag=4;
			}
			
		$amounts=getMonthsBalanceSheet($lid,$key,$yr,$flag);		
		}
		else{$amounts=getMonthsBalanceSheet($lid,$key,$yr,'');}		
		$totalDR=$amounts[0];
		$totalCR=$amounts[1];
	?>
		<tr onMouseOver="this.style.backgroundColor='#fffeb3'; this.style.color='#00f';" onMouseOut="this.style.backgroundColor='#FFF'; this.style.color='#000000';">
		<td><a href="#" onclick="popup('bal_sheet_popup.php?q=<?php echo $lid;?>&mnth=<?php echo $key;?>&yr=<?php echo $yr;?>',1000,500)"><?php echo $val;//."---".$yr,"----".$flag;?></a></td>
		<td style="text-align:right;"><?php if($totalDR==0){echo "";}else{echo $totalDR;} $arrayDR[]=$totalDR;?></td>
		<td style="text-align:right;"><?php if($totalCR==0){echo "";}else{echo $totalCR;} $arrayCR[]=$totalCR;?></td>
		<td style="text-align:right;"><?php 
		if($totalDR==0 && $totalCR==0){$closing=''; $type='';}else
		if($totalDR>$totalCR){$closing=$totalDR-$totalCR; $closing_dr1[]=$closing; $type="DR";}
		else
		if($totalCR>$totalDR){$closing=$totalCR-$totalDR; $closing_cr1[]=$closing; $type="CR";}
		echo $closing." ".$type;
		?></td>
		</tr>
	<?php 	
	}
	$openingCR='';
	$openingDR='';
	$led_opening1=getledgeropeningyearwise($_REQUEST['lid']);
		// print_r($led_opening1);
		if($led_opening1[1]=='DR')
		{
		$openingDR=$led_opening1[0];  $openingCR='0';
		}
		else if($led_opening1[1]=='CR')
		{
		$openingCR=$led_opening1[0];  $openingDR='0';
		}
		else{
			$openingDR=0;
			$openingCR=0;
		}


?>
<tr>
<td style="text-align:right"><strong>Opening</strong></td>
<td style="text-align:right"><strong><?php echo $openingDR;?></strong></td>
<td style="text-align:right"><strong><?php echo $openingCR;?></strong></td>
<td style="text-align:right">&nbsp;</td>
</tr>
<tr>
<td style="text-align:right"><strong>Total</strong></td>
<td style="text-align:right"><strong><?php  $DR=array_sum($arrayDR); if($openingDR!='' && $openingDR!='0' ) {
	$DR=$openingDR+$DR;} echo $DR; ?></strong></td>
<td style="text-align:right"><strong><?php  $CR=array_sum($arrayCR); if($openingCR!='' && $openingCR!='0' ) {
	$CR=$openingCR+$CR;} echo $CR;?></strong></td>
<td style="text-align:right"><strong><?php //if($DR>$CR){echo $DR-$CR; echo " DR";}elseif($CR>$DR){echo $CR-$DR; echo " CR";} 
if(!empty($closing_cr1)) 
$tot_cr=array_sum($closing_cr1);
else
$tot_cr=0;
if(!empty($closing_dr1))
$tot_dr=array_sum($closing_dr1);
else
$tot_dr=0;
if($tot_dr>$tot_cr){echo $tot_dr-$tot_cr; echo " DR";}elseif($tot_cr>$tot_dr){echo $tot_cr-$tot_dr; echo " CR";} else{echo ""; }
?></strong></td>
</tr>
<tr>
<td style="text-align:right"><strong>Total Closing</strong></td>
<?php 
/*
if($opening_type!=''){
if($opening_type=='DR'){$DRR=$DR+$opening_bal; $CRR=$CR;}elseif($opening_type=='CR'){$CRR=$CR+$opening_bal; $DRR=$DR;}}
else
{$DRR=$DR; $CRR=$CR;}
if($DRR>$CRR){$closeDR=$DRR-$CRR;}elseif($CRR>$DRR){$closeCR=$CRR-$DRR;}*/
if($led_opening1[1]=='DR')
	{$DRR=$DR; $CRR=$CR;}
else
{
	$DRR=$DR; $CRR=$CR;
}

if($led_opening1[1]=='CR')
	{$CRR=$CR; $DRR=$DR;}
else
{
	$DRR=$DR; $CRR=$CR;
}
if($DRR>$CRR){ $closeDR=$DRR-$CRR;}elseif($CRR>$DRR){  $closeCR=$CRR-$DRR;}
?>
<td style="text-align:right"><strong><?php echo $closeDR;?></strong></td>
<td style="text-align:right"><strong><?php echo $closeCR;?></strong></td>
<td style="text-align:right"></td>
</tr>
</table>
<?php 
}
?>
