<?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(); 
$task				= isset($_POST['action'])				? trim($_POST['action']) : "";
$mainid				= isset($_POST['mainid'])				? trim($_POST['mainid']) : ""; 
$window				= isset($_POST['bsfp_window']) 			? trim(strip_tags($_POST['bsfp_window'])) : ""; 
$dateDelivered		= isset($_POST['bsfp_date_delivered']) 	? trim(strip_tags($_POST['bsfp_date_delivered'])) : ""; 
$dateInspection		= isset($_POST['bsfp_date_inspection']) ? trim(strip_tags($_POST['bsfp_date_inspection'])) : "";    
$appraiser			= isset($_POST['bsfp_appraiser']) 		? trim(strip_tags($_POST['bsfp_appraiser'])) : "";    
$noa				= isset($_POST['bsfp_noa']) 			? trim(strip_tags($_POST['bsfp_noa'])) : "";    
$postApproval		= isset($_POST['bsfp_post_approval']) 	? trim(strip_tags($_POST['bsfp_post_approval'])) : "";    
$takeout			= isset($_POST['bsfp_takeout']) 		? trim(strip_tags($_POST['bsfp_takeout'])) : "";    
$reason				= isset($_POST['bsfp_reason']) 			? trim(strip_tags($_POST['bsfp_reason'])) : "";    
$hlid				= isset($_POST['bsfp_hlid']) 			? trim(strip_tags($_POST['bsfp_hlid'])) : "";    



$dd				= isset($_POST['dd']) 			? trim(strip_tags($_POST['dd'])) : "";    
  
$currentUser 		= $_SESSION['danvilles_username'];
$timeStamp		= date('Y-m-d H:i:s');
$mainArr			= array();
$mainArr2			= array();
$bpmArr				= array();
$bsmArr				= array();
$output 			= "";
$mainArr['Window'] 						= $window;
$mainArr['DateDelivered'] 				= date('Y-m-d', strtotime($dateDelivered));
$mainArr['DateInspection'] 				= date('Y-m-d', strtotime($dateInspection));
$mainArr['Appraiser'] 					= $appraiser;
$mainArr['NOA'] 						= date('Y-m-d', strtotime($noa));
$mainArr['PostApproval'] 				= date('Y-m-d', strtotime($postApproval));
$mainArr['TakeOut'] 					= date('Y-m-d', strtotime($takeout));
$mainArr['Reason'] 						= $reason;
$mainArr['HLID'] 						= $hlid;

$condArr = array();
$condArr2 = array();
$condBSM = array();
$condBPM = array();
if($task=="edit"):
	$mainArr['UpdatedDate'] 	= $timeStamp;
	$mainArr['UpdatedBy'] 		= $currentUser;
	$condArr['ReservationId']	= $mainid;
	
	//update takeout date in buyer payment monitoring and buyer status monitoring table
	$bpmArr['TakeOutDate'] 					= date('Y-m-d', strtotime($takeout));	
	$bsmArr['PagibigTakeOutDate']			= date('Y-m-d', strtotime($takeout));	
	$condBSM['ReservationId']	= $mainid;
	$condBPM['ReservationId']	= $mainid;
	$mecon->updateQuery($bpmArr,"buyer_payment_monitoring",$condBPM);
	$mecon->updateQuery($bsmArr,"buyer_status_monitoring",$condBSM);
	
	
	if(!empty($dateDelivered) && $dateDelivered!='01/01/1970'){
	     $mainArr2['ScheduleLoan']       = "delivered";
	     $mainArr2['ToBeDeliveredDate']  = date('Y-m-d', strtotime($dateDelivered));
	     $mainArr2['StatusUpdateDate']   = date('Y-m-d');
		 $condArr2['Id']	= $mainid;
	     $mecon->updateQuery($mainArr2,"reservation",$condArr2);
	}else{
		 $mainArr2['ScheduleLoan']       = "";
	     $mainArr2['ToBeDeliveredDate']  = "";
	     $mainArr2['StatusUpdateDate']   = "";
		 $condArr2['Id']	= $mainid;
	     $mecon->updateQuery($mainArr2,"reservation",$condArr2);
	}
	
			

		$existUnit = $utility->checkExistField('ReservationId','buyer_status_folder_pagibig','ReservationId',$mainid);
		if($existUnit===false){  
			$mainArr['Id']				= $mainid;	
			$ReservationId				= $mainid;
			$mainArr['CreatedDate'] 	= $timeStamp;
			$mainArr['CreatedBy'] 		= $currentUser;
			$output						= $mecon->insertSingleQuery($mainArr,"buyer_status_folder_pagibig"); 	 
			
			//LOG TASK 
			$actArr['UserType']			= "assistant_accountant,accountant"; 
			$actArr['Remarks']			= "Created New Buyer Status Folder Pag-ibig : $ReservationId";
			$actArr['Action']			= "create";
			$actArr['ActionType']		= "buyer_status_folder_pagibig";
			$actArr['ActionContent']	= json_encode($mainArr);
			$actArr['ActionBy'] 		= $currentUser;	
			$actArr['ActionDate']		= $timeStamp;
			$actArr['TransId']			= $ReservationId;
			$utility->encodeActivityLog($actArr);
		}else{
			$output 					= $mecon->updateQuery($mainArr,"buyer_status_folder_pagibig",$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 Status Folder Pag-ibig : [$mainid] - $last_name , $first_name $middle_name";
			$actArr['TransId']			= $mainid; 
			$actArr['Action']			= "edit";
			$actArr['ActionType']		= "buyer_status_folder_pagibig";
			$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();