<?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();   
$pdc_or_num			= isset($_POST['pdc_or_num'])				? htmlentities(addslashes(strip_tags(trim($_POST['pdc_or_num'])))) : ""; 
$pdc_replace_option	= isset($_POST['pdc_replace_option'])		? htmlentities(addslashes(strip_tags(trim($_POST['pdc_replace_option'])))) : ""; 
$pdc_status_date	= isset($_POST['pdc_status_date'])			? htmlentities(addslashes(strip_tags(trim($_POST['pdc_status_date'])))) : ""; 
$pdc_status_select	= isset($_POST['pdc_status_select'])		? htmlentities(addslashes(strip_tags(trim($_POST['pdc_status_select'])))) : ""; 
$pdc_status_remarks	= isset($_POST['pdc_status_remarks'])		? htmlentities(addslashes(strip_tags(trim($_POST['pdc_status_remarks'])))) : "";

//replace cash
$pdc_date_replace_cash		= isset($_POST['pdc_date_replace_cash'])		? htmlentities(addslashes(strip_tags(trim($_POST['pdc_date_replace_cash'])))) : "";
$pdc_amount_replace_cash	= isset($_POST['pdc_amount_replace_cash'])		? htmlentities(addslashes(strip_tags(trim($_POST['pdc_amount_replace_cash'])))) : "";
//replace cheque
$pdc_date_replace_cheque	= isset($_POST['pdc_date_replace_cheque'])		? htmlentities(addslashes(strip_tags(trim($_POST['pdc_date_replace_cheque'])))) : "";
$pdc_bank_replace_cheque	= isset($_POST['pdc_bank_replace_cheque'])		? htmlentities(addslashes(strip_tags(trim($_POST['pdc_bank_replace_cheque'])))) : "";
$pdc_ck_replace_cheque		= isset($_POST['pdc_ck_replace_cheque'])		? htmlentities(addslashes(strip_tags(trim($_POST['pdc_ck_replace_cheque'])))) : "";
$pdc_amount_replace_cheque	= isset($_POST['pdc_amount_replace_cheque'])		? htmlentities(addslashes(strip_tags(trim($_POST['pdc_amount_replace_cheque'])))) : "";
 





 
$task			= isset($_POST['action'])			? trim($_POST['action']) : "";
$reserveId		= isset($_POST['mainrid'])			? trim($_POST['mainrid']) : "";
$mainid			= isset($_POST['mainid'])			? trim($_POST['mainid']) : "";

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



 
if($pdc_replace_option=="cash" || $pdc_replace_option=="cheque" || $pdc_replace_option=="online" || $pdc_replace_option=="over_the_counter_deposit"){ //27JUL25 
	 
		//UPDATE
	$mainArr['ReservationId']		=	$reserveId; 
	$mainArr['ChequeProcessDate'] 	=	date("Y-m-d", strtotime($pdc_status_date)); 
	$mainArr['ChequeStatus']		=	$pdc_status_select; 
	if(strtolower($pdc_status_select)=="returned"){
		//$mainArr['ReturnRemarks']		=	$pdc_status_remarks;
		//$mainArr['ReturnRemarks']		=	"INSUFFICIENT FUNDS";
		$mainArr['PenaltyStatus']		=	"Unpaid"; 
	}else{
	//$mainArr['ReturnRemarks']		=	"";
	}
	$mainArr['OrNum']				=	$pdc_or_num; 
	$mainArr['ReplaceOption']		=	$pdc_replace_option; 
	
		
if($pdc_replace_option=="cash"){
	$mainArr['ReplaceOption'] 		=	"cash"; 
	$mainArr['ReplaceCashAmount'] 	=	str_replace(',','',$pdc_amount_replace_cash); 
	$mainArr['ReplaceDate'] 		=	date("Y-m-d", strtotime($pdc_date_replace_cash));  
}else if($pdc_replace_option=="online"){
	$mainArr['ReplaceOption'] 		=	"online"; 
	$mainArr['ReplaceCashAmount'] 	=	str_replace(',','',$pdc_amount_replace_cash); 
	$mainArr['ReplaceDate'] 		=	date("Y-m-d", strtotime($pdc_date_replace_cash));  
}else if($pdc_replace_option=="over_the_counter_deposit"){
	$mainArr['ReplaceOption'] 		=	"over_the_counter_deposit"; 
	$mainArr['ReplaceCashAmount'] 	=	str_replace(',','',$pdc_amount_replace_cash); 
	$mainArr['ReplaceDate'] 		=	date("Y-m-d", strtotime($pdc_date_replace_cash));  
}else if($pdc_replace_option=="cheque"){
	$mainArr['ReplaceDate'] 		=	date("Y-m-d", strtotime($pdc_date_replace_cheque));  
	$mainArr['ReplaceOption'] 		=	"cheque";
	$mainArr['ReplaceBank']			=	$pdc_bank_replace_cheque;
	$mainArr['ReplaceChequeNo']		=	$pdc_ck_replace_cheque;
	$mainArr['ReplaceAmount']		=	str_replace(',','',$pdc_amount_replace_cheque);
}
	$condArr = array();
	if($task=="edit"):
		$mainArr['UpdatedDate'] 		= $timeStamp;
		$mainArr['UpdatedBy'] 			= $currentUser;
		$condArr['Id']					= $mainid;
		$condArr['ReservationId']		= $reserveId;
	if($pdc_status_select=="Cleared"){	
		$output = $mecon->updateQuery($mainArr,"buyer_cheque_monitoring_past_due",$condArr); 
	}else{
		$mecon->updateQuery($mainArr,"buyer_cheque_monitoring_past_due",$condArr); 
	}	
		
		//LOG TASK 
				$actArr['UserType']			= "assistant_accountant,accountant"; 
				$first_name 	= $utility->getFieldId("FirstName","Id",$reserveId,"reservation");
				$last_name 		= $utility->getFieldId("LastName","Id",$reserveId,"reservation");
				$middle_name 	= $utility->getFieldId("MiddleName","Id",$reserveId,"reservation");
				$actArr['Remarks']			= "Update Postdated Check Status : [".$mainid."] - $last_name , $first_name $middle_name";
				$actArr['TransId']			= $mainid; 
				$actArr['Action']			= "edit";
				$actArr['ActionType']		= "buyer_cheque_monitoring_past_due";
				$actArr['ActionContent']	= json_encode($mainArr);
				$actArr['ActionBy'] 		= $currentUser;	
				$actArr['ActionDate']		= $timeStamp; 
				$utility->encodeActivityLog($actArr);
	endif;
if($pdc_status_select!="Cleared"){
	//INSERT
	$mainArr2['Id']				= $mecon->getNextId("Id","buyer_cheque_monitoring_past_due"); 
	$mainArr2['RefId']			= $mainid;
	$mainArr2['CreatedDate'] 	= $timeStamp;
	$mainArr2['CreatedBy'] 		= $currentUser;
	$cheque_no	 				= $utility->getFieldId("ChequeNo","Id",$mainid,"buyer_cheque_monitoring_past_due");
	$mainArr2['ReservationId']		=	$reserveId;
if($pdc_replace_option=="cash"){
	$mainArr2['ChequeDate'] 		=	date("Y-m-d", strtotime($pdc_date_replace_cash)); 
	$mainArr2['ReplaceDate'] 		=	date("Y-m-d", strtotime($pdc_date_replace_cash)); 
	$mainArr2['Amount']				=	str_replace(',','',$pdc_amount_replace_cash);
	$mainArr2['ReplaceOption']		=	"cash"; //$pdc_replace_option;
	$mainArr2['ModePayment']		=	"cash"; //$pdc_replace_option;
	$mainArr2['Remarks']			=	"REPLACED ".$cheque_no; 
	$mainArr2['ChequeStatus']		=	"Replaced";
}else if($pdc_replace_option=="online"){ //27JUL25
	$mainArr2['ChequeDate'] 		=	date("Y-m-d", strtotime($pdc_date_replace_cash)); 
	$mainArr2['ReplaceDate'] 		=	date("Y-m-d", strtotime($pdc_date_replace_cash)); 
	$mainArr2['Amount']				=	str_replace(',','',$pdc_amount_replace_cash);
	$mainArr2['ReplaceOption']		=	"online"; //$pdc_replace_option;
	$mainArr2['ModePayment']		=	"online"; //$pdc_replace_option;
	$mainArr2['Remarks']			=	"REPLACED ".$cheque_no; 
	$mainArr2['ChequeStatus']		=	"Replaced";
}else if($pdc_replace_option=="over_the_counter_deposit"){ //27JUL25
	$mainArr2['ChequeDate'] 		=	date("Y-m-d", strtotime($pdc_date_replace_cash)); 
	$mainArr2['ReplaceDate'] 		=	date("Y-m-d", strtotime($pdc_date_replace_cash)); 
	$mainArr2['Amount']				=	str_replace(',','',$pdc_amount_replace_cash);
	$mainArr2['ReplaceOption']		=	"over_the_counter_deposit"; //$pdc_replace_option;
	$mainArr2['ModePayment']		=	"over_the_counter_deposit"; //$pdc_replace_option;
	$mainArr2['Remarks']			=	"REPLACED ".$cheque_no; 
	$mainArr2['ChequeStatus']		=	"Replaced";
}else if($pdc_replace_option=="cheque"){
	$mainArr2['ChequeDate'] 		=	date("Y-m-d", strtotime($pdc_date_replace_cheque)); 
	$mainArr2['ReplaceDate'] 		=	date("Y-m-d", strtotime($pdc_date_replace_cheque)); 
	$mainArr2['ChequeNo']			=	$pdc_ck_replace_cheque;
	$mainArr2['Bank']				=	$pdc_bank_replace_cheque;
	$mainArr2['ReplaceOption']		=	$pdc_replace_option;
	$mainArr2['ModePayment']		=	$pdc_replace_option;
	$mainArr2['Amount']				=	str_replace(',','',$pdc_amount_replace_cheque);
	$mainArr2['Remarks']			=	"REPLACED ".$cheque_no; 
	$mainArr2['ChequeStatus']		=	"Replaced";
}	


	$output = $mecon->insertSingleQuery($mainArr2,"buyer_cheque_monitoring_past_due");
	
	//LOG TASK 
			$actArr['UserType']			= "assistant_accountant,accountant"; 
			$first_name 				= $utility->getFieldId("FirstName","Id",$reserveId,"reservation");
			$last_name 					= $utility->getFieldId("LastName","Id",$reserveId,"reservation");
			$middle_name 				= $utility->getFieldId("MiddleName","Id",$reserveId,"reservation");
			$cheque_no	 				= $utility->getFieldId("ChequeNo","Id",$mainid,"buyer_cheque_monitoring_past_due");
			$actArr['Remarks']			= "Replaced Postdated Check : [ ".$mainid." $cheque_no ] - $last_name , $first_name $middle_name";
			$actArr['TransId']			= $mainArr2['Id']; 
			$actArr['Action']			= "create";
			$actArr['ActionType']		= "buyer_cheque_monitoring_past_due";
			$actArr['ActionContent']	= json_encode($mainArr2);
			$actArr['ActionBy'] 		= $currentUser;	
			$actArr['ActionDate']		= $timeStamp; 
			$utility->encodeActivityLog($actArr);
}			
			
			
}else{
	//UPDATE
	$mainArr['ReservationId']		=	$reserveId; 
	$mainArr['ChequeProcessDate'] 	=	date("Y-m-d", strtotime($pdc_status_date)); 
	$mainArr['ChequeStatus']		=	$pdc_status_select;
if(strtolower($pdc_status_select)=="returned"){	
	//$mainArr['ReturnRemarks']		=	$pdc_status_remarks; 
	//$mainArr['ReturnRemarks']		=	"INSUFFICIENT FUNDS"; 
	$mainArr['PenaltyStatus']		=	"Unpaid";
}else{
	//$mainArr['ReturnRemarks']		=	""; 
}	
	$mainArr['OrNum']				=	$pdc_or_num; 
	$mainArr['ReplaceOption']		=	$pdc_replace_option; 
		 
	$condArr = array();
	if($task=="edit"):
		$mainArr['UpdatedDate'] 		= $timeStamp;
		$mainArr['UpdatedBy'] 			= $currentUser;
		$condArr['Id']					= $mainid;
		$condArr['ReservationId']		= $reserveId;
		$output 						= $mecon->updateQuery($mainArr,"buyer_cheque_monitoring_past_due",$condArr); 
		
		//LOG TASK 
				$actArr['UserType']			= "assistant_accountant,accountant"; 
				$first_name 	= $utility->getFieldId("FirstName","Id",$reserveId,"reservation");
				$last_name 		= $utility->getFieldId("LastName","Id",$reserveId,"reservation");
				$middle_name 	= $utility->getFieldId("MiddleName","Id",$reserveId,"reservation");
				$actArr['Remarks']			= "Update Postdated Check Status : [".$mainid."] - $last_name , $first_name $middle_name";
				$actArr['TransId']			= $mainid; 
				$actArr['Action']			= "edit";
				$actArr['ActionType']		= "buyer_cheque_monitoring_past_due";
				$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();