<?php
error_reporting(0); //7dec25
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['danvilles_username'];
$timeStamp		= date('Y-m-d H:i:s');
 
$mainArr	= array(); 
$mainArr2	= array(); 
$output 	= ""; 

 

 
//if($pdc_replace_option=="cash" || $pdc_replace_option=="cheque"){
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" || strtolower($pdc_status_select)=="returned by bank" || strtolower($pdc_status_select)=="returned to buyer"){
		//$mainArr['ReturnRemarks']		=	$pdc_status_remarks;
		//$mainArr['ReturnRemarks']		=	"INSUFFICIENT FUNDS";
		$mainArr['PenaltyStatus']		=	"UNPAID"; 
	}else if(strtolower($pdc_status_select)=="closed"){
		//$mainArr['ReturnRemarks']		=	$pdc_status_remarks;
		//$mainArr['ReturnRemarks']		=	"ACCOUNT CLOSED";
		$mainArr['PenaltyStatus']		=	"UNPAID"; 


	//update all succeeding checks to closed 30DEC24
	$checkId = $mainid; 
	getAccountDetails($mainid,$reserveId,$pdc_status_date);


	}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"){ //27JUL25
		$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"){ //27JUL25
		$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",$condArr); 
	}else{
		$mecon->updateQuery($mainArr,"buyer_cheque_monitoring",$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";
				$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"); 
			$mainArr2['RefId']			= $mainid;
			$mainArr2['CreatedDate'] 	= $timeStamp;
			$mainArr2['CreatedBy'] 		= $currentUser;
			$cheque_no	 				= $utility->getFieldId("ChequeNo","Id",$mainid,"buyer_cheque_monitoring");
			$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");
		
		//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");
				$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";
				$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" || strtolower($pdc_status_select)=="returned by bank" || strtolower($pdc_status_select)=="returned to buyer"){	
	//$mainArr['ReturnRemarks']		=	$pdc_status_remarks; 
	//$mainArr['ReturnRemarks']		=	"INSUFFICIENT FUNDS"; 
	$mainArr['PenaltyStatus']		=	"UNPAID";
}else if(strtolower($pdc_status_select)=="closed"){	
	//$mainArr['ReturnRemarks']		=	$pdc_status_remarks; 
	//$mainArr['ReturnRemarks']		=	"ACCOUNT CLOSED"; 
	$mainArr['PenaltyStatus']		=	"UNPAID";

	//update all succeeding checks to closed 30DEC24
	$checkId = $mainid; 
	getAccountDetails($mainid,$reserveId,$pdc_status_date);



}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",$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";
				$actArr['ActionContent']	= json_encode($mainArr);
				$actArr['ActionBy'] 		= $currentUser;	
				$actArr['ActionDate']		= $timeStamp; 
				$utility->encodeActivityLog($actArr);
	endif;
}

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

 
function closeAccount($mainId="",$reserveId="",$createdDate="",$bank="",$checkno="",$chequeProcessDate=""){
 
		$mecon		=	new condbClass();
		$mecon2		=	new condbClass(); 
		/******************************************/
		 $currentUser 	= $_SESSION['danvilles_username'];
		$timeStamp		= date('Y-m-d H:i:s');

					//update to closed for the succeeding checks of the closed bank account
					$mainQuery2 	= "SELECT * FROM buyer_cheque_monitoring WHERE ModePayment='cheque' AND Id>=$mainId AND DATE(CreatedDate)='$createdDate' AND Bank='$bank' AND ReservationId=$reserveId"; 
					$mecon2->query($mainQuery2);
					$result2		= $mecon2->resultset();   
						if(count($result2)!=0){ 
						 			for($b=0; $b<count($result2); $b++){ 
											$mainArr = array();	
											$condArr = array();	
						 					$mainArr['ChequeStatus'] 		= "Closed";
						 					//$mainArr['ReturnRemarks'] 		= "ACCOUNT CLOSED";
						 					$mainArr['PenaltyStatus'] 		= "UNPAID";
						 					$mainArr['ChequeProcessDate'] 	= $chequeProcessDate;
						 					$mainArr['UpdatedDate'] 		= $timeStamp;
											$mainArr['UpdatedBy'] 			= $currentUser; 


											$condArr['Id'] 					= $result2[$b]['Id'];											
											$condArr['ReservationId']		= $reserveId;
											$mecon2->updateQuery($mainArr,"buyer_cheque_monitoring",$condArr); 
									}  
						}			
}



function getAccountDetails($mainId="", $reserveId="",$pdc_status_date=""){
 
		$mecon		=	new condbClass();
		$mecon2		=	new condbClass(); 
		/******************************************/

		$mainQuery4 	= "SELECT * FROM buyer_cheque_monitoring WHERE Id=$mainId AND ReservationId=$reserveId";  
		$mecon2->query($mainQuery4);
		$result4			= $mecon2->resultset();   
		 
		//get details of the closed bank account
					for($a=0; $a<count($result4); $a++){
						$createdDate 	= date("Y-m-d", strtotime($result4[$a]['CreatedDate']));
						$bank 			= $result4[$a]['Bank'];
						$checkno		= $result4[$a]['ChequeNo']; 
						$chequeProcessDate	= date("Y-m-d", strtotime($pdc_status_date));  
					 	closeAccount($mainId,$reserveId,$createdDate,$bank,$checkno,$chequeProcessDate);
					} 
}
 

exit();
ob_end_flush();