<?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();
$utility	=	new utilityClass(); 
$task					= isset($_POST['action'])					? trim($_POST['action']) : "";
$mainid					= isset($_POST['mainid'])					? trim($_POST['mainid']) : ""; 
$reservedDate			= isset($_POST['bsm_reservation_date']) 	? trim(strip_tags($_POST['bsm_reservation_date'])) : ""; 
$refundDate				= isset($_POST['bsm_refund_date']) 	? trim(strip_tags($_POST['bsm_refund_date'])) : ""; 
$constuctionBondPaid	= isset($_POST['bsm_construction_bond_paid']) 	? trim(strip_tags($_POST['bsm_construction_bond_paid'])) : ""; 
$constructionNo			= isset($_POST['bsm_permit_to_construct_no']) 	? trim(strip_tags($_POST['bsm_permit_to_construct_no'])) : ""; 
$fullRefundWithViolation = isset($_POST['bsm_fullrefund_or_withviolation']) 	? trim(strip_tags($_POST['bsm_fullrefund_or_withviolation'])) : ""; 
$penalty 				= isset($_POST['bsm_penalty']) 	? str_replace(',','',trim(strip_tags($_POST['bsm_penalty']))) : ""; 
$amount 				= isset($_POST['bsm_amount']) 	? str_replace(',','',trim(strip_tags($_POST['bsm_amount']))) : ""; 
$remarks				= isset($_POST['bsm_remarks']) 	? trim(strip_tags($_POST['bsm_remarks'])) : ""; 
   
/***15APR23**/   
$constuctionBondPaidAmount	= isset($_POST['bsm_construction_bond_paid_amount']) 	? str_replace(',','',trim(strip_tags($_POST['bsm_construction_bond_paid_amount']))) : ""; 
$paymentDetails 			= isset($_POST['bsm_payment_details']) 	? trim(strip_tags($_POST['bsm_payment_details'])) : ""; 

$checkReleaseDetails		= isset($_POST['bsm_check_release_details']) 	? trim(strip_tags($_POST['bsm_check_release_details'])) : ""; 
$remarks2					= isset($_POST['bsm_remarks2']) 	? trim(strip_tags($_POST['bsm_remarks2'])) : ""; 

$currentUser 		= $_SESSION['castalia_username'];
$timeStamp			= date('Y-m-d H:i:s');
$mainArr			= array();
$output 			= ""; 

$mainArr['PermitToConstructNo']			= $constructionNo;
$mainArr['ConstructionBondPaid'] 		= date('Y-m-d', strtotime($constuctionBondPaid));
$mainArr['RefundDate'] 					= date('Y-m-d', strtotime($refundDate));
$mainArr['FullRefundOrWithViolation']	= $fullRefundWithViolation;
$mainArr['Penalty'] 					= $penalty;
$mainArr['Remarks'] 					= $remarks;
$mainArr['Amount'] 						= $amount; 

$mainArr['ConstructionBondPaidAmount'] 	= $constuctionBondPaidAmount;
$mainArr['Remarks2'] 					= $remarks2;
$mainArr['PaymentDetails'] 				= $paymentDetails;
$mainArr['CheckReleaseDetails'] 		= $checkReleaseDetails;
    
$condArr = array();
if($task=="edit"):

	//check if reservationId exist, exist update record
	$isExist = $utility->getFieldId("ReservationId","ReservationId",$mainid,"buyer_house_renovation_monitoring");
	if($isExist!="" || $isExist!=0){		 
			$mainArr['UpdatedDate'] 	= $timeStamp;
			$mainArr['UpdatedBy'] 		= $currentUser;
			$condArr['ReservationId']	= $mainid;
			$output 					= $mecon->updateQuery($mainArr,"buyer_house_renovation_monitoring",$condArr);
			 
			//LOG TASK 
					$actArr['UserType']			= "assistant_accountant,accountant"; 
					$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 Buyer House Renovation Monitoring : [$mainid] - $last_name , $first_name $middle_name";
					$actArr['TransId']			= $mainid; 
					$actArr['Action']			= "edit";
					$actArr['ActionType']		= "buyer_house_renovation_monitoring";
					$actArr['ActionContent']	= json_encode($mainArr);
					$actArr['ActionBy'] 		= $currentUser;	
					$actArr['ActionDate']		= $timeStamp; 
					$utility->encodeActivityLog($actArr);
	}else{
		//insert
			$mainArr['ReservationId'] 				= $mainid;
			$mainArr['CreatedDate'] 	= $timeStamp;
			$mainArr['CreatedBy'] 		= $currentUser;  
			$output = $mecon->insertSingleQuery($mainArr,"buyer_house_renovation_monitoring");
			 
			//LOG TASK 
					$actArr['UserType']			= "assistant_accountant,accountant"; 
					$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']			= "Create Buyer House Renovation Monitoring : [$mainid] - $last_name , $first_name $middle_name";
					$actArr['TransId']			= $mainid; 
					$actArr['Action']			= "create";
					$actArr['ActionType']		= "buyer_house_renovation_monitoring";
					$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();