<?php
error_reporting(0);
ob_start("ob_gzhandler");
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();
$utility			=	new utilityClass(); 
$task				= 	isset($_POST['action'])		? trim($_POST['action']) : "";
$mainid				= 	isset($_POST['mainid'])		? trim($_POST['mainid']) : "";
 
 
$reservationId			= 	isset($_POST['reservation_id']) 			? trim(strip_tags($_POST['reservation_id'])) : ""; 


$firstReleaseCheckbox	= 	isset($_POST['first_release_checkbox']) 		? trim(strip_tags($_POST['first_release_checkbox'])) : ""; 
$firstBrokerAmount		= 	isset($_POST['first_broker_amount']) 			? trim(strip_tags($_POST['first_broker_amount'])) : ""; 
$firstAgentAmount		= 	isset($_POST['first_agent_amount']) 			? trim(strip_tags($_POST['first_agent_amount'])) : ""; 
$firstReleaseDate		= 	isset($_POST['first_release_date']) 			? trim(strip_tags($_POST['first_release_date'])) : ""; 

$secondReleaseCheckbox	= 	isset($_POST['second_release_checkbox']) 		? trim(strip_tags($_POST['second_release_checkbox'])) : ""; 
$secondBrokerAmount		= 	isset($_POST['second_broker_amount']) 			? trim(strip_tags($_POST['second_broker_amount'])) : ""; 
$secondAgentAmount		= 	isset($_POST['second_agent_amount']) 			? trim(strip_tags($_POST['second_agent_amount'])) : ""; 
$secondReleaseDate		= 	isset($_POST['second_release_date']) 			? trim(strip_tags($_POST['second_release_date'])) : ""; 

$thirdReleaseCheckbox	= 	isset($_POST['third_release_checkbox']) 		? trim(strip_tags($_POST['third_release_checkbox'])) : ""; 
$thirdBrokerAmount		= 	isset($_POST['third_broker_amount']) 			? trim(strip_tags($_POST['third_broker_amount'])) : ""; 
$thirdAgentAmount		= 	isset($_POST['third_agent_amount']) 			? trim(strip_tags($_POST['third_agent_amount'])) : ""; 
$thirdReleaseDate		= 	isset($_POST['third_release_date']) 			? trim(strip_tags($_POST['third_release_date'])) : ""; 

$fourthReleaseCheckbox	= 	isset($_POST['fourth_release_checkbox']) 		? trim(strip_tags($_POST['fourth_release_checkbox'])) : ""; 
$fourthBrokerAmount		= 	isset($_POST['fourth_broker_amount']) 			? trim(strip_tags($_POST['fourth_broker_amount'])) : ""; 
$fourthAgentAmount		= 	isset($_POST['fourth_agent_amount']) 			? trim(strip_tags($_POST['fourth_agent_amount'])) : ""; 
$fourthReleaseDate		= 	isset($_POST['fourth_release_date']) 			? trim(strip_tags($_POST['fourth_release_date'])) : ""; 

$fifthReleaseCheckbox	= 	isset($_POST['fifth_release_checkbox']) 		? trim(strip_tags($_POST['fifth_release_checkbox'])) : ""; 
$fifthBrokerAmount		= 	isset($_POST['fifth_broker_amount']) 			? trim(strip_tags($_POST['fifth_broker_amount'])) : ""; 
$fifthAgentAmount		= 	isset($_POST['fifth_agent_amount']) 			? trim(strip_tags($_POST['fifth_agent_amount'])) : ""; 
$fifthReleaseDate		= 	isset($_POST['fifth_release_date']) 			? trim(strip_tags($_POST['fifth_release_date'])) : ""; 

$sixReleaseCheckbox		= 	isset($_POST['six_release_checkbox']) 		? trim(strip_tags($_POST['six_release_checkbox'])) : ""; 
$sixBrokerAmount		= 	isset($_POST['six_broker_amount']) 			? trim(strip_tags($_POST['six_broker_amount'])) : ""; 
$sixAgentAmount			= 	isset($_POST['six_agent_amount']) 			? trim(strip_tags($_POST['six_agent_amount'])) : ""; 
$sixReleaseDate			= 	isset($_POST['six_release_date']) 			? trim(strip_tags($_POST['six_release_date'])) : ""; 

$sevenReleaseCheckbox		= 	isset($_POST['seven_release_checkbox']) 		? trim(strip_tags($_POST['seven_release_checkbox'])) : ""; 
$sevenBrokerAmount			= 	isset($_POST['seven_broker_amount']) 			? trim(strip_tags($_POST['seven_broker_amount'])) : ""; 
$sevenAgentAmount			= 	isset($_POST['seven_agent_amount']) 			? trim(strip_tags($_POST['seven_agent_amount'])) : ""; 
$sevenReleaseDate			= 	isset($_POST['seven_release_date']) 			? trim(strip_tags($_POST['seven_release_date'])) : ""; 

$eightReleaseCheckbox		= 	isset($_POST['eight_release_checkbox']) 		? trim(strip_tags($_POST['eight_release_checkbox'])) : ""; 
$eightBrokerAmount			= 	isset($_POST['eight_broker_amount']) 			? trim(strip_tags($_POST['eight_broker_amount'])) : ""; 
$eightAgentAmount			= 	isset($_POST['eight_agent_amount']) 			? trim(strip_tags($_POST['eight_agent_amount'])) : ""; 
$eightReleaseDate			= 	isset($_POST['eight_release_date']) 			? trim(strip_tags($_POST['eight_release_date'])) : ""; 

$nineReleaseCheckbox		= 	isset($_POST['nine_release_checkbox']) 			? trim(strip_tags($_POST['nine_release_checkbox'])) : ""; 
$nineBrokerAmount			= 	isset($_POST['nine_broker_amount']) 			? trim(strip_tags($_POST['nine_broker_amount'])) : ""; 
$nineAgentAmount			= 	isset($_POST['nine_agent_amount']) 				? trim(strip_tags($_POST['nine_agent_amount'])) : ""; 
$nineReleaseDate			= 	isset($_POST['nine_release_date']) 				? trim(strip_tags($_POST['nine_release_date'])) : ""; 

$tenReleaseCheckbox			= 	isset($_POST['ten_release_checkbox']) 			? trim(strip_tags($_POST['ten_release_checkbox'])) : ""; 
$tenBrokerAmount			= 	isset($_POST['ten_broker_amount']) 				? trim(strip_tags($_POST['ten_broker_amount'])) : ""; 
$tenAgentAmount				= 	isset($_POST['ten_agent_amount']) 				? trim(strip_tags($_POST['ten_agent_amount'])) : ""; 
$tenReleaseDate				= 	isset($_POST['ten_release_date']) 				? trim(strip_tags($_POST['ten_release_date'])) : ""; 
 
$currentUser 	= $_SESSION['danvilles_username'];
$timeStamp		= date('Y-m-d H:i:s');
//SAVING CHECKLIST DETAILS   
$mainArr	= array();
$mainArr2	= array();
$output 	= "";
$output2 	= ""; 
 
$mainArr['FirstReleaseDate'] 		= date('Y-m-d', strtotime($firstReleaseDate));
$mainArr['SecondReleaseDate'] 		= date('Y-m-d', strtotime($secondReleaseDate)); 
$mainArr['ThirdReleaseDate']		= date('Y-m-d', strtotime($thirdReleaseDate));
$mainArr['FourthReleaseDate']		= date('Y-m-d', strtotime($fourthReleaseDate));
$mainArr['FifthReleaseDate']		= date('Y-m-d', strtotime($fifthReleaseDate));
$mainArr['SixReleaseDate']			= date('Y-m-d', strtotime($sixReleaseDate));
$mainArr['SevenReleaseDate']		= date('Y-m-d', strtotime($sevenReleaseDate));
$mainArr['EightReleaseDate']		= date('Y-m-d', strtotime($eightReleaseDate));
$mainArr['NineReleaseDate']			= date('Y-m-d', strtotime($nineReleaseDate));
$mainArr['TenReleaseDate']			= date('Y-m-d', strtotime($tenReleaseDate));
 
$mainArr['FirstReleaseCheckbox'] 	= $firstReleaseCheckbox;
$mainArr['SecondReleaseCheckbox'] 	= $secondReleaseCheckbox;
$mainArr['ThirdReleaseCheckbox'] 	= $thirdReleaseCheckbox;
$mainArr['FourthReleaseCheckbox'] 	= $fourthReleaseCheckbox;
$mainArr['FifthReleaseCheckbox'] 	= $fifthReleaseCheckbox;
$mainArr['SixReleaseCheckbox'] 		= $sixReleaseCheckbox;
$mainArr['SevenReleaseCheckbox'] 	= $sevenReleaseCheckbox;
$mainArr['EightReleaseCheckbox'] 	= $eightReleaseCheckbox;
$mainArr['NineReleaseCheckbox'] 	= $nineReleaseCheckbox;
$mainArr['TenReleaseCheckbox'] 		= $tenReleaseCheckbox;
 
$mainArr['ReservationId']			= $reservationId; 


$mainArr['FirstReleaseBrokerAmount'] 		= str_replace(',','',$firstBrokerAmount);
$mainArr['FirstReleaseAgentAmount'] 		= str_replace(',','',$firstAgentAmount); 

$mainArr['SecondReleaseBrokerAmount'] 		= str_replace(',','',$secondBrokerAmount);
$mainArr['SecondReleaseAgentAmount'] 		= str_replace(',','',$secondAgentAmount); 

$mainArr['ThirdReleaseBrokerAmount'] 		= str_replace(',','',$thirdBrokerAmount);
$mainArr['ThirdReleaseAgentAmount'] 		= str_replace(',','',$thirdAgentAmount); 
 
$mainArr['FourthReleaseBrokerAmount'] 		= str_replace(',','',$fourthBrokerAmount);
$mainArr['FourthReleaseAgentAmount'] 		= str_replace(',','',$fourthAgentAmount); 
 
$mainArr['FifthReleaseBrokerAmount'] 		= str_replace(',','',$fifthBrokerAmount);
$mainArr['FifthReleaseAgentAmount'] 		= str_replace(',','',$fifthAgentAmount); 

$mainArr['SixReleaseBrokerAmount'] 			= str_replace(',','',$sixBrokerAmount);
$mainArr['SixReleaseAgentAmount'] 			= str_replace(',','',$sixAgentAmount);  

$mainArr['SevenReleaseBrokerAmount'] 		= str_replace(',','',$sevenBrokerAmount);
$mainArr['SevenReleaseAgentAmount'] 		= str_replace(',','',$sevenAgentAmount);  

$mainArr['EightReleaseBrokerAmount'] 		= str_replace(',','',$eightBrokerAmount);
$mainArr['EightReleaseAgentAmount'] 		= str_replace(',','',$eightAgentAmount);  

$mainArr['NineReleaseBrokerAmount'] 		= str_replace(',','',$nineBrokerAmount);
$mainArr['NineReleaseAgentAmount'] 			= str_replace(',','',$nineAgentAmount);  

$mainArr['TenReleaseBrokerAmount'] 			= str_replace(',','',$tenBrokerAmount);
$mainArr['TenReleaseAgentAmount'] 			= str_replace(',','',$tenAgentAmount);  
 
$condArr = array();
//if($task=="edit"):
 
	/**********************/

	$existReservation = $utility->checkExistField('ReservationId','partial_commission_releases','reservationId',$reservationId);
	if($existReservation===false){
		//insert

			$mainArr['CreatedDate'] 	= $timeStamp;
			$mainArr['CreatedBy'] 		= $currentUser; 
	 		$output 					= $mecon->insertSingleQuery($mainArr,"partial_commission_releases"); 

	 		//NEED TO INSERT releases_to_broker_wht

			$mainArr2['ReservationId']			= $reservationId; 
			

			$mainArr2['FirstReleaseCheckbox'] 	= $firstReleaseCheckbox;
			$mainArr2['SecondReleaseCheckbox'] 	= $secondReleaseCheckbox;
			$mainArr2['ThirdReleaseCheckbox'] 	= $thirdReleaseCheckbox;
			$mainArr2['FourthReleaseCheckbox'] 	= $fourthReleaseCheckbox;
			$mainArr2['FifthReleaseCheckbox'] 	= $fifthReleaseCheckbox;
			$mainArr2['SixReleaseCheckbox'] 	= $sixReleaseCheckbox;
			$mainArr2['SevenReleaseCheckbox'] 	= $sevenReleaseCheckbox;
			$mainArr2['EightReleaseCheckbox'] 	= $eightReleaseCheckbox;
			$mainArr2['NineReleaseCheckbox'] 	= $nineReleaseCheckbox;
			$mainArr2['TenReleaseCheckbox'] 	= $tenReleaseCheckbox;
 
			$mainArr2['FirstReleaseBrokerAmount'] 		= str_replace(',','',$firstBrokerAmount);
			$mainArr2['FirstReleaseAgentAmount'] 		= str_replace(',','',$firstAgentAmount); 

			$mainArr2['SecondReleaseBrokerAmount'] 		= str_replace(',','',$secondBrokerAmount);
			$mainArr2['SecondReleaseAgentAmount'] 		= str_replace(',','',$secondAgentAmount); 

			$mainArr2['ThirdReleaseBrokerAmount'] 		= str_replace(',','',$thirdBrokerAmount);
			$mainArr2['ThirdReleaseAgentAmount'] 		= str_replace(',','',$thirdAgentAmount); 
			 
			$mainArr2['FourthReleaseBrokerAmount'] 		= str_replace(',','',$fourthBrokerAmount);
			$mainArr2['FourthReleaseAgentAmount'] 		= str_replace(',','',$fourthAgentAmount); 
			 
			$mainArr2['FifthReleaseBrokerAmount'] 		= str_replace(',','',$fifthBrokerAmount);
			$mainArr2['FifthReleaseAgentAmount'] 		= str_replace(',','',$fifthAgentAmount); 

			$mainArr2['SixReleaseBrokerAmount'] 		= str_replace(',','',$sixBrokerAmount);
			$mainArr2['SixReleaseAgentAmount'] 			= str_replace(',','',$sixAgentAmount);  

			$mainArr2['SevenReleaseBrokerAmount'] 		= str_replace(',','',$sevenBrokerAmount);
			$mainArr2['SevenReleaseAgentAmount'] 		= str_replace(',','',$sevenAgentAmount);  

			$mainArr2['EightReleaseBrokerAmount'] 		= str_replace(',','',$eightBrokerAmount);
			$mainArr2['EightReleaseAgentAmount'] 		= str_replace(',','',$eightAgentAmount);  

			$mainArr2['NineReleaseBrokerAmount'] 		= str_replace(',','',$nineBrokerAmount);
			$mainArr2['NineReleaseAgentAmount'] 		= str_replace(',','',$nineAgentAmount);  

			$mainArr2['TenReleaseBrokerAmount'] 		= str_replace(',','',$tenBrokerAmount);
			$mainArr2['TenReleaseAgentAmount'] 			= str_replace(',','',$tenAgentAmount);  
			 
			$output2		 					= $mecon->insertSingleQuery($mainArr2,"releases_to_broker_for_wht"); 
  
	}else{
		//reservation exist update record
			$mainArr['UpdatedDate'] 	= $timeStamp;
			$mainArr['UpdatedBy'] 		= $currentUser;
			$condArr['ReservationId']	= $mainid;
	 		$output 					= $mecon->updateQuery($mainArr,"partial_commission_releases",$condArr);

	 		//NEED TO UPDATE releases_to_broker_wht
			$mainArr2['ReservationId']			= $reservationId;  

			$mainArr2['FirstReleaseCheckbox'] 	= $firstReleaseCheckbox;
			$mainArr2['SecondReleaseCheckbox'] 	= $secondReleaseCheckbox;
			$mainArr2['ThirdReleaseCheckbox'] 	= $thirdReleaseCheckbox;
			$mainArr2['FourthReleaseCheckbox'] 	= $fourthReleaseCheckbox;
			$mainArr2['FifthReleaseCheckbox'] 	= $fifthReleaseCheckbox;
			$mainArr2['SixReleaseCheckbox'] 	= $sixReleaseCheckbox;
			$mainArr2['SevenReleaseCheckbox'] 	= $sevenReleaseCheckbox;
			$mainArr2['EightReleaseCheckbox'] 	= $eightReleaseCheckbox;
			$mainArr2['NineReleaseCheckbox'] 	= $nineReleaseCheckbox;
			$mainArr2['TenReleaseCheckbox'] 	= $tenReleaseCheckbox;
			
			
			$mainArr2['FirstReleaseBrokerAmount'] 		= str_replace(',','',$firstBrokerAmount);
			$mainArr2['FirstReleaseAgentAmount'] 		= str_replace(',','',$firstAgentAmount); 

			$mainArr2['SecondReleaseBrokerAmount'] 		= str_replace(',','',$secondBrokerAmount);
			$mainArr2['SecondReleaseAgentAmount'] 		= str_replace(',','',$secondAgentAmount); 

			$mainArr2['ThirdReleaseBrokerAmount'] 		= str_replace(',','',$thirdBrokerAmount);
			$mainArr2['ThirdReleaseAgentAmount'] 		= str_replace(',','',$thirdAgentAmount); 
			 
			$mainArr2['FourthReleaseBrokerAmount'] 		= str_replace(',','',$fourthBrokerAmount);
			$mainArr2['FourthReleaseAgentAmount'] 		= str_replace(',','',$fourthAgentAmount); 
			 
			$mainArr2['FifthReleaseBrokerAmount'] 		= str_replace(',','',$fifthBrokerAmount);
			$mainArr2['FifthReleaseAgentAmount'] 		= str_replace(',','',$fifthAgentAmount); 

			$mainArr2['SixReleaseBrokerAmount'] 		= str_replace(',','',$sixBrokerAmount);
			$mainArr2['SixReleaseAgentAmount'] 			= str_replace(',','',$sixAgentAmount);  

			$mainArr2['SevenReleaseBrokerAmount'] 		= str_replace(',','',$sevenBrokerAmount);
			$mainArr2['SevenReleaseAgentAmount'] 		= str_replace(',','',$sevenAgentAmount);  

			$mainArr2['EightReleaseBrokerAmount'] 		= str_replace(',','',$eightBrokerAmount);
			$mainArr2['EightReleaseAgentAmount'] 		= str_replace(',','',$eightAgentAmount);  

			$mainArr2['NineReleaseBrokerAmount'] 		= str_replace(',','',$nineBrokerAmount);
			$mainArr2['NineReleaseAgentAmount'] 		= str_replace(',','',$nineAgentAmount);  

			$mainArr2['TenReleaseBrokerAmount'] 		= str_replace(',','',$tenBrokerAmount);
			$mainArr2['TenReleaseAgentAmount'] 			= str_replace(',','',$tenAgentAmount);  
			 
			$condArr['ReservationId']			= $mainid;
	 		$output2 							= $mecon->updateQuery($mainArr2,"releases_to_broker_for_wht",$condArr);
  
	} 
	/***********************/
 	
	//LOG TASK 
			$actArr['UserType']			= "accounting2"; 
			$first_name 	= $utility->getFieldId("FirstName","Id",$mainid,"reservation");
			$last_name 		= $utility->getFieldId("LastName","Id",$mainid,"reservation");
			$middle_name 	= $utility->getFieldId("MiddleName","Id",$mainid,"reservation");
			$actArr['Remarks']			= "Updated Partial Commission Releases : [$mainid] - $last_name , $first_name $middle_name";
			$actArr['TransId']			= $mainid; 
			$actArr['Action']			= "edit";
			$actArr['ActionType']		= "partial_commission_releases";
			$actArr['ActionContent']	= json_encode($mainArr);
			$actArr['ActionBy'] 		= $currentUser;	
			$actArr['ActionDate']		= $timeStamp; 
			$utility->encodeActivityLog($actArr);
//endif;

$resultArr 						= array();
$resultArr['output']	  		= $output;

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