 <?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);


if (PHP_SAPI == 'cli')
	die('This example should only be run from a Web Browser');

/** Include PHPExcel */
//include_once('Classes/PHPExcel.php');


include_once('Classes/PHPExcel/IOFactory.php');

$inputFileName = 'reports/pl-divisional.xlsx';
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory;
PHPExcel_Settings::setCacheStorageMethod($cacheMethod);


/*check point*/

// Read the existing excel file
//$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(false);
$objPHPExcel = $objReader->load($inputFileName);

// Update it's data

	$NumberOfMonths = $_POST['ToPeriod'] - $_POST['FromPeriod'] + 1;

	if ($NumberOfMonths >12){
		echo '<br />';
		prnMsg(_('A period up to 12 months in duration can be specified') . ' - ' . _('the system automatically shows a comparative for the same period from the previous year') . ' - ' . _('it cannot do this if a period of more than 12 months is specified') . '. ' . _('Please select an alternative period range'),'error');
		include('includes/footer.inc');
		exit;
	}

	$sql = "SELECT lastdate_in_period FROM periods WHERE periodno='" . $_POST['ToPeriod'] . "'";
	$PrdResult = DB_query($sql);
	$myrow = DB_fetch_row($PrdResult);
	$PeriodToDate = MonthAndYearFromSQLDate($myrow[0]);




	


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
//$objPHPExcel->setActiveSheetIndex();

// Add months and till date information derived from above
//$objPHPExcel->getActiveSheet()->duplicateStyle($objPHPExcel-‌​>getActiveSheet()->g‌​etStyle('B2'), 'B3:B20');

        $objPHPExcel->getActiveSheet()->setCellValue('D1', 'General Manager');
		$objPHPExcel->getActiveSheet()->setCellValue('B2', $NumberOfMonths);
		$objPHPExcel->getActiveSheet()->setCellValue('D2', $PeriodToDate);
		
		
$SQL = "SELECT accountgroups.sectioninaccounts,
					accountgroups.groupname,
					accountgroups.parentgroupname,
					chartdetails.accountcode ,
					chartmaster.accountname,
					SUM(CASE WHEN chartdetails.period='" . $_POST['FromPeriod'] . "' THEN chartdetails.bfwdgm ELSE 0 END) AS firstprdbfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['FromPeriod'] . "' THEN chartdetails.bfwdbudgetgm ELSE 0 END) AS firstprdbudgetbfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['ToPeriod'] . "' THEN chartdetails.bfwdgm + chartdetails.actualgm ELSE 0 END) AS lastprdcfwd,
					SUM(CASE WHEN chartdetails.period='" . ($_POST['FromPeriod'] - 12) . "' THEN chartdetails.bfwdgm ELSE 0 END) AS lyfirstprdbfwd,
					SUM(CASE WHEN chartdetails.period='" . ($_POST['ToPeriod']-12) . "' THEN chartdetails.bfwdgm + chartdetails.actualgm ELSE 0 END) AS lylastprdcfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['ToPeriod'] . "' THEN chartdetails.bfwdbudgetgm + chartdetails.budgetgm ELSE 0 END) AS lastprdbudgetcfwd
			FROM chartmaster INNER JOIN accountgroups
			ON chartmaster.group_ = accountgroups.groupname INNER JOIN chartdetails
			ON chartmaster.accountcode= chartdetails.accountcode
			WHERE accountgroups.pandl=1 AND chartmaster.group_ = 'Sales'
				GROUP BY accountgroups.sectioninaccounts,
					accountgroups.groupname,
					accountgroups.parentgroupname,
					chartdetails.accountcode,
					chartmaster.accountname,
					accountgroups.sequenceintb
				ORDER BY accountgroups.sectioninaccounts,
					accountgroups.sequenceintb,
					accountgroups.groupname,
					chartdetails.accountcode";

	$AccountsResult = DB_query($SQL);

	

$num= DB_num_rows($AccountsResult);


$rowCount= 0;	
while ($rowCount< $num) {
	$cellcount = 6;	
	
	while ($myrow=DB_fetch_array($AccountsResult)) {
		
		
	$AccountCode = $myrow['accountcode'];
	$AccountDesc = $myrow['accountname'];
	
	$AccountPeriodActual = $myrow['lastprdcfwd'] - $myrow['firstprdbfwd'];
	$AccountPeriodLY = $myrow['lylastprdcfwd'] - $myrow['lyfirstprdbfwd'];
	$AccountPeriodBudget = $myrow['lastprdbudgetcfwd'] - $myrow['firstprdbudgetbfwd'];
		
	
		
		$objPHPExcel->getActiveSheet()->setCellValue('A'.$cellcount, $AccountCode);
		$objPHPExcel->getActiveSheet()->setCellValue('B'.$cellcount, $AccountDesc);
		$objPHPExcel->getActiveSheet()->setCellValue('C'.$cellcount,  $AccountPeriodActual);
		$objPHPExcel->getActiveSheet()->setCellValue('D'.$cellcount, $AccountPeriodBudget);
		$objPHPExcel->getActiveSheet()->setCellValue('E'.$cellcount, $AccountPeriodLY);
		$cellcount++;
	
		
	}
		
		$rowCount++;
	

}


$SQL = "SELECT accountgroups.sectioninaccounts,
					accountgroups.groupname,
					accountgroups.parentgroupname,
					chartdetails.accountcode ,
					chartmaster.accountname,
					SUM(CASE WHEN chartdetails.period='" . $_POST['FromPeriod'] . "' THEN chartdetails.bfwdgm ELSE 0 END) AS firstprdbfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['FromPeriod'] . "' THEN chartdetails.bfwdbudgetgm ELSE 0 END) AS firstprdbudgetbfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['ToPeriod'] . "' THEN chartdetails.bfwdgm + chartdetails.actualgm ELSE 0 END) AS lastprdcfwd,
					SUM(CASE WHEN chartdetails.period='" . ($_POST['FromPeriod'] - 12) . "' THEN chartdetails.bfwdgm ELSE 0 END) AS lyfirstprdbfwd,
					SUM(CASE WHEN chartdetails.period='" . ($_POST['ToPeriod']-12) . "' THEN chartdetails.bfwdgm + chartdetails.actualgm ELSE 0 END) AS lylastprdcfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['ToPeriod'] . "' THEN chartdetails.bfwdbudgetgm + chartdetails.budgetgm ELSE 0 END) AS lastprdbudgetcfwd
			FROM chartmaster INNER JOIN accountgroups
			ON chartmaster.group_ = accountgroups.groupname INNER JOIN chartdetails
			ON chartmaster.accountcode= chartdetails.accountcode
			WHERE accountgroups.pandl=1 AND chartmaster.group_ = 'Cost of Goods Sold'
				GROUP BY accountgroups.sectioninaccounts,
					accountgroups.groupname,
					accountgroups.parentgroupname,
					chartdetails.accountcode,
					chartmaster.accountname,
					accountgroups.sequenceintb
				ORDER BY accountgroups.sectioninaccounts,
					accountgroups.sequenceintb,
					accountgroups.groupname,
					chartdetails.accountcode";

	$AccountsResult = DB_query($SQL);

	

$num= DB_num_rows($AccountsResult);


$rowCount= 0;	
while ($rowCount< $num) {
	$cellcount = 34;	
	
	while ($myrow=DB_fetch_array($AccountsResult)) {
		
		
	$AccountCode = $myrow['accountcode'];
	$AccountDesc = $myrow['accountname'];
	
	$AccountPeriodActual = $myrow['lastprdcfwd'] - $myrow['firstprdbfwd'];
	$AccountPeriodLY = $myrow['lylastprdcfwd'] - $myrow['lyfirstprdbfwd'];
	$AccountPeriodBudget = $myrow['lastprdbudgetcfwd'] - $myrow['firstprdbudgetbfwd'];
		
	
		
		$objPHPExcel->getActiveSheet()->setCellValue('A'.$cellcount, $AccountCode);
		$objPHPExcel->getActiveSheet()->setCellValue('B'.$cellcount, $AccountDesc);
		$objPHPExcel->getActiveSheet()->setCellValue('C'.$cellcount,  $AccountPeriodActual);
		$objPHPExcel->getActiveSheet()->setCellValue('D'.$cellcount, $AccountPeriodBudget);
		$objPHPExcel->getActiveSheet()->setCellValue('E'.$cellcount, $AccountPeriodLY);
		$cellcount++;
	
		
	}
		
		$rowCount++;
	

}


$SQL = "SELECT accountgroups.sectioninaccounts,
					accountgroups.groupname,
					accountgroups.parentgroupname,
					chartdetails.accountcode ,
					chartmaster.accountname,
					SUM(CASE WHEN chartdetails.period='" . $_POST['FromPeriod'] . "' THEN chartdetails.bfwdgm ELSE 0 END) AS firstprdbfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['FromPeriod'] . "' THEN chartdetails.bfwdbudgetgm ELSE 0 END) AS firstprdbudgetbfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['ToPeriod'] . "' THEN chartdetails.bfwdgm + chartdetails.actualgm ELSE 0 END) AS lastprdcfwd,
					SUM(CASE WHEN chartdetails.period='" . ($_POST['FromPeriod'] - 12) . "' THEN chartdetails.bfwdgm ELSE 0 END) AS lyfirstprdbfwd,
					SUM(CASE WHEN chartdetails.period='" . ($_POST['ToPeriod']-12) . "' THEN chartdetails.bfwdgm + chartdetails.actualgm ELSE 0 END) AS lylastprdcfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['ToPeriod'] . "' THEN chartdetails.bfwdbudgetgm + chartdetails.budgetgm ELSE 0 END) AS lastprdbudgetcfwd
			FROM chartmaster INNER JOIN accountgroups
			ON chartmaster.group_ = accountgroups.groupname INNER JOIN chartdetails
			ON chartmaster.accountcode= chartdetails.accountcode
			WHERE accountgroups.pandl=1 AND chartmaster.group_ = 'Personnel'
				GROUP BY accountgroups.sectioninaccounts,
					accountgroups.groupname,
					accountgroups.parentgroupname,
					chartdetails.accountcode,
					chartmaster.accountname,
					accountgroups.sequenceintb
				ORDER BY accountgroups.sectioninaccounts,
					accountgroups.sequenceintb,
					accountgroups.groupname,
					chartdetails.accountcode";

	$AccountsResult = DB_query($SQL);

	

$num= DB_num_rows($AccountsResult);


$rowCount= 0;	
while ($rowCount< $num) {
	$cellcount = 69;	
	
	while ($myrow=DB_fetch_array($AccountsResult)) {
		
		
	$AccountCode = $myrow['accountcode'];
	$AccountDesc = $myrow['accountname'];
	
	$AccountPeriodActual = $myrow['lastprdcfwd'] - $myrow['firstprdbfwd'];
	$AccountPeriodLY = $myrow['lylastprdcfwd'] - $myrow['lyfirstprdbfwd'];
	$AccountPeriodBudget = $myrow['lastprdbudgetcfwd'] - $myrow['firstprdbudgetbfwd'];
		
	
		
		$objPHPExcel->getActiveSheet()->setCellValue('A'.$cellcount, $AccountCode);
		$objPHPExcel->getActiveSheet()->setCellValue('B'.$cellcount, $AccountDesc);
		$objPHPExcel->getActiveSheet()->setCellValue('C'.$cellcount,  $AccountPeriodActual);
		$objPHPExcel->getActiveSheet()->setCellValue('D'.$cellcount, $AccountPeriodBudget);
		$objPHPExcel->getActiveSheet()->setCellValue('E'.$cellcount, $AccountPeriodLY);
		$cellcount++;
	
		
	}
		
		$rowCount++;
	

}



$SQL = "SELECT accountgroups.sectioninaccounts,
					accountgroups.groupname,
					accountgroups.parentgroupname,
					chartdetails.accountcode ,
					chartmaster.accountname,
					SUM(CASE WHEN chartdetails.period='" . $_POST['FromPeriod'] . "' THEN chartdetails.bfwdgm ELSE 0 END) AS firstprdbfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['FromPeriod'] . "' THEN chartdetails.bfwdbudgetgm ELSE 0 END) AS firstprdbudgetbfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['ToPeriod'] . "' THEN chartdetails.bfwdgm + chartdetails.actualgm ELSE 0 END) AS lastprdcfwd,
					SUM(CASE WHEN chartdetails.period='" . ($_POST['FromPeriod'] - 12) . "' THEN chartdetails.bfwdgm ELSE 0 END) AS lyfirstprdbfwd,
					SUM(CASE WHEN chartdetails.period='" . ($_POST['ToPeriod']-12) . "' THEN chartdetails.bfwdgm + chartdetails.actualgm ELSE 0 END) AS lylastprdcfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['ToPeriod'] . "' THEN chartdetails.bfwdbudgetgm + chartdetails.budgetgm ELSE 0 END) AS lastprdbudgetcfwd
			FROM chartmaster INNER JOIN accountgroups
			ON chartmaster.group_ = accountgroups.groupname INNER JOIN chartdetails
			ON chartmaster.accountcode= chartdetails.accountcode
			WHERE accountgroups.pandl=1 AND chartmaster.group_ = 'Premises'
				GROUP BY accountgroups.sectioninaccounts,
					accountgroups.groupname,
					accountgroups.parentgroupname,
					chartdetails.accountcode,
					chartmaster.accountname,
					accountgroups.sequenceintb
				ORDER BY accountgroups.sectioninaccounts,
					accountgroups.sequenceintb,
					accountgroups.groupname,
					chartdetails.accountcode";

	$AccountsResult = DB_query($SQL);

	

$num= DB_num_rows($AccountsResult);


$rowCount= 0;	
while ($rowCount< $num) {
	$cellcount = 130;	
	
	while ($myrow=DB_fetch_array($AccountsResult)) {
		
		
	$AccountCode = $myrow['accountcode'];
	$AccountDesc = $myrow['accountname'];
	
	$AccountPeriodActual = $myrow['lastprdcfwd'] - $myrow['firstprdbfwd'];
	$AccountPeriodLY = $myrow['lylastprdcfwd'] - $myrow['lyfirstprdbfwd'];
	$AccountPeriodBudget = $myrow['lastprdbudgetcfwd'] - $myrow['firstprdbudgetbfwd'];
		
	
		
		$objPHPExcel->getActiveSheet()->setCellValue('A'.$cellcount, $AccountCode);
		$objPHPExcel->getActiveSheet()->setCellValue('B'.$cellcount, $AccountDesc);
		$objPHPExcel->getActiveSheet()->setCellValue('C'.$cellcount,  $AccountPeriodActual);
		$objPHPExcel->getActiveSheet()->setCellValue('D'.$cellcount, $AccountPeriodBudget);
		$objPHPExcel->getActiveSheet()->setCellValue('E'.$cellcount, $AccountPeriodLY);
		$cellcount++;
	
		
	}
		
		$rowCount++;
	

}


$SQL = "SELECT accountgroups.sectioninaccounts,
					accountgroups.groupname,
					accountgroups.parentgroupname,
					chartdetails.accountcode ,
					chartmaster.accountname,
					SUM(CASE WHEN chartdetails.period='" . $_POST['FromPeriod'] . "' THEN chartdetails.bfwdgm ELSE 0 END) AS firstprdbfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['FromPeriod'] . "' THEN chartdetails.bfwdbudgetgm ELSE 0 END) AS firstprdbudgetbfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['ToPeriod'] . "' THEN chartdetails.bfwdgm + chartdetails.actualgm ELSE 0 END) AS lastprdcfwd,
					SUM(CASE WHEN chartdetails.period='" . ($_POST['FromPeriod'] - 12) . "' THEN chartdetails.bfwdgm ELSE 0 END) AS lyfirstprdbfwd,
					SUM(CASE WHEN chartdetails.period='" . ($_POST['ToPeriod']-12) . "' THEN chartdetails.bfwdgm + chartdetails.actualgm ELSE 0 END) AS lylastprdcfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['ToPeriod'] . "' THEN chartdetails.bfwdbudgetgm + chartdetails.budgetgm ELSE 0 END) AS lastprdbudgetcfwd
			FROM chartmaster INNER JOIN accountgroups
			ON chartmaster.group_ = accountgroups.groupname INNER JOIN chartdetails
			ON chartmaster.accountcode= chartdetails.accountcode
			WHERE accountgroups.pandl=1 AND chartmaster.group_ = 'Computer'
				GROUP BY accountgroups.sectioninaccounts,
					accountgroups.groupname,
					accountgroups.parentgroupname,
					chartdetails.accountcode,
					chartmaster.accountname,
					accountgroups.sequenceintb
				ORDER BY accountgroups.sectioninaccounts,
					accountgroups.sequenceintb,
					accountgroups.groupname,
					chartdetails.accountcode";


	$AccountsResult = DB_query($SQL);

	

$num= DB_num_rows($AccountsResult);


$rowCount= 0;	
while ($rowCount< $num) {
	$cellcount = 154;	
	
	while ($myrow=DB_fetch_array($AccountsResult)) {
		
		
	$AccountCode = $myrow['accountcode'];
	$AccountDesc = $myrow['accountname'];
	
	$AccountPeriodActual = $myrow['lastprdcfwd'] - $myrow['firstprdbfwd'];
	$AccountPeriodLY = $myrow['lylastprdcfwd'] - $myrow['lyfirstprdbfwd'];
	$AccountPeriodBudget = $myrow['lastprdbudgetcfwd'] - $myrow['firstprdbudgetbfwd'];
		
	
		
		$objPHPExcel->getActiveSheet()->setCellValue('A'.$cellcount, $AccountCode);
		$objPHPExcel->getActiveSheet()->setCellValue('B'.$cellcount, $AccountDesc);
		$objPHPExcel->getActiveSheet()->setCellValue('C'.$cellcount,  $AccountPeriodActual);
		$objPHPExcel->getActiveSheet()->setCellValue('D'.$cellcount, $AccountPeriodBudget);
		$objPHPExcel->getActiveSheet()->setCellValue('E'.$cellcount, $AccountPeriodLY);
		$cellcount++;
	
		
	}
		
		$rowCount++;
	

}	

$SQL = "SELECT accountgroups.sectioninaccounts,
					accountgroups.groupname,
					accountgroups.parentgroupname,
					chartdetails.accountcode ,
					chartmaster.accountname,
					SUM(CASE WHEN chartdetails.period='" . $_POST['FromPeriod'] . "' THEN chartdetails.bfwdgm ELSE 0 END) AS firstprdbfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['FromPeriod'] . "' THEN chartdetails.bfwdbudgetgm ELSE 0 END) AS firstprdbudgetbfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['ToPeriod'] . "' THEN chartdetails.bfwdgm + chartdetails.actualgm ELSE 0 END) AS lastprdcfwd,
					SUM(CASE WHEN chartdetails.period='" . ($_POST['FromPeriod'] - 12) . "' THEN chartdetails.bfwdgm ELSE 0 END) AS lyfirstprdbfwd,
					SUM(CASE WHEN chartdetails.period='" . ($_POST['ToPeriod']-12) . "' THEN chartdetails.bfwdgm + chartdetails.actualgm ELSE 0 END) AS lylastprdcfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['ToPeriod'] . "' THEN chartdetails.bfwdbudgetgm + chartdetails.budgetgm ELSE 0 END) AS lastprdbudgetcfwd
			FROM chartmaster INNER JOIN accountgroups
			ON chartmaster.group_ = accountgroups.groupname INNER JOIN chartdetails
			ON chartmaster.accountcode= chartdetails.accountcode
			WHERE accountgroups.pandl=1 AND chartmaster.group_ = 'Other Expenses'
				GROUP BY accountgroups.sectioninaccounts,
					accountgroups.groupname,
					accountgroups.parentgroupname,
					chartdetails.accountcode,
					chartmaster.accountname,
					accountgroups.sequenceintb
				ORDER BY accountgroups.sectioninaccounts,
					accountgroups.sequenceintb,
					accountgroups.groupname,
					chartdetails.accountcode";


	$AccountsResult = DB_query($SQL);

	

$num= DB_num_rows($AccountsResult);


$rowCount= 0;	
while ($rowCount< $num) {
	$cellcount = 175;	
	
	while ($myrow=DB_fetch_array($AccountsResult)) {
		
		
	$AccountCode = $myrow['accountcode'];
	$AccountDesc = $myrow['accountname'];
	
	$AccountPeriodActual = $myrow['lastprdcfwd'] - $myrow['firstprdbfwd'];
	$AccountPeriodLY = $myrow['lylastprdcfwd'] - $myrow['lyfirstprdbfwd'];
	$AccountPeriodBudget = $myrow['lastprdbudgetcfwd'] - $myrow['firstprdbudgetbfwd'];
		
	
		
		$objPHPExcel->getActiveSheet()->setCellValue('A'.$cellcount, $AccountCode);
		$objPHPExcel->getActiveSheet()->setCellValue('B'.$cellcount, $AccountDesc);
		$objPHPExcel->getActiveSheet()->setCellValue('C'.$cellcount,  $AccountPeriodActual);
		$objPHPExcel->getActiveSheet()->setCellValue('D'.$cellcount, $AccountPeriodBudget);
		$objPHPExcel->getActiveSheet()->setCellValue('E'.$cellcount, $AccountPeriodLY);
		$cellcount++;
	
		
	}
		
		$rowCount++;
	

}


$SQL = "SELECT accountgroups.sectioninaccounts,
					accountgroups.groupname,
					accountgroups.parentgroupname,
					chartdetails.accountcode ,
					chartmaster.accountname,
					SUM(CASE WHEN chartdetails.period='" . $_POST['FromPeriod'] . "' THEN chartdetails.bfwdgm ELSE 0 END) AS firstprdbfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['FromPeriod'] . "' THEN chartdetails.bfwdbudgetgm ELSE 0 END) AS firstprdbudgetbfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['ToPeriod'] . "' THEN chartdetails.bfwdgm + chartdetails.actualgm ELSE 0 END) AS lastprdcfwd,
					SUM(CASE WHEN chartdetails.period='" . ($_POST['FromPeriod'] - 12) . "' THEN chartdetails.bfwdgm ELSE 0 END) AS lyfirstprdbfwd,
					SUM(CASE WHEN chartdetails.period='" . ($_POST['ToPeriod']-12) . "' THEN chartdetails.bfwdgm + chartdetails.actualgm ELSE 0 END) AS lylastprdcfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['ToPeriod'] . "' THEN chartdetails.bfwdbudgetgm + chartdetails.budgetgm ELSE 0 END) AS lastprdbudgetcfwd
			FROM chartmaster INNER JOIN accountgroups
			ON chartmaster.group_ = accountgroups.groupname INNER JOIN chartdetails
			ON chartmaster.accountcode= chartdetails.accountcode
			WHERE accountgroups.pandl=1 AND chartmaster.group_ = 'Provisions'
				GROUP BY accountgroups.sectioninaccounts,
					accountgroups.groupname,
					accountgroups.parentgroupname,
					chartdetails.accountcode,
					chartmaster.accountname,
					accountgroups.sequenceintb
				ORDER BY accountgroups.sectioninaccounts,
					accountgroups.sequenceintb,
					accountgroups.groupname,
					chartdetails.accountcode";

	$AccountsResult = DB_query($SQL);

	

$num= DB_num_rows($AccountsResult);


$rowCount= 0;	
while ($rowCount< $num) {
	$cellcount = 222;	
	
	while ($myrow=DB_fetch_array($AccountsResult)) {
		
		
	$AccountCode = $myrow['accountcode'];
	$AccountDesc = $myrow['accountname'];
	
	$AccountPeriodActual = $myrow['lastprdcfwd'] - $myrow['firstprdbfwd'];
	$AccountPeriodLY = $myrow['lylastprdcfwd'] - $myrow['lyfirstprdbfwd'];
	$AccountPeriodBudget = $myrow['lastprdbudgetcfwd'] - $myrow['firstprdbudgetbfwd'];
		
	
		
		$objPHPExcel->getActiveSheet()->setCellValue('A'.$cellcount, $AccountCode);
		$objPHPExcel->getActiveSheet()->setCellValue('B'.$cellcount, $AccountDesc);
		$objPHPExcel->getActiveSheet()->setCellValue('C'.$cellcount,  $AccountPeriodActual);
		$objPHPExcel->getActiveSheet()->setCellValue('D'.$cellcount, $AccountPeriodBudget);
		$objPHPExcel->getActiveSheet()->setCellValue('E'.$cellcount, $AccountPeriodLY);
		$cellcount++;
	
		
	}
		
		$rowCount++;
	

}	

$SQL = "SELECT accountgroups.sectioninaccounts,
					accountgroups.groupname,
					accountgroups.parentgroupname,
					chartdetails.accountcode ,
					chartmaster.accountname,
					SUM(CASE WHEN chartdetails.period='" . $_POST['FromPeriod'] . "' THEN chartdetails.bfwdgm ELSE 0 END) AS firstprdbfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['FromPeriod'] . "' THEN chartdetails.bfwdbudgetgm ELSE 0 END) AS firstprdbudgetbfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['ToPeriod'] . "' THEN chartdetails.bfwdgm + chartdetails.actualgm ELSE 0 END) AS lastprdcfwd,
					SUM(CASE WHEN chartdetails.period='" . ($_POST['FromPeriod'] - 12) . "' THEN chartdetails.bfwdgm ELSE 0 END) AS lyfirstprdbfwd,
					SUM(CASE WHEN chartdetails.period='" . ($_POST['ToPeriod']-12) . "' THEN chartdetails.bfwdgm + chartdetails.actualgm ELSE 0 END) AS lylastprdcfwd,
					SUM(CASE WHEN chartdetails.period='" . $_POST['ToPeriod'] . "' THEN chartdetails.bfwdbudgetgm + chartdetails.budgetgm ELSE 0 END) AS lastprdbudgetcfwd
			FROM chartmaster INNER JOIN accountgroups
			ON chartmaster.group_ = accountgroups.groupname INNER JOIN chartdetails
			ON chartmaster.accountcode= chartdetails.accountcode
			WHERE accountgroups.pandl=1 AND chartmaster.group_ = 'Income Tax'
				GROUP BY accountgroups.sectioninaccounts,
					accountgroups.groupname,
					accountgroups.parentgroupname,
					chartdetails.accountcode,
					chartmaster.accountname,
					accountgroups.sequenceintb
				ORDER BY accountgroups.sectioninaccounts,
					accountgroups.sequenceintb,
					accountgroups.groupname,
					chartdetails.accountcode";


	$AccountsResult = DB_query($SQL);

	

$num= DB_num_rows($AccountsResult);


$rowCount= 0;	
while ($rowCount< $num) {
	$cellcount = 254;	
	
	while ($myrow=DB_fetch_array($AccountsResult)) {
		
		
	$AccountCode = $myrow['accountcode'];
	$AccountDesc = $myrow['accountname'];
	
	$AccountPeriodActual = $myrow['lastprdcfwd'] - $myrow['firstprdbfwd'];
	$AccountPeriodLY = $myrow['lylastprdcfwd'] - $myrow['lyfirstprdbfwd'];
	$AccountPeriodBudget = $myrow['lastprdbudgetcfwd'] - $myrow['firstprdbudgetbfwd'];
		
	
		
		$objPHPExcel->getActiveSheet()->setCellValue('A'.$cellcount, $AccountCode);
		$objPHPExcel->getActiveSheet()->setCellValue('B'.$cellcount, $AccountDesc);
		$objPHPExcel->getActiveSheet()->setCellValue('C'.$cellcount,  $AccountPeriodActual);
		$objPHPExcel->getActiveSheet()->setCellValue('D'.$cellcount, $AccountPeriodBudget);
		$objPHPExcel->getActiveSheet()->setCellValue('E'.$cellcount, $AccountPeriodLY);
		$cellcount++;
	
		
	}
		
		$rowCount++;
	

}	

	
// Generate an updated excel file
// Redirect output to a client’s web browser (Excel2007)



//save the file to the server (Excel2007)

include('includes/header.inc');
	


$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
//$objWriter->save($inputFileName);
$objWriter->save($inputFileName);


    
    //<?php
	
echo '<br /><p></p><p></p> <p></p> <p></p> <p></p>  <p class="page_title_text"><a href="' . $inputFileName . '">' . _('File generated o.k. Click to view this P&L excel File ') . '</p>';




//echo $AccountCode;



$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);

?>