<?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" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td width="14%"></td>
	<td width="13%" 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%" 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"><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-TrialBalance_Print.php" target="_blank" class="search">Print</a> <a href="APRGroupWise-TrialBalance_Exl.php" class="search">Export</a></span>
  </p>

<table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
<tr>
	<th>Account Groups</th>
	<th width="10%" style="text-align:center;">Opening DR</th>
	<th width="10%" style="text-align:center;">Opening CR</th>
	<th width="10%" style="text-align:center;">Total Debit</th>
	<th width="10%" style="text-align:center;">Total Credit</th>
	<th width="10%" style="text-align:center;">Closing Balance</th>
</tr>
  <?php 
  $account_groups="Select * from accountgroupmaster where MainGroupId=0 and Type='G' order by MainGroupId asc, GroupName asc";
  $account_group_res=DB_query($account_groups,$db);
  while($account_group_row=DB_fetch_array($account_group_res))
  {
	$link="index.php?route=Account/AccountingReport&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."&tabid=".$_REQUEST['tabid']."&page=".$_REQUEST['page']."&gid=".$account_group_row['GroupId']."";
	//echo "<br>";echo "Select * from accountledger where UnderGroup='".$account_group_row['GroupId']."'";
	//echo $_SESSION['FDATE']; echo "&nbsp;".$_SESSION['TDATE'];
	if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
	{
  		$prevdate=date('Y-m-d', strtotime('-1 day', strtotime($_SESSION['FDATE'])));
		$groupDetails=getGroupBalanceSheet($account_group_row['GroupId'],$_SESSION['FDATE'],$_SESSION['TDATE']);
	}
	else
	{$groupDetails=getGroupBalanceSheet($account_group_row['GroupId'],'','');}
	
	//$groupDetails=getGroupBalanceSheet($account_group_row['GroupId'],$from_date='',$to_date='');
	//print_r($groupDetails);
	$grand_DR=$groupDetails[0];	
	$grand_CR=$groupDetails[1];
	$opening_DR=$groupDetails[2];
	$opening_CR=$groupDetails[3];
	if($opening_DR>$opening_CR){$OP=$opening_DR-$opening_CR; $OPTYPE="DR"; $OPDR1=$opening_DR-$opening_CR;$OPCR1='';}elseif($opening_CR>$opening_DR){$OP=$opening_CR-$opening_DR; $OPTYPE="CR"; $OPCR1=$opening_CR-$opening_DR;$OPDR1='';}else{$OP=''; $OPTYPE='';$OPDR1='';$OPCR1='';}
	
	if($grand_DR!='' || $grand_CR!='' || $opening_DR!='' || $opening_CR!='')
	{
  ?>
  <tr>
    <td><a href="<?php echo $link;?>"><?php 
	//echo "<pre>";print_r($groupDetails); echo "<br />";
	echo $account_group_row['GroupName'];?></a></td>
    <td align="right"><?php echo $opening_DR; $opdrarray[]=$opening_DR;?></td>
	<td align="right"><?php echo $opening_CR; $opcrarray[]=$opening_CR;?></td>
	<td align="right"><?php if($grand_DR=='0'){echo "";}else{echo $grand_DR;}$DR_ARR[]=$grand_DR;?></td>
    <td align="right"><?php if($grand_CR=='0'){echo " ";}else{echo $grand_CR;}$CR_ARR[]=$grand_CR;?></td>
    <?php 
	if($OP!='')
	{
		if($OPTYPE=='DR'){$GDR=$OP+$grand_DR; $GCR=$grand_CR;}
		elseif($OPTYPE=='CR'){$GCR=$OP+$grand_CR; $GDR=$grand_DR;}
	}
	else{$GDR=$grand_DR; $GCR=$grand_CR;}	
	
	if($GDR>$GCR){$closing=$GDR-$GCR; $type="DR"; $closingd=$GDR-$GCR;$closingc='';  $closingdarr[]=$closingd;}
	elseif($GCR>$GDR){$closing=$GCR-$GDR; $type="CR"; $closingc=$GCR-$GDR;$closingd='';$closingcarr[]=$closingc;}
	else{$closing='';$type='';}
	?>
	<td align="right"><?php 
	
	if($closing=='0'){echo "";}else{echo $closing."&nbsp;".$type;}?></td>
  </tr>
  <?php }} ?>
  <tr>
    <td align="right"><strong>Total</strong></td>
    <td align="right"><strong><?php echo array_sum($opdrarray);?></strong></td>
	<td align="right"><strong><?php echo array_sum($opcrarray);?></strong></td>
	<td align="right"><strong> <?php if($DR_ARR!='')echo array_sum($DR_ARR);?></strong></td>
    <td align="right"><strong><?php if($CR_ARR!='')echo array_sum($CR_ARR);?></strong></td>
    <td align="right"><strong> <?php if($closingdarr!=''){$arrsum1=array_sum($closingdarr);}else{$arrsum1='';}
						if($closingcarr!=''){$arrsum2=array_sum($closingcarr);}else{$arrsum2='';}
						if($arrsum1>$arrsum2){echo $arrsum1-$arrsum2. "&nbsp;DR";}
						elseif($arrsum2>$arrsum1){echo $arrsum2-$arrsum1. "&nbsp;CR";}
						?></strong></td>
  </tr>
</table>
<?php } 
else 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="right">
 <span style="float:left;"><strong><?php echo $period;?></strong></span>
  <a href="APRGroupWise-TrialBalance_Libilities-print.php" target="_blank" class="search">Print</a> <a href="APRGroupWise-TrialBalance_Libilities_Exl.php" class="search">Export</a>
  </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 
  $getSubGroups=DB_query("Select * from accountgroupmaster where MainGroupId=$gid",$db);
  $openingCR_array=''; $openingDR_array='';
  //$closing='';
  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];
		if($res[0]!='' || $res[1]!='' || $res[2]!='' || $res[3]!=''){
?>
  <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']);?>"><strong><?php echo $subGroups['GroupName'];
  //echo "<br />CR -- " ; echo $tot_CR_open;
  //echo "<br />DR---"; echo $tot_DR_open;
     ?></strong></a></td>
	 <td align="right"><?php 
	 $DROPEN=$res[2];
	 $CROPEN=$res[3];
	 if($DROPEN>$CROPEN)
	 {echo $TOTALOPDR=$DROPEN-$CROPEN; echo "&nbsp;DR"; $openingDR_array[]=$TOTALOPDR; $TOTALOPCR='';}
	 elseif($CROPEN>$DROPEN)
	 {echo $TOTALOPCR=$CROPEN-$DROPEN; echo "&nbsp;CR"; $openingCR_array[]=$TOTALOPCR; $TOTALOPDR='';}else{$TOTALOPCR=''; $TOTALOPDR='';}
	 ?></td>
  <td align="right"><?php if($TD==0){echo "";}else echo $TD; $TDA[]=$TD;?></td>
  <td align="right"><?php if($TC==0){echo "";}else echo $TC; $TCA[]=$TC;?></td>
 <?php 
 $Gtotal_DR=$TOTALOPDR+$TD;
 $Gtotal_CR=$TOTALOPCR+$TC;
 if($Gtotal_DR>$Gtotal_CR){$closing=$Gtotal_DR-$Gtotal_CR; $type="DR";}
 elseif($Gtotal_CR>$Gtotal_DR){$closing=$Gtotal_CR-$Gtotal_DR; $type="CR";}else{$closing='';$type="";} 
 ?>
  <td align="right"><?php echo $closing."&nbsp;".$type; //echo "<br />".$Gtotal_DR."<br />".$Gtotal_CR; echo "<br />------------<br />".$TOTALOPDR."----".$TD; echo "<br />------------<br />".$TOTALOPCR."----".$TC;?></td>
  </tr><?php }} ?>
  <?php //echo $_SESSION['FDATE']; echo "------------";echo $_SESSION['TDATE'];
  $getLedgers=DB_query("Select * from accountledger where UnderGroup=$gid order by AccountName ASC",$db);
  while($Ledgers=DB_fetch_array($getLedgers))
  {
  	$ledger_id=$Ledgers['AccountsID'];
	
	$opening_amt='';

	$opening_bal='';
	$opening_type='';
	$dramt='';
	$cramt='';

	//echo $opening_bal..

	if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
	{
		
		$query_details="Select (B.openingBalance) as openingBalance,B.openingtype as openingtype from accountledger A inner join accountledger_dtls B on A.AccountsID=B.accountledger_id
		 where A.AccountsID in ($ledger_id) and B.openingdate<='".$_SESSION['TDATE']."'";
	    
	//$opening_bal=$s->GetFieldValue('accountledger',"AccountsID=$ledger_id and openingdate<='".$_SESSION['TDATE']."'",'openingBalance');
     }
	else
	{
	  
		$query_details="Select (B.openingBalance) as openingBalance,B.openingtype as openingtype from accountledger A inner join accountledger_dtls B on A.AccountsID=B.accountledger_id
		 where A.AccountsID in ($ledger_id) ";
	   

	//$opening_bal=$s->getField('accountledger','AccountsID',$ledger_id,'openingBalance');
    }
    $sql_check_dtls=DB_query($query_details,$db);
     while($row_dtls=DB_fetch_array($sql_check_dtls))
	        {

	         $opening_amt[$row_dtls['openingtype']][]=$row_dtls['openingBalance']; 
	        }	      

	         if(!empty($opening_amt[DR]))
	          $dramt=array_sum($opening_amt[DR]);
	          if(!empty($opening_amt[CR]))
	          $cramt=array_sum($opening_amt[CR]);
	         
	         if($dramt>$cramt)
	         {
	         $opening_bal=$dramt-$cramt;
	         $opening_type='DR';
	         }
	         else
	         {
	         $opening_bal=$cramt-$dramt;   
	         $opening_type='CR';   
             }

  	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','','');}
	
	
	if($opening_bal!='' || ($totVoucherDR)!='' || ($totVoucherCR)!=''){
  ?>
  <tr bgcolor="#FFFFFF" 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']);?>"><?php echo $Ledgers['AccountName']; //echo "----".$openingCR."-------".$openingDR;
  
  //print_r($ledgerPosting);?></a></td>  
  <td align="right"><?php if($opening_bal!=''){echo $opening_bal."&nbsp;".$opening_type; }
  if($opening_type=='CR'){
  $openingCR_array[]=$opening_bal; $OPCR=$opening_bal; $OPDR='';}
  else{$openingDR_array[]=$opening_bal; $OPDR=$opening_bal; $OPCR='';}?></td>  
  <td align="right"><?php  $total_DR=$totVoucherDR; if($total_DR==0)echo " "; else echo $total_DR; $ARRDR[]=$total_DR;?></td>
  <td align="right"><?php $total_CR=$totVoucherCR; if($total_CR==0)echo " "; else echo $total_CR; $ARRCR[]=$total_CR;?></td>
  <?php 
	$Gtotal_DR1=$OPDR+$total_DR;
 	$Gtotal_CR1=$OPCR+$total_CR;
 	if($Gtotal_DR1>$Gtotal_CR1){$closing=$Gtotal_DR1-$Gtotal_CR1; $type="DR"; }
 	elseif($Gtotal_CR1>$Gtotal_DR1){$closing=$Gtotal_CR1-$Gtotal_DR1; $type="CR";}else{$closing='';$type="";}
	?>
  <td align="right"><?php echo $closing."&nbsp;".$type;?></td>
  </tr><?php }} ?>
 <tr>
    <td align="right"><strong>Total</strong></td>	
	 <td align="right"><strong><?php 	 
	 if($openingDR_array!='')$opDR=array_sum($openingDR_array);
	 if($openingCR_array!='')$opCR=array_sum($openingCR_array);
	 if($opDR>$opCR){echo $tot_op_DR=$opDR-$opCR; echo "&nbsp;DR";}else{echo $tot_op_CR=$opCR-$opDR; echo "&nbsp;CR";}	 
	 ?></strong></td>	
    <td 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 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>
	<?php 
	$FinalDR=$tot_op_DR+$xx+$yy;
	$FinalCR=$tot_op_CR+$mm+$nn;
	if($FinalDR>$FinalCR){$closing=$FinalDR-$FinalCR; $type="DR"; }
 	elseif($FinalCR>$FinalDR){$closing=$FinalCR-$FinalDR; $type="CR";}else{$closing='';$type="";}
	?>
	<td align="right"><strong><?php echo $closing."&nbsp;".$type;?></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;
	$ledger_id=$_REQUEST['lid'];
	//echo $_SESSION['financialYrStart'];
	//echo $_SESSION['financialYrEnd'];
	
	$opening_amt='';

	$opening_bal='';
	$opening_type='';
	$dramt='';
	$cramt='';

	//echo $opening_bal..
	$Ledgerstype=$s->getField('accountledger','AccountsID',$lid,'ledgertype');
	if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
	{
		
		$query_details="Select (B.openingBalance) as openingBalance,B.openingtype as openingtype from accountledger A inner join accountledger_dtls B on A.AccountsID=B.accountledger_id
		 where A.AccountsID in ($ledger_id) and B.openingdate<='".$_SESSION['TDATE']."'";
	    
	//$opening_bal=$s->GetFieldValue('accountledger',"AccountsID=$ledger_id and openingdate<='".$_SESSION['TDATE']."'",'openingBalance');
     }
	else
	{	 
		$query_details="Select (B.openingBalance) as openingBalance,B.openingtype as openingtype from accountledger A inner join accountledger_dtls B on A.AccountsID=B.accountledger_id
		 where A.AccountsID in ($ledger_id) ";	   

	//$opening_bal=$s->getField('accountledger','AccountsID',$ledger_id,'openingBalance');
    }
    $sql_check_dtls=DB_query($query_details,$db);
     while($row_dtls=DB_fetch_array($sql_check_dtls))
	        {
	         $opening_amt[$row_dtls['openingtype']][]=$row_dtls['openingBalance']; 
	        }

	      if(!empty($opening_amt[DR]))
	      {
	          $openingDR=array_sum($opening_amt[DR]);
	          $opening_type='DR';  
	       }   
	          if(!empty($opening_amt[CR]))
	          {	
	          $openingCR=array_sum($opening_amt[CR]);
	           $opening_type='CR';  
	          }
	/*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="right">
 <span style="float:left;"><strong><?php echo $period;?></strong></span>
  <a href="APRGroupWise-TrialBalanceSheet-Lid-Print.php" target="_blank" class="search">Print</a> <a href="APRGroupWise-TrialBalanceSheet-Lid-Exl.php" class="search">Export</a>
  </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']);
		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;}
			}
		$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 align="right"><?php if($totalDR==0){echo "";}else{echo $totalDR;} $arrayDR[]=$totalDR;?></td>
		<td align="right"><?php if($totalCR==0){echo "";}else{echo $totalCR;} $arrayCR[]=$totalCR;?></td>
		<td align="right"><?php 
		if($totalDR==0 && $totalCR==0){$closing=''; $type='';}else
		if($totalDR>$totalCR){$closing=$totalDR-$totalCR; $type="DR";}
		else
		if($totalCR>$totalDR){$closing=$totalCR-$totalDR; $type="CR";}
		echo $closing." ".$type;
		?></td>
		</tr>
	<?php 	
	}
?>
<tr>
<td align="right"><strong>Opening</strong></td>
<td align="right"><strong><?php echo $openingDR;?></strong></td>
<td align="right"><strong><?php echo $openingCR;?></strong></td>
<td>&nbsp;</td>
</tr>
<tr>
<td align="right"><strong>Total</strong></td>
<td align="right"><strong><?php echo $DR=array_sum($arrayDR)+$openingDR;?></strong></td>
<td align="right"><strong><?php echo $CR=array_sum($arrayCR)+$openingCR;?></strong></td>
<td><?php //if($DR>$CR){echo $DR-$CR; echo " DR";}elseif($CR>$DR){echo $CR-$DR; echo " CR";}?></td>
</tr>
<tr>
<td align="right"><strong>Total Closing</strong></td>
<?php 

if($opening_type!=''){
$DRR=$DR;$CRR=$CR; $DRR=$DR;}
else
{$DRR=$DR; $CRR=$CR;}
//echo $DRR;
if($DRR>$CRR){$closeDR=$DRR-$CRR;}elseif($CRR>$DRR){$closeCR=$CRR-$DRR;}
?>
<td align="right"><strong><?php echo $closeDR;?></strong></td>
<td align="right"><strong><?php echo $closeCR;?></strong></td>
<td></td>
</tr>
</table>
<?php 
}
?>