<?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();
$mecon3		= 	new condbClass();
$mecon4		= 	new condbClass();
$value		= isset($_POST['uvalue'])		?	trim(strip_tags($_POST['uvalue']))	: "";  
$valuedate  = isset($_POST['uvaluedate'])	?	trim(strip_tags($_POST['uvaluedate']))	: "";  
$mid		= isset($_POST['mainid'])		?	trim(strip_tags($_POST['mainid']))	: "";    
$ttype		= isset($_POST['transtype'])	?	trim(strip_tags($_POST['transtype'])) : "";    

$currentUser 	= $_SESSION['danvilles_username'];
$timeStamp		= date('Y-m-d H:i:s');
$curDate 		= date('Y-m-d');
$mainArr		= array();
$mainArr2		= array();
$mainArr3		= array();
$condArr 		= array();
$condArr2 		= array();
$condArr3 		= array();
$output1 		= "";
$output2 		= "";
$output3 		= "";
$output 		= "";

if($ttype=="pdc_submitted"){
	$mainArr['PDCSubmitted'] = $value;
}else if($ttype=="pdc_submitted_status"){
	$mainArr['PDCSubmittedStatus'] = $value;
}else if($ttype=="pdc_submitted_status_pagibig"){
	$mainArr['PDCSubmittedStatusPagibig'] = $value; 
}else if($ttype=="lacking_pdc"){
	$mainArr['LackingPDC'] = $value;
}else if($ttype=="pdc_status"){
	$mainArr['PDCStatus'] = $value;
	 
	if($value!='fully_paid' && $value!=''){
		$mainArr['PDCStatusDate'] = date('Y-m-d');
	}

	if($value=='fully_paid'){
		$mainArr['PDCFullyPaidDate'] = date('Y-m-d');
	} 

}else if($ttype=="pdc_status_date"){
	$mainArr['PDCStatus'] = $value;
	$mainArr['PDCStatusDate'] = date('Y-m-d', strtotime($valuedate));

	
}else if($ttype=="pdc_discount"){
	$mainArr['PDCDiscount'] = $value;

}else if($ttype=="pdc_downpayment"){
	$mainArr['PDCDownpayment'] = $value;  
}
  
$condArr['ReservationId']		= $mid; 
$output 			= $mecon->updateQuery($mainArr,"buyer_payment_monitoring",$condArr); 
 

//UPDATE CHECKLIST IN DOC1 AND FULLCOM IN ACCTG2 START OCT 2, 2021

$resultQuery 	=   "SELECT * FROM checklist";  
$sql			=	$resultQuery."  WHERE ReservationId = '$mid' ";
$mecon2			=	new condbClass();  
$mecon2->query($sql);  
$result 		= $mecon2->resultset();
$standarArr 	= array();
$updBuyer   	= array();
$finallistArr 	= array();
$standard   	= "";
$listArr = array();

	for($a=0; $a<count($result); $a++){
       $standarArr = json_decode($result[$a]['BuyerStandardReqDetails'],true);
       if($value == 'Complete' || $value == 'complete'){ 
 			if($ttype=="pdc_submitted_status_pagibig"){
 				if(!in_array('twenty_four_checks', $standarArr)){
 					//CHECK IF NOT IN ARRAY ADD
	 				$standarArr[] = 'twenty_four_checks';
	 				$standard     = json_encode($standarArr); 
	 			}else{
	 				for($a=0; $a<=count($standarArr); $a++){
	 					if($standarArr[$a]!='twenty_four_checks'){
	 						$listArr[] = $standarArr[$a];	
	 					} 
	 				}	
	 				$standard     	= json_encode($listArr); 
	 			}	
 			}else if($ttype=="pdc_submitted_status"){ 
 				if(!in_array('postdated_checks', $standarArr)){
 					//CHECK IF NOT IN ARRAY ADD
	 				$standarArr[] = 'postdated_checks';
	 				$standard     = json_encode($standarArr); 
	 			}else{
	 				for($a=0; $a<=count($standarArr); $a++){
	 					if($standarArr[$a]!='postdated_checks'){
	 						$listArr[] = $standarArr[$a];
	 					}	
	 				}	
	 				$standard     	= json_encode($listArr);
	 			}
 			}
 			
 		}else if($value == 'Incomplete' || $value == 'incomplete'){ 
 			if($ttype=="pdc_submitted_status_pagibig"){
 				if(in_array('twenty_four_checks', $standarArr)){
 					//CHECK IF IN ARRAY REMOVE 
	 				$delete_item 	= 'twenty_four_checks';  
  
					for($a=0; $a<=count($standarArr); $a++){
						if($standarArr[$a]==$delete_item){
							unset($standarArr[$a]); 
						}else{
							if($standarArr[$a]!=NULL || $standarArr[$a]!=""){
								$listArr[] = $standarArr[$a];
							}
						}
	 				}	
	 				$standard     	= json_encode($listArr);
	 			}else{
	 				for($a=0; $a<=count($standarArr); $a++){
	 					if($standarArr[$a]!='twenty_four_checks'){
	 						if($standarArr[$a]!=NULL || $standarArr[$a]!=""){
	 							$listArr[] = $standarArr[$a];	
	 						} 
	 					}
	 				}	
	 				$standard     	= json_encode($listArr);
	 			}	
 			}else if($ttype=="pdc_submitted_status"){ 
 				if(in_array('postdated_checks', $standarArr)){
 					//CHECK IF IN ARRAY REMOVE 
	 				$delete_item 	= 'postdated_checks';  
					  
					for($a=0; $a<=count($standarArr); $a++){
						if($standarArr[$a]==$delete_item){
							unset($standarArr[$a]); 
						}else{
							if($standarArr[$a]!=NULL || $standarArr[$a]!=""){
								$listArr[] = $standarArr[$a];
							}
						} 
	 				}	
	 				$standard     	= json_encode($listArr);

	 			}else{
	 				for($a=0; $a<=count($standarArr); $a++){
	 					if($standarArr[$a]!='postdated_checks'){
	 						if($standarArr[$a]!=NULL || $standarArr[$a]!=""){
	 							$listArr[] = $standarArr[$a];
	 						}
	 					}	
	 				}	
	 				$standard     	= json_encode($listArr); 
	 			}
 			}
 		} 
    }
  

if($ttype != '' && ($value=='Complete' || $value=='Incomplete' || $value=='complete' || $value=='incomplete')){

	$updBuyer['BuyerStandardReqDetails'] 		= $standard; 
	$condArr2['ReservationId']					= $mid; 
	$output2 									= $mecon2->updateQuery($updBuyer,"checklist",$condArr2); 

}
 
//UPDATE CHECKLIST IN DOC1 AND FULLCOM IN ACCTG2 END OCT 2, 2021


//ACCTG 2 IF COMPLETE WILL TICK FULL COMM
if($ttype=="pdc_submitted_status_pagibig"){
	$condArr3									= [];
	$pdcPagibig  								= [];
	if($value=='Complete' || $value=='complete'){
		$pdcPagibig['FullCheckbox'] 			= "on"; 	
	}else{
		$pdcPagibig['FullCheckbox'] 			= ""; 
	} 
	$condArr3['ReservationId']					= $mid; 
	$output3 									= $mecon3->updateQuery($pdcPagibig,"releases_to_broker_for_wht",$condArr3); 
} 
//ACCTG 2 IF COMPLETE WILL TICK FULL COMM

//ACCTG 2 IF PDC STATUS FULLY PAID PARTIAL COMM (BALANCE) CHECKBOX WILL BE TICKED
	$condArr4								= [];
	$pdcStat 	 							= [];

if($ttype=="pdc_status" && $value=='fully_paid'){
 		$pdcStat['Balancecheckbox'] 			= "on"; 	
 		$condArr4['ReservationId']				= $mid; 
		$output4 								= $mecon4->updateQuery($pdcStat,"releases_to_broker_for_wht",$condArr4);
}else if($ttype=="pdc_status" && $value!='fully_paid'){
 		$pdcStat['Balancecheckbox'] 			= "";
 		$condArr4['ReservationId']				= $mid; 
		$output4 								= $mecon4->updateQuery($pdcStat,"releases_to_broker_for_wht",$condArr4);
}
//ACCTG 2 IF PDC STATUS FULLY PAID PARTIAL COMM (BALANCE) CHECBOX WILL BE TICKED



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