<?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']) : ""; 
$facility			= isset($_POST['bsfb_facility']) 		? trim(strip_tags($_POST['bsfb_facility'])) : ""; 
$dateDelivered		= isset($_POST['bsfb_date_delivered']) 	? trim(strip_tags($_POST['bsfb_date_delivered'])) : ""; 
$returnedDate		= isset($_POST['bsfb_returned_date']) 	? trim(strip_tags($_POST['bsfb_returned_date'])) : "";    
$bankOfficer		= isset($_POST['bsfb_bank_account_officer']) 		? trim(strip_tags($_POST['bsfb_bank_account_officer'])) : "";    
$log				= isset($_POST['bsfb_log']) 			? trim(strip_tags($_POST['bsfb_log'])) : "";    
$booking 			= isset($_POST['bsfb_booking']) 		? trim(strip_tags($_POST['bsfb_booking'])) : "";    
$takeout			= isset($_POST['bsfb_takeout']) 		? trim(strip_tags($_POST['bsfb_takeout'])) : "";    
$reason				= isset($_POST['bsfb_reason']) 			? trim(strip_tags($_POST['bsfb_reason'])) : "";    
$dd				= isset($_POST['dd']) 			? trim(strip_tags($_POST['dd'])) : "";    
  
$currentUser 		= $_SESSION['danvillee_username'];
$timeStamp		= date('Y-m-d H:i:s');
$mainArr			= array();
$mainArr2			= array();
$mainArr3			= array();
$bpmArr				= array();
$bsmArr				= array();
$output 			= "";
$mainArr['Facility'] 					= $facility;
$mainArr['DateDelivered'] 				= date('Y-m-d', strtotime($dateDelivered));
$mainArr['ReturnedDate'] 				= date('Y-m-d', strtotime($returnedDate));
$mainArr['BankOfficer'] 				= $bankOfficer;
$mainArr['LOG'] 						= date('Y-m-d', strtotime($log));
$mainArr['Booking'] 					= date('Y-m-d', strtotime($booking));
$mainArr['TakeOut'] 					= date('Y-m-d', strtotime($takeout));
$mainArr['Reason'] 						= $reason;
$condArr = array();
$condArr2 = array();
$condArr3 = 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_bank','ReservationId',$mainid);
 
		if($existUnit===false){  
			$mainArr['ReservationId']   = $mainid;	
			$ReservationId				= $mainid;
			$mainArr['CreatedDate'] 	= $timeStamp;
			$mainArr['CreatedBy'] 		= $currentUser;
			$output						= $mecon->insertSingleQuery($mainArr,"buyer_status_folder_bank"); 	 
			


			//if LOG released has values

			//update liason  log check if exist if not insert an entry
 				$existUnit2 = $utility->checkExistField('ReservationId','conversion','ReservationId',$mainid);
				if($existUnit2===false){  
					$mainArr3['ReservationId']	= $mainid;	 
					$mainArr3['ReleasedDate'] 	= date('Y-m-d', strtotime($log));
					$mainArr3['CreatedDate'] 	= $timeStamp;
					$mainArr3['CreatedBy'] 		= $currentUser;
					$output						= $mecon->insertSingleQuery($mainArr3,"conversion"); 	 
				}else{
					//update released date in conversion table
					 $mainArr3['ReleasedDate'] 	= date('Y-m-d', strtotime($log));
					 $condArr3['ReservationId']	= $mainid;
				     $mecon->updateQuery($mainArr3,"conversion",$condArr3);
				}	
 
			//LOG TASK 
			$actArr['UserType']			= "assistant_accountant,accountant"; 
			$actArr['Remarks']			= "Created New Buyer Status Folder Bank : $ReservationId";
			$actArr['Action']			= "create";
			$actArr['ActionType']		= "buyer_status_folder_bank";
			$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_bank",$condArr);

			//update liason  log check if exist if not insert an entry
			$existUnit2 = $utility->checkExistField('ReservationId','conversion','ReservationId',$mainid);
				if($existUnit2===false){  
					$mainArr3['ReservationId']	= $mainid;	 
					$mainArr3['ReleasedDate'] 	= date('Y-m-d', strtotime($log));
					$mainArr3['CreatedDate'] 	= $timeStamp;
					$mainArr3['CreatedBy'] 		= $currentUser;
					$output						= $mecon->insertSingleQuery($mainArr3,"conversion"); 	 
				}else{
					//update released date in conversion table
					 $mainArr3['ReleasedDate'] 	= date('Y-m-d', strtotime($log));
					 $condArr3['ReservationId']	= $mainid;
				     $mecon->updateQuery($mainArr3,"conversion",$condArr3);
				}

			//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 Bank : [$mainid] - $last_name , $first_name $middle_name";
					$actArr['TransId']			= $mainid; 
					$actArr['Action']			= "edit";
					$actArr['ActionType']		= "buyer_status_folder_bank";
					$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();