<?php include('fun-Trial_branch.php'); 
 $url="modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."&tabid=".$_REQUEST['tabid']."&page=".$_REQUEST['page'];
if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!=''){$period=ConvertSQLDate($_SESSION['FDATE']).' to '.ConvertSQLDate($_SESSION['TDATE']);}else{$period=ConvertSQLDate($_SESSION['financialYrStart']).' to '.ConvertSQLDate($_SESSION['financialYrEnd']);}
?><form name="form1" method="post" action=""> 

<table width="100%" border="0" cellpadding="3" cellspacing="0">
  <tr class="row1">
  	<td width="13%" align="right">Branch</td>
    <td width="35%"><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">Group</td>
	<td>
	<?php $pcode=$_REQUEST['GroupSummaryLedger'];?>
 	<select name="GroupSummaryLedger" id="GroupSummaryLedger" >	   
  <?php 
	$dashVal = "&nbsp;&nbsp;&nbsp;";
	$sqlCate = "select * from  accountgroupmaster  where    MainGroupId   = 0";
	$rsCate  = DB_query($sqlCate,$db);
	if(DB_num_rows($rsCate)>0)
	{
		while($rowCate =DB_fetch_array($rsCate))
		{
 				$sqlCateSel = "select * from accountgroupmaster where MainGroupId   =".$rowCate['GroupId']." ";
				$rsCateSel	= DB_query($sqlCateSel,$db);
				$px='';
				 if($rowCate['GroupId']==$_REQUEST['gid'])
				 $px="selected";
			echo $dashVal;	
		  ?>
		<option value="<?php echo $rowCate['GroupId'];?>"<?php echo $px ?>> 
		<?php echo  $rowCate['GroupName'] ;?></option>
 		<?php			
			 CategoryShow($pcode, $rowCate['GroupId'], $dashVal);
		}
	}
?></select>
	</td>
</tr>
<tr class="row1">
	<td align="right">From Date</td>
    <td><input type="text" name="from_date" value="<?php if(isset($_SESSION['FDATE']))echo ConvertSQLDate($_SESSION['FDATE']);?>" class="datepicker"  /></td>
    <td align="right">To Date</td>
    <td><input type="text" name="to_date" value="<?php if(isset($_SESSION['TDATE']))echo ConvertSQLDate($_SESSION['TDATE']);?>" class="datepicker"  /></td>
</tr>
<tr class="row1">
    <td colspan="4" style="text-align:center;">
      <input type="submit" name="Go" value="Search" class="search"> <input type="submit" name="Reset" value="Reset" class="search">
    </td>
  </tr>
</table>
</form>

<?php 
if($_REQUEST['Reset']!=''){unset($_SESSION['FDATE']);unset($_SESSION['TDATE']);}
if($_REQUEST['Go']!='')
{

	$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'];

	 
	 echo "<script>location.href='index.php?route=".$_REQUEST['route']."&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."&tabid=".$_REQUEST['tabid']."&page=".$_REQUEST['page']."&gid=".$_REQUEST['GroupSummaryLedger']."'</script>";
  ?>
  <p align="right">
  <a href="APRGroupSummary-print.php" target="_blank" class="search">Print</a> <a href="accountgroupsumm_exl.php" class="search">Export</a>
  </p>
 <?php } ?>   
<?php 
if($_REQUEST['gid']!='')
{
  
$gid=$_REQUEST['gid'];
$_SESSION['gid']=$gid;
$groupType=DB_fetch_array(DB_query("Select Type from accountgroupmaster where GroupId=".$_REQUEST['gid']."",$db));
$Gtype=$groupType['Type'];
?>
<br>
<h3><?php echo $s->getField('accountgroupmaster','GroupId',$gid,'GroupName')?> ( <?php echo $period;?> )</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="right">
  <a href="APRGroupWise-TrialBalance_Libilities-print.php" target="_blank" class="search">Print</a> <a href="GroupSummary_exl.php" class="search">Export</a>
</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 DR</th>
	<th width="10%" class="text-center">Opening CR</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 

  if($Gtype=='G'){
  $getSubGroups=DB_query("Select * from accountgroupmaster where MainGroupId=$gid",$db);}
  elseif($Gtype=='N'){
 
  $getSubGroups=DB_query("Select * from accountgroupmaster where MainGroupId=0 and Type='G' and NatureOfGroup=".$_REQUEST['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'],$_SESSION['branch_group']);}
		else
		{
		$res=getGroupBalanceSheet($subGroups['GroupId'],'','',$_SESSION['branch_group']);}
		//print_r($res);
		$TD=$res[0];
		$TC=$res[1];
		if($res[0]!='' || $res[1]!='' || $res[2]!='' || $res[3]!=''){
		//echo $TD;
  ?>
  <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'];?>" title="<?php echo ucwords($subGroups['GroupName']);?>"><strong><?php echo $subGroups['GroupName']; ?></strong></a></td>
  <?php 
	 $DROPEN=$res[2];
	 $CROPEN=$res[3];
	 $TOTALOPDR=$DROPEN; 
	 $TOTALOPCR=$CROPEN; 
	 ?>
  <td style="text-align:right;"><?php echo $openingDR_array[]=$TOTALOPDR;?></td>
  <td style="text-align:right;"><?php echo $openingCR_array[]=$TOTALOPCR;?></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=$Gtotal_DR-$Gtotal_CR; $type="DR";}
 elseif($Gtotal_CR>$Gtotal_DR){$closing=$Gtotal_CR-$Gtotal_DR; $type="CR";}else{$closing='';$type="";} 
 ?>
  <td style="text-align:right;"><?php echo $closing."&nbsp;".$type; ?></td>
  </tr><?php } }?>
 
  <?php 
 if($Gtype=='G'){
 $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'];	
	$OPCR='';
	$OPDR='';
	$dramt='';
	$cramt='';
    $opening_amt='';
    $closing='';
    $total_DR='';
    $total_CR='';
      $dr_opening_fdate='';
    $cr_opening_fdate='';
	//echo $opening_bal..
	if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
	{
		if($_SESSION['branch_group']!='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 between '".$_SESSION['FDATE']."' and '".$_SESSION['TDATE']."' and B.site in('".$_SESSION['branch_group']."')";
		 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 between '".$_SESSION['FDATE']."' and '".$_SESSION['TDATE']."'";
	}
	else
	{
		if($_SESSION['branch_group']!='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('".$_SESSION['branch_group']."')";
		 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) ";
   }
   $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']; 
   }
   /*---------------------------------------  calculate Opening Debit Amount Starts Here ----------------- */
   if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!=''){
	  	if($_SESSION['branch_group']!='0')
	  	{
	  		  $query_details_filter_opening_dr="Select sum(amount) as total_amount from account_book A 
	  		  inner join account_book_details B on A.id=B.account_bookid
                where B.ledgerid in ($ledger_id) and A.Dates<'".$_SESSION['FDATE']."' and A.Dates between '".$_SESSION['FDATE']."' and '".$_SESSION['TDATE']."' and A.branch in('".$_SESSION['branch_group']."') and amount_type='DR' and cancel_status='0' ";
	  	}
	  	else
	  	{
	  		$query_details_filter_opening_dr="Select sum(amount) as total_amount from account_book A 
	  		inner join account_book_details B on A.id=B.account_bookid
                where B.ledgerid in ($ledger_id) and A.Dates<'".$_SESSION['FDATE']."' and A.Dates between '".$_SESSION['FDATE']."' and '".$_SESSION['TDATE']."' and amount_type='DR' and cancel_status='0' ";
	  	}
   }
   else
   {
   	if($_SESSION['branch_group']!='0')
   	  {
   	  	$query_details_filter_opening_dr="Select sum(amount) as total_amount from account_book A 
   	  	inner join account_book_details B on A.id=B.account_bookid
                where B.ledgerid in ($ledger_id) and A.branch in('".$_SESSION['branch_group']."') and amount_type='DR' and cancel_status='0'";

   	  }
   	  else
   	  {
   	  	$query_details_filter_opening_dr="Select sum(amount) as total_amount from account_book A 
   	  	inner join account_book_details B on A.id=B.account_bookid
                where B.ledgerid in ($ledger_id) and A.branch in('".$site."') and amount_type='DR' and cancel_status='0' ";
   	  }
  }

  $result_details_filter_opening_dr=DB_query($query_details_filter_opening_dr,$db);
  $rs_details_filter_opening_dr=DB_fetch_array($result_details_filter_opening_dr);

  $total_opening_dr_fdate=$rs_details_filter_opening_dr[0];
  /*---------------------------------------  calculate Opening for particular Ledger Ends Here -------------------- */
  /*---------------------------------------  calculate Opening Credit Amount Starts Here ----------------- */
  if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
  {
	  	if($_SESSION['branch_group']!='0')
	  	{
	  		 $query_details_filter_opening_cr="Select sum(amount) as total_amount from account_book A 
	  		  inner join account_book_details B on A.id=B.account_bookid
                where B.ledgerid in ($ledger_id) and A.Dates<'".$_SESSION['FDATE']."' and A.Dates between '".$_SESSION['FDATE']."' and '".$_SESSION['TDATE']."' and A.branch in('".$_SESSION['branch_group']."') and amount_type='CR' and cancel_status='0'";
	  	}
	  	else
	  	{
	  		$query_details_filter_opening_cr="Select sum(amount) as total_amount from account_book A 
	  		inner join account_book_details B on A.id=B.account_bookid
                where B.ledgerid in ($ledger_id) and A.Dates<'".$_SESSION['FDATE']."' and A.Dates between '".$_SESSION['FDATE']."' and '".$_SESSION['TDATE']."' and amount_type='CR' and cancel_status='0' ";
	  	}
  }
  else
  {
   	  if($_SESSION['branch_group']!='0')
   	  {
   	  	$query_details_filter_opening_cr="Select sum(amount) as total_amount from account_book A 
   	  	inner join account_book_details B on A.id=B.account_bookid
                where B.ledgerid in ($ledger_id) and A.branch in('".$_SESSION['branch_group']."') and amount_type='CR' and cancel_status='0' ";

   	  }
   	  else
   	  {
   	  	$query_details_filter_opening_cr="Select sum(amount) as total_amount from account_book A 
   	  	inner join account_book_details B on A.id=B.account_bookid
                where B.ledgerid in ($ledger_id) and A.branch in('".$_SESSION['branch_group']."') and amount_type='CR' and cancel_status='0' ";
   	  }
	   	
  }

  $result_details_filter_opening_cr=DB_query($query_details_filter_opening_cr,$db);
  $rs_details_filter_opening_cr=DB_fetch_array($result_details_filter_opening_cr);

  $total_opening_cr_fdate=$rs_details_filter_opening_cr[0];

  if($total_opening_dr_fdate>=$total_opening_cr_fdate)
  {
  	$dr_opening_fdate=$total_opening_dr_fdate-$total_opening_cr_fdate;
  }
  else
  {
    $cr_opening_fdate=$total_opening_cr_fdate-$total_opening_dr_fdate;
  }
  /* ----------------------------------- calculate Opening for Credit Ledger Ends Here  -----------------------------------  */ 
  if(!empty($opening_amt[DR]))
  {
  $dramt=array_sum($opening_amt[DR])+$dr_opening_fdate;
   $opening_type='DR';
  }
  if(!empty($opening_amt[CR]))
  {	
  $cramt=array_sum($opening_amt[CR])+$cr_opening_fdate;
   $opening_type='CR';
  }

  	if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
	{$totVoucherDR=Get_AccountByLedger($Ledgers['AccountsID'],'DR',$_SESSION['FDATE'],$_SESSION['TDATE'],$_SESSION['branch_group']);
  	$totVoucherCR=Get_AccountByLedger($Ledgers['AccountsID'],'CR',$_SESSION['FDATE'],$_SESSION['TDATE'],$_SESSION['branch_group']);}
	else{
	$totVoucherDR=Get_AccountByLedger($Ledgers['AccountsID'],'DR','','',$_SESSION['branch_group']);
  	$totVoucherCR=Get_AccountByLedger($Ledgers['AccountsID'],'CR','','',$_SESSION['branch_group']);}

	$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!=''){
	if($ledgertype=='excisetax'){
	    if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
	    {$etaxDetails=yearlyETaxAccountByTransactions($master_tax_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$_SESSION['branch_group']);}
	  else {$etaxDetails=yearlyETaxAccountByTransactions($master_tax_id,'','',$_SESSION['branch_group']);} 
	  $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'],$_SESSION['branch_group']);}else
		{$staxDetails=yearlySTaxAccountByTransactions($master_tax_id,'','',$_SESSION['branch_group']);} $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'],$_SESSION['branch_group']);}else
		{$vendorDetails=yearlyVendorAccountByTransactions($master_party_id,'','',$_SESSION['branch_group']);} $purchCR=$vendorDetails;
	}else{$vendorDetails='';$purchCR='';}
	if($ledgertype=='client')
	{
		if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
		{$clientDetails=yearlyClientAccountByTransactions($master_party_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$_SESSION['branch_group']);}
		else{$clientDetails=yearlyClientAccountByTransactions($master_party_id,'','',$_SESSION['branch_group']);} $invDR=$clientDetails;
	}else{$clientDetails='';$invDR='';}
	if($ledgertype=='freight')
	{
		if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
		{$freightDetails=yearlyFreightAccountByTransactions($ledger_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$_SESSION['branch_group']);}
		else
		{$freightDetails=yearlyFreightAccountByTransactions($ledger_id,'','',$_SESSION['branch_group']);} $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'],$_SESSION['branch_group']);}
		else
		{$surDetails=yearlySurchargeAccountByTransactions($ledger_id,'','',$_SESSION['branch_group']);} 
          $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'],$_SESSION['branch_group']);}
		else
		{$packDetails=yearlyPkgChargesAccountByTransactions($ledger_id,'','',$_SESSION['branch_group']);} 
        $packDR=$packDetails[0]; $packDRArr[]=$packDR; $packCR=$packDetails[1]; $packCRArr[]=$packCR; 
	$packDR=$packDetails;
	}else{$packDetails='';$packDR='';$packCR='';}
	if($ledgertype=='addcharge')
	{
		if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
		{$addDetails=yearlyAddChargesAccountByTransactions($ledger_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$_SESSION['branch_group']);}
		else
		{$addDetails=yearlyAddChargesAccountByTransactions($ledger_id,'','',$_SESSION['branch_group']);} 
       $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'],$_SESSION['branch_group']);}
		else
		{$cessDetails=yearlyCessAccountByTransactions($master_tax_id,'','',$_SESSION['branch_group']);}  
	
		$cessDR=$cessDetails[0]; $cessCR=$cessDetails[1];  
	  
	}else{$cessDetails='';$cessDR='';$cessCR='';}

	     if($ledgertype=='insurance')		
			{
			if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
			{$insurance=yearlyinsuranceAccountByTransactions($master_tax_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$_SESSION['branch_group']);}
		    else{ $insurance=yearlyinsuranceAccountByTransactions($master_tax_id,'','',$_SESSION['branch_group']);}
			$insuranceDR=$insurance[0]; $insuranceCR=$insurance[1]; 
			}else{$insurance='';$insuranceDR='';$insuranceCR='';}
			
	    if($ledgertype=='sales')		
			{
			if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
			{$salesdata=yearlyLedgerPostingByTransactions($ledger_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$_SESSION['branch_group']);}
		    else{ $salesdata=yearlyLedgerPostingByTransactions($ledger_id,'','',$_SESSION['branch_group']);}
			$salesDR=$salesdata[0]; $salesCR=$salesdata[1]; 
			}else{$salesdata='';$salesDR='';$salesCR='';}
			
	if($ledgertype=='round')
	{
		if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
		{$roundDetails=yearlyRoundOffByTransactions($ledger_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$_SESSION['branch_group']);}
		else{$roundDetails=yearlyRoundOffByTransactions($ledger_id,'','',$_SESSION['branch_group']);} 
		$roundDR=$roundDetails[0];
		$roundCR=$roundDetails[1];
	}else{$roundDetails='';$roundDR='';$roundCR='';}	
	$legderDR='';$legderCR='';
	
	}
	else
	{
		if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
		{$ledgerPosting=yearlyLedgerPostingByTransactions($ledger_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$_SESSION['branch_group']);}else{
		$ledgerPosting=yearlyLedgerPostingByTransactions($ledger_id,'','',$_SESSION['branch_group']);} 
		$legderDR=$ledgerPosting[0]; $legderCR=$ledgerPosting[1];
		
		$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='';
		
	}
	if($cramt!='' || $dramt!='' || ($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'];?>" title="<?php echo ucwords($Ledgers['AccountName']);?>"><strong><font color="#000000"><?php echo $Ledgers['AccountName']; //echo "----".$openingCR."-------".$openingDR;//print_r($ledgerPosting);?></font></strong></a></td>
 <?php 
  $openingCR_array[]=$cramt; $OPCR=$cramt; $openingDR_array[]=$dramt; $OPDR=$dramt; ?>
  <td style="text-align:right;"><?php echo $OPDR;?></td>
  <td style="text-align:right;"><?php echo $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;
 	$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 style="text-align:right;"><?php echo $closing."&nbsp;".$type;?></td>
  </tr>
  <?php 
  $xcount++;
}

}

  }?>
 <tr>
 	<td></td>
    <td style="text-align:right"><strong>Total</strong></td>
	<?php 
	 if($openingDR_array!='')$opDR=array_sum($openingDR_array);
	 if($openingCR_array!='')$opCR=array_sum($openingCR_array);
	 if($opDR>$opCR){$tot_op_DR=$opDR-$opCR;}else{$tot_op_CR=$opCR-$opDR;}
	?>
	<td style="text-align:right"><strong><?php echo $opDR;?></strong></td>
	<td style="text-align:right"><strong><?php echo $opCR;?></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>
	<?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 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='';

	//echo $opening_bal..
	$Ledgerstype=$s->getField('accountledger','AccountsID',$lid,'ledgertype');

	if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
	{
		if($_SESSION['branch_group']!='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 ($lid) and B.openingdate between '".$_SESSION['FDATE']."' and '".$_SESSION['TDATE']."' and B.site in('".$_SESSION['branch_group']."')";
		 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 ($lid) and B.openingdate between '".$_SESSION['FDATE']."' and '".$_SESSION['TDATE']."'";
	   
	//$opening_bal=$s->GetFieldValue('accountledger',"AccountsID=$ledger_id and openingdate<='".$_SESSION['TDATE']."'",'openingBalance');
     }
	else
	{
	    if($_SESSION['branch_group']!='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 ($lid) and B.site in('".$_SESSION['branch_group']."') ";
	     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 ($lid) ";
	   

	//$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');?> ( <?php echo $period;?> )</h3>
<!--<p align="right"><a href="javascript:history.go(-1);"><strong><font color="#000000">Back</font></strong></a></p>-->
 <p align="right">
  <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>
<!-- <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];}
		$flag='';
		$amounts=getMonthsBalanceSheet($lid,$key,$yr,'',$_SESSION['branch_group']);

		$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;?>&site=<?php echo  $_SESSION['branch_group'];?>',1000,500)"><strong><font color="#000000"><?php echo $val;//."---".$yr,"----".$flag;?></font></strong></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; $type="DR";}
		else
		if($totalCR>$totalDR){$closing=$totalCR-$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"><?php //if($DR>$CR){echo $DR-$CR; echo " DR";}elseif($CR>$DR){echo $CR-$DR; echo " CR";}?></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=$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 
}
?> 
