<?php
error_reporting(0); //7dec25
ini_set('memory_limit', '1024M');
error_reporting(0);
set_time_limit(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['current_project']) && isset($_POST['current_model'])){
		$project		= isset($_POST['current_project']) ? str_replace('_',' ',htmlentities(addslashes(strip_tags(trim($_POST['current_project']))))) : ""; 
		$model			= isset($_POST['current_model']) 		? str_replace('_',' ',htmlentities(addslashes(strip_tags(trim($_POST['current_model']))))) : ""; 
		$remarks		= isset($_POST['pricelist_remarks']) 		? htmlentities(addslashes(trim($_POST['pricelist_remarks']))) : "";
		$updatedDate	= isset($_POST['pricelist_updated_date']) 	? trim($_POST['pricelist_updated_date']) : "";
		$reservationFee	= isset($_POST['pricelist_reservation_fee']) 	? str_replace(',','',$_POST['pricelist_reservation_fee']) : "";
		$task			= isset($_POST['action'])			? trim($_POST['action']) : "";
		$mainid			= isset($_POST['mainid'])			? trim($_POST['mainid']) : "";
		$terms_discount	= isset($_POST['terms_discount'])	? strip_tags(str_replace(',','',trim($_POST['terms_discount']))) : "";
		$terms			= isset($_POST['terms'])			? strip_tags(trim($_POST['terms'])) : "";
		$cash_discount	= isset($_POST['cash_discount']) 	? strip_tags(str_replace(',','',trim($_POST['cash_discount']))) : "";
		
		
		
		$currentUser 	= $_SESSION['danvilles_username'];
		$timeStamp		= date('Y-m-d H:i:s');
		$detailsArr 	= array();
		 
		$pricelist_status	=	isset($_POST['pricelist_status']) 	? $_POST['pricelist_status'] : "";
		$phase				=	isset($_POST['phase']) 				? $_POST['phase'] 			: "";
		$block				=	isset($_POST['block']) 				? $_POST['block'] 			: "";
		$lot				=	isset($_POST['lot']) 				? $_POST['lot']			 	: "";
		$lot_size			=	isset($_POST['lot_size']) 			? $_POST['lot_size'] 		: "";
		$lot_type			=	isset($_POST['lot_type']) 			? $_POST['lot_type'] 		: "";
		$floor_area			=	isset($_POST['floor_area']) 		? $_POST['floor_area'] 		: "";
		//$selling_price		=	isset($_POST['selling_price']) 		? $_POST['selling_price'] 	: "";
		//$taxes				=	isset($_POST['taxes']) 				? $_POST['taxes'] 			: "";
		$contract_price		=	isset($_POST['contract_price']) 	? number_format($_POST['contract_price'],2) 	: "";
		$loanable_amount	=	isset($_POST['loanable_amount']) 	? number_format($_POST['loanable_amount'],2) : "";
		$pf_net				=	isset($_POST['pf_net']) 			? number_format($_POST['pf_net'],2) 			: "";
		$pf_payable			=	isset($_POST['pf_payable']) 		? number_format($_POST['pf_payable'],2) 		: "";
		$required_gross		=	isset($_POST['required_gross']) 	? number_format($_POST['required_gross'],2) 	: "";
		$amortization		=	isset($_POST['amortization']) 		? number_format($_POST['amortization'],2) 	: "";
		$detail_terms		=	isset($_POST['detail_terms']) 		? $_POST['detail_terms'] 	: "";
		//SAVING PROMO TABLE DETAILS
		$ctrDetails	= count($phase); 
		if(!empty($_POST['phase'])){
		
		if($task=="create"):
			$pricelistId				=	$mecon->getNextId("Id","pricelist"); 
		elseif($task=="edit"): 
			$pricelistId				=	$mainid;
		endif;
			$b = 1;
			for($a=0; $a<$ctrDetails; $a++){
						$detailsArr[$a]['id'] 					= $b;
						$detailsArr[$a]['pricelist_status'] 	= $pricelist_status[$a];
						$detailsArr[$a]['detail_terms'] 		= $detail_terms[$a];
						$detailsArr[$a]['phase'] 				= $phase[$a];
						$detailsArr[$a]['block'] 				= $block[$a];
						$detailsArr[$a]['lot'] 					= $lot[$a];
						$detailsArr[$a]['lot_size'] 			= $lot_size[$a];
						$detailsArr[$a]['lot_type'] 			= $lot_type[$a];
						$detailsArr[$a]['floor_area'] 			= $floor_area[$a]; 
						$detailsArr[$a]['contract_price'] 		= $contract_price[$a];
						$detailsArr[$a]['loanable_amount'] 		= $loanable_amount[$a];
						$detailsArr[$a]['pf_net'] 				= $pf_net[$a];
						$detailsArr[$a]['pf_payable'] 			= $pf_payable[$a];
						$detailsArr[$a]['required_gross'] 		= $required_gross[$a];
						$detailsArr[$a]['amortization'] 		= $amortization[$a];
						$detailsArr[$a]['pricelist_id'] 		= $pricelistId;
						$b++;
			}
		}
		$outputDetails 					= $detailsArr; 
		$resultDetailsArr 				= array();
		$resultDetailsArr['details']  	= $outputDetails;
		$pricelistTableDetails			= json_encode($resultDetailsArr);
		
/*
for($i=0;$i<$something;$++) {
   $data[] = array(
      'monthText' => $Month->getMonthText($month),
      'gross' => $formatGross,
      'net' =>  $formatNet
    );
}*/		
 
		 
				
		$mainArr						= array(); 
		$output 						= ""; 
		$mainArr['Project']				=	$project;
		$mainArr['Model']				=	$model; 
		$mainArr['PricelistDate']		=	date("Y-m-d", strtotime($updatedDate));
		$mainArr['ReservationFee']		=	$reservationFee;
		$mainArr['Remarks']				=	$remarks; 
		$mainArr['Terms']				=	$terms; 
		$mainArr['TermsDiscount']		=	$terms_discount; 
		$mainArr['CashDiscount']		=	$cash_discount; 
		$mainArr['PricelistDetails']	=	$pricelistTableDetails;
		 
		$condArr = array();
		if($task=="create"):
			$mainArr['Id']				=	$mecon->getNextId("Id","pricelist");
			$mainArr['CreatedDate'] 	=	$timeStamp;
			$mainArr['CreatedBy'] 		=	$currentUser;
			$output = $mecon->insertSingleQuery($mainArr,"pricelist");
			
			//LOG TASK
			$actArr['UserType']			= "accountant,assistant_accountant";
			$actArr['Remarks']			= "Created New Pricelist : $updatedDate";
			$actArr['Action']			= "create";
			$actArr['ActionType']		= "pricelist";
			$actArr['ActionContent']	= json_encode($mainArr);
			$actArr['ActionBy'] 		= $currentUser;	
			$actArr['ActionDate']		= $timeStamp;
			$actArr['TransId']			= $pricelistId;
			$utility->encodeActivityLog($actArr);
		elseif($task=="edit"):
			$mainArr['UpdatedDate'] 	=	$timeStamp;
			$mainArr['UpdatedBy'] 		=	$currentUser;
			$condArr['Id']				=	$mainid;
			$output = $mecon->updateQuery($mainArr,"pricelist",$condArr);
			
			//LOG TASK
			/*$actArr['UserType']			= "accountant,assistant_accountant";
			$actArr['Remarks']			= "Updated Pricelist : $updatedDate";
			$actArr['Action']			= "edit";
			$actArr['ActionType']		= "pricelist";
			$actArr['ActionContent']	= json_encode($mainArr);
			$actArr['ActionBy'] 		= $currentUser;	
			$actArr['ActionDate']		= $timeStamp;
			$actArr['TransId']			= $pricelistId;
			$utility->encodeActivityLog($actArr);*/
		endif;

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

}
exit();
ob_end_flush();