<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
//Convert number to words
if ( ! function_exists('excel')){
	
	function excel($headerData = '',$bodyData = '',$footerData = ''){
		//Excell download ready
		$CI =& get_instance();
		$CI->load->library('parser');
		$excelTitle = $headerData['rptTitle']."_(comp-".element('compName', $CI->session->userdata('logged_in'));
		$myFile = str_replace(" ", "_",$excelTitle).").xls";
		
		//pass retrieved data into template and return as a string
		$stringData = $CI->parser->parse('layouts/report/excel_header', $headerData, true);
		
		foreach($bodyData as $v){
			$stringData .= $CI->parser->parse('layouts/report/excelTransactionSummary', $v, true);
			$stringData .= "<tr><td colspan='2'></td></tr><tr><td colspan='2'></td></tr><tr><td colspan='2'></td></tr>";
		}
		$footerData['fields'] = count($bodyData[0]['selectFields']);
		$stringData .= $CI->parser->parse('layouts/report/footer', $footerData, true);
		/*echo $stringData;
		exit();*/
		//open excel and write string into excel
		$fh = fopen($myFile, 'w') or die("can't open file");
		fwrite($fh, $stringData);
		
		fclose($fh);
		//download excel file
		header("Content-Length: " . filesize($myFile));
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment; filename='.$myFile);
		
		readfile($myFile);
	}
}
?>
