<?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(); 
if(isset($_POST['block'])){
$block		= isset($_POST['block'])	? trim($_POST['block']): "";   
$lot		= isset($_POST['lot'])		? trim($_POST['lot']): "";   


$tax_dec_land_processed		= isset($_POST['tax_dec_land_processed'])		? trim($_POST['tax_dec_land_processed']): "";   
$tax_dec_land_released		= isset($_POST['tax_dec_land_released'])		? trim($_POST['tax_dec_land_released']): "";   

$tax_dec_building_processed		= isset($_POST['tax_dec_building_processed'])		? trim($_POST['tax_dec_building_processed']): "";   
$tax_dec_building_released		= isset($_POST['tax_dec_building_released'])		? trim($_POST['tax_dec_building_released']): "";   

$tax_receipt_land_processed		= isset($_POST['tax_receipt_land_processed'])		? trim($_POST['tax_receipt_land_processed']): "";   
$tax_receipt_land_released		= isset($_POST['tax_receipt_land_released'])		? trim($_POST['tax_receipt_land_released']): "";   



$tax_receipt_building_processed	= isset($_POST['tax_receipt_building_processed'])	? trim($_POST['tax_receipt_building_processed']): "";   
$tax_receipt_building_released	= isset($_POST['tax_receipt_building_released'])		? trim($_POST['tax_receipt_building_released']): "";   
$building_permit_processed		= isset($_POST['building_permit_processed'])		? trim($_POST['building_permit_processed']): "";   
$building_permit_released		= isset($_POST['building_permit_released'])		? trim($_POST['building_permit_released']): "";   
$electrical_permit_processed	= isset($_POST['electrical_permit_processed'])		? trim($_POST['electrical_permit_processed']): "";   
$electrical_permit_released		= isset($_POST['electrical_permit_released'])		? trim($_POST['electrical_permit_released']): "";   
$sanitary_permit_processed		= isset($_POST['sanitary_permit_processed'])		? trim($_POST['sanitary_permit_processed']): "";   
$sanitary_permit_released		= isset($_POST['sanitary_permit_released'])		? trim($_POST['sanitary_permit_released']): "";   
$occupancy_permit_processed		= isset($_POST['occupancy_permit_processed'])		? trim($_POST['occupancy_permit_processed']): "";   
$occupancy_permit_released		= isset($_POST['occupancy_permit_released'])		? trim($_POST['occupancy_permit_released']): "";   

 


//$task		= isset($_POST['action'])	? trim($_POST['action']) : "";
$mainid		= isset($_POST['mainid'])	? trim($_POST['mainid']) : "";

$currentUser 	= $_SESSION['danvillee_username'];
$timeStamp		= date('Y-m-d H:i:s'); 
$mainArr	= array();  
$output 	= "";
$mainArr['Block']				=	$block;
$mainArr['Lot']					=	$lot; 

$mainArr['TaxDecLandProcessed'] 		= date('Y-m-d', strtotime($tax_dec_land_processed)); 
$mainArr['TaxDecLandReleased'] 			= date('Y-m-d', strtotime($tax_dec_land_released)); 

$mainArr['TaxDecBuildingProcessed'] 		= date('Y-m-d', strtotime($tax_dec_building_processed)); 
$mainArr['TaxDecBuildingReleased'] 			= date('Y-m-d', strtotime($tax_dec_building_released)); 
$mainArr['TaxReceiptLandProcessed'] 		= date('Y-m-d', strtotime($tax_receipt_land_processed)); 
$mainArr['TaxReceiptLandReleased'] 			= date('Y-m-d', strtotime($tax_receipt_land_released)); 

$mainArr['TaxReceiptBuildingProcessed'] 	= date('Y-m-d', strtotime($tax_receipt_building_processed)); 	
$mainArr['TaxReceiptBuildingReleased'] 		= date('Y-m-d', strtotime($tax_receipt_building_released)); 
$mainArr['BuildingPermitProcessed'] 		= date('Y-m-d', strtotime($building_permit_processed)); 
$mainArr['BuildingPermitReleased'] 			= date('Y-m-d', strtotime($building_permit_released)); 
$mainArr['ElectricalPermitProcessed'] 		= date('Y-m-d', strtotime($electrical_permit_processed)); 
$mainArr['ElectricalPermitReleased'] 		= date('Y-m-d', strtotime($electrical_permit_released)); 
$mainArr['SanitaryPermitProcessed'] 		= date('Y-m-d', strtotime($sanitary_permit_processed)); 
$mainArr['SanitaryPermitReleased'] 			= date('Y-m-d', strtotime($sanitary_permit_released)); 
$mainArr['OccupancyPermitProcessed'] 		= date('Y-m-d', strtotime($occupancy_permit_processed)); 
$mainArr['OccupancyPermitReleased'] 		= date('Y-m-d', strtotime($occupancy_permit_released)); 
  
 
$condArr = array(); 


  $existUnit = $utility->checkExistField('Id','permits_and_licenses','Id',$mainid);
if($existUnit===false){
	//$mainArr['Id']				= $mecon->getNextId("Id","permits_and_licenses");	
	//$permitId					= $mecon->getNextId("Id","permits_and_licenses"); 

	$mainArr['Id']				= $mainid;	
	$mainArr['UnitId']		= $mainid;	
	$permitId							= $mainid;
	$mainArr['CreatedDate'] 	= $timeStamp;
	$mainArr['CreatedBy'] 		= $currentUser;
	$output						= $mecon->insertSingleQuery($mainArr,"permits_and_licenses"); 	 
	
	//LOG TASK 
	$actArr['UserType']			= "liaison";
	$actArr['Remarks']			= "Created New Permits and Licenses : $permitId";
	$actArr['Action']			= "create";
	$actArr['ActionType']		= "permits_and_licenses";
	$actArr['ActionContent']	= json_encode($mainArr);
	$actArr['ActionBy'] 		= $currentUser;	
	$actArr['ActionDate']		= $timeStamp;
	$actArr['TransId']			= $permitId;
	$utility->encodeActivityLog($actArr);
}else{
	$mainArr['UpdatedDate'] 		= $timeStamp;
	$mainArr['UpdatedBy'] 			= $currentUser;
	$condArr['Id']					= $mainid;
	$output 						= $mecon->updateQuery($mainArr,"permits_and_licenses",$condArr); 
	
	//LOG TASK 
	$actArr['UserType']			= "liaison";
	$actArr['Remarks']			= "Updated Permits and Licenses : $mainid";
	$actArr['Action']			= "edit";
	$actArr['ActionType']		= "permits_and_licenses";
	$actArr['ActionContent']	= json_encode($mainArr);
	$actArr['ActionBy'] 		= $currentUser;	
	$actArr['ActionDate']		= $timeStamp;
	$actArr['TransId']			= $mainid;
	$utility->encodeActivityLog($actArr);
}	 



//UPDATING UNIT TD LAND NO AND UNIT TD BLDG NO START
$mainUArr = array();
$condUArr = array();
$output2 	= "";
 $number 	= count($_POST["UnitBlock"]);  
		 if($number > 0)  
		 {  
		      for($i=0; $i<$number; $i++)  
		      {    
		            
									$unitsId				= $mainid; 
									if(!empty($_POST["UnitBlock"][$i]) && !empty($_POST["UnitLot"][$i]) && !empty($_POST["UnitId"][$i])){
											$mainUArr['UnitId'] 			= $unitsId;
									    $mainUArr['Block']				= $_POST["UnitBlock"][$i];
									    $mainUArr['Lot'] 					= $_POST["UnitLot"][$i];
									    $mainUArr['TDLandNo']			= $_POST["LandNo"][$i];
									    $mainUArr['TDBldgNo'] 		= $_POST["BldgNo"][$i];
									     
									    $mainUArr['UpdatedDate'] 	= $timeStamp;
											$mainUArr['UpdatedBy'] 		= $currentUser;
											$condUArr['Id']						= $_POST["UnitId"][$i];
											$condUArr['UnitId']				= $mainid;
											$output2 									= $mecon->updateQuery($mainUArr,"units_block_lot",$condUArr); 
									 
									}
				    
		      }   
		 }     
		 
//UPDATING UNIT TD LAND NO AND UNIT TD BLDG NO END






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