<?php error_reporting(0);
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start();
session_start();
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Asia/Manila');
if (PHP_SAPI == 'cli')
	die('This example should only be run from a Web Browser'); 
require_once dirname(__FILE__) . '/Classes/PHPExcel/IOFactory.php'; 
$objReader = PHPExcel_IOFactory::createReader('Excel5');
PHPExcel_Cell::setValueBinder( new PHPExcel_Cell_AdvancedValueBinder() );
include_once('../config/minho.php');
include_once('../config/autoloadClass.php');
$currentUser 	= $_SESSION['danvilles_username'];
$timeStamp	= date('Y-m-d');
$autoload	=	new autoloadClass();
$mecon		=	new condbClass();
$utility	=	new utilityClass();
$actArr 	= array();
$main		=	array();
$main2		=	array();
$main3		=	array();
$main4		=	array();
$promoId = (isset($_GET['id'])) ? strip_tags(trim($_GET['id'])) : "";
$mecon->query("SELECT Id, Project, Model, Promo, StartDate, EndDate, Amount, Conditions, PromoStatus, PromoDetails FROM promo WHERE Status = :status AND Id=:promoId ORDER BY StartDate DESC, Id");
$mecon->bind(':status', 'active');
$mecon->bind(':promoId', $promoId);
$main = $mecon->resultset(); 
$counter = count($main); 
$arr1 = array();
$arr2 = array();  

$arr1['id']					= $main[0]['Id']; 
$arr1['promo_status']		= html_entity_decode($main[0]['PromoStatus']); 
$arr1['conditions']			= html_entity_decode($main[0]['Conditions']); 
$arr1['promo']				= html_entity_decode($main[0]['Promo']); 
$arr1['promodetails']		= $main[0]['PromoDetails']; 
$arr1['amount']				= number_format($main[0]['Amount'],2);
$arr1['project']			= strtoupper(trim(html_entity_decode(stripslashes($main[0]['Project']))));
$arr1['model']				= strtoupper(trim(html_entity_decode(stripslashes($main[0]['Model']))));
$arr1['start_date_file']	= date('mdY', strtotime($main[0]['StartDate'])); 
$arr1['end_date_file']		= date('mdY', strtotime($main[0]['EndDate'])); 
$arr1['start_date']			= $main[0]['StartDate']; 
$arr1['end_date']			= $main[0]['EndDate']; 
$arr2[] 					= $arr1; 
$data = $arr2;
$fileName    = "promo_".strtolower($arr2[0]['model'])."_".$arr2[0]['start_date_file']."_".$arr2[0]['end_date_file'].".xls";
//STANDARD INFORMATION
	$objPHPExcel = $objReader->load("templates/promo.xls"); 
	foreach($data as $r => $dataRow){
		$objPHPExcel->setActiveSheetIndex(0)
				->setCellValue('A1', $dataRow['project'])
				->setCellValue('A2', $dataRow['model'])
				->setCellValue('B4', $dataRow['start_date'])
				->setCellValue('D4', $dataRow['end_date'])
				->setCellValue('B5', stripslashes($dataRow['promo']))
				->setCellValue('B6', $dataRow['promo_status'])
				->setCellValue('D6', $dataRow['amount'])
				->setCellValue('B7', stripslashes($dataRow['conditions']));

				$objPHPExcel->getActiveSheet()->getStyle('B4')->getNumberFormat()->setFormatCode('mm/dd/yyyy');
				$objPHPExcel->getActiveSheet()->getStyle('D4')->getNumberFormat()->setFormatCode('mm/dd/yyyy');
	}
 
//GETTING DETAILS
$dbDetailsArr 		= array();
$promoDetailsArr 	= array();
$promoMainArr 		= array();
$dbDetailsArr 		= json_decode($arr2[0]['promodetails']);
$ctr 				= count($dbDetailsArr->details);
for($a=0; $a<$ctr; $a++){ 
		$promoDetailsArr['id']				=	$a+1;
		$promoDetailsArr['phase']			=	$dbDetailsArr->details[$a]->phase;
		$promoDetailsArr['block']			=	$dbDetailsArr->details[$a]->block;
		$promoDetailsArr['lot']				=	$dbDetailsArr->details[$a]->lot; 
		$promoMainArr[]	=	$promoDetailsArr;
}
 
$baseRow = 10;
  
		if(count($promoMainArr)!=0){
		$data2 = $promoMainArr;
		//$objPHPExcel->getActiveSheet()->insertNewRowBefore($baseRow,1);
			foreach($data2 as $r => $dataRow){
				$row = $baseRow + $r + 1;
				$objPHPExcel->getActiveSheet()->insertNewRowBefore($row,1); 
				$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $dataRow['id'])
											  ->setCellValue('B'.$row, $dataRow['phase'])
											  ->setCellValue('C'.$row, $dataRow['block'])
											  ->setCellValue('D'.$row, $dataRow['lot']);
			}
		}
 

$actArr['UserType']			= "accountant,assistant_accountant";
$actArr['Remarks']			= "Downloaded Promo : ".$arr1['promo'];
$actArr['Action']			= "download";
$actArr['ActionType']		= "promo";
$actArr['ActionContent']	= json_encode($arr1);
$actArr['ActionBy'] 		= $currentUser;	
$actArr['ActionDate']		= $timeStamp;
$actArr['TransId']			= $promoId;
$utility->encodeActivityLog($actArr);
 
$objPHPExcel->getActiveSheet()->removeRow($baseRow-1,1);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$fileName.'"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
//header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
//header ('Pragma: public'); // HTTP/1.0

header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
header("Pragma: no-cache"); // HTTP 1.0.
header("Expires: 0"); // Proxies.


$objWriter->save('php://output'); 