<?php 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;
}
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']=='')
{	
	$getPLOpening=DB_fetch_array(DB_query("Select openingtype,openingBalance from accountledger where AccountName='Proffit And Loss A/C'",$db));
	
	
	
?>
<h3></h3>
<form action="" method="post">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr class="row1">
<td width="13%" align="right">Branch</td>
<td><select name="site" id="site">
     <option value="0">Select</option>
     <?php 
        $sql_loc="select loccode,locationname from locations where CompanyID='".$_SESSION['ACGCompanyId']."'";
        $result_loc=DB_query($sql_loc,$db);
        while($rs_loc=DB_fetch_array($result_loc))
        {
     ?>
     <option value="<?php echo $rs_loc['loccode']; ?>" <?php if($_REQUEST['site']==$rs_loc['loccode']) {?> selected="selected" <?php } ?>><?php echo $rs_loc['locationname']; ?></option>

     <?php } ?>
     </select>
</td>
<td width="13%" align="right">From Date</td>
<td width="20%"><input type="text" name="from_date" readonly value="<?php if(isset($_SESSION['FDATE'])) { echo ConvertSQLDate($_SESSION['FDATE']); } else { echo ConvertSQLDate($_SESSION['financialYrStart']); }?>" class="datepicker"  /></td>
<td width="13%" align="right">To Date</td>
<td width="20%"><input type="text" name="to_date" readonly value="<?php if(isset($_SESSION['TDATE'])) { echo ConvertSQLDate($_SESSION['TDATE']); } else { 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" onclick="return validation();" /> <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-BalanceSheet-print.php" target="_blank" class="search">Print</a> <a href="APRGroupWise-BalanceSheet_Exl.php" class="search">Export</a></span></p>


<table width="100%" cellpadding="3" cellspacing="0" border="0">
  <tr>
    <td width="50%" valign="top">
    <h3>LIABILITIES</h3>
	<table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
  <?php 
 	  $table='accountgroupmaster';
	  $getLiabilityId=$s->getField($table,'GroupName','LIABILITIES','GroupId');
	  $getUnderGroups=DB_query("select * from $table where NatureOfGroup=$getLiabilityId and MainGroupId=0",$db);
  
    if($_REQUEST['site']=='0')
    {
      $location_fetch=DB_query("select loccode from locations where CompanyID='".$_SESSION['ACGCompanyId']."'",$db);
      while($loc1=DB_fetch_array($location_fetch))
      {
        $siteactive[]=$loc1['loccode'];
      }
     }
     else
     {
      $siteactive[]=$_REQUEST['site'];
     } 
    //$siteactive=explode("','",$_SESSION['AssignedBranch']);
 
    for($t=0;$t<count($siteactive);$t++)
    {
      $activebranch=$s->getField('locations','loccode',$siteactive[$t],'active'); 
      if($activebranch=='Y')
       $active_branch[]=$siteactive[$t]; 
    }
      if(count($active_branch)>0)
      $site=implode("','",$active_branch);
      else
      $site=0;
   // echo $site;
		//$site=
    $_SESSION['site_balance']=$site;
		while($underGroups=DB_fetch_array($getUnderGroups))
		{

			if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
			{$x=getGroupBalanceSheet($underGroups['GroupId'],$_SESSION['FDATE'],$_SESSION['TDATE'],$site);}
			else{$x=getGroupBalanceSheet($underGroups['GroupId'],'','',$site);}
			
			//echo "--".$underGroups['GroupId'];
		//	echo "<pre>";
		//	print_r($x);
			
			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%"><a href="index.php?route=<?php echo $_REQUEST['route'];?>&<?php echo $url;?>&gid=<?php echo $underGroups['GroupId'];?>&site=<?php echo $_REQUEST['site'];?>" title="<?php echo ucwords($underGroups['GroupName']);?>"><strong><font color="#000000"><?php echo ucwords($underGroups['GroupName']);?></font></strong></a></td>
			  <td style="text-align:right;"><strong>
			  <?php 
			  
			  //echo $x[2]; echo "---"; echo $x[3]; echo "<br />";
			 
			 if($x[2]>$x[3]){$LIABOPDR=$x[2]-$x[3];$LIABOPCR=''; $LIABOPDRARR[]=$LIABOPDR;}elseif($x[3]>$x[2]){$LIABOPCR=$x[3]-$x[2];$LIABOPDR='';$LIABOPCRARR[]=$LIABOPCR;}
			//echo $LIABOPDR; echo "---"; echo $LIABOPCR; echo "<br />"; 
			  
			  if(($x[0]+$x[2])>($x[1]+$x[3])){echo $LIABDR=($x[0]+$x[2])-($x[1]+$x[3]); echo " DR"; $LIABDR_ARR[]=$LIABDR;}
					elseif(($x[1]+$x[3])>($x[0]+$x[2])){echo $LIABCR=($x[1]+$x[3])-($x[0]+$x[2]); echo " CR"; $LIABCR_ARR[]=$LIABCR;}
					?></strong></td>
			  </tr>
	<?php 
		}
		} ?>
  </table></td>
  <td valign="top">
  <h3>ASSETS</h3>
  <table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
  <?php 
	  $getLiabilityId=$s->getField($table,'GroupName','ASSET','GroupId');
	  $getUnderGroups=DB_query("select * from $table where NatureOfGroup=$getLiabilityId and MainGroupId=0",$db);
		while($underGroups=DB_fetch_array($getUnderGroups))
		{
			if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
			{$y=getGroupBalanceSheet($underGroups['GroupId'],$_SESSION['FDATE'],$_SESSION['TDATE'],$site);}
			else{
			$y=getGroupBalanceSheet($underGroups['GroupId'],'','',$site);}
			//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%"><a href="index.php?route=<?php echo $_REQUEST['route'];?>&<?php echo $url;?>&gid=<?php echo $underGroups['GroupId'];?>&site=<?php echo $_REQUEST['site']; ?>" title="<?php echo ucwords($underGroups['GroupName']);?>"><strong><font color="#000000"><?php echo ucwords($underGroups['GroupName']);?></font></strong></a></td>
			  <td style="text-align:right;"> 
			 <strong> <?php 
			 
			 //echo $y[2]; echo "---"; echo $y[3]; echo "<br />";
			 
			 if($y[2]>$y[3]){$ASSOPDR=$y[2]-$y[3];$ASSOPCR=''; $ASSOPDRARR[]=$ASSOPDR;}elseif($y[3]>$y[2]){$ASSOPCR=$y[3]-$y[2];$ASSOPDR='';$ASSOPCRARR[]=$ASSOPCR;}
			//echo $OPDR; echo "---"; echo $OPCR; echo "<br />";
			 
			 if(($y[0]+$y[2])>($y[1]+$y[3])){echo $ASSDR=($y[0]+$y[2])-($y[1]+$y[3]); echo " DR"; $ASSDR_ARR[]=$ASSDR;}
					elseif(($y[1]+$y[3])>($y[0]+$y[2])){echo $ASSCR=($y[1]+$y[3])-($y[0]+$y[2]); echo " CR"; $ASSCR_ARR[]=$ASSCR;}
					?></strong></td>
			  </tr>
	<?php 
		}} ?>
  </table></td>
  </tr>
  <?php 
  if($LIABOPCRARR!=''){$a1=array_sum($LIABOPCRARR);}else{$a1=0;}
  if($LIABOPDRARR!=''){$b1=array_sum($LIABOPDRARR);}else{$b1=0;}
	$LIABOPBAL=($a1)-($b1);
  $LIABOPBAL=round($LIABOPBAL,2);
  
  if($ASSOPCRARR!=''){$a2=array_sum($ASSOPCRARR);}else{$a1=0;}
  if($ASSOPDRARR!=''){$b2=array_sum($ASSOPDRARR);}else{$b1=0;}
	$ASSETOPBAL=($b2)-($a2);
  $ASSETOPBAL=round($ASSETOPBAL,2);
  
  if($ASSETOPBAL>$LIABOPBAL){$diffDR=$ASSETOPBAL-$LIABOPBAL;}
  elseif($LIABOPBAL>$ASSETOPBAL){$diffCR=$LIABOPBAL-$ASSETOPBAL;}
  ?>
  <tr>
  <td colspan="2">
  <table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
  <tr>
    <td width="25%"><strong><?php if($diffDR!=''){?>Difference in opening balances<?php } else echo "&nbsp;";?></strong></td>
    <td width="25%" style="text-align:right"><strong><?php if($diffDR!=''){echo $diffDR;}else echo "&nbsp;"; ?></strong></td>
    <td width="25%"><strong><?php if($diffCR!=''){?>Difference in opening balances<?php } else echo "&nbsp;";?></strong></td>
    <td style="text-align:right"><strong><?php if($diffCR!=''){echo $diffCR;}else echo "&nbsp;"; ?></strong></td>
  </tr>
  </table>
  </td>
  </tr>
  <?php 
  	$qqq=proffitAndLoss($site);
	if($qqq[0]!=''){$profit=$qqq[0];$ptype=$qqq[1];}
	elseif($qqq[2]!=''){$loss=$qqq[2];$ltype=$qqq[3];}
  ?>
  <tr>
  <td colspan="2">
  <table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
  <tr>
    <td width="25%"><strong><?php if($profit!=''){?>Profit<?php } else echo "&nbsp;";?></strong></td>
    <td width="25%" style="text-align:right;"><strong><?php if($profit!=''){echo number_format($profit)."&nbsp;".$ptype;}else echo "&nbsp;"; ?></strong></td>
    <td width="25%"><strong><?php if($loss!=''){?>Loss<?php } else echo "&nbsp;";?></strong></td>
    <td style="text-align:right;"><strong><?php if($loss!=''){echo number_format($loss)."&nbsp;".$ltype;}else echo "&nbsp;"; ?></strong></td>
  </tr>
  </table>
  </td>
  </tr>  
  <tr>
  <td colspan="2">
  <table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
  <tr>
  <td width="25%"><strong>Total</strong></td>
  <td width="25%" style="text-align:right"><strong><?php 
  
  if($LIABDR_ARR!=''){$sum1=array_sum($LIABDR_ARR);}
  if($LIABCR_ARR!=''){$sum2=array_sum($LIABCR_ARR);}
  if($sum1>$sum2)
  {$final_value1=($diffDR+$sum1-$sum2); $final_type1="DR";}
  else
  {$final_value1=($diffDR+$sum2-$sum1); $final_type1="CR";}
  
  if($profit!='')
  {
  	if($ptype==$final_type1)
  	{$balsheetliabvalue=$final_value1+$profit; echo $balsheetliabvalue."&nbsp;".$final_type1;}
  	elseif($ltype!=$final_type1)
  	{
			
  		if($final_value1>$profit){
		echo $final_value1-$profit."&nbsp;".$final_type1;}
		elseif($profit>$final_value1){
		echo $profit-$final_value1."&nbsp;".$ltype;}
  	}
  }
  else
  {
  	echo number_format($final_value1)."&nbsp;".$final_type1;
  }
  
  ?></strong></td>
   <td width="25%"><strong>Total</strong></td>
  <td style="text-align:right"><strong>
  <?php
   
  if($ASSDR_ARR!=''){$sumA1=array_sum($ASSDR_ARR);}
  if($ASSCR_ARR!=''){$sumA2=array_sum($ASSCR_ARR);}
  if($sumA1>$sumA2)
  {$final_value2=($diffCR+$sumA1-$sumA2); $final_type2="DR";}
  else
  {$final_value2=($diffCR+$sumA2-$sumA1); $final_type2="CR";}

  if($loss!='')
  {
  	if($ltype==$final_type2)
  	{$balsheetassetvalue=$final_value2+$loss; echo number_format($balsheetassetvalue)."&nbsp;".$final_type2;}
  	elseif($ltype!=$final_type2)
  	{
  		if($final_value2>$loss){
		echo number_format($final_value2-$loss)."&nbsp;".$final_type2;}
		elseif($loss>$final_value2){
		echo number_format($loss-$final_value2)."&nbsp;".$ltype;}
  	}
  }
  else
  {
  	echo number_format($final_value2)."&nbsp;".$final_type2;
  }
  ?></strong></td>
  </tr>
  </table>
  </td>
  </tr>
</table>
<?php 
}
if($_REQUEST['gid']!='')
{
	 $gid=$_REQUEST['gid'];
	$_SESSION['gid']=$gid;
  $site=$_SESSION['site_balance'];
?>
<br>
<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="center"><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-BalanceSheetliabilities-print.php" target="_blank" class="search">Print</a>&nbsp;/&nbsp;<a href="APRGroupWise-BalanceSheetliabilities_Exl.php" class="search">Export</a></span>
</p>
<table width="100%" border="0" cellpadding="3" cellspacing="0" class="thborder">
  <tr>
    <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 
  $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'],$site);}
		else
		{$res=getGroupBalanceSheet($subGroups['GroupId'],'','',$site);}
		//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'];?>&site=<?php echo $_REQUEST['site']; ?>" 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 style="text-align:right;"><strong><?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='';}
	 ?></strong></td>
  <td style="text-align:right;"><strong><?php if($TD==0){echo "";}else echo $TD; $TDA[]=$TD;?></strong></td>
  <td style="text-align:right;"><strong><?php if($TC==0){echo "";}else echo $TC; $TCA[]=$TC;?></strong></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; padding-right:5px"><strong><?php echo $closing."&nbsp;".$type; //echo "<br />".$Gtotal_DR."<br />".$Gtotal_CR; echo "<br />------------<br />".$TOTALOPDR."----".$TD; echo "<br />------------<br />".$TOTALOPCR."----".$TC;?></strong></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='';
	
	if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
	  {
      //$opening_bal=$s->GetFieldValue('accountledger',"AccountsID=$ledger_id and openingdate<='".$_SESSION['TDATE']."'",'openingBalance');
             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']."' ";

              }
    }
	   else
      {
       // $opening_bal=$s->getField('accountledger','AccountsID',$ledger_id,'openingBalance');
          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_type=$s->getField('accountledger','AccountsID',$ledger_id,'openingtype');

      $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'],$site);
  	     $totVoucherCR=Get_AccountByLedger($Ledgers['AccountsID'],'CR',$_SESSION['FDATE'],$_SESSION['TDATE'],$site);}
	else{
	     $totVoucherDR=Get_AccountByLedger($Ledgers['AccountsID'],'DR','','',$site);
  	   $totVoucherCR=Get_AccountByLedger($Ledgers['AccountsID'],'CR','','',$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'],$site);}else
          		{$etaxDetails=yearlyETaxAccountByTransactions($master_tax_id,'','',$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'],$site);}
               else {$staxDetails=yearlySTaxAccountByTransactions($master_tax_id,'','',$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'],$site);}
            else {$vendorDetails=yearlyVendorAccountByTransactions($master_party_id,'','',$site);}
            $purchCR=$vendorDetails;
         }else{$vendorDetails='';$purchCR='';}

      	 if($ledgertype=='client')	{
        		 if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
        		 {$clientDetails=yearlyClientAccountByTransactions($master_party_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$site);}
        		 else{$clientDetails=yearlyClientAccountByTransactions($master_party_id,'','',$site);} 
             $invDR=$clientDetails;
      	 }else{$clientDetails='';$invDR='';}

      	if($ledgertype=='freight'){
        		if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
        		{$freightDetails=yearlyFreightAccountByTransactions($ledger_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$site);}
        		else{$freightDetails=yearlyFreightAccountByTransactions($ledger_id,'','',$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'],$site);}
      		else {$surDetails=yearlySurchargeAccountByTransactions($ledger_id,'','',$site);} 
          $surDR=$surDetails[0];  $surCR=$surDetails[1]; 
      	}else{$surDetails='';$surDR='';$surCR='';}

      	if($ledgertype=='pckcharge'){
      		if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
      		{$packDetails=yearlyPkgChargesAccountByTransactions($ledger_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$site);}
      		else
      		{$packDetails=yearlyPkgChargesAccountByTransactions($ledger_id,'','',$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'],$site);}
      		else{$addDetails=yearlyAddChargesAccountByTransactions($ledger_id,'','',$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'],$site);}
      		else{$cessDetails=yearlyCessAccountByTransactions($master_tax_id,'','',$site);} 
          $cessDR=$cessDetails[0]; $cessCR=$cessDetails[1]; 
      	}else{$cessDetails='';$cessDR='';$cessC='';}

        if($ledgertype=='insurance') {
            if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
            {$insurance=yearlyinsuranceAccountByTransactions($master_tax_id,$_SESSION['FDATE'],$_SESSION['TDATE'],$site);}
              else{ $insurance=yearlyinsuranceAccountByTransactions($master_tax_id,'','',$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'],$site); }
          else {  $roundDetails=yearlyRoundOffByTransactions($ledger_id,'','',$site);} 
          $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'],$site);}
    else{	$ledgerPosting=yearlyLedgerPostingByTransactions($ledger_id,'','',$site);} 
		$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='';
	}
   
    $ledger_dr_tot=get_opening_filter_basis($ledger_id,$site,$_SESSION['FDATE'],'DR');
    $ledger_cr_tot=get_opening_filter_basis($ledger_id,$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;
    }
    
   // echo $opening_bal."##".$totVoucherDR."##".$totVoucherCR;


	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)!='')
  {

//echo $ledger_id."<br>";
  ?>
  <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'];?>&site=<?php echo $_REQUEST['site']; ?>" title="<?php echo ucwords($Ledgers['AccountName']);?>"><strong><font color="#000000"><?php echo $Ledgers['AccountName']; //echo "----".$openingCR."-------".$openingDR;//print_r($ledgerPosting);
 
  ?></font></strong></a></td>  
  <td style="text-align:right;"><strong><?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='';}?></strong></td>
  
  <td style="text-align:right;"><strong><?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;?></strong></td>
  <td style="text-align:right;"><strong><?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;?></strong></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;"><strong><?php echo $closing."&nbsp;".$type;?></strong></td>
  </tr><?php }} ?>
 <tr>
    <th width="60%">Total</th>	
	 <th width="10%"><?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";}	 
	 ?></th>	
    <th width="10%"><?php 
	if($TDA!=''){$xx=array_sum($TDA);}else{$xx=0;}
	if($ARRDR!=''){$yy=array_sum($ARRDR);}else{$yy=0;}
	echo $xx+$yy;
	?></th>
	<th width="10%"><?php 
	if($TCA!=''){$mm=array_sum($TCA);}else{$mm=0;}
	if($ARRCR!=''){$nn=array_sum($ARRCR);}else{$nn=0;}
	echo $mm+$nn;?></th>
	<?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="";}
	?>
	<th width="10%"><?php echo $closing."&nbsp;".$type;?></th>
  </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;
  $site=$_SESSION['site_balance'];
	//echo $_SESSION['financialYrStart'];
	//echo $_SESSION['financialYrEnd'];
  $opening_amt='';
  $opening_bal='';
  $opening_type='';
  $dramt='';
  $cramt='';
  $ledger_id=$_REQUEST['lid'];
	
	

  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');
    }
//echo $query_details;
    $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>

<!-- <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-BalanceSheet-Lid-Print.php" target="_blank">Print</a>&nbsp;/&nbsp;<a href="APRGroupWise-BalanceSheet-Lid-Exl.php">Export</a></span>
</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='';
	$amounts=getMonthsBalanceSheet($lid,$key,$yr,'',$site);

		$totalDR=$amounts[0];
		$totalCR=$amounts[1];
	?>
		<tr bgcolor="<?php //echo $color;?>" onMouseOver="this.style.backgroundColor='#fffeb3'; this.style.color='#00f';" onMouseOut="this.style.backgroundColor='<?php echo $color;?>'; this.style.color='#000000';">
		<td><a href="#" onclick="popup('bal_sheet_popup_branch.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; //echo "---".$month_num;//."---".$yr,"----".$flag;?></font></strong></a></td>
		<td style="text-align:right;"><strong><?php if($totalDR==0){echo "";}else{echo $totalDR;} $arrayDR[]=$totalDR;?></strong></td>
		<td style="text-align:right;"><strong><?php if($totalCR==0){echo "";}else{echo $totalCR;} $arrayCR[]=$totalCR;?></strong></td>
		<td style="text-align:right;"><strong><?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;
		?></strong></td>
		</tr>
	<?php 	
	}
?>
<tr>
<th><strong>Opening</strong></th>
<th width="20%"><strong><?php echo $openingDR;?></strong></th>
<th width="20%"><strong><?php echo $openingCR;?></strong></th>
<th width="20%">&nbsp;</th>
</tr>
<tr>
<th><strong>Total</strong></th>
<th width="20%"><strong><?php echo $DR=array_sum($arrayDR);?></strong></th>
<th width="20%"><strong><?php echo $CR=array_sum($arrayCR);?></strong></th>
<th width="20%"><?php //if($DR>$CR){echo $DR-$CR; echo " DR";}elseif($CR>$DR){echo $CR-$DR; echo " CR";}?></th>
</tr>
<tr>
<th><strong>Total Closing</strong></th>
<?php if($opening_type!='')
   {
           if($opening_type=='DR'){ $DRR=$DR+$openingDR; $CRR=$CR;}elseif($opening_type=='CR'){$CRR=$CR+$openingCR; $DRR=$DR;}}
      else {$DRR=$DR; $CRR=$CR;}

if($DRR>$CRR){$closeDR=$DRR-$CRR;}elseif($CRR>$DRR){$closeCR=$CRR-$DRR;}
?>
<th width="20%"><strong><?php echo $closeDR;?></strong></th>
<th width="20%"><strong><?php echo $closeCR;?></strong></th>
<th width="20%"></th>
</tr>
</table><?php } ?>
<script type="text/javascript">
  function validation()
  {
    if(document.getElementById("site").value=='0')
    {
      alert("Please Select Branch");
      return false;
    }
  }
</script>