<?php
ob_flush();
include('fun-Trial_branch.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;
	$_SESSION['branch_group']=$_REQUEST['site'];
}
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" cellspacing="0" cellpadding="3">
<tr class="row1">
	<td width="13%" align="right">Branch</td>
	<td width="20%"><select name="site" name="site" >
		<option value="<?php echo $_SESSION['AssignedBranch']; ?>">Select</option>
		<?php $sql_branch=DB_query("select * from locations where loccode in ('".$_SESSION['AssignedBranch']."')",$db);
		while($row_branch=DB_fetch_array($sql_branch))
		{
		?>
		<option value="<?php echo $row_branch['loccode']; ?>" <?php if($row_branch['loccode']==$_SESSION['branch_group']) { ?> selected <?php } ?>><?php echo $row_branch['locationname']; ?></option>	
	   <?php } ?>
	   </select>
	</td>	
	<td width="13%" align="right">From Date</td>
	<td width="20%"><input type="text" name="from_date" value="<?php if(isset($_SESSION['financialYrStart']))echo ConvertSQLDate($_SESSION['financialYrStart']);?>" class="datepicker" /></td>
	<td width="13%" align="right">To Date</td>
	<td><input type="text" name="to_date" value="<?php if(isset($_SESSION['financialYrEnd']))echo ConvertSQLDate($_SESSION['financialYrEnd']);?>" class="datepicker" /></td>
</tr>
<tr class="row1">
	<td colspan="6" style="text-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?b=true" target="_blank" class="search">Print</a> <a href="APRGroupWise-TrialBalance_Exl.php" class="search">Export</a></span>
  </p>


<?php if(isset($_POST['Show'])) 
{
 ?>
<table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
<tr>
	<th>Account Groups</th>
	<th width="10%" class="text-center">Opening DR</th>
	<th width="10%" class="text-center">Opening CR</th>
	<th width="10%" class="text-center">Total Debit</th>
	<th width="10%" class="text-center">Total Credit</th>	
	<th width="10%" class="text-center">Closing Balance (DR)</th>
	<th width="10%" class="text-center">Closing Balance (CR)</th>	
	<th width="10%" class="text-center">Closing Balance</th>
</tr>
  <?php     
  $account_group_id=DB_query("select GroupId from accountgroupmaster  where   type='N'",$db);
   while($r_group=DB_fetch_array($account_group_id))
   {	   	
   	//echo $group_nature;

  $account_groups="Select * from accountgroupmaster where MainGroupId=0  and  GroupName not like 'Closing%' and Type='G' and NatureOfGroup='".$r_group['GroupId']."'    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/search_trial_balance&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."&tabid=".$_REQUEST['tabid']."&page=".$_REQUEST['page']."&gid=".$account_group_row['GroupId']."&site=".$_REQUEST['site']."";
	//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'],$_REQUEST['site']);
	}
	else
	{
	$groupDetails=getGroupBalanceSheet($account_group_row['GroupId'],'','',$_REQUEST['site']);
    }       
      
	
	//$groupDetails=getGroupBalanceSheet($account_group_row['GroupId'],$from_date='',$to_date='');

	$grand_DR=$groupDetails[0];	
	$grand_CR=$groupDetails[1];
	$opening_DR=$groupDetails[2];
	$opening_CR=$groupDetails[3];
   // echo "<pre>";
    //print_r($groupDetails);

	//$opening_DR=0;
	//$opening_CR=0;
	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!='')
	{
		$OPDR1=0;
		$OPCR1=0;
    ?>
  <tr>
    <td><a href="<?php echo $link;?>"><strong><?php echo $account_group_row['GroupName'];?></strong></a></td>
    <td style="text-align:right;"><?php echo $opening_DR; $opdrarray[]=$opening_DR;?></td>
	<td style="text-align:right;"><?php echo $opening_CR; $opcrarray[]=$opening_CR;?></td>
	<td style="text-align:right;"><?php if($grand_DR=='0'){echo "";}else{echo $grand_DR;}$DR_ARR[]=$grand_DR;?></td>
    <td style="text-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;}
	
	$closing_dr_amt=$grand_DR+$opening_DR;
	$closing_cr_amt=$grand_CR+$opening_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 style="text-align:right;"><?php echo $closing_dr_amt1[]=$closing_dr_amt; ?></td>
	<td style="text-align:right;"><?php echo $closing_cr_amt1[]=$closing_cr_amt; ?></td> 	
	<td style="text-align:right;"><?php 
	
	if($closing=='0'){echo "";}else{echo $closing."&nbsp;".$type;} ?></td>
  </tr>
  <?php
  $group_trial[]=array("groupname"=>$account_group_row['GroupName'],"opening_dr"=>$opening_DR,"opening_cr"=>$opening_CR,"total_debit"=>$grand_DR,"total_credit"=>$grand_CR,"closing_bal"=>$closing,"closing_type"=>$type,"closing_dr"=>$closing_dr_amt,"closing_cr"=>$closing_cr_amt);
   }
$_SESSION['group_trial']=$group_trial;

//echo "<pre>";

//print_r($_SESSION['group_trial']);
} 
}
?>
  <tr>
    <td style="text-align:right;"><strong>Total</strong></td>
    <td style="text-align:right;"><strong><?php echo array_sum($opdrarray);?></strong></td>
	<td style="text-align:right;"><strong><?php echo array_sum($opcrarray);?></strong></td>
	<td style="text-align:right;"><strong> <?php if($DR_ARR!='')echo array_sum($DR_ARR);?></strong></td>
    <td style="text-align:right;"><strong><?php if($CR_ARR!='')echo array_sum($CR_ARR);?></strong></td>    
    <td style="text-align:right;"><strong><?php if(!empty($closing_dr_amt1)){$arrsum1=array_sum($closing_dr_amt1);}else{$arrsum1='';}
						echo $arrsum1; ?></strong></td>
	<td style="text-align:right;"><strong><?php if(!empty($closing_cr_amt1)){$arrsum2=array_sum($closing_cr_amt1);}else{$arrsum2='';}
						echo $arrsum2; ?></strong></td>						
    <td style="text-align:right;"><strong><?php if($closingdarr!=''){$arrsum1=array_sum($closingdarr);}else{$arrsum1='';}
						if($closingcarr!=''){$arrsum2=array_sum($closingcarr);}else{$arrsum2='';}
						if($arrsum1>$arrsum2){echo trim($arrsum1)-trim($arrsum2). "&nbsp;DR";}
						elseif($arrsum2>$arrsum1){echo trim($arrsum2)-trim($arrsum1). "&nbsp;CR";}
						?></strong></td>
  </tr>
</table>
<?php } }
else if($_REQUEST['gid']!='')
{	
	unset($_SESSION['group_trial']);
	$gid=$_REQUEST['gid'];
	$_SESSION['gid']=$gid;
?>

<h3><?php echo $s->getField('accountgroupmaster','GroupId',$gid,'GroupName')?></h3>

<!--<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-TrialBalance_Libilities-print.php?b=true" target="_blank" class="search">Print</a>  <a href="APRGroupWise-TrialBalance_Libilities_Exl.php" class="search">Export</a></span></p>
 <!-- <p align="right"><a href="#" onclick="history.go(-1);">Back</a></p> -->
<table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
<tr>
    <th width="5%">S. No.</th>
    <th>Particulars</th>
	<th width="10%" class="text-center">Opening</th>
    <th width="10%" class="text-center">Debit</th>
	<th width="10%" class="text-center">Credit</th>
	<th width="10%" class="text-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='';
  //$closing='';
 
   $num=DB_num_rows($getSubGroups);
   
  while($subGroups=DB_fetch_array($getSubGroups))
  {
  		if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!=''){
  		$res=getGroupBalanceSheet($subGroups['GroupId'],$_SESSION['FDATE'],$_SESSION['TDATE'],$_REQUEST['site']);}
		else
		{$res=getGroupBalanceSheet($subGroups['GroupId'],'','',$_REQUEST['site']);}

		$TD=$res[0];
		$TC=$res[1];
		//if($res[0]!='' || $res[1]!='' )
		if($res[0]!='' || $res[1]!='' || $res[2]!='' || $res[3]!='')
		{
			//$DROPEN=0; $CROPEN=0;
?>
  <tr onMouseOver="this.style.backgroundColor='#fffeb3'; this.style.color='#00f';" onMouseOut="this.style.backgroundColor='#FFF'; this.style.color='#000000';">
  <td></td>
  <td><a href="index.php?route=<?php echo $_REQUEST['route'];?>&<?php echo $url;?>&gid=<?php echo $subGroups['GroupId'];?>&site=<?php echo $_REQUEST['site']; ?>" title="<?php echo ucwords($subGroups['GroupName']);?>"><strong><?php echo $subGroups['GroupName'];  //echo "<br />CR -- " ; echo $tot_CR_open;   ?></strong></a></td>
	 <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; $TOTALOPCR=''; $openingvalue=$TOTALOPDR; $optype='DR'; }
	 elseif($CROPEN>$DROPEN)
	 {echo $TOTALOPCR=$CROPEN-$DROPEN; echo "&nbsp;CR"; $openingCR_array[]=$TOTALOPCR; $TOTALOPDR=''; $openingvalue=$TOTALOPCR; $optype='CR';}
	else{$TOTALOPCR=''; $TOTALOPDR='';$openingvalue='';$optype='';}
	 ?></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>
 <?php  $Gtotal_DR=$TOTALOPDR+$TD; $Gtotal_CR=$TOTALOPCR+$TC;
  if($Gtotal_DR>$Gtotal_CR){$closing=trim($Gtotal_DR)-trim($Gtotal_CR); $type="DR";}
 else if($Gtotal_CR>$Gtotal_DR){$closing=trim($Gtotal_CR)-trim($Gtotal_DR); $type="CR";}else{$closing='';$type="";} 
 ?>
  <td style="text-align:right;"><?php echo $closing."&nbsp;".$type;  ?></td>
  </tr><?php 
  $gid_group[]=array("account_group"=>$subGroups['GroupName'],"opening"=>$openingvalue,"optype"=>$optype,"debit"=>$TD,"credit"=>$TC,"closing"=>$closing,"closing_type"=>$type);

}
if($num>0)
$_SESSION['gid_group']=$gid_group;
}
if($num<=0)
unset($_SESSION['gid_group']);	

 ?>
  <?php //echo $_SESSION['FDATE']; echo "------------";echo $_SESSION['TDATE'];

  $getLedgers=DB_query("Select * from accountledger where UnderGroup=$gid order by AccountName ASC",$db);
  $xcount=1;
  while($Ledgers=DB_fetch_array($getLedgers))
  {
  	$ledger_id=$Ledgers['AccountsID'];
  		$opening_amt='';
		$opening_bal='';
		$opening_type='';
		$dramt='';
		$cramt='';
	
	if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
	{	
          /*if($_REQUEST['site']!='0')
          {
           $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']."' and B.site in('".$_REQUEST['site']."')";
          }	
          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) and B.openingdate<='".$_SESSION['TDATE']."' ";
          }	*/

          if($_REQUEST['site']!='0')
          {
           $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']."' and B.site in('".$_REQUEST['site']."') and B.openingdate BETWEEN '".$_SESSION['financialYrStart']."' and '".$_SESSION['financialYrEnd']."'";
          }	
          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) and B.openingdate<='".$_SESSION['TDATE']."' and B.openingdate BETWEEN '".$_SESSION['financialYrStart']."' and '".$_SESSION['financialYrEnd']."' ";
          }
	//$opening_bal=$s->GetFieldValue('accountledger',"AccountsID=$ledger_id and openingdate<='".$_SESSION['TDATE']."'",'openingBalance');
     }
	else
	{	   
          if($_REQUEST['site']!='0')
		   {
		   	$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.site in('".$_REQUEST['site']."') ";
		   }
		  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'],$_REQUEST['site']);
		$totVoucherCR=Get_AccountByLedger($Ledgers['AccountsID'],'CR',$_SESSION['FDATE'],$_SESSION['TDATE'],$_REQUEST['site']);
	}
	else
	{
		$totVoucherDR=Get_AccountByLedger($Ledgers['AccountsID'],'DR','','',$_REQUEST['site']);
	  	$totVoucherCR=Get_AccountByLedger($Ledgers['AccountsID'],'CR','','',$_REQUEST['site']);
	}
	
	$master_party_id=$s->getField('accountledger','AccountsID',$ledger_id,'vender_client');	
	$master_tax_id=$s->getField('accountledger','AccountsID',$ledger_id,'taxid');	
	 $ledgertype=$s->getField('accountledger','AccountsID',$ledger_id,'ledgertype');	

	if($ledgertype!='' && $ledgertype!='sales')
	{

				if($ledgertype=='excisetax'){
				if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
				    {$etaxDetails=yearlyETaxAccountByTransactions($master_tax_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$_REQUEST['site']);}else
					{$etaxDetails=yearlyETaxAccountByTransactions($master_tax_id,'','',$_REQUEST['site']);} $etaxDR=$etaxDetails[0]; $etaxCR=$etaxDetails[1];
					}else{$etaxDetails='';$etaxDR='';$etaxCR='';}		
					
				if($ledgertype=='salestax')	{
				if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
				{$staxDetails=yearlySTaxAccountByTransactions($master_tax_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$_REQUEST['site']);}else
					{$staxDetails=yearlySTaxAccountByTransactions($master_tax_id,'','',$_REQUEST['site']);}
					 $staxDR=$staxDetails[0]; $staxCR=$staxDetails[1];
				}else{$staxDetails='';$staxDR=''; $staxCR='';}

				if($ledgertype=='vendor'){
					if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
					{$vendorDetails=yearlyVendorAccountByTransactions($master_party_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$_REQUEST['site']);}else
					{$vendorDetails=yearlyVendorAccountByTransactions($master_party_id,'','',$_REQUEST['site']);}
					$purchCR=$vendorDetails;
				}else{$vendorDetails='';$purchCR='';}

				if($ledgertype=='client'){
				if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!=''){
					$clientDetails=yearlyClientAccountByTransactions($master_party_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$_REQUEST['site']);	}
					else{$clientDetails=yearlyClientAccountByTransactions($master_party_id,'','',$_REQUEST['site']);} 
					$invserviceDR=$clientDetails1;$invDR=$clientDetails;}
					else{$clientDetails='';$invDR='';$clientDetails1='';$invserviceDR='';}
				
				if($ledgertype=='freight')	{
					if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
					{$freightDetails=yearlyFreightAccountByTransactions($ledger_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$_REQUEST['site']);}
					else
					{$freightDetails=yearlyFreightAccountByTransactions($ledger_id,'','',$_REQUEST['site']);}
					
					$freightDR=$freightDetails[0];$freightCR=$freightDetails[1];
				}else{$freightDetails='';$freightDR='';$freightCR='';}

				if($ledgertype=='surcharge'){
					if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
					{$surDetails=yearlySurchargeAccountByTransactions($ledger_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$_REQUEST['site']);}
					else
					{$surDetails=yearlySurchargeAccountByTransactions($ledger_id,'','',$_REQUEST['site']);}  
				    $surDR=$surDetails[0];  $surCR=$surDetails[1]; 
				    //$surDR=$surDetails;
				}else{$surDetails='';$surDR='';$surCR='';}

				if($ledgertype=='pckcharge'){
					if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
					{$packDetails=yearlyPkgChargesAccountByTransactions($ledger_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$_REQUEST['site']);}
					else
					{$packDetails=yearlyPkgChargesAccountByTransactions($ledger_id,'','',$_REQUEST['site']);}  
				   $packDR=$packDetails[0];  $packCR=$packDetails[1];; 
				}else{$packDetails='';$packDR='';$packCR='';}

				if($ledgertype=='addcharge')	{
					if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
					{$addDetails=yearlyAddChargesAccountByTransactions($ledger_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$_REQUEST['site']);}
					else
					{$addDetails=yearlyAddChargesAccountByTransactions($ledger_id,'','',$_REQUEST['site']);}  
					 $addDR=$addDetails[0];$addCR=$addDetails[1]; 
				}else{$addDetails='';$addDR='';$addCR='';}

				if($ledgertype=='cess')	{
					if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
					{$cessDetails=yearlyCessAccountByTransactions($master_tax_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$_REQUEST['site']);}
					else
					{$cessDetails=yearlyCessAccountByTransactions($master_tax_id,'','',$_REQUEST['site']);} 
				    $cessDR=$cessDetails[0]; $cessCR=$cessDetails[1];  
				}else{$cessDetails='';$cessDR='';$cessCR='';}
				
				
				if($ledgertype=='insurance'){

						if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
						{
							$insurance=yearlyinsuranceAccountByTransactions($ledger_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$_REQUEST['site']);

						}
					    else{ $insurance=yearlyinsuranceAccountByTransactions($ledger_id,'','',$_REQUEST['site']);}
					   
					   
						$insuranceDR=$insurance[0]; $insuranceCR=$insurance[1]; 
						}else{$insurance='';$insuranceDR='';$insuranceCR='';}
				
				if($ledgertype=='round'){
					if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!=''){
					$roundDetails=yearlyRoundOffByTransactions($ledger_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$_REQUEST['site']);}
					else{$roundDetails=yearlyRoundOffByTransactions($ledger_id,'','',$_REQUEST['site']);} 
					$roundDR=$roundDetails[0];$roundCR=$roundDetails[1];			
				}else{$roundDetails='';$roundDR='';$roundCR=''; $roundserviceDetails='';$roundserviceDR='';$roundserviceCR='';}	
				$legderDR='';$legderCR='';$ledgerserviceCR='';$ledgerPostingservice=''; 
	
	}
	else
	{
		if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!=''){
		$ledgerPosting=yearlyLedgerPostingByTransactions($ledger_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$_REQUEST['site']);}
		else{$ledgerPosting=yearlyLedgerPostingByTransactions($ledger_id,'','',$_REQUEST['site']);	} 
		$etaxDetails='';$etaxDR=''; $etaxCR=''; $staxDetails='';$staxDR='';$staxCR=''; $vendorDetails='';$purchCR=''; $clientDetails='';$invDR='';
		$freightDetails='';$freightDR='';$surDetails='';$surDR='';$packDetails='';$packDR='';$addDetails='';$addDR='';$salesDR='';$salesCR='';
		$cessDetails='';$cessDR='';$insuranceDR='';$insuranceCR='';	$roundDetails='';$roundDR='';$roundCR='';$freightCR='';$surCR='';
		$packCR='';$addCR='';$cessCR='';
		
	}

    $ledger_dr_tot=get_opening_filter_basis($ledger_id,$_REQUEST['site'],$_SESSION['FDATE'],'DR');
	$ledger_cr_tot=get_opening_filter_basis($ledger_id,$_REQUEST['site'],$_SESSION['FDATE'],'CR');

	if($ledger_dr_tot>$ledger_cr_tot)
	{
		$diff_dr=$ledger_dr_tot-$ledger_cr_tot;
	}
	else
	{
		$diff_cr=$ledger_cr_tot-$ledger_dr_tot;
	}

	if($opening_bal!='' || 
		($totVoucherDR+$etaxDR+$staxDR+$invDR+$legderDR+$freightDR+$surDR+$packDR+$addDR+$cessDR+$roundDR+$insuranceDR+$salesDR)!='' || 
		($totVoucherCR+$etaxCR+$staxCR+$purchCR+$legderCR+$roundCR+$freightCR+$surCR+$packCR+$addCR+$cessCR+$insuranceCR+$salesCR)!='')
	{
  	?>
		<tr onMouseOver="this.style.backgroundColor='#B7CFD9'; this.style.color='#00f';" onMouseOut="this.style.backgroundColor='#FFF'; this.style.color='#000000';">
			  <td><?php echo $xcount;?></td>
			  <td><a href="index.php?route=<?php echo $_REQUEST['route'];?>&<?php echo $url;?>&lid=<?php echo $Ledgers['AccountsID'];?>&site=<?php echo $_REQUEST['site']; ?>" title="<?php echo ucwords($Ledgers['AccountName']);?>"><strong><font color="#000000"><?php echo $Ledgers['AccountName']; ?></font></strong></a></td>
			  
			   <td style="text-align:right;"><?php 
			   if($opening_bal!=''){
			   	//echo $opening_bal+$diff_dr."&nbsp;".$opening_type; 
			   	echo $opening_bal."&nbsp;".$opening_type; 
			   } 
			   $tot_op_dr[]=$diff_dr; $tot_op_cr[]=$diff_cr;
			  if($opening_type=='CR'){
			  	$openingCR_array[]=$opening_bal; 
			  	$OPCR=$opening_bal; $OPDR='';
			  }
			  else{
			  	$openingDR_array[]=$opening_bal; 
			  	$OPDR=$opening_bal; $OPCR='';
			  	}?></td>
			  
			   <td style="text-align:right;">
			   <?php  
			  // $total_DR=$totVoucherDR+$etaxDR+$staxDR+$invDR+$legderDR+$freightDR+$surDR+$packDR+$addDR+$cessDR+$roundDR+$insuranceDR+$salesDR; 
			   $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+$etaxCR+$staxCR+$purchCR+$legderCR+$roundCR+$freightCR+$surCR+$packCR+$addCR+$cessCR+$insuranceCR+$salesCR; 
			  $total_CR=$totVoucherCR; 

  if($total_CR==0)echo " "; else echo $total_CR; $ARRCR[]=$total_CR;?></td>
  <?php 
 
	//$Gtotal_DR1=$OPDR+$total_DR;//+$diff_dr;
	$Gtotal_DR1=$total_DR+$OPDR;//+$diff_dr;//+$OPDR;
 	$Gtotal_CR1=$total_CR+$OPCR;//+$diff_cr;//+$OPCR;
 	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=$opening_bal;
 		$type=$opening_type;
 	}
	?>
  <td style="text-align:right;"><?php echo $closing."&nbsp;".$type;
	/*echo "Debit --- ".$Gtotal_DR1;
	echo "<br/>";
	echo "Credit --- ".$Gtotal_CR1;
	echo "<br/>";*/
	?></td>
  </tr>
	<?php 
	$xcount++;
	  $gid_group1[]=array("account_group"=>$Ledgers['AccountName'],"opening"=>$opening_bal,"opening_type"=>$opening_type,"debit"=>$total_DR,"credit"=>$total_CR);   
	}
	
    $_SESSION['gid_group1']=$gid_group1; 
} ?>
 <tr>
     <td></td>	
     <td><strong>Total</strong></td>	
	 <td width="10%" style="text-align:right"><strong>
	 <?php
	 /*echo "<pre>";print_r($openingDR_array);echo "</pre>";
	 echo "<pre>";print_r($tot_op_dr);echo "</pre>";
	 echo "<pre>";print_r($openingCR_array);echo "</pre>";
	 echo "<pre>";print_r($tot_op_cr);echo "</pre>";*/
	 if($openingDR_array!='')$opDR=array_sum($openingDR_array);//+array_sum($tot_op_dr);
	 if($openingCR_array!='')$opCR=array_sum($openingCR_array);//+array_sum($tot_op_cr);
	 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 width="10%" 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 width="10%" 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>
	<?php 
	/*$FinalDR=$tot_op_DR+$xx+$yy;
	$FinalCR=$tot_op_CR+$mm+$nn;
	if($FinalDR>$FinalCR){$closing=trim($FinalDR)-trim($FinalCR); $type="DR"; }
 	elseif($FinalCR>$FinalDR){$closing=trim($FinalCR)-trim($FinalDR); $type="CR";}else{$closing='';$type="";}*/

 	$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 width="10%" style="text-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;

	//echo $_SESSION['financialYrStart'];
	//echo $_SESSION['financialYrEnd'];

	$opening_amt='';

	$opening_bal='';
	$opening_type='';
	$dramt='';
	$cramt='';
    $ledger_id=$_REQUEST['lid'];
	//echo $opening_bal..
	$Ledgerstype=$s->getField('accountledger','AccountsID',$lid,'ledgertype');
	if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
	{
		         if($_REQUEST['site']!='0')
		         {
		          $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']."' and B.site in('".$_REQUEST['site']."')";
		         }	
		         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) and B.openingdate<='".$_SESSION['TDATE']."'";
		         }		
	   
	//$opening_bal=$s->GetFieldValue('accountledger',"AccountsID=$ledger_id and openingdate<='".$_SESSION['TDATE']."'",'openingBalance');
     }
	else
	{	 
         	if($_REQUEST['site']!='0')
         	{
        $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.site in('".$_REQUEST['site']."') ";	

         	}
         	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'];*/	
?>

<h3><?php echo $s->getField('accountledger','AccountsID',$lid,'AccountName');?></h3>

<!--<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-TrialBalanceSheet-Lid-Print.php?site=<?php echo $_REQUEST['site']; ?>" target="_blank" class="search">Print</a>  <a href="APRGroupWise-TrialBalanceSheet-Lid-Exl.php?site=<?php echo $_REQUEST['site']; ?>" class="search">Export</a></span>
  </p>
   <!-- <p align="right"><a href="#" onclick="history.go(-1);">Back</a></p> -->
<table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
<tr>
<th>Months</th>
<th width="20%" class="text-center">Debit</th>
<th width="20%" class="text-center">Credit</th>
<th width="20%" class="text-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];}
		
		$month=$key;
		if($key<4){
		$month_num=$key+12;}else
		{
			if(substr($key,0,1)=='0')
			{$month_num=substr($key,-1);}
			else{$month_num=$key;}
		}
		
		$color='#FFFFFF';
		$flag='';
		$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;$color='#CC99FF';}else
				if($tmnth==$key){$flag=3;$color='#CC99FF';}else
				if($month_num<$to_month_num && $month_num>$from_month_num){$flag=4;$color='#CC99FF';}
			}
		$amounts=getMonthsBalanceSheet($lid,$key,$yr,$flag);
		}
		else{*/
			$amounts=getMonthsBalanceSheet($lid,$key,$yr,'',$_REQUEST['site']);	  
	
		$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 style="padding-left:5px"><a href="#" onclick="popup('bal_sheet_popup.php?q=<?php echo $lid;?>&mnth=<?php echo $key;?>&yr=<?php echo $yr;?>&site=<?php echo $_REQUEST['site']; ?>',1000,500)"><strong><font color="#000000"><?php echo $val;//."---".$yr,"----".$flag;?></font></strong></a></td>
		<td style="text-align:right; padding-right:5px;"><?php if($totalDR==0){echo "";}else{echo $totalDR;} $arrayDR[]=$totalDR;?></td>
		<td style="text-align:right; padding-right:5px;"><?php if($totalCR==0){echo "";}else{echo $totalCR;} $arrayCR[]=$totalCR;?></td>
		<td style="text-align:right; padding-right:5px;"><?php 
		if($totalDR==0 && $totalCR==0){$closing=''; $type='';}else
		if($totalDR>$totalCR){$closing=trim($totalDR)-trim($totalCR); $type="DR";}
		else
		if($totalCR>$totalDR){$closing=trim($totalCR)-trim($totalDR); $type="CR";}
		echo $closing." ".$type;
		?></td>
		</tr>
	<?php 	
	}
?>
<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 echo $DR=array_sum($arrayDR)+$openingDR;?></strong></td>
<td style="text-align:right"><strong><?php echo $CR=array_sum($arrayCR)+$openingCR;?></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";}?></strong></td>
</tr>
<tr>
<td style="text-align:right"><strong>Total Closing</strong></td>
<?php {	$DRR=$DR; $CRR=$CR; }
if($DRR>$CRR){$closeDR=$DRR-$CRR;}elseif($CRR>$DRR){$closeCR=trim($CRR)-trim($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 }  ?>
