<?php error_reporting(0);
set_time_limit(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');

$autoload	=	new autoloadClass();
$mecon		=	new condbClass();
$main		=	array();
$main2		=	array();
$main3		=	array();
$main4		=	array();

$monthFrom		= isset($_GET['month_from']) ? strip_tags($_GET['month_from']) : "";
$monthTo		= isset($_GET['month_to']) ? strip_tags($_GET['month_to']) : "";
$filterSearch = isset($_GET['filter']) ? str_replace("'"," ",$_GET['filter']) : "";
//$filterSearch	= isset($_GET['filter']) ? strip_tags($_GET['filter']) : "";
$isfilterdate = isset($_GET['isfilterdate']) ? strip_tags($_GET['isfilterdate']) : "yes";
  
$sql	=	"SELECT r.Id as Id, r.Project as Project, r.Model as Model, r.ReservationDate as ReservationDate, r.Phase as Phase, r.Block as Block, r.Lot as Lot, CONCAT(r.LastName,', ',r.FirstName,' ',r.MiddleName) as FullName, r.Broker as Broker, r.Agent as Agent, r.Spouse as Spouse, r.HomeTel as HomeTel, r.CellphoneNo as CellphoneNo, r.SpouseContactNo as SpouseContactNo, r.Address1 as Address1, r.Address2 as Address2, r.EmployerName as EmployerName, r.EmployeeType as EmployeeType, r.GrossIncome as GrossIncome, r.DiscountPromo as DiscountPromo, r.Position as Position, r.BuyerProfile as BuyerProfile, r.SpouseIncome as SpouseIncome, r.TinNo as TinNo, r.OfficeTel as OfficeTel, r.AggregateIncome as AggregateIncome, r.NoCoBorrower as NoCoBorrower, r.ReservationStatus as ReservationStatus, r.Status as rStatus, r.EmployeeType as EmployeeType, bsf.TakeOut as TakeOut, con.ReleasedPagibig as ReleasedPagibig, con.ProcessedDate as ProcessedDate, con.ReleasedDate as ReleasedDate, DATE_ADD(bsf.TakeOut, INTERVAL 18 MONTH) as conversions, con.BirDateSubmitted as BirDateSubmitted, con.BirDateReleased as BirDateReleased, con.BirEcar as BirEcar, con.RegistryDateSubmitted as RegistryDateSubmitted,con.RegistryDateReleased as RegistryDateReleased, con.RegistryNewTCTNo as RegistryNewTCTNo, con.CityHallDateSubmitted as CityHallDateSubmitted, con.CityHallDateReleased as CityHallDateReleased, con.CityHallNewTaxDecLand as CityHallNewTaxDecLand, con.CityHallNewTaxDecBldg as CityHallNewTaxDecBldg, con.Remarks as Remarks  FROM  reservation r LEFT JOIN buyer_status_folder_pagibig bsf ON bsf.ReservationId=r.Id LEFT JOIN conversion con ON con.ReservationId=r.Id WHERE r.Status = 'active' AND r.ReservationStatus='active' AND (bsf.TakeOut!='1970-01-01' OR bsf.TakeOut!=NULL) "; 
  
if(!empty($filterSearch)) { 
	$arr = explode(" ",$filterSearch); 
	foreach($arr as $ind){
		$ind = str_replace(',','',$ind);
		$sql.=" AND (Block LIKE '".$ind."%' ";
		$sql.=" OR CONCAT(LastName,', ',FirstName,' ',MiddleName) LIKE '%".htmlentities($ind)."%' ";
		$sql.=" OR UPPER(DATE_FORMAT(ReleasedPagibig,'%m/%d/%Y')) LIKE '".$ind."%' "; 
		$sql.=" OR Broker LIKE '%".htmlentities($ind)."%' ";

		$sql.=" OR UPPER(DATE_FORMAT(con.BirDateSubmitted,'%m/%d/%Y')) LIKE '".$ind."%' "; 
		$sql.=" OR UPPER(DATE_FORMAT(con.BirDateReleased,'%m/%d/%Y')) LIKE '".$ind."%' "; 
		$sql.=" OR con.BirEcar LIKE '%".htmlentities($ind)."%' ";
		$sql.=" OR UPPER(DATE_FORMAT(con.RegistryDateSubmitted,'%m/%d/%Y')) LIKE '".$ind."%' "; 
		$sql.=" OR UPPER(DATE_FORMAT(con.RegistryDateReleased,'%m/%d/%Y')) LIKE '".$ind."%' "; 
		$sql.=" OR con.RegistryNewTCTNo LIKE '%".htmlentities($ind)."%' ";
		$sql.=" OR UPPER(DATE_FORMAT(con.CityHallDateSubmitted,'%m/%d/%Y')) LIKE '".$ind."%' "; 
		$sql.=" OR UPPER(DATE_FORMAT(con.CityHallDateReleased,'%m/%d/%Y')) LIKE '".$ind."%' "; 
		$sql.=" OR con.CityHallNewTaxDecLand LIKE '%".htmlentities($ind)."%' ";
		$sql.=" OR con.CityHallNewTaxDecBldg LIKE '%".htmlentities($ind)."%' ";
		$sql.=" OR con.Remarks LIKE '%".htmlentities($ind)."%' ";


		$sql.=" OR EmployeeType LIKE '%".$ind."%' ";
		$sql.=" OR Agent LIKE '%".htmlentities($ind)."%' ";
		$sql.=" OR Model LIKE '%".$ind."%' ";  
		$sql.=" OR Spouse LIKE '%".htmlentities($ind)."%' ";
		$sql.=" OR Project LIKE '%".$ind."%' ";
		$sql.=" OR EmployerName LIKE '%".htmlentities($ind)."%' ";
		$sql.=" OR Position LIKE '%".htmlentities($ind)."%' ";
		$sql.=" OR Address1 LIKE '%".htmlentities($ind)."%' ";
		$sql.=" OR Address2 LIKE '%".htmlentities($ind)."%' ";
		$sql.=" OR HomeTel LIKE '".$ind."%' ";
		$sql.=" OR OfficeTel LIKE '".$ind."%' "; 
		$sql.=" OR CellphoneNo LIKE '".$ind."%' ";
		$sql.=" OR GrossIncome LIKE '".$ind."%' ";
		$sql.=" OR TinNo LIKE '".$ind."%' ";
		$sql.=" OR BuyerProfile LIKE '".$ind."%' "; 
		$sql.=" OR ReservationStatus  LIKE '".$ind."%' ";
		$sql.=" OR Lot LIKE '".$ind."%' )"; 
	}
}

if($isfilterdate=="yes"){ 
		if( !empty($monthFrom) && !empty($monthTo) ){
		$sql .= " AND con.ReleasedPagibig BETWEEN ('".date('Y-m-d', strtotime($monthFrom))."') AND ('".date('Y-m-d', strtotime($monthTo))."')";
		}else if( !empty($monthFrom)){
			$sql .= " AND con.ReleasedPagibig >= ('".date('Y-m-d', strtotime($monthFrom))."') ";
		}else if( !empty($monthTo)){
			$sql .= " AND con.ReleasedPagibig <= ('".date('Y-m-d', strtotime($monthTo))."') ";
		}
} 
$requestData	=	$_REQUEST; 
//$sql .= 'ORDER BY Id desc ';
//$sql .= ' ORDER BY cast(r.Block as unsigned), cast(r.Lot as unsigned) asc  '; 
//$sql .= " ORDER BY bsf.TakeOut asc, cast(r.Block as unsigned) , cast(r.Lot as unsigned) asc  ";
$sql .= " ORDER BY bsf.TakeOut desc, cast(r.Block as unsigned) , cast(r.Lot as unsigned) asc  ";

$mecon->query($sql);  
$main = $mecon->resultset(); 
$counter = count($main); 
$arr1 = array();
$arr2 = array();  
$arr3 = array();  

for($a=0; $a<$counter; $a++){  
	$arr1[0]	= $a+1; 
	$arr1[1] 	= strtoupper(trim(html_entity_decode(stripslashes($main[$a]['FullName']))));
	$arr1[2] 	= trim($main[$a]['Block']);
	$arr1[3] 	= trim($main[$a]['Lot']); 
	//$arr1[4] 	= (($main[$a]['TakeOut']=='1970-01-01') || ($main[$a]['TakeOut']=='')) ? "" : date('m/d/Y', strtotime($main[$a]['TakeOut'])); 

	$arr1[4] 	= (($main[$a]['TakeOut']=='1970-01-01') || ($main[$a]['TakeOut']=='')) ? "" : $main[$a]['TakeOut'];  
   	$takeoutDate 	= $main[$a]['TakeOut'];
   	//$conversionDate = date('m/d/Y', strtotime($takeoutDate. ' + 18 month'));  
	//$arr1[5] 	= $conversionDate; 
	$arr1[5]   = $main[$a]['conversions'];

	//$releasedPagibigDate = date('m/d/Y', strtotime($main[$a]['ReleasedPagibig']));
	$releasedPagibigDate = $main[$a]['ReleasedPagibig'];
	$releasedPagibigDate = ($releasedPagibigDate=='01/01/1970' || $releasedPagibigDate=='1970-01-01' || $releasedPagibigDate=='') ? "" : $releasedPagibigDate;
	$arr1[6] 	= $releasedPagibigDate; 

	/*
 	$processedDate 	= date('m/d/Y', strtotime($main[$a]['ProcessedDate']));
 	$processedDate 	= ($processedDate=='01/01/1970') ? "" : $processedDate;
	$arr1[7] 	= $processedDate; 
	$releasedDate 	= date('m/d/Y', strtotime($main[$a]['ReleasedDate']));
	$releasedDate 	= ($releasedDate=='01/01/1970') ? "" : $releasedDate;
	$arr1[8] 	= $releasedDate; */


	//BIR Date Submitted
				//$BirDateSubmitted 	= date('m/d/Y', strtotime($main[$a]['BirDateSubmitted']));
				$BirDateSubmitted 	= $main[$a]['BirDateSubmitted'];
				$BirDateSubmitted 	= ($BirDateSubmitted=='01/01/1970' || $BirDateSubmitted=='1970-01-01' || $BirDateSubmitted=='') ? "" : $BirDateSubmitted; 
				$arr1[7] 			= $BirDateSubmitted; 
 
 				//BIR Date Released
				//$BirDateReleased 	= date('m/d/Y', strtotime($main[$a]['BirDateReleased']));
				$BirDateReleased 	= $main[$a]['BirDateReleased'];
				$BirDateReleased 	= ($BirDateReleased=='01/01/1970' || $BirDateReleased=='1970-01-01' || $BirDateReleased=='') ? "" : $BirDateReleased; 
				$arr1[8] 			= $BirDateReleased; 

				//BIR ECAR
				$BirEcar 			= $main[$a]['BirEcar']; 
				$arr1[9] 			= $BirEcar; 

				//Registry Date Released
				//$RegistryDateReleased 		= date('m/d/Y', strtotime($main[$a]['RegistryDateReleased']));
				$RegistryDateReleased 		= $main[$a]['RegistryDateReleased'];
				$RegistryDateReleased 		= ($RegistryDateReleased=='01/01/1970' || $RegistryDateReleased=='1970-01-01' || $RegistryDateReleased=='') ? "" : $RegistryDateReleased; 
				$arr1[10] 					= $RegistryDateReleased; 

				//Registry Date Submitted
				//$RegistryDateSubmitted 		= date('m/d/Y', strtotime($main[$a]['RegistryDateSubmitted']));
				$RegistryDateSubmitted 		= $main[$a]['RegistryDateSubmitted'];
				$RegistryDateSubmitted 		= ($RegistryDateSubmitted=='01/01/1970' || $RegistryDateSubmitted=='1970-01-01' || $RegistryDateSubmitted=='') ? "" : $RegistryDateSubmitted; 
				$arr1[11] 					= $RegistryDateSubmitted;

				//Registry New TCT No. 
				$RegistryNewTCTNo 			= $main[$a]['RegistryNewTCTNo']; 
				$arr1[12] 					= $RegistryNewTCTNo; 

				//City Hall Date Submitted
				//$CityHallDateSubmitted 		= date('m/d/Y', strtotime($main[$a]['CityHallDateSubmitted']));
				$CityHallDateSubmitted 		= $main[$a]['CityHallDateSubmitted'];
				$CityHallDateSubmitted 		= ($CityHallDateSubmitted=='01/01/1970' || $CityHallDateSubmitted=='1970-01-01' || $CityHallDateSubmitted=='') ? "" : $CityHallDateSubmitted; 
				$arr1[13] 					= $CityHallDateSubmitted;

				//City Hall Date Released
				//$CityHallDateReleased 		= date('m/d/Y', strtotime($main[$a]['CityHallDateReleased']));
				$CityHallDateReleased 		= $main[$a]['CityHallDateReleased'];
				$CityHallDateReleased 		= ($CityHallDateReleased=='01/01/1970' || $CityHallDateReleased=='1970-01-01' || $CityHallDateReleased=='') ? "" : $CityHallDateReleased; 
				$arr1[14] 					= $CityHallDateReleased;

				//City Hall New Tax Dec Land
				$CityHallNewTaxDecLand		= $main[$a]['CityHallNewTaxDecLand']; 
				$arr1[15] 					= $CityHallNewTaxDecLand; 

				//City Hall New Tax Dec Bldg
				$CityHallNewTaxDecBldg		= $main[$a]['CityHallNewTaxDecBldg']; 
				$arr1[16] 					= $CityHallNewTaxDecBldg; 


				//Remarks
				$Remarks					= $main[$a]['Remarks']; 
				$arr1[17] 					= $Remarks; 




 	$arr2[] 	= $arr1;
}

$arr3['data'] = $arr2;
$fileName    = "status_of_accounts_for_conversion.xls";
$objPHPExcel = $objReader->load("templates/status_of_accounts_for_conversion.xls"); 
$baseRow = 4; 
for($a=0; $a<count($arr3['data']); $a++){ 
	$row = $baseRow + $a + 1;
				$objPHPExcel->getActiveSheet()->insertNewRowBefore($row,1); 
				$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $arr3['data'][$a][1])
											  ->setCellValue('B'.$row, $arr3['data'][$a][2])
											  ->setCellValue('C'.$row, $arr3['data'][$a][3])
											  ->setCellValue('D'.$row, $arr3['data'][$a][4])
											  ->setCellValue('E'.$row, $arr3['data'][$a][5])
											  ->setCellValue('F'.$row, $arr3['data'][$a][6])
											  ->setCellValue('G'.$row, $arr3['data'][$a][7])
											  ->setCellValue('H'.$row, $arr3['data'][$a][8])
											  ->setCellValue('I'.$row, $arr3['data'][$a][9])
											  ->setCellValue('J'.$row, $arr3['data'][$a][10])
											  ->setCellValue('K'.$row, $arr3['data'][$a][11])
											  ->setCellValue('L'.$row, $arr3['data'][$a][12])
											  ->setCellValue('M'.$row, $arr3['data'][$a][13])
											  ->setCellValue('N'.$row, $arr3['data'][$a][14])
											  ->setCellValue('O'.$row, $arr3['data'][$a][15])
											  ->setCellValue('P'.$row, $arr3['data'][$a][16])
											  ->setCellValue('Q'.$row, $arr3['data'][$a][17]);



											  
			$objPHPExcel->getActiveSheet()->getStyle('D'.$row)->getNumberFormat()->setFormatCode('mm/dd/yyyy');
			$objPHPExcel->getActiveSheet()->getStyle('E'.$row)->getNumberFormat()->setFormatCode('mm/dd/yyyy');								
			$objPHPExcel->getActiveSheet()->getStyle('F'.$row)->getNumberFormat()->setFormatCode('mm/dd/yyyy');		
			$objPHPExcel->getActiveSheet()->getStyle('G'.$row)->getNumberFormat()->setFormatCode('mm/dd/yyyy');		
			$objPHPExcel->getActiveSheet()->getStyle('H'.$row)->getNumberFormat()->setFormatCode('mm/dd/yyyy');		
			$objPHPExcel->getActiveSheet()->getStyle('J'.$row)->getNumberFormat()->setFormatCode('mm/dd/yyyy');		
			$objPHPExcel->getActiveSheet()->getStyle('K'.$row)->getNumberFormat()->setFormatCode('mm/dd/yyyy');		
			$objPHPExcel->getActiveSheet()->getStyle('M'.$row)->getNumberFormat()->setFormatCode('mm/dd/yyyy');		
			$objPHPExcel->getActiveSheet()->getStyle('N'.$row)->getNumberFormat()->setFormatCode('mm/dd/yyyy');					     
 }  
$objPHPExcel->getActiveSheet()->removeRow($baseRow,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'); 