<?php include('fun-Trial.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']);
}
?>
<script>
function chkdteformat(obj){
var pattern =/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/;
  var input=$(obj).val();
  if(!pattern.test(input))
  {
      alert("Date should be in dd/mm/yyyy Format");
      $(obj).val("");
  }
}

</script>
<form name="form1" method="post" action=""> 

<table width="100%" border="0" cellpadding="3" cellspacing="0">
  <tr class="row1">
    <td width="12%" align="right">Group</td>
	<td width="20%">
	<?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>
	 <td width="12%" align="right">From Date</td>
    <td width="20%"><input type="text"  name="from_date" value="<?php if(isset($_SESSION['FDATE']))echo ConvertSQLDate($_SESSION['FDATE']);?>" class="datepicker" placeholder="dd/mm/yyyy" onchange="chkdteformat(this)" /></td>
<td width="12%" align="right">To Date</td>
<td><input type="text"  name="to_date" value="<?php if(isset($_SESSION['TDATE']))echo ConvertSQLDate($_SESSION['TDATE']);?>" class="datepicker" placeholder="dd/mm/yyyy" onchange="chkdteformat(this)" /></td>
</tr>
<tr class="row1">
    <td colspan="6" align="center" valign="middle">
      <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'])
  {
	 if($_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	 {
	 	$from_date=FormatDateForSQL($_REQUEST['from_date']);
		$to_date=FormatDateForSQL($_REQUEST['to_date']);
	 }
	 
	 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')?></h3>
<!-- <h2><?php //echo $period;?></h2> -->
<!--<p align="right"><a href="index.php?route=<?php //echo $_REQUEST['route'];?>&<?php //echo $url;?>"><font color="#000000">Back</font></a></p>
--><!--<p align="right"><a href="javascript:history.go(-1);"><font color="#000000">Back</font></a></p>-->
 <p align="right">
 <span style="float:left;"><?php echo $period;?></span>
  <a href="APRGroupWise-TrialBalance_Libilities-print.php" target="_blank" class="search">Print</a> <a href="GroupSummary_exl.php" class="search">Export</a>
</p>
<table width="100%" border="0" cellpadding="3" cellspacing="0" class="thborder">
<tr>
    <th>Particulars</th>
	<th width="10%" style="text-align:center;">Opening DR</th>
	<th width="10%" style="text-align:center;">Opening CR</th>
    <th width="10%" style="text-align:center;">Debit</th>
	<th width="10%" style="text-align:center;">Credit</th>
	<th width="10%" style="text-align:center;">Closing</th>
  </tr>
  <?php 
  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']);}
		else
		{
		$res=getGroupBalanceSheet($subGroups['GroupId'],'','');}
		//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><a href="index.php?route=<?php echo $_REQUEST['route'];?>&<?php echo $url;?>&gid=<?php echo $subGroups['GroupId'];?>" title="<?php echo ucwords($subGroups['GroupName']);?>"><?php echo $subGroups['GroupName']; ?></a>
  </td>
  <?php 
	 $DROPEN=$res[2];
	 $CROPEN=$res[3];
	 $TOTALOPDR=$DROPEN; 
	 $TOTALOPCR=$CROPEN; 
	 ?>
  <td align="right"><?php echo $openingDR_array[]=$TOTALOPDR;?></td>
  <td align="right"><?php echo $openingCR_array[]=$TOTALOPCR;?></td>
  <td align="right"><?php if($TD==0){echo "";}else echo $TD; $TDA[]=$TD;?></td>
  <td align="right"><?php if($TC==0){echo "";}else echo $TC; $TCA[]=$TC;?></td>
 <?php 
   $Gtotal_DR=$TOTALOPDR+$TD;
   $Gtotal_CR=$TOTALOPCR+$TC;
   if($Gtotal_DR>$Gtotal_CR){$closing=$Gtotal_DR-$Gtotal_CR; $type="DR";}
   elseif($Gtotal_CR>$Gtotal_DR){$closing=$Gtotal_CR-$Gtotal_DR; $type="CR";}else{$closing='';$type="";} 
 ?>
  <td align="right"><?php echo $closing."&nbsp;".$type; ?></td>
  </tr><?php } } ?>
 
  <?php 
 if($Gtype=='G'){
 $getLedgers=DB_query("Select * from accountledger where UnderGroup=$gid order by AccountName ASC",$db);
  while($Ledgers=DB_fetch_array($getLedgers))
  {
  	$ledger_id=$Ledgers['AccountsID'];	
	$OPCR='';
	$OPDR='';
	$dramt='';
	$cramt='';
    $opening_amt='';
    $closing='';
    $total_DR='';
    $total_CR='';
	//echo $opening_bal..
	if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
	{		
		$query_details="Select (B.openingBalance) as openingBalance,B.openingtype as openingtype from accountledger A inner join accountledger_dtls B on A.AccountsID=B.accountledger_id
		 where A.AccountsID in ($ledger_id) and B.openingdate<='".$_SESSION['TDATE']."'";
	    
	//$opening_bal=$s->GetFieldValue('accountledger',"AccountsID=$ledger_id and openingdate<='".$_SESSION['TDATE']."'",'openingBalance');
     }
	else
	{	   
		$query_details="Select (B.openingBalance) as openingBalance,B.openingtype as openingtype from accountledger A inner join accountledger_dtls B on A.AccountsID=B.accountledger_id
		 where A.AccountsID in ($ledger_id) ";	  
   }	 
   
    $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]);
	           $opening_type='DR';
	          }
	          if(!empty($opening_amt[CR]))
	          {	
	          $cramt=array_sum($opening_amt[CR]);
	          $opening_type='CR';
	          }

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

	if($cramt!='' || $dramt!='' || ($totVoucherDR)!='' || ($totVoucherCR)!=''){
  ?>
  <tr onMouseOver="this.style.backgroundColor='#B7CFD9'; this.style.color='#00f';" onMouseOut="this.style.backgroundColor='#FFF'; this.style.color='#000000';">
  <td><a href="index.php?route=<?php echo $_REQUEST['route'];?>&<?php echo $url;?>&lid=<?php echo $Ledgers['AccountsID'];?>" title="<?php echo ucwords($Ledgers['AccountName']);?>"><font color="#000000"><?php echo $Ledgers['AccountName']; //echo "----".$openingCR."-------".$openingDR;//print_r($ledgerPosting);?></font></a></td>
 <?php 
  $openingCR_array[]=$cramt; $OPCR=$cramt; $openingDR_array[]=$dramt; $OPDR=$dramt; ?>
  <td align="right"><?php echo $OPDR;?></td>
  <td align="right"><?php echo $OPCR;?></td>  
  <td align="right"><?php  $total_DR=$totVoucherDR; if($total_DR==0)echo " "; else echo $total_DR; $ARRDR[]=$total_DR;?></td>
  <td align="right"><?php $total_CR=$totVoucherCR; if($total_CR==0)echo " "; else echo $total_CR; $ARRCR[]=$total_CR;?></td>
  <?php 
	$Gtotal_DR1=$OPDR+$total_DR;
 	$Gtotal_CR1=$OPCR+$total_CR;
 	if($Gtotal_DR1>$Gtotal_CR1){$closing=$Gtotal_DR1-$Gtotal_CR1; $type="DR"; }
 	elseif($Gtotal_CR1>$Gtotal_DR1){$closing=$Gtotal_CR1-$Gtotal_DR1; $type="CR";}else{$closing='';$type="";}
	?>
  <td align="right"><?php echo $closing."&nbsp;".$type;?></td>
  </tr><?php }} }?>
 <tr>
    <td align="right"><strong>Total</strong></td>
	<?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 align="right"><strong><?php echo $opDR;?></strong></td>
	<td align="right"><strong><?php echo $opCR;?></strong></td>	
    <td align="right"><strong><?php 
	if($TDA!=''){$xx=array_sum($TDA);}else{$xx=0;}
	if($ARRDR!=''){$yy=array_sum($ARRDR);}else{$yy=0;}
	echo $xx+$yy;
	?></strong></td>
	<td align="right"><strong><?php 
	if($TCA!=''){$mm=array_sum($TCA);}else{$mm=0;}
	if($ARRCR!=''){$nn=array_sum($ARRCR);}else{$nn=0;}
	echo $mm+$nn;?></strong></td>
	<?php 
	$FinalDR=$tot_op_DR+$xx+$yy;
	$FinalCR=$tot_op_CR+$mm+$nn;
	if($FinalDR>$FinalCR){$closing=$FinalDR-$FinalCR; $type="DR"; }
 	elseif($FinalCR>$FinalDR){$closing=$FinalCR-$FinalDR; $type="CR";}else{$closing='';$type="";}
	?>
	<td align="right"><strong><?php echo $closing."&nbsp;".$type;?></strong></td>
  </tr>
</table>	
<?php 	
}
if($_REQUEST['lid']!='')
{

	$month_array=array('04'=>'April','05'=>'May','06'=>'June','07'=>'July','08'=>'August','09'=>'September','10'=>'October','11'=>'November','12'=>'December','01'=>'January','02'=>'February','03'=>'March');
	$lid=$_REQUEST['lid'];
	$_SESSION['lid']=$lid;
	//echo $_SESSION['financialYrStart'];
	//echo $_SESSION['financialYrEnd'];
	
	$opening_amt='';

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

	//echo $opening_bal..
	$Ledgerstype=$s->getField('accountledger','AccountsID',$lid,'ledgertype');
	if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
	{
		
		$query_details="Select (B.openingBalance) as openingBalance,B.openingtype as openingtype from accountledger A inner join accountledger_dtls B on A.AccountsID=B.accountledger_id
		 where A.AccountsID in ($lid) and B.openingdate<='".$_SESSION['TDATE']."'";
	   
	//$opening_bal=$s->GetFieldValue('accountledger',"AccountsID=$ledger_id and openingdate<='".$_SESSION['TDATE']."'",'openingBalance');
     }
	else
	{
	 
		$query_details="Select (B.openingBalance) as openingBalance,B.openingtype as openingtype from accountledger A inner join accountledger_dtls B on A.AccountsID=B.accountledger_id
		 where A.AccountsID in ($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');?></h3>
<!-- <h2><?php //echo $period;?></h2> -->
<!--<p align="right"><a href="javascript:history.go(-1);"><font color="#000000">Back</font></a></p>-->
 <p align="right">
 <span style="float:left;"><?php echo $period;?></span>
  <a href="APRGroupWise-TrialBalanceSheet-Lid-Print.php" target="_blank" class="search">Print</a> <a href="APRGroupWise-TrialBalanceSheet-Lid-Exl.php" class="search">Export</a>
</p>
<table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
<tr>
	<th>Months</th>
	<th width="12%" style="text-align:center;">Debit</th>
	<th width="12%" style="text-align:center;">Credit</th>
	<th width="12%" style="text-align:center;">Closing</th>
</tr>
<?php 
	foreach($month_array as $key=>$val)
	{
		$a=explode('-',$_SESSION['financialYrStart']);
		$b=explode('-',$_SESSION['financialYrEnd']);
		if($key>='04' && $key<='12'){$yr=$a[0];}elseif($key>='01' && $key<'03'){$yr=$b[0];}
		$flag='';
		if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
		{
			$FDATEBRK=explode('/',$_SESSION['FDATE']);
			$TDATEBRK=explode('/',$_SESSION['TDATE']);
			$fmnth=$FDATEBRK[1];
			$tmnth=$TDATEBRK[1];
			$flag=0;
			if($fmnth==$tmnth)
			{
				if($key==$fmnth){$flag=1;}
			}
			elseif($fmnth!=$tmnth)
			{
				if($fmnth==$key){$flag=2;}else
				if($tmnth==$key){$flag=3;}
			}
		$amounts=getMonthsBalanceSheet($lid,$key,$yr,$flag);
		}
		else{$amounts=getMonthsBalanceSheet($lid,$key,$yr,'');}
		$totalDR=$amounts[0];
		$totalCR=$amounts[1];
	?>
		<tr onMouseOver="this.style.backgroundColor='#fffeb3'; this.style.color='#00f';" onMouseOut="this.style.backgroundColor='#FFF'; this.style.color='#000000';">
		<td><a href="#" onclick="popup('bal_sheet_popup.php?q=<?php echo $lid;?>&mnth=<?php echo $key;?>&yr=<?php echo $yr;?>',1000,500)"><font color="#000000"><?php echo $val;//."---".$yr,"----".$flag;?></font></a></td>
		<td align="right"><?php if($totalDR==0){echo "";}else{echo $totalDR;} $arrayDR[]=$totalDR;?></td>
		<td align="right"><?php if($totalCR==0){echo "";}else{echo $totalCR;} $arrayCR[]=$totalCR;?></td>
		<td align="right"><?php 
		if($totalDR==0 && $totalCR==0){$closing=''; $type='';}else
		if($totalDR>$totalCR){$closing=$totalDR-$totalCR; $type="DR";}
		else
		if($totalCR>$totalDR){$closing=$totalCR-$totalDR; $type="CR";}
		echo $closing." ".$type;
		?></td>
		</tr>
	<?php 	
	}
?>
<tr>
<td align="right"><strong>Opening</strong></td>
<td align="right"><strong><?php echo $openingDR;?></strong></td>
<td align="right"><strong><?php echo $openingCR;?></strong></td>
<td>&nbsp;</td>
</tr>
<tr>
<td align="right"><strong>Total</strong></td>
<td align="right"><strong><?php echo $DR=array_sum($arrayDR)+$openingDR;?></strong></td>
<td align="right"><strong><?php echo $CR=array_sum($arrayCR)+$openingCR;?></strong></td>
<td><?php //if($DR>$CR){echo $DR-$CR; echo " DR";}elseif($CR>$DR){echo $CR-$DR; echo " CR";}?></td>
</tr>
<tr>
<td align="right"><strong>Total Closing</strong></td>
<?php 

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