<?   
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_for_wht 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.Agent as Agent, m.DateRFDeposited, rb.LessDiscount, rb.MiscExpensesRate, rb.Rate, rb.WHT, rb.FirstReleaseCheckbox, rb.FirstReleaseType, rb.FirstReleaseBrokerAmount, rb.FirstReleaseAgentAmount, rb.FirstReleaseDate, rb.FirstReleaseBrokerCheck, rb.FirstReleaseAgentCheck, rb.SecondReleaseCheckbox, rb.SecondReleaseType, rb.SecondReleaseBrokerAmount, rb.SecondReleaseAgentAmount, 
	rb.SecondReleaseDate, rb.SecondReleaseBrokerCheck, rb.SecondReleaseAgentCheck, rb.ThirdReleaseCheckbox, rb.ThirdReleaseType, rb.ThirdReleaseBrokerAmount, rb.ThirdReleaseAgentAmount, rb.ThirdReleaseDate, rb.ThirdReleaseBrokerCheck, rb.ThirdReleaseAgentCheck, rb.FourthReleaseCheckbox, rb.FourthReleaseType, rb.FourthReleaseBrokerAmount, rb.FourthReleaseAgentAmount, rb.FourthReleaseDate, rb.FourthReleaseBrokerCheck, rb.FourthReleaseAgentCheck, rb.FifthReleaseCheckbox, rb.FifthReleaseType, rb.FifthReleaseBrokerAmount, rb.FifthReleaseAgentAmount, rb.FifthReleaseDate, rb.FifthReleaseBrokerCheck, rb.FifthReleaseAgentCheck, rb.SixReleaseCheckbox, rb.SixReleaseType, rb.SixReleaseBrokerAmount, rb.SixReleaseAgentAmount, rb.SixReleaseDate, rb.SixReleaseBrokerCheck, rb.SixReleaseAgentCheck, rb.SevenReleaseCheckbox, rb.SevenReleaseType, rb.SevenReleaseBrokerAmount, rb.SevenReleaseAgentAmount, rb.SevenReleaseDate, rb.SevenReleaseBrokerCheck, rb.SevenReleaseAgentCheck, rb.EightReleaseCheckbox, rb.EightReleaseType, rb.EightReleaseBrokerAmount, rb.EightReleaseAgentAmount, rb.EightReleaseDate, rb.EightReleaseBrokerCheck, rb.EightReleaseAgentCheck, rb.NineReleaseCheckbox, rb.NineReleaseType, rb.NineReleaseBrokerAmount, rb.NineReleaseAgentAmount, rb.NineReleaseDate, rb.NineReleaseBrokerCheck, rb.NineReleaseAgentCheck, rb.TenReleaseCheckbox, rb.TenReleaseType, rb.TenReleaseBrokerAmount, rb.TenReleaseAgentAmount, rb.TenReleaseDate, rb.TenReleaseBrokerCheck, rb.TenReleaseAgentCheck, rb.ElevenReleaseCheckbox, rb.ElevenReleaseType, rb.ElevenReleaseBrokerAmount, rb.ElevenReleaseAgentAmount, rb.ElevenReleaseDate, rb.ElevenReleaseBrokerCheck, rb.ElevenReleaseAgentCheck, rb.TwelveReleaseCheckbox, rb.TwelveReleaseType, rb.TwelveReleaseBrokerAmount, rb.TwelveReleaseAgentAmount, rb.TwelveReleaseDate, rb.TwelveReleaseBrokerCheck, rb.TwelveReleaseAgentCheck, rb.BrokerAgentAmount,  rb.BrokerAmount, rb.AgentAmount, rb.AddCashAdvance,rb.TotalReleaseAmount, rb.FullCheckbox, rb.FullAmount, rb.FullNet, rb.FullCheck, rb.FullDate, rb.TotalAmount, rb.TotalWHT, rb.ReleasesStatus, rb.ReleasesRemarks, rb.Reservation as Reservation, rb.LessCA as LessCA, rb.ReservationLessCA as ReservationLessCA, 	CONCAT(ba.FirstName,' ',ba.MiddleName,' ',ba.LastName) as Broker, rb.FullDate, ba.Realty as Realty,
	(SELECT AccountNumber FROM brokeragent WHERE brokeragent.Realty=ba.Realty AND LOWER(brokeragent.Fullname)=LOWER(r.Agent) AND brokeragent.BrokerOrAgent='agent'  AND LOWER(brokeragent.Realty)=LOWER(r.Broker) LIMIT 1) as AgentAccount, 
    (SELECT AccountNumber FROM brokeragent WHERE brokeragent.Id=ba.Id AND brokeragent.Realty=ba.Realty AND brokeragent.Fullname=ba.Fullname AND brokeragent.BrokerOrAgent='broker') as BrokerAccount,  
	m.TakeOutDate as TakeOutDate, rb.NetSellingPrice as NetSellingPrice, m.LoanableAmount as LoanableAmount    
	FROM reservation r 
 	LEFT JOIN releases_to_broker_for_wht rb ON rb.ReservationId=r.Id LEFT JOIN buyer_payment_monitoring m ON m.ReservationId=r.Id 
	LEFT JOIN brokeragent ba ON ba.Realty=r.Broker
	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']						= strtoupper(trim(html_entity_decode(stripslashes($main[$a]['Broker'])))); 
$arr1['agent']						= trim(html_entity_decode(stripslashes($main[$a]['Agent']))); 
$arr1['realty']						= strtoupper(trim(html_entity_decode(stripslashes($main[$a]['Realty'])))); 
$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['net_selling_price']			= trim(html_entity_decode(stripslashes($main[$a]['NetSellingPrice']))); 
$arr1['loanable_amount']			= trim(html_entity_decode(stripslashes($main[$a]['LoanableAmount']))); 


 


$arr1['first_release_checkbox']				= trim(html_entity_decode(stripslashes($main[$a]['FirstReleaseCheckbox'])));  
$arr1['first_release_type']					= trim(html_entity_decode(stripslashes($main[$a]['FirstReleaseType']))); 
$arr1['first_release_broker_amount']		= number_format(trim(html_entity_decode(stripslashes($main[$a]['FirstReleaseBrokerAmount']))),2); 
$arr1['first_release_agent_amount']			= number_format(trim(html_entity_decode(stripslashes($main[$a]['FirstReleaseAgentAmount']))),2); 
$arr1['first_release_date']					= date('m/d/Y', strtotime($main[$a]['FirstReleaseDate'])); 
$arr1['first_release_broker_check']			= trim(html_entity_decode(stripslashes($main[$a]['FirstReleaseBrokerCheck']))); 
$arr1['first_release_agent_check']			= trim(html_entity_decode(stripslashes($main[$a]['FirstReleaseAgentCheck']))); 

$arr1['second_release_checkbox']			= trim(html_entity_decode(stripslashes($main[$a]['SecondReleaseCheckbox'])));
$arr1['second_release_type']				= trim(html_entity_decode(stripslashes($main[$a]['SecondReleaseType'])));  
$arr1['second_release_broker_amount']		= number_format(trim(html_entity_decode(stripslashes($main[$a]['SecondReleaseBrokerAmount']))),2); 
$arr1['second_release_agent_amount']		= number_format(trim(html_entity_decode(stripslashes($main[$a]['SecondReleaseAgentAmount']))),2);
$arr1['second_release_date']				= date('m/d/Y', strtotime($main[$a]['SecondReleaseDate'])); 
$arr1['second_release_broker_check']		= trim(html_entity_decode(stripslashes($main[$a]['SecondReleaseBrokerCheck']))); 
$arr1['second_release_agent_check']			= trim(html_entity_decode(stripslashes($main[$a]['SecondReleaseAgentCheck']))); 

$arr1['third_release_checkbox']				= trim(html_entity_decode(stripslashes($main[$a]['ThirdReleaseCheckbox']))); 
$arr1['third_release_type']					= trim(html_entity_decode(stripslashes($main[$a]['ThirdReleaseType'])));  
$arr1['third_release_broker_amount']		= number_format(trim(html_entity_decode(stripslashes($main[$a]['ThirdReleaseBrokerAmount']))),2);
$arr1['third_release_agent_amount']			= number_format(trim(html_entity_decode(stripslashes($main[$a]['ThirdReleaseAgentAmount']))),2);
$arr1['third_release_date']					= date('m/d/Y', strtotime($main[$a]['ThirdReleaseDate'])); 
$arr1['third_release_broker_check']			= trim(html_entity_decode(stripslashes($main[$a]['ThirdReleaseBrokerCheck']))); 
$arr1['third_release_agent_check']			= trim(html_entity_decode(stripslashes($main[$a]['ThirdReleaseAgentCheck']))); 
 
$arr1['fourth_release_checkbox']			= trim(html_entity_decode(stripslashes($main[$a]['FourthReleaseCheckbox']))); 
$arr1['fourth_release_type']				= trim(html_entity_decode(stripslashes($main[$a]['FourthReleaseType'])));  
$arr1['fourth_release_broker_amount']		= number_format(trim(html_entity_decode(stripslashes($main[$a]['FourthReleaseBrokerAmount']))),2);
$arr1['fourth_release_agent_amount']		= number_format(trim(html_entity_decode(stripslashes($main[$a]['FourthReleaseAgentAmount']))),2);
$arr1['fourth_release_date']				= date('m/d/Y', strtotime($main[$a]['FourthReleaseDate'])); 
$arr1['fourth_release_broker_check']		= trim(html_entity_decode(stripslashes($main[$a]['FourthReleaseBrokerCheck']))); 
$arr1['fourth_release_agent_check']			= trim(html_entity_decode(stripslashes($main[$a]['FourthReleaseAgentCheck']))); 

$arr1['fifth_release_checkbox']				= trim(html_entity_decode(stripslashes($main[$a]['FifthReleaseCheckbox']))); 
$arr1['fifth_release_type']					= trim(html_entity_decode(stripslashes($main[$a]['FifthReleaseType'])));  
$arr1['fifth_release_broker_amount']		= number_format(trim(html_entity_decode(stripslashes($main[$a]['FifthReleaseBrokerAmount']))),2);
$arr1['fifth_release_agent_amount']			= number_format(trim(html_entity_decode(stripslashes($main[$a]['FifthReleaseAgentAmount']))),2);
$arr1['fifth_release_date']					= date('m/d/Y', strtotime($main[$a]['FifthReleaseDate'])); 
$arr1['fifth_release_broker_check']			= trim(html_entity_decode(stripslashes($main[$a]['FifthReleaseBrokerCheck']))); 
$arr1['fifth_release_agent_check']			= trim(html_entity_decode(stripslashes($main[$a]['FifthReleaseAgentCheck']))); 

$arr1['six_release_checkbox']				= trim(html_entity_decode(stripslashes($main[$a]['SixReleaseCheckbox']))); 
$arr1['six_release_type']					= trim(html_entity_decode(stripslashes($main[$a]['SixReleaseType'])));  
$arr1['six_release_broker_amount']			= number_format(trim(html_entity_decode(stripslashes($main[$a]['SixReleaseBrokerAmount']))),2);
$arr1['six_release_agent_amount']			= number_format(trim(html_entity_decode(stripslashes($main[$a]['SixReleaseAgentAmount']))),2);
$arr1['six_release_date']					= date('m/d/Y', strtotime($main[$a]['SixReleaseDate'])); 
$arr1['six_release_broker_check']			= trim(html_entity_decode(stripslashes($main[$a]['SixReleaseBrokerCheck']))); 
$arr1['six_release_agent_check']			= trim(html_entity_decode(stripslashes($main[$a]['SixReleaseAgentCheck']))); 

$arr1['seven_release_checkbox']				= trim(html_entity_decode(stripslashes($main[$a]['SevenReleaseCheckbox']))); 
$arr1['seven_release_type']					= trim(html_entity_decode(stripslashes($main[$a]['SevenReleaseType'])));  
$arr1['seven_release_broker_amount']		= number_format(trim(html_entity_decode(stripslashes($main[$a]['SevenReleaseBrokerAmount']))),2);
$arr1['seven_release_agent_amount']			= number_format(trim(html_entity_decode(stripslashes($main[$a]['SevenReleaseAgentAmount']))),2);
$arr1['seven_release_date']					= date('m/d/Y', strtotime($main[$a]['SevenReleaseDate'])); 
$arr1['seven_release_broker_check']			= trim(html_entity_decode(stripslashes($main[$a]['SevenReleaseBrokerCheck']))); 
$arr1['seven_release_agent_check']			= trim(html_entity_decode(stripslashes($main[$a]['SevenReleaseAgentCheck']))); 

$arr1['eight_release_checkbox']				= trim(html_entity_decode(stripslashes($main[$a]['EightReleaseCheckbox']))); 
$arr1['eight_release_type']					= trim(html_entity_decode(stripslashes($main[$a]['EightReleaseType'])));  
$arr1['eight_release_broker_amount']		= number_format(trim(html_entity_decode(stripslashes($main[$a]['EightReleaseBrokerAmount']))),2);
$arr1['eight_release_agent_amount']			= number_format(trim(html_entity_decode(stripslashes($main[$a]['EightReleaseAgentAmount']))),2);
$arr1['eight_release_date']					= date('m/d/Y', strtotime($main[$a]['EightReleaseDate'])); 
$arr1['eight_release_broker_check']			= trim(html_entity_decode(stripslashes($main[$a]['EightReleaseBrokerCheck']))); 
$arr1['eight_release_agent_check']			= trim(html_entity_decode(stripslashes($main[$a]['EightReleaseAgentCheck']))); 
 
$arr1['nine_release_checkbox']				= trim(html_entity_decode(stripslashes($main[$a]['NineReleaseCheckbox']))); 
$arr1['nine_release_type']					= trim(html_entity_decode(stripslashes($main[$a]['NineReleaseType'])));  
$arr1['nine_release_broker_amount']			= number_format(trim(html_entity_decode(stripslashes($main[$a]['NineReleaseBrokerAmount']))),2);
$arr1['nine_release_agent_amount']			= number_format(trim(html_entity_decode(stripslashes($main[$a]['NineReleaseAgentAmount']))),2);
$arr1['nine_release_date']					= date('m/d/Y', strtotime($main[$a]['NineReleaseDate'])); 
$arr1['nine_release_broker_check']			= trim(html_entity_decode(stripslashes($main[$a]['NineReleaseBrokerCheck']))); 
$arr1['nine_release_agent_check']			= trim(html_entity_decode(stripslashes($main[$a]['NineReleaseAgentCheck']))); 

$arr1['ten_release_checkbox']				= trim(html_entity_decode(stripslashes($main[$a]['TenReleaseCheckbox']))); 
$arr1['ten_release_type']					= trim(html_entity_decode(stripslashes($main[$a]['TenReleaseType'])));  
$arr1['ten_release_broker_amount']			= number_format(trim(html_entity_decode(stripslashes($main[$a]['TenReleaseBrokerAmount']))),2);
$arr1['ten_release_agent_amount']			= number_format(trim(html_entity_decode(stripslashes($main[$a]['TenReleaseAgentAmount']))),2);
$arr1['ten_release_date']					= date('m/d/Y', strtotime($main[$a]['TenReleaseDate'])); 
$arr1['ten_release_broker_check']			= trim(html_entity_decode(stripslashes($main[$a]['TenReleaseBrokerCheck']))); 
$arr1['ten_release_agent_check']			= trim(html_entity_decode(stripslashes($main[$a]['TenReleaseAgentCheck']))); 

$arr1['eleven_release_checkbox']			= trim(html_entity_decode(stripslashes($main[$a]['ElevenReleaseCheckbox']))); 
$arr1['eleven_release_type']				= trim(html_entity_decode(stripslashes($main[$a]['ElevenReleaseType'])));  
$arr1['eleven_release_broker_amount']		= number_format(trim(html_entity_decode(stripslashes($main[$a]['ElevenReleaseBrokerAmount']))),2);
$arr1['eleven_release_agent_amount']		= number_format(trim(html_entity_decode(stripslashes($main[$a]['ElevenReleaseAgentAmount']))),2);
$arr1['eleven_release_date']				= date('m/d/Y', strtotime($main[$a]['ElevenReleaseDate'])); 
$arr1['eleven_release_broker_check']		= trim(html_entity_decode(stripslashes($main[$a]['ElevenReleaseBrokerCheck']))); 
$arr1['eleven_release_agent_check']			= trim(html_entity_decode(stripslashes($main[$a]['ElevenReleaseAgentCheck']))); 

$arr1['twelve_release_checkbox']			= trim(html_entity_decode(stripslashes($main[$a]['TwelveReleaseCheckbox']))); 
$arr1['twelve_release_type']				= trim(html_entity_decode(stripslashes($main[$a]['TwelveReleaseType'])));  
$arr1['twelve_release_broker_amount']		= number_format(trim(html_entity_decode(stripslashes($main[$a]['TwelveReleaseBrokerAmount']))),2);
$arr1['twelve_release_agent_amount']		= number_format(trim(html_entity_decode(stripslashes($main[$a]['TwelveReleaseAgentAmount']))),2);
$arr1['twelve_release_date']				= date('m/d/Y', strtotime($main[$a]['TwelveReleaseDate'])); 
$arr1['twelve_release_broker_check']		= trim(html_entity_decode(stripslashes($main[$a]['TwelveReleaseBrokerCheck']))); 
$arr1['twelve_release_agent_check']			= trim(html_entity_decode(stripslashes($main[$a]['TwelveReleaseAgentCheck']))); 
 
$arr1['full_date']					= date('m/d/Y', strtotime($main[$a]['FullDate']));  

$arr1['subtotal_amount']			= trim(html_entity_decode(stripslashes($main[$a]['BrokerAgentAmount']))); 

$arr1['full_release_checkbox']		= trim(html_entity_decode(stripslashes($main[$a]['FullCheckbox'])));  
$arr1['full_amount']				= trim(html_entity_decode(stripslashes($main[$a]['FullAmount'])));   
$arr1['full_net']					= trim(html_entity_decode(stripslashes($main[$a]['FullNet']))); 
$arr1['full_check']					= trim(html_entity_decode(stripslashes($main[$a]['FullCheck']))); 

$arr1['total_release_amount']		= trim(html_entity_decode(stripslashes($main[$a]['TotalAmount']))); 
$arr1['total_release_wht']			= trim(html_entity_decode(stripslashes($main[$a]['TotalWHT'])));    

$arr1['release_status']				= trim(html_entity_decode(stripslashes($main[$a]['ReleasesStatus']))); 
$arr1['release_remarks']			= trim(html_entity_decode(stripslashes($main[$a]['ReleasesRemarks']))); 
 
$arr1['take_out_date']				= date('m/d/Y', strtotime($main[$a]['TakeOutDate'])); 

$arr1['broker_account']				= trim(html_entity_decode(stripslashes($main[$a]['BrokerAccount']))); 
$arr1['agent_account']				= trim(html_entity_decode(stripslashes($main[$a]['AgentAccount']))); 
$arr1['add_cash_advance']			= trim(html_entity_decode(stripslashes($main[$a]['AddCashAdvance']))); 
$arr1['total_release_amount']		= trim(html_entity_decode(stripslashes($main[$a]['TotalReleaseAmount']))); 

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


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

