<?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_date		= isset($_POST['pdc_date'])			? htmlentities(addslashes(strip_tags(trim($_POST['pdc_date'])))) : ""; 
$pdc_bank		= isset($_POST['pdc_bank'])			? htmlentities(addslashes(strip_tags(trim($_POST['pdc_bank'])))) : ""; 
$pdc_ck			= isset($_POST['pdc_ck'])			? htmlentities(addslashes(strip_tags(trim($_POST['pdc_ck'])))) : ""; 
$pdc_amount		= isset($_POST['pdc_amount'])		? htmlentities(addslashes(strip_tags(trim($_POST['pdc_amount'])))) : ""; 
$pdc_remarks	= isset($_POST['pdc_remarks'])		? htmlentities(addslashes(strip_tags(trim($_POST['pdc_remarks'])))) : ""; 
$pdc_mode		= isset($_POST['pdc_mode'])			? htmlentities(addslashes(strip_tags(trim($_POST['pdc_mode'])))) : ""; 
$pdc_no_cheque	= isset($_POST['pdc_no_cheque'])			? htmlentities(addslashes(strip_tags(trim($_POST['pdc_no_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(); 
$output 	= ""; 


     
$condArr = array();
if($task=="create"):
  if($pdc_no_cheque!="" || $pdc_no_cheque!=0){
	for($a=1; $a<=$pdc_no_cheque; $a++){
		$mainArr['ReservationId']		=	$reserveId;
		//$mainArr['ChequeDate'] 			=	date("Y-m-d", strtotime($pdc_date));
		$apd = $a - 1;
		$mainArr['ChequeDate'] 			=	($pdc_no_cheque>1 && !empty($pdc_date)) ? date('Y-m-d', strtotime("+$apd months", strtotime($pdc_date))) : ((!empty($pdc_date) && $pdc_date!='1970-01-01') ? date("Y-m-d", strtotime($pdc_date)) : NULL);
		$mainArr['ChequeNo']			=	($pdc_no_cheque>1) ? $pdc_ck+$apd : $pdc_ck;
		$mainArr['Bank']				=	$pdc_bank;
		$mainArr['ModePayment']			=	$pdc_mode;
		$mainArr['Amount']				=	str_replace(',','',$pdc_amount);
		$mainArr['Remarks']				=	$pdc_remarks;
	
	$mainArr['Id']				= $mecon->getNextId("Id","buyer_cheque_monitoring_pagibig"); 
	$mainArr['RefId']			= $mainArr['Id'];
	$mainArr['CreatedDate'] 	= $timeStamp;
	$mainArr['CreatedBy'] 		= $currentUser;
	$output = $mecon->insertSingleQuery($mainArr,"buyer_cheque_monitoring_pagibig");
	
	//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']			= "Create new Postdated Check : [".$mainArr['Id']."] - $last_name , $first_name $middle_name";
			$actArr['TransId']			= $mainArr['Id']; 
			$actArr['Action']			= "create";
			$actArr['ActionType']		= "buyer_cheque_monitoring_pagibig";
			$actArr['ActionContent']	= json_encode($mainArr);
			$actArr['ActionBy'] 		= $currentUser;	
			$actArr['ActionDate']		= $timeStamp; 
			$utility->encodeActivityLog($actArr);
		}	
    }else{
		$mainArr['ReservationId']		=	$reserveId;
		//$mainArr['ChequeDate'] 			=	date("Y-m-d", strtotime($pdc_date));
		$apd = $a - 1;
		$mainArr['ChequeDate'] 			=	($pdc_no_cheque>1 && !empty($pdc_date)) ? date('Y-m-d', strtotime("+$apd months", strtotime($pdc_date))) : ((!empty($pdc_date) && $pdc_date!='1970-01-01') ? date("Y-m-d", strtotime($pdc_date)) : NULL);
		$mainArr['ChequeNo']			=	($pdc_no_cheque>1) ? $pdc_ck+$apd : $pdc_ck;
		$mainArr['Bank']				=	$pdc_bank;
		$mainArr['ModePayment']			=	$pdc_mode;
		$mainArr['Amount']				=	str_replace(',','',$pdc_amount);
		$mainArr['Remarks']				=	$pdc_remarks;
	
	$mainArr['Id']				= $mecon->getNextId("Id","buyer_cheque_monitoring_pagibig"); 
	$mainArr['RefId']			= $mainArr['Id'];
	$mainArr['CreatedDate'] 	= $timeStamp;
	$mainArr['CreatedBy'] 		= $currentUser;
	$output = $mecon->insertSingleQuery($mainArr,"buyer_cheque_monitoring_pagibig");
	
	//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']			= "Create new Postdated Check for Pag-IBIG Loan Amortization : [".$mainArr['Id']."] - $last_name , $first_name $middle_name";
			$actArr['TransId']			= $mainArr['Id']; 
			$actArr['Action']			= "create";
			$actArr['ActionType']		= "buyer_cheque_monitoring_pagibig";
			$actArr['ActionContent']	= json_encode($mainArr);
			$actArr['ActionBy'] 		= $currentUser;	
			$actArr['ActionDate']		= $timeStamp; 
			$utility->encodeActivityLog($actArr);
	}
		
elseif($task=="edit"):
			
			$mainArr['ReservationId']		=	$reserveId;
			$mainArr['ChequeDate'] 			=	(!empty($pdc_date) && $pdc_date!='1970-01-01') ? date("Y-m-d", strtotime($pdc_date)) : NULL; 
			$mainArr['ChequeNo']			=	$pdc_ck;
			$mainArr['Bank']				=	$pdc_bank;
			$mainArr['ModePayment']			=	$pdc_mode;
			$mainArr['Amount']				=	str_replace(',','',$pdc_amount);
			$mainArr['Remarks']				=	$pdc_remarks;
			
			$mainArr['UpdatedDate'] 		= $timeStamp;
			$mainArr['UpdatedBy'] 			= $currentUser;
			$mainArr['RefId']				= $mainid;
			$condArr['Id']					= $mainid;
			
			$condArr['ReservationId']		= $reserveId;
			$output 						= $mecon->updateQuery($mainArr,"buyer_cheque_monitoring_pagibig",$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 for Pag-IBIG Loan Amortization : [".$mainid."] - $last_name , $first_name $middle_name";
			$actArr['TransId']			= $mainid; 
			$actArr['Action']			= "edit";
			$actArr['ActionType']		= "buyer_cheque_monitoring_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();