<?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');
//include('exportconfig.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='4'><h2>".$period."</h2></td>";
$data.="</tr>";
$data.="<tr>";
$data.="<th width='50%' style='border-bottom: #666666 solid 2px;border-left: #FFFFFF solid 2px;border-top: #FFFFFF solid 2px;border-right: #666666 solid 2px;'>EXPENSE</th>";
$data.="<th width='50%' style='border-bottom: #666666 solid 2px;border-left: #FFFFFF solid 2px;border-top: #FFFFFF solid 2px;border-right: #666666 solid 2px;'>INCOME</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';
	  $getExpenseId=$s->getField($table,'GroupName','EXPENSE','GroupId');
	  $getUnderGroups=DB_query("select * from $table where NatureOfGroup=$getExpenseId and MainGroupId=0",$db);
		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><strong>";
if($x[0]>$x[1])
{
$data.=$LIABDR=$x[0]-$x[1]; 
$data.=" DR"; 
$LIABDR_ARR[]=$LIABDR;
}
elseif($x[1]>$x[0])
{
$data.=$LIABCR=$x[1]-$x[0]; 
$data.=" CR"; $LIABCR_ARR[]=$LIABCR;
}
$data.="</strong></strong></td>";
$data.="</tr>";
		}
		}
$data.="<tr>";
$data.="<td style='border-bottom: #666666 solid 2px;border-left: #FFFFFF solid 2px;border-top: #FFFFFF solid 2px;border-right: #666666 solid 2px;'><strong>Total</strong></td>";
$data.="<td style='border-bottom: #666666 solid 2px;border-left: #FFFFFF solid 2px;border-top: #FFFFFF solid 2px;border-right: #666666 solid 2px; text-align:right'><strong>";
if($LIABDR_ARR!='')
{
$sum1=array_sum($LIABDR_ARR);
}
if($LIABCR_ARR!='')
{
$sum2=array_sum($LIABCR_ARR);
}
if($sum1>$sum2)
{
$data.=$expense=($sum1-$sum2);
$expense_type='DR'; 
$data.="&nbsp;".$expense_type;
}
else
{
$data.=$expense=($sum2-$sum1); 
$expense_type='CR'; 
$data.="&nbsp;".$expense_type;
}
$data.="</strong></td>";
$data.="</tr>";
$data.="</table>";
$data.="</td>";
$data.="<td valign='top'><table width='100%' border='0' cellspacing='3' cellpadding='3' style='border:#999999 solid 1px'>";
$getIncomeId=$s->getField($table,'GroupName','INCOME','GroupId');
$getUnderGroups=DB_query("select * from $table where NatureOfGroup=$getIncomeId and MainGroupId=0",$db);
$ASSDR_ARR='';
$sumA1='';
$proffit='';
$proffit_type='';
while($underGroups=DB_fetch_array($getUnderGroups))
{
if($from_date!='' && $to_date!='')
{$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[0]>$y[1])
{
$data.=$ASSDR=$y[0]-$y[1]; 
$data.=" DR"; 
$ASSDR_ARR[]=$ASSDR;
}
elseif($y[1]>$y[0])
{
$data.=$ASSCR=$y[1]-$y[0]; 
$data.=" CR"; 
$ASSCR_ARR[]=$ASSCR;
}
$data.="</strong></td>";
$data.="</tr>";
}} 
$data.="<tr>";
$data.="<td style='border-bottom: #666666 solid 2px;border-left: #FFFFFF solid 2px;border-top: #FFFFFF solid 2px;border-right: #666666 solid 2px;'><strong>Total</strong></td>";
$data.="<td style='border-bottom: #666666 solid 2px;border-left: #FFFFFF solid 2px;border-top: #FFFFFF solid 2px;border-right: #666666 solid 2px; text-align:right'><strong>";
if($ASSDR_ARR!='')
{
$sumA1=array_sum($ASSDR_ARR);
}
if($ASSCR_ARR!='')
{
$sumA2=array_sum($ASSCR_ARR);
}
if($sumA1>$sumA2)
{
$data.=$income=($sumA1-$sumA2); 
$income_type='DR'; 
$data.="&nbsp;".$income_type;
}
else
{
$data.=$income=($sumA2-$sumA1); 
$income_type='CR'; 
$data.="&nbsp;".$income_type;
}
$data.="</strong></td>";
$data.="</tr>";
$data.="</table>";
$data.="</td>";
$data.="</tr>";
   if($expense_type==$income_type)
  {
     if($expense_type=='CR')
	 {
	 	$proffit=$expense+$income;
		$proffit_type=$expense_type;
	 }
	 elseif($expense_type=='DR')
	 {
	 	$loss=$expense+$income;
		$loss_type=$expense_type;
	 }
  }
  elseif($expense_type!=$income_type)
  {
  		if($expense>$income)
		{
			$value=$expense-$income;
			$value_type=$expense_type;
		}
		elseif($income>$expense)
		{
			$value=$income-$expense;
			$value_type=$income_type;
		}
		
		if($value_type=='CR')
		{$proffit=$value;$proffit_type=$value_type;}
		elseif($value_type=='DR')
		{$loss=$value;$loss_type=$value_type;}
  }
  else{$loss=''; $loss_type=''; $proffit=''; $proffit_type='';}
$data.="<tr>";
$data.="<td valign='top' colspan='2'><table width='100%' border='0' cellspacing='3' cellpadding='3' style='border:#999999 solid 1px'>";
$data.="<tr>";
$data.="<td style='border-bottom: #666666 solid 2px;border-left: #FFFFFF solid 2px;border-top: #FFFFFF solid 2px;border-right: #666666 solid 2px; width:25%;'><strong>Loss</strong></td>";
$data.="<td style='border-bottom: #666666 solid 2px;border-left: #FFFFFF solid 2px;border-top: #FFFFFF solid 2px;border-right: #666666 solid 2px; width:25%; text-align:right'><strong>".$loss."&nbsp;".$loss_type."</strong></td>";
$data.="<td style='border-bottom: #666666 solid 2px;border-left: #FFFFFF solid 2px;border-top: #FFFFFF solid 2px;border-right: #666666 solid 2px; width:25%;'><strong>Proffit</strong></td>";
$data.="<td style='border-bottom: #666666 solid 2px;border-left: #FFFFFF solid 2px;border-top: #FFFFFF solid 2px;border-right: #666666 solid 2px; width:25%; text-align:right'><strong>".$proffit."&nbsp;".$proffit_type."</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=Profitandloss.xls"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
print "$header\n$data"; 
?>
