<?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'])) : ""; 

$dateRFDeposited	= 	isset($_POST['bpm_date_rf_deposited']) 		? trim(strip_tags($_POST['bpm_date_rf_deposited'])) : ""; 
$reservation		= 	isset($_POST['bpm_reservation']) 			? trim(strip_tags($_POST['bpm_reservation'])) : ""; 

$lessCA				= 	isset($_POST['bpm_less_ca']) 				? trim(strip_tags($_POST['bpm_less_ca'])) : ""; 
$reservationLessCA	= 	isset($_POST['bpm_reservation_less_ca']) 	? trim(strip_tags($_POST['bpm_reservation_less_ca'])) : ""; 
$tcp				= 	isset($_POST['bpm_tcp']) 					? trim(strip_tags($_POST['bpm_tcp'])) : "0"; 
$lessDiscount		= 	isset($_POST['bpm_less_discount']) 			? trim(strip_tags($_POST['bpm_less_discount'])) : ""; 

$miscExpenses		= 	isset($_POST['bpm_misc_expenses']) 			? trim(strip_tags($_POST['bpm_misc_expenses'])) : ""; 
$basisCommission	= 	isset($_POST['bpm_basis_commission']) 		? trim(strip_tags($_POST['bpm_basis_commission'])) : ""; 
$rate 				= 	isset($_POST['bpm_rate']) 				?	 trim(strip_tags($_POST['bpm_rate'])) : ""; 
$commission			= 	isset($_POST['bpm_commission']) 			? trim(strip_tags($_POST['bpm_commission'])) : ""; 

$miscExpenseRate	= 	isset($_POST['bpm_misc_expense_rate']) 		? trim(strip_tags($_POST['bpm_misc_expense_rate'])) : ""; 
$withholdingTax		= 	isset($_POST['withholding_tax']) 			? trim(strip_tags($_POST['withholding_tax'])) : ""; 

$firstReleaseAmount		= 	isset($_POST['first_release_amount']) 			? trim(strip_tags($_POST['first_release_amount'])) : ""; 
$firstReleaseWHT		= 	isset($_POST['first_release_wht']) 				? trim(strip_tags($_POST['first_release_wht'])) : ""; 
$firstReleaseCash		= 	isset($_POST['first_release_cash']) 			? trim(strip_tags($_POST['first_release_cash'])) : ""; 
$firstReleaseNet		= 	isset($_POST['first_release_net']) 				? trim(strip_tags($_POST['first_release_net'])) : ""; 
$firstReleaseDate		= 	isset($_POST['first_release_date']) 			? trim(strip_tags($_POST['first_release_date'])) : ""; 
$firstReleaseCheck		= 	isset($_POST['first_release_check']) 			? trim(strip_tags($_POST['first_release_check'])) : ""; 
$secondReleaseAmount	= 	isset($_POST['second_release_amount']) 			? trim(strip_tags($_POST['second_release_amount'])) : ""; 
$secondReleaseWHT		= 	isset($_POST['second_release_wht']) 			? trim(strip_tags($_POST['second_release_wht'])) : ""; 
$secondReleaseCash		= 	isset($_POST['second_release_cash']) 			? trim(strip_tags($_POST['second_release_cash'])) : ""; 
$secondReleaseNet		= 	isset($_POST['second_release_net']) 			? trim(strip_tags($_POST['second_release_net'])) : ""; 
$secondReleaseDate		= 	isset($_POST['second_release_date']) 			? trim(strip_tags($_POST['second_release_date'])) : ""; 
$secondReleaseCheck		= 	isset($_POST['second_release_check']) 			? trim(strip_tags($_POST['second_release_check'])) : ""; 
$thirdReleaseAmount		= 	isset($_POST['third_release_amount']) 			? trim(strip_tags($_POST['third_release_amount'])) : ""; 
$thirdReleaseWHT		= 	isset($_POST['third_release_wht']) 				? trim(strip_tags($_POST['third_release_wht'])) : ""; 
$thirdReleaseCash		= 	isset($_POST['third_release_cash']) 			? trim(strip_tags($_POST['third_release_cash'])) : ""; 
$thirdReleaseNet		= 	isset($_POST['third_release_net']) 				? trim(strip_tags($_POST['third_release_net'])) : ""; 
$thirdReleaseDate		= 	isset($_POST['third_release_date']) 			? trim(strip_tags($_POST['third_release_date'])) : ""; 
$thirdReleaseCheck		= 	isset($_POST['third_release_check']) 			? trim(strip_tags($_POST['third_release_check'])) : ""; 
$totalReleaseAmount		= 	isset($_POST['total_release_amount']) 			? trim(strip_tags($_POST['total_release_amount'])) : ""; 
$totalReleaseWHT		= 	isset($_POST['total_release_wht']) 				? trim(strip_tags($_POST['total_release_wht'])) : ""; 
$totalReleaseCash		= 	isset($_POST['total_release_cash']) 			? trim(strip_tags($_POST['total_release_cash'])) : ""; 
$totalReleaseNet		= 	isset($_POST['total_release_net']) 				? trim(strip_tags($_POST['total_release_net'])) : ""; 
$releasesStatus			= 	isset($_POST['releases_status']) 				? trim(strip_tags($_POST['releases_status'])) : ""; 
$releasesRemarks		= 	isset($_POST['releases_remarks']) 				? trim(strip_tags($_POST['releases_remarks'])) : ""; 

$caCheckbox				= 	isset($_POST['ca_checkbox']) 					? trim(strip_tags($_POST['ca_checkbox'])) : ""; 
$firstReleaseCheckbox	= 	isset($_POST['first_release_checkbox']) 		? trim(strip_tags($_POST['first_release_checkbox'])) : ""; 
$secondReleaseCheckbox	= 	isset($_POST['second_release_checkbox']) 		? trim(strip_tags($_POST['second_release_checkbox'])) : ""; 
$thirdReleaseCheckbox	= 	isset($_POST['third_release_checkbox']) 		? trim(strip_tags($_POST['third_release_checkbox'])) : ""; 

     
$currentUser 	= $_SESSION['danvillee_username'];
$timeStamp		= date('Y-m-d H:i:s');
//SAVING CHECKLIST DETAILS   
$mainArr	= array();
$output 	= "";
 
$mainArr['DateRFDeposited'] 		= date('Y-m-d', strtotime($dateRFDeposited));
$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['ReservationId']			= $reservationId; 
$mainArr['Reservation']				= str_replace(',','',$reservation); 
$mainArr['LessCA']					= str_replace(',','',$lessCA); 
$mainArr['ReservationLessCA'] 		= str_replace(',','',$reservationLessCA);
$mainArr['TCP'] 					= str_replace(',','',$tcp);
$mainArr['LessDiscount'] 			= str_replace(',','',$lessDiscount);
$mainArr['MiscExpenses'] 			= str_replace(',','',$miscExpenses);
$mainArr['MiscExpensesRate'] 		= str_replace(',','',$miscExpenseRate);
$mainArr['BasisCommission'] 		= str_replace(',','',$basisCommission);
$mainArr['Rate'] 					= str_replace(',','',$rate);
$mainArr['Commission'] 				= str_replace(',','',$commission);
$mainArr['WHT'] 					= str_replace(',','',$withholdingTax); 
$mainArr['FirstReleaseAmount'] 		= str_replace(',','',$firstReleaseAmount);
$mainArr['FirstReleaseWHT'] 		= str_replace(',','',$firstReleaseWHT);
$mainArr['FirstReleaseCash'] 		= str_replace(',','',$firstReleaseCash);
$mainArr['FirstReleaseNet'] 		= str_replace(',','',$firstReleaseNet);
$mainArr['FirstReleaseCheck'] 		= str_replace(',','',$firstReleaseCheck);
$mainArr['SecondReleaseAmount'] 	= str_replace(',','',$secondReleaseAmount);
$mainArr['SecondReleaseWHT'] 		= str_replace(',','',$secondReleaseWHT);
$mainArr['SecondReleaseCash'] 		= str_replace(',','',$secondReleaseCash);
$mainArr['SecondReleaseNet'] 		= str_replace(',','',$secondReleaseNet); 
$mainArr['SecondReleaseCheck'] 		= str_replace(',','',$secondReleaseCheck);
$mainArr['ThirdReleaseAmount'] 		= str_replace(',','',$thirdReleaseAmount);
$mainArr['ThirdReleaseWHT'] 		= str_replace(',','',$thirdReleaseWHT);   
$mainArr['ThirdReleaseCash'] 		= str_replace(',','',$thirdReleaseCash);
$mainArr['ThirdReleaseNet'] 		= str_replace(',','',$thirdReleaseNet);
$mainArr['ThirdReleaseCheck'] 		= str_replace(',','',$thirdReleaseCheck);
$mainArr['TotalReleaseAmount'] 		= str_replace(',','',$totalReleaseAmount);
$mainArr['TotalReleaseWHT'] 		= str_replace(',','',$totalReleaseWHT);
$mainArr['TotalReleaseCash'] 		= str_replace(',','',$totalReleaseCash);
$mainArr['TotalReleaseNet'] 		= str_replace(',','',$totalReleaseNet);
$mainArr['ReleasesStatus'] 			= str_replace(',','',$releasesStatus);
$mainArr['ReleasesRemarks'] 		= str_replace(',','',$releasesRemarks);


$mainArr['CACheckbox'] 				= $caCheckbox;
$mainArr['FirstReleaseCheckbox'] 	= $firstReleaseCheckbox;
$mainArr['SecondReleaseCheckbox'] 	= $secondReleaseCheckbox;
$mainArr['ThirdReleaseCheckbox'] 	= $thirdReleaseCheckbox;
  
  
$condArr = array();
//if($task=="edit"):
 
	/**********************/

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

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

	}else{
		//reservation exist update record
			$mainArr['UpdatedDate'] 	= $timeStamp;
			$mainArr['UpdatedBy'] 		= $currentUser;
			$condArr['ReservationId']	= $mainid;
			$output 					= $mecon->updateQuery($mainArr,"releases_to_broker",$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 Releases to Broker : [$mainid] - $last_name , $first_name $middle_name";
			$actArr['TransId']			= $mainid; 
			$actArr['Action']			= "edit";
			$actArr['ActionType']		= "releases_to_broker";
			$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();