<?   
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'] : ""; 
 
//check first if reservation id exist in releases_to_broker database table else get records in buyer_payment_monitoring and reservation table
 
$mecon->query("SELECT r.Id as Id, CONCAT(r.LastName,', ',r.FirstName,' ',r.MiddleName) as FullName, r.Phase as Phase, r.Block as Block, r.Lot as Lot, m.TCP, r.Model, r.Project, r.Broker as Broker, r.Agent as Agent, rb.DateRFDeposited, rb.LessDiscount, rb.MiscExpensesRate, rb.Rate, rb.WHT, rb.FirstReleaseCheckbox, rb.CACheckbox, rb.FirstReleaseAmount, rb.FirstReleaseWHT, rb.FirstReleaseCash, rb.FirstReleaseNet, rb.FirstReleaseDate, rb.FirstReleaseCheck, rb.SecondReleaseCheckbox, rb.SecondReleaseAmount, rb.SecondReleaseWHT, rb.SecondReleaseCash, rb.SecondReleaseNet, rb.SecondReleaseDate, rb.SecondReleaseCheck, rb.ThirdReleaseCheckbox, rb.ThirdReleaseAmount, rb.ThirdReleaseWHT, rb.ThirdReleaseCash, rb.ThirdReleaseNet, rb.ThirdReleaseDate, rb.ThirdReleaseCheck, rb.TotalReleaseAmount, rb.TotalReleaseWHT, rb.TotalReleaseCash, rb.TotalReleaseNet, rb.ReleasesStatus, rb.ReleasesRemarks, rb.Reservation as Reservation, rb.LessCA as LessCA, rb.ReservationLessCA as ReservationLessCA FROM reservation r LEFT JOIN releases_to_broker rb ON rb.ReservationId=r.Id LEFT JOIN buyer_payment_monitoring m ON m.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(); 
$userType	=	$_SESSION['castalia_user_type'];
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']							= trim(html_entity_decode(stripslashes($main[$a]['Id']))); 
$arr1['full_name']					= trim(html_entity_decode(stripslashes($main[$a]['FullName']))); 
$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['tcp']						= trim(html_entity_decode(stripslashes($main[$a]['TCP']))); 

$arr1['tcp']	= (!empty($main[$a]['TCP'])) ?  trim(html_entity_decode(stripslashes($main[$a]['TCP']))) : $other['contract_price']; 
 
$arr1['reservation']				= trim(html_entity_decode(stripslashes($main[$a]['Reservation']))); 
$arr1['less_ca']					= trim(html_entity_decode(stripslashes($main[$a]['LessCA']))); 
$arr1['reservation_less_ca']		= trim(html_entity_decode(stripslashes($main[$a]['ReservationLessCA']))); 

$arr1['model']						= trim(html_entity_decode(stripslashes($main[$a]['Model']))); 
$arr1['project']					= trim(html_entity_decode(stripslashes($main[$a]['Project']))); 
$arr1['broker']						= trim(html_entity_decode(stripslashes($main[$a]['Broker']))); 
$arr1['agent']						= trim(html_entity_decode(stripslashes($main[$a]['Agent']))); 
$arr1['date_rf_deposited']			= date('m/d/Y', strtotime($main[$a]['DateRFDeposited']));
$arr1['less_discount']				= trim(html_entity_decode(stripslashes($main[$a]['LessDiscount']))); 
$arr1['misc_expense_rate']			= trim(html_entity_decode(stripslashes($main[$a]['MiscExpensesRate']))); 
$arr1['rate']						= trim(html_entity_decode(stripslashes($main[$a]['Rate']))); 
$arr1['wht']						= trim(html_entity_decode(stripslashes($main[$a]['WHT']))); 
$arr1['first_release_checkbox']		= trim(html_entity_decode(stripslashes($main[$a]['FirstReleaseCheckbox']))); 
$arr1['ca_checkbox']				= trim(html_entity_decode(stripslashes($main[$a]['CACheckbox']))); 
$arr1['first_release_amount']		= trim(html_entity_decode(stripslashes($main[$a]['FirstReleaseAmount']))); 
$arr1['first_release_wht']			= trim(html_entity_decode(stripslashes($main[$a]['FirstReleaseWHT']))); 
$arr1['first_release_cash']			= trim(html_entity_decode(stripslashes($main[$a]['FirstReleaseCash']))); 
$arr1['first_release_net']			= trim(html_entity_decode(stripslashes($main[$a]['FirstReleaseNet']))); 
$arr1['first_release_date']			= date('m/d/Y', strtotime($main[$a]['FirstReleaseDate'])); 
$arr1['first_release_check']		= trim(html_entity_decode(stripslashes($main[$a]['FirstReleaseCheck']))); 

$arr1['second_release_checkbox']	= trim(html_entity_decode(stripslashes($main[$a]['SecondReleaseCheckbox']))); 
$arr1['second_release_amount']		= trim(html_entity_decode(stripslashes($main[$a]['SecondReleaseAmount']))); 
$arr1['second_release_wht']			= trim(html_entity_decode(stripslashes($main[$a]['SecondReleaseWHT']))); 
$arr1['second_release_cash']		= trim(html_entity_decode(stripslashes($main[$a]['SecondReleaseCash']))); 
$arr1['second_release_net']			= trim(html_entity_decode(stripslashes($main[$a]['SecondReleaseNet']))); 
 
$arr1['second_release_date']		= date('m/d/Y', strtotime($main[$a]['SecondReleaseDate'])); 

$arr1['second_release_check']		= trim(html_entity_decode(stripslashes($main[$a]['SecondReleaseCheck']))); 

$arr1['third_release_checkbox']		= trim(html_entity_decode(stripslashes($main[$a]['ThirdReleaseCheckbox']))); 
$arr1['third_release_amount']		= trim(html_entity_decode(stripslashes($main[$a]['ThirdReleaseAmount']))); 
$arr1['third_release_wht']			= trim(html_entity_decode(stripslashes($main[$a]['ThirdReleaseWHT']))); 
$arr1['third_release_cash']			= trim(html_entity_decode(stripslashes($main[$a]['ThirdReleaseCash']))); 
$arr1['third_release_net']			= trim(html_entity_decode(stripslashes($main[$a]['ThirdReleaseNet']))); 
 
$arr1['third_release_date']			= date('m/d/Y', strtotime($main[$a]['ThirdReleaseDate'])); 

$arr1['third_release_check']		= trim(html_entity_decode(stripslashes($main[$a]['ThirdReleaseCheck']))); 
$arr1['total_release_amount']		= trim(html_entity_decode(stripslashes($main[$a]['TotalReleaseAmount']))); 
$arr1['total_release_wht']			= trim(html_entity_decode(stripslashes($main[$a]['TotalReleaseWHT']))); 
$arr1['total_release_cash']			= trim(html_entity_decode(stripslashes($main[$a]['TotalReleaseCash']))); 
$arr1['total_release_net']			= trim(html_entity_decode(stripslashes($main[$a]['TotalReleaseNet']))); 
$arr1['release_status']				= trim(html_entity_decode(stripslashes($main[$a]['ReleasesStatus']))); 
$arr1['release_remarks']			= trim(html_entity_decode(stripslashes($main[$a]['ReleasesRemarks']))); 

/********************/


	$arr2[] = $arr1;
}
$arr3['data'] = $arr2;
echo json_encode($arr3); 
exit();
ob_end_flush();

