<?php   
error_reporting(0);
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start();
session_start();
session_cache_limiter('private');
$cache_limiter 	= session_cache_limiter();
session_cache_expire(60);
$cache_expire 	= session_cache_expire(); 
date_default_timezone_set('Asia/Manila'); 
$CURRENT_DATE 	= date('Y-m-d H:i:s');
include_once('../config/minho.php');
include_once('../config/autoloadClass.php');
$autoload	=	new autoloadClass();
$mecon		=	new condbClass();
$formClass	=	new formClass();
$main		=	array();
$main2		=	array();
$arr3		=	array();
$main3		=	array();
$main4		=	array();
$mainId		=	(isset($_POST['mainid'])) ? $_POST['mainid'] : "";
$mecon->query("SELECT r.Id as Id, r.Project as Project, r.Model as Model, r.ReservationDate as ReservationDate, r.PricelistDate as PricelistDate, r.Phase as Phase, r.Block as Block, r.Lot as Lot, r.LastName as LastName, r.FirstName as FirstName, r.MiddleName as MiddleName, 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.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.CoBorrowerDetails as CoBorrowerDetails, r.Status as Status, r.ReservationStatus as ReservationStatus, r.CustomPriceDate as CustomPriceDate, bpm.NetEquity as NetEquity, bpm.StartOfEquity as StartOfEquity, bpm.PDCSubmitted as PDCSubmitted, bpm.PDCSubmittedStatus as PDCSubmittedStatus, bpm.PDCSubmittedStatusPagibig as PDCSubmittedStatusPagibig, bpm.LackingPDC as LackingPDC, bpm.MonthlyEquity as MonthlyEquity, bpm.TermsOfPayment as TermsOfPayment, bpm.PDCStatus as PDCStatus, bpm.PDCStatusDate as PDCStatusDate, bpm.ApprovedEquityDate as ApprovedEquityDate, r.SPA as spa, r.SPAContactNo as spa_contactno, r.SPAEmail as spa_email, r.SpouseEmail as spouse_email, r.ConstructionStatusUponReservation as construction_status, bpm.LoanableAmount as loanable_amount, bpm.TCP as tcp, bsm.PTOStatus as PTOStatus, bpm.Financing as Financing, bsm.PermitToOccupy as PermitToOccupy, bpm.PDCDownpayment as PDCDownpayment, bpm.PDCDiscount as PDCDiscount FROM reservation r LEFT JOIN buyer_payment_monitoring bpm ON bpm.ReservationId=r.Id LEFT JOIN buyer_status_monitoring bsm ON bsm.ReservationId=r.Id WHERE r.Status = :status AND r.Id = :id ORDER BY r.Id DESC");

$mecon->bind(':status', 'active');
$mecon->bind(':id', $mainId);
$main = $mecon->resultset(); 
$counter = count($main); 
$arr1 = array();
$arr2 = array();
$arr3 = array();
$other = array(); 

 
for($a=0; $a<$counter; $a++){ 

		$models						= $main[$a]['Model']; 
		$project					= $main[$a]['Project']; 
		$pricedate					= $main[$a]['PricelistDate']; 
		$phase						= $main[$a]['Phase']; 
		$block						= $main[$a]['Block']; 
		$lot						= $main[$a]['Lot']; 
		$other = $formClass->getOtherDetails($pricedate,$models,$project,$phase,$block,$lot);		
		/**********************/
		

		$arr1['id']					= $main[$a]['Id'];  
		
		$arr1['custom_price']		=  $main[$a]['CustomPriceDate'];
		$arr1['project']			= trim(html_entity_decode(stripslashes($main[$a]['Project'])));
		$arr1['model']				= trim(html_entity_decode(stripslashes($main[$a]['Model'])));
		$arr1['reservation_date']	=  date('m/d/Y', strtotime($main[$a]['ReservationDate'])); 
		$arr1['start_of_equity']	=  (($main[$a]['StartOfEquity']=='1970-01-01') || ($main[$a]['StartOfEquity']=='')) ? "" : date('m/d/Y', strtotime($main[$a]['StartOfEquity']));
		$arr1['pricelist_date']		=  date('m/d/Y', strtotime($main[$a]['PricelistDate']));
		$arr1['phase']				= trim(html_entity_decode(stripslashes($main[$a]['Phase'])));
		$arr1['block']				= trim(html_entity_decode(stripslashes($main[$a]['Block'])));
		$arr1['lot']				= trim(html_entity_decode(stripslashes($main[$a]['Lot'])));
		$arr1['last_name']			= trim(html_entity_decode(stripslashes($main[$a]['LastName'])));
		$arr1['first_name']			= trim(html_entity_decode(stripslashes($main[$a]['FirstName'])));
		$arr1['middle_name']		= trim(html_entity_decode(stripslashes($main[$a]['MiddleName'])));
		$arr1['broker']				= trim(html_entity_decode(stripslashes($main[$a]['Broker'])));
		$arr1['agent']				= trim(html_entity_decode(stripslashes($main[$a]['Agent'])));
		$arr1['spouse']				= trim(html_entity_decode(stripslashes($main[$a]['Spouse'])));
		$arr1['home_tel']			= trim(html_entity_decode(stripslashes($main[$a]['HomeTel'])));
		$arr1['cell_no']			= trim(html_entity_decode(stripslashes($main[$a]['CellphoneNo'])));
		$arr1['spouse_contact']		= trim(html_entity_decode(stripslashes($main[$a]['SpouseContactNo'])));
		$arr1['address1']			= trim(html_entity_decode(stripslashes($main[$a]['Address1'])));
		$arr1['address2']			= trim(html_entity_decode(stripslashes($main[$a]['Address2'])));
		$arr1['employer_name']		= trim(html_entity_decode(stripslashes($main[$a]['EmployerName'])));
		$arr1['employee_type']		= trim(html_entity_decode(stripslashes($main[$a]['EmployeeType'])));
		$arr1['gross_income']		= ($main[$a]['GrossIncome']!=0) ? $main[$a]['GrossIncome'] : $main[$a]['GrossIncome']; 
		$arr1['position']		= trim(html_entity_decode(stripslashes($main[$a]['Position'])));
		$arr1['buyer_profile']	= trim(html_entity_decode(stripslashes($main[$a]['BuyerProfile'])));
		$arr1['spouse_income']	= ($main[$a]['SpouseIncome']!=0) ? $main[$a]['SpouseIncome'] : $main[$a]['SpouseIncome'];
		$arr1['tinno']				= trim(html_entity_decode(stripslashes($main[$a]['TinNo'])));
		$arr1['office_tel']			= trim(html_entity_decode(stripslashes($main[$a]['OfficeTel'])));
		$arr1['aggregate_income']	= ($main[$a]['AggregateIncome']!=0) ? $main[$a]['AggregateIncome'] : $main[$a]['AggregateIncome'];
		$arr1['num_borrower']		= trim(html_entity_decode(stripslashes($main[$a]['NoCoBorrower'])));
		$arr1['details']			= trim(html_entity_decode(stripslashes($main[$a]['CoBorrowerDetails'])));
		$arr1['net_equity']			= trim(html_entity_decode(stripslashes($main[$a]['NetEquity'])));
		$arr1['spa']				= trim(html_entity_decode(stripslashes($main[$a]['spa'])));
		$arr1['spa_contactno'] 		= trim(html_entity_decode(stripslashes($main[$a]['spa_contactno'])));
		$arr1['spa_email'] 			= trim(html_entity_decode(stripslashes($main[$a]['spa_email'])));
		$arr1['spouse_email'] 		= trim(html_entity_decode(stripslashes($main[$a]['spouse_email']))); //26MAR23
		$arr1['construction_status'] = trim(html_entity_decode(stripslashes($main[$a]['construction_status']))); //28MAR23
		$arr1['loanable_amount']	= trim(html_entity_decode(stripslashes($main[$a]['loanable_amount'])));
		$arr1['tcp'] 				= trim(html_entity_decode(stripslashes($main[$a]['tcp'])));
		$arr1['financing']			=  ucfirst($main[$a]['Financing']);
		$arr1['permit_to_occupy']   =  ($main[$a]['PermitToOccupy']!='1970-01-01') ? $main[$a]['PermitToOccupy'] : "";
		
		$arr1['pto_status']			=  $main[$a]['PTOStatus'];
		$arr1['pdc_submitted']		=  $main[$a]['PDCSubmitted'];
		$arr1['pdc_submitted_status']		=  $main[$a]['PDCSubmittedStatus'];
		$arr1['pdc_submitted_status_pagibig']		=  $main[$a]['PDCSubmittedStatusPagibig'];
		$arr1['lacking_pdc']		=  $main[$a]['LackingPDC'];
		$arr1['monthly_equity']		=  $main[$a]['MonthlyEquity'];
		$arr1['terms_of_payment']	=  $main[$a]['TermsOfPayment'];
		$arr1['pop_pdc_status']			=  $main[$a]['PDCStatus'];
		$arr1['pop_pdc_status_date']	=  (($main[$a]['PDCStatusDate']=='1970-01-01') || ($main[$a]['PDCStatusDate']=='')) ? "" : date('m/d/Y', strtotime($main[$a]['PDCStatusDate']));
		$arr1['approved_equity_date']	=  (($main[$a]['ApprovedEquityDate']=='1970-01-01') || ($main[$a]['ApprovedEquityDate']=='')) ? "" : date('m/d/Y', strtotime($main[$a]['ApprovedEquityDate']));

		$arr1['pdc_downpayment']	= ($main[$a]['PDCDownpayment']!=0.00) ?  trim(html_entity_decode(stripslashes(str_replace(',','',$main[$a]['PDCDownpayment'])))) : str_replace(',','',$other['downpayment']); 

		 

		$arr1['pdc_discount']	= ($main[$a]['PDCDiscount']!=0.00) ?  trim(html_entity_decode(stripslashes(str_replace(',','',$main[$a]['PDCDiscount'])))) :  0.00; 



		//$arr1['pdc_discount']		=  $main[$a]['PDCDiscount'];


 		
	$arr2[] = $arr1;
}

$arr3['data'] = $arr2;

echo json_encode($arr3); 
exit();
ob_end_flush();