<?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'] : ""; 
 
//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, r.ReservationDate as ReservationDate, rb.FirstReleaseCheckbox, rb.FirstReleaseBrokerAmount, rb.FirstReleaseAgentAmount, rb.FirstReleaseDate, rb.SecondReleaseCheckbox, rb.SecondReleaseBrokerAmount, rb.SecondReleaseAgentAmount, rb.SecondReleaseDate, rb.ThirdReleaseCheckbox, rb.ThirdReleaseBrokerAmount, rb.ThirdReleaseAgentAmount, rb.ThirdReleaseDate, rb.FourthReleaseCheckbox, rb.FourthReleaseBrokerAmount, rb.FourthReleaseAgentAmount, rb.FourthReleaseDate, rb.FifthReleaseCheckbox, rb.FifthReleaseBrokerAmount, rb.FifthReleaseAgentAmount, rb.FifthReleaseDate, rb.SixReleaseCheckbox, rb.SixReleaseBrokerAmount, rb.SixReleaseAgentAmount, rb.SixReleaseDate, rb.SevenReleaseCheckbox, rb.SevenReleaseBrokerAmount, rb.SevenReleaseAgentAmount, rb.SevenReleaseDate, rb.EightReleaseCheckbox, rb.EightReleaseBrokerAmount, rb.EightReleaseAgentAmount, rb.EightReleaseDate, rb.NineReleaseCheckbox, rb.NineReleaseBrokerAmount, rb.NineReleaseAgentAmount, rb.NineReleaseDate, 
	rb.TenReleaseCheckbox, rb.TenReleaseBrokerAmount, rb.TenReleaseAgentAmount, rb.TenReleaseDate, 
	rb.ElevenReleaseCheckbox, rb.ElevenReleaseBrokerAmount, rb.ElevenReleaseAgentAmount, rb.ElevenReleaseDate, 
	rb.TwelveReleaseCheckbox, rb.TwelveReleaseBrokerAmount, rb.TwelveReleaseAgentAmount, rb.TwelveReleaseDate, 
	ba.Fullname as Broker, 
	ba.Realty as Realty, 
	 (SELECT Fullname 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 Agent,
    
    (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 Fullname FROM brokeragent WHERE brokeragent.Id=ba.Id AND brokeragent.Realty=ba.Realty AND brokeragent.Fullname=ba.Fullname AND brokeragent.BrokerOrAgent='broker') as Broker,    
    (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  
	FROM reservation r 
 	LEFT JOIN partial_commission_releases 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['danvillee_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['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['agent_account']				= trim(html_entity_decode(stripslashes($main[$a]['AgentAccount']))); 
$arr1['broker_account']				= trim(html_entity_decode(stripslashes($main[$a]['BrokerAccount']))); 

$arr1['reservation_date']			= date('m/d/Y', strtotime($main[$a]['ReservationDate']));


$arr1['first_release_checkbox']			= trim(html_entity_decode(stripslashes($main[$a]['FirstReleaseCheckbox']))); 
$arr1['first_release_broker_amount']	= trim(html_entity_decode(stripslashes($main[$a]['FirstReleaseBrokerAmount']))); 
$arr1['first_release_agent_amount']		= trim(html_entity_decode(stripslashes($main[$a]['FirstReleaseAgentAmount']))); 
$arr1['first_release_date']				= date('m/d/Y', strtotime($main[$a]['FirstReleaseDate'])); 

$arr1['second_release_checkbox']		= trim(html_entity_decode(stripslashes($main[$a]['SecondReleaseCheckbox']))); 
$arr1['second_release_broker_amount']	= trim(html_entity_decode(stripslashes($main[$a]['SecondReleaseBrokerAmount']))); 
$arr1['second_release_agent_amount']	= trim(html_entity_decode(stripslashes($main[$a]['SecondReleaseAgentAmount']))); 
$arr1['second_release_date']			= date('m/d/Y', strtotime($main[$a]['SecondReleaseDate'])); 
 
$arr1['third_release_checkbox']			= trim(html_entity_decode(stripslashes($main[$a]['ThirdReleaseCheckbox']))); 
$arr1['third_release_broker_amount']	= trim(html_entity_decode(stripslashes($main[$a]['ThirdReleaseBrokerAmount']))); 
$arr1['third_release_agent_amount']	 	= trim(html_entity_decode(stripslashes($main[$a]['ThirdReleaseAgentAmount']))); 
$arr1['third_release_date']				= date('m/d/Y', strtotime($main[$a]['ThirdReleaseDate'])); 

$arr1['fourth_release_checkbox']		= trim(html_entity_decode(stripslashes($main[$a]['FourthReleaseCheckbox']))); 
$arr1['fourth_release_broker_amount']	= trim(html_entity_decode(stripslashes($main[$a]['FourthReleaseBrokerAmount']))); 
$arr1['fourth_release_agent_amount']	= trim(html_entity_decode(stripslashes($main[$a]['FourthReleaseAgentAmount']))); 
$arr1['fourth_release_date']			= date('m/d/Y', strtotime($main[$a]['FourthReleaseDate'])); 
 
$arr1['fifth_release_checkbox']			= trim(html_entity_decode(stripslashes($main[$a]['FifthReleaseCheckbox']))); 
$arr1['fifth_release_broker_amount']	= trim(html_entity_decode(stripslashes($main[$a]['FifthReleaseBrokerAmount']))); 
$arr1['fifth_release_agent_amount']		= trim(html_entity_decode(stripslashes($main[$a]['FifthReleaseAgentAmount']))); 
$arr1['fifth_release_date']				= date('m/d/Y', strtotime($main[$a]['FifthReleaseDate'])); 
   
$arr1['six_release_checkbox']			= trim(html_entity_decode(stripslashes($main[$a]['SixReleaseCheckbox']))); 
$arr1['six_release_broker_amount']		= trim(html_entity_decode(stripslashes($main[$a]['SixReleaseBrokerAmount']))); 
$arr1['six_release_agent_amount']		= trim(html_entity_decode(stripslashes($main[$a]['SixReleaseAgentAmount']))); 
$arr1['six_release_date']				= date('m/d/Y', strtotime($main[$a]['SixReleaseDate'])); 
   
$arr1['seven_release_checkbox']			= trim(html_entity_decode(stripslashes($main[$a]['SevenReleaseCheckbox']))); 
$arr1['seven_release_broker_amount']	= trim(html_entity_decode(stripslashes($main[$a]['SevenReleaseBrokerAmount']))); 
$arr1['seven_release_agent_amount']		= trim(html_entity_decode(stripslashes($main[$a]['SevenReleaseAgentAmount']))); 
$arr1['seven_release_date']				= date('m/d/Y', strtotime($main[$a]['SevenReleaseDate'])); 
 
$arr1['eight_release_checkbox']			= trim(html_entity_decode(stripslashes($main[$a]['EightReleaseCheckbox']))); 
$arr1['eight_release_broker_amount']	= trim(html_entity_decode(stripslashes($main[$a]['EightReleaseBrokerAmount']))); 
$arr1['eight_release_agent_amount']		= trim(html_entity_decode(stripslashes($main[$a]['EightReleaseAgentAmount']))); 
$arr1['eight_release_date']				= date('m/d/Y', strtotime($main[$a]['EightReleaseDate'])); 
 
$arr1['nine_release_checkbox']			= trim(html_entity_decode(stripslashes($main[$a]['NineReleaseCheckbox']))); 
$arr1['nine_release_broker_amount']		= trim(html_entity_decode(stripslashes($main[$a]['NineReleaseBrokerAmount']))); 
$arr1['nine_release_agent_amount']		= trim(html_entity_decode(stripslashes($main[$a]['NineReleaseAgentAmount']))); 
$arr1['nine_release_date']				= date('m/d/Y', strtotime($main[$a]['NineReleaseDate'])); 
  
$arr1['ten_release_checkbox']			= trim(html_entity_decode(stripslashes($main[$a]['TenReleaseCheckbox']))); 
$arr1['ten_release_broker_amount']		= trim(html_entity_decode(stripslashes($main[$a]['TenReleaseBrokerAmount']))); 
$arr1['ten_release_agent_amount']		= trim(html_entity_decode(stripslashes($main[$a]['TenReleaseAgentAmount']))); 
$arr1['ten_release_date']				= date('m/d/Y', strtotime($main[$a]['TenReleaseDate'])); 



$arr1['eleven_release_checkbox']			= trim(html_entity_decode(stripslashes($main[$a]['ElevenReleaseCheckbox']))); 
$arr1['eleven_release_broker_amount']		= trim(html_entity_decode(stripslashes($main[$a]['ElevenReleaseBrokerAmount']))); 
$arr1['eleven_release_agent_amount']		= trim(html_entity_decode(stripslashes($main[$a]['ElevenReleaseAgentAmount']))); 
$arr1['eleven_release_date']				= date('m/d/Y', strtotime($main[$a]['ElevenReleaseDate'])); 



$arr1['twelve_release_checkbox']			= trim(html_entity_decode(stripslashes($main[$a]['TwelveReleaseCheckbox']))); 
$arr1['twelve_release_broker_amount']		= trim(html_entity_decode(stripslashes($main[$a]['TwelveReleaseBrokerAmount']))); 
$arr1['twelve_release_agent_amount']		= trim(html_entity_decode(stripslashes($main[$a]['TwelveReleaseAgentAmount']))); 
$arr1['twelve_release_date']				= date('m/d/Y', strtotime($main[$a]['TwelveReleaseDate'])); 
    
 

	$arr2[] = $arr1;
} 

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