<?php
session_name('CAKEPHP'); session_start();
ob_start();
$PageSecurity = 1;
include('config.php');
include('includes/ConnectDB.inc');
include('includes/function_lib.php');
include('includes/LanguageSetup.php');
include('includes/DateFunctions.inc');
include('Account/fun-Trial.php');
$from_date='';
$to_date='';
//include('includes/function_lib.php');
if(isset($_SESSION['FDATE']))
{
$from_date=$_SESSION['FDATE'];
}
if(isset($_SESSION['TDATE']))
{
$to_date=$_SESSION['TDATE'];
}

if($from_date!='' && $to_date!='')
{
$period=ConvertSQLDate($from_date).' to '.ConvertSQLDate($to_date);
}
else
{
$period=ConvertSQLDate($_SESSION['financialYrStart']).' to '.ConvertSQLDate($_SESSION['financialYrEnd']);
}






$data='';
$data.="<table width='100%' border='0' style='border:#999999 solid 1px'>";
$data.="<tr>";
$data.="<td colspan='2'><h2>".$period."</h2></td>";
$data.="</tr>";
$data.="<tr>";
$data.="<th width='50%'>LIABILITIES</th>";
$data.="<th width='50%'>ASSETS</th>";
$data.="</tr>";
$data.="<tr>";
$data.="<td valign='top'> ";
$data.="<table width='100%' border='0' cellspacing='3' cellpadding='3' style='border:#999999 solid 1px'>";

$table='accountgroupmaster';
$getLiabilityId=$s->getField($table,'GroupName','LIABILITIES','GroupId');
$getUnderGroups=DB_query("select * from $table where NatureOfGroup=$getLiabilityId and MainGroupId=0",$db);
$LIABDR_ARR='';
$LIABCR_ARR='';
while($underGroups=DB_fetch_array($getUnderGroups)){
if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
{$x=getGroupBalanceSheet($underGroups['GroupId'],$_SESSION['FDATE'],$_SESSION['TDATE']);}
else{ $x=getGroupBalanceSheet($underGroups['GroupId'],'','');}
			//echo "--".$underGroups['GroupId'];
			//echo "<pre>";
			//print_r($x);
if($x[0]!='' || $x[1]!='' || $x[2]!='' || $x[3]!=''){

$data.="<tr>";
$data.="<td width='60%' style='padding-left:5px'><strong><font color='#000000'>".ucwords($underGroups['GroupName'])."</font></strong></td>";

$data.="<td width='40%' style='text-align:right; padding-left:5px'><strong>";



 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;}




 if(($x[0]+$x[2])>($x[1]+$x[3]))
 {
$data.=$LIABDR=($x[0]+$x[2])-($x[1]+$x[3]); 
$data.=" DR"; 
$LIABDR_ARR[]=$LIABDR;
 }
elseif(($x[1]+$x[3])>($x[0]+$x[2]))
{
$data.=$LIABCR=($x[1]+$x[3])-($x[0]+$x[2]); 
$data.=" CR"; 
$LIABCR_ARR[]=$LIABCR;
}
$data.="</strong></td>";
$data.="</tr>";
 }} 
 
$data.="</table>";
$data.="</td>";
$data.="<td valign='top'>";
$data.="<table width='100%' border='0' cellspacing='3' cellpadding='3' style='border:#999999 solid 1px'>";

$getLiabilityId=$s->getField($table,'GroupName','ASSET','GroupId');
$getUnderGroups=DB_query("select * from $table where NatureOfGroup=$getLiabilityId and MainGroupId=0",$db);
$ASSDR_ARR='';
$ASSCR_ARR='';
while($underGroups=DB_fetch_array($getUnderGroups)){
if($_SESSION['FDATE']!='' && $_SESSION['TDATE']!='')
{$y=getGroupBalanceSheet($underGroups['GroupId'],$_SESSION['FDATE'],$_SESSION['TDATE']);}
else{
$y=getGroupBalanceSheet($underGroups['GroupId']);}
//echo "--".$underGroups['GroupId'];
//echo "<pre>";
//print_r($y);
if($y[0]!='' || $y[1]!='' || $y[2]!='' || $y[3]!=''){



$data.="<tr>";
$data.="<td width='60%' style='padding-left:5px'><strong><font color='#000000'>".ucwords($underGroups['GroupName'])."</font></strong></a></td>";
$data.="<td width='40%' style='text-align:right; padding-left:5px'><strong>";


 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;}



if(($y[0]+$y[2])>($y[1]+$y[3]))
{
$data.=$ASSDR=($y[0]+$y[2])-($y[1]+$y[3]); 
$data.= $DRA=" DR"; $ASSDR_ARR[]=$ASSDR;
}
elseif(($y[1]+$y[3])>($y[0]+$y[2]))
{
$data.= $ASSCR=($y[1]+$y[3])-($y[0]+$y[2]); 
$data.= $DRA=" CR"; $ASSCR_ARR[]=$ASSCR;
}
$data.="</strong></td>";
$data.="</tr>";
 } } 
$data.="</table>";
$data.="</td>";
$data.="</tr>";
 
 
 if($LIABOPCRARR!=''){$a1=array_sum($LIABOPCRARR);}else{$a1=0;}
  if($LIABOPDRARR!=''){$b1=array_sum($LIABOPDRARR);}else{$b1=0;}
  $LIABOPBAL=($a1)-($b1);
  
  if($ASSOPCRARR!=''){$a2=array_sum($ASSOPCRARR);}else{$a1=0;}
  if($ASSOPDRARR!=''){$b2=array_sum($ASSOPDRARR);}else{$b1=0;}
  $ASSETOPBAL=($b2)-($a2);
  
  if($ASSETOPBAL>$LIABOPBAL){$diffDR=$ASSETOPBAL-$LIABOPBAL;}
  elseif($LIABOPBAL>$ASSETOPBAL){$diffCR=$LIABOPBAL-$ASSETOPBAL;}
 
 
  $diffCR;
 
$data.="<tr>";
$data.="<td colspan='2'>";
$data.="<table width='100%' border='0' cellspacing='3' cellpadding='3' style='border:#999999 solid 1px'>";
$data.="<tr>";
$data.="<td style='width:25%'><strong>";
if($diffDR!=''){
$data.='Difference in opening balances'; } 
else 
$data.='&nbsp;';
$data.="</strong></td>";
$data.="<td style='width:25%; text-align:right'><strong>";
if($diffDR!=''){
$data.=$diffDR;}
else 
$data.='&nbsp;';
$data.="</strong></td>";
$data.="<td style='width:25%'><strong>";
if($diffCR!=''){
$data.='Difference in opening balances';
} 
else 
$data.='&nbsp;';
$data.="</strong></td>";
$data.="<td style='width:25%; text-align:right'><strong>";
if($diffCR!=''){
$data.=$diffCR;
}
else 
$data.='&nbsp;';
$data.="</strong></td>";
$data.="</tr>";
$data.="</table>";
$data.="</td>";
$data.="</tr>";
 
 
 
  $qqq=proffitAndLoss();
	if($qqq[0]!=''){$profit=$qqq[0];$ptype=$qqq[1];}
	elseif($qqq[2]!=''){$loss=$qqq[2];$ltype=$qqq[3];}
 
 
 
 
 
 

$data.="<tr>";
$data.="<td colspan='2'>";
$data.="<table width='100%' border='0' cellspacing='3' cellpadding='3' style='border:#999999 solid 1px'>";
$data.="<tr>";
$data.="<td style='width:25%;'><strong>";
if($profit!=''){
$data.='Profit';
}
 else
$data.="&nbsp;";
$data.="</strong></td>";
$data.="<td style='width:25%; text-align:right'><strong>";
if($profit!='')
{
$data.=number_format($profit)."&nbsp;".$ptype;}
else 
$data.="&nbsp;";
$data.="</strong></td>";
$data.="<td style='width:25%;'><strong>";
if($loss!=''){
$data.='Loss';
} 
else
$data.="&nbsp;";
$data.="</strong></td>";
$data.="<td style='width:25%; text-align:right'><strong>";
if($loss!='')
{
$data.=number_format($loss)."&nbsp;".$ltype;}
else 
$data.="&nbsp;";
$data.="</strong></td>";
$data.="</tr>";
$data.="</table>";
$data.="</td>";
$data.="</tr>"; 

 
 
 
 
 

 
 
 
 
 
$data.="<tr>";
$data.="<td colspan='2'>";
$data.="<table width='100%' border='0' cellspacing='3' cellpadding='3' style='border:#999999 solid 1px'>";
$data.="<tr>";
$data.="<td><strong>Total</strong></td>";
$data.="<td  style='text-align:right; padding-left:5px'><strong>";
$sum1='';
$sum2='';
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; $data.=$balsheetliabvalue."&nbsp;".$final_type1;}
  	elseif($ltype!=$final_type1)
  	{
			
  		if($final_value1>$profit){
$data.=$final_value1-$profit."&nbsp;".$final_type1;}
		elseif($profit>$final_value1){
$data.=$profit-$final_value1."&nbsp;".$ltype;}
  	}
  }
  else
  {
$data.=number_format($final_value1)."&nbsp;".$final_type1;
  }
$data.="</strong></td>";



$data.="<td ><strong>Total</strong></td>";
$data.="<td  style='text-align:right; padding-left:5px'><strong>";
$sumA1='';
$sumA2='';
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; $data.=number_format($balsheetassetvalue)."&nbsp;".$final_type2;}
  	elseif($ltype!=$final_type2)
  	{
  		if($final_value2>$loss){
$data.=number_format($final_value2-$loss)."&nbsp;".$final_type2;}
		elseif($loss>$final_value2){
$data.=number_format($loss-$final_value2)."&nbsp;".$ltype;}
  	}
  }
  else
  {
$data.=number_format($final_value2)."&nbsp;".$final_type2;
  }
$data.="</strong></td>";

$data.="</tr>";
$data.="</table>";

$data.="</td>";




$data.="</tr>";
 
 
 
 
 
 
 
 
$data.="</table>";
//echo $data;
$header='';  	
header("Content-type: application/x-msdownload"); 
header("Content-Disposition: attachment; filename=APRGroupWise-BalanceSheet_Exl.xls"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
print "$header\n$data"; 
?>