<?
 
class formClass extends condbClass{ 
	private $output;
	public function getReservationDetails($id){
		self::query("SELECT Id, Project, Model, ReservationDate, Phase, Block, Lot, LastName, FirstName, MiddleName, Broker, Agent, Spouse, HomeTel, CellphoneNo, SpouseContactNo, Address1, Address2, EmployerName, EmployeeType, GrossIncome, DiscountPromo, Position, BuyerProfile, SpouseIncome, TinNo, OfficeTel, AggregateIncome, NoCoBorrower, CoBorrowerDetails, Status, PricelistDate FROM reservation WHERE Status = :status AND Id = :id ORDER BY Id DESC");
		self::bind(':status', 'active');
		self::bind(":id", $id);
		$output = self::resultset();  
		return $output;
	}
	
	public function getPromoDetails($id){
		self::query("SELECT Id, Project, Model, Promo, StartDate, EndDate, Amount, Conditions, PromoStatus,PromoDetails, Status FROM promo WHERE Status = :status AND Id = :id ORDER BY Id DESC");
		self::bind(':status', 'active');
		self::bind(":id", $id);
		$output = self::resultset();  
		return $output;
	}
	
	public function getBrokerAgent($id){
		self::query("SELECT Id, Broker, Agent, Status FROM agent WHERE Status = :status AND Id = :id ORDER BY Id DESC");
		self::bind(':status', 'active');
		self::bind(":id", $id);
		$output = self::resultset();  
		return $output;
	}
	
	public function getDefaultModelValues($model){
		self::query("SELECT Id, Model, Project, PricelistTemplate, DefaultRequiredGrossIncome, DefaultAmortization, DefaultPricelistRemarks, ReservationFee, MoveInFee, TransferFee, DefaultSellingPrice, DefaultLoanablePrice, DefaultStatus, Status, IsDefault FROM model WHERE Status = :status AND Model = :model ORDER BY Id DESC"); 
		self::bind(':status', 'active');
		self::bind(":model", str_replace("_"," ",strtoupper($model))); 
		$output = self::resultset();  
		return $output;
	}
	
	public function getPricelistDetails($id){
		$dateNow = date("Y-m-d");
		self::query("SELECT Id, Project, Model, ReservationFee, Remarks, CashDiscount, Terms, TermsDiscount, PricelistDate, PricelistDetails, Status FROM pricelist WHERE Status = :status AND Id = :id ORDER BY PricelistDate DESC, Id DESC"); 
		self::bind(':status', 'active');
		self::bind(":id", $id); 
		$output = self::resultset();  
		return $output;
	}
	
	public function selectBox($arr){
		$field 			= $arr['field'];
		$fvalue		 	= $arr['field_value'];
		$table 			= $arr['table'];
		$condField		= $arr['condition_field'];
		$condValue		= $arr['condition_value'];
		$notCondField	= isset($arr['not_condition_field']) ? $arr['not_condition_field'] : "";
		$notCondValue	= isset($arr['not_condition_value']) ? $arr['not_condition_value'] : "";
		$selectName		= $arr['select_name'];
		$selectId		= $arr['select_id'];
		$selectClass	= $arr['select_class'];
		$selectDefault	= $arr['select_default'];
		$selectAction	= $arr['action'];
		$selectPlaceholder	= $arr['placeholder'];
		
		$cond = " WHERE Status='active' "; 
		if($condField!=""){
			$cond .= "AND $condField = :confield";
		}else{
			$cond .= "";
		}
		
		if($notCondField!=""){
			$cond .= " AND $notCondField != :notconfield";
		}
		
		
		self::query("SELECT $field FROM $table $cond");
		if($condField!=""){
			self::bind(":confield", $condValue); 
		}
		if($notCondField!=""){
			self::bind(":notconfield", $notCondValue); 
		}
		
		$r = array();
		$row = self::resultset(); 
		$select 	= "";
		$options = array();
		for($a=0; $a<count($row); $a++){ 
			$options[$row[$a][$field]] = $row[$a][$fvalue];
		} 
		$select = self::generateSelect($selectName,$options,$selectId,$selectClass,$selectDefault,$selectAction,$selectPlaceholder);
		$output = $select;
		return $output;
	}
	
	public function selectBox2($arr){
		$field 			= $arr['field'];
		$fvalue		 	= $arr['field_value'];
		$table 			= $arr['table'];
		$condField		= $arr['condition_field'];
		$condValue		= $arr['condition_value'];
		$notCondField	= isset($arr['not_condition_field']) ? $arr['not_condition_field'] : "";
		$notCondValue	= isset($arr['not_condition_value']) ? $arr['not_condition_value'] : "";
		$selectName		= $arr['select_name'];
		$selectId		= $arr['select_id'];
		$selectClass	= $arr['select_class'];
		$selectDefault	= $arr['select_default'];
		$selectAction	= $arr['action'];
		$selectPlaceholder	= $arr['placeholder'];
		if($condField!=""){
			$cond = "WHERE $condField = :confield";
		}else{
			$cond = "";
		}
		
		if($notCondField!=""){
			$cond .= " AND $notCondField != :notconfield";
		}
		
		
		self::query("SELECT $field FROM $table $cond");
		if($condField!=""){
			self::bind(":confield", $condValue); 
		}
		if($notCondField!=""){
			self::bind(":notconfield", $notCondValue); 
		}
		
		$r = array();
		$row = self::resultset(); 
		$select 	= "";
		$options = array();
		for($a=0; $a<count($row); $a++){ 
			$options[$row[$a][$field]] = $row[$a][$fvalue];
		} 
		$select = self::generateSelect($selectName,$options,$selectId,$selectClass,$selectDefault,$selectAction,$selectPlaceholder);
		$output = $select;
		return $output;
	}
	
	  
	
	public function getNextPriceDate($arr){
		$table 			= $arr['table'];
		$project		= $arr['project'];
		$model			= $arr['model'];
		$reserveDate	= $arr['reservation_date'];   
		$action			= $arr['action'];
		$reserveDate = date('Y-m-d', strtotime($reserveDate));
		self::query("SELECT PricelistDate FROM pricelist WHERE Project=:project AND Model=:model AND Status=:status AND  PricelistDate > :reserveDate ORDER BY PricelistDate LIMIT 1");
		self::bind(":project", str_replace("_"," ",$project));  
		self::bind(":model", str_replace("_"," ",strtoupper($model))); 
		self::bind(":reserveDate", $reserveDate);   
		self::bind(":status", 'active');
		$result = self::single();
		return $result['PricelistDate'];
	}
	
	public function selectPricelistBox($arr){ 
		$field 			= $arr['field'];
		$fvalue		 	= $arr['field_value'];
		$table 			= $arr['table'];
		$project		= $arr['project'];
		$model			= $arr['model'];
		$pricelistDate	= $arr['pricelist_date'];   
		$action			= $arr['action'];   
		$dateNow 		= date("Y-m-d");

		//not transfer
		if($action=="transfer"){
		
		}else{
			self::query("SELECT $field FROM $table WHERE Project=:project AND UPPER(Model)=:model AND Status=:status AND  PricelistDate = :priceDate ORDER BY PricelistDate DESC"); //DEFAULT SCENARIO
		}
		$pricelistDate = date('Y-m-d', strtotime($pricelistDate));
		self::bind(":project", str_replace("_"," ",$project));
		self::bind(":model", str_replace("_"," ",strtoupper($model)));
		self::bind(":priceDate", $pricelistDate);
		self::bind(":status", 'active');
		$row = self::resultset();
		return $row;
	}
  
  function generatePricelist($action,$placeholder,$default,$project,$model,$reserveDate,$custom){
		$reserveDate = date('Y-m-d', strtotime($reserveDate));
		//query
		if($custom=="false"){
			self::query("SELECT DISTINCT(PricelistDate) as PricelistDate FROM pricelist WHERE Project=:project AND UPPER(Model)=:model AND PricelistDate<=:reserveDate ORDER BY PricelistDate DESC");
			self::bind(":project", str_replace("_"," ",$project)); 
			self::bind(":model", str_replace("_"," ",strtoupper($model))); 
			self::bind(":reserveDate", $reserveDate);  
		}elseif($custom=="true"){
			self::query("SELECT DISTINCT(PricelistDate) as PricelistDate FROM pricelist WHERE Project=:project AND UPPER(Model)=:model ORDER BY PricelistDate DESC");
			self::bind(":project", $project); 
			self::bind(":model", str_replace("_"," ",strtoupper($model))); 
		}else{
			self::query("SELECT DISTINCT(PricelistDate) as PricelistDate FROM pricelist WHERE Project=:project AND UPPER(Model)=:model AND PricelistDate<=:reserveDate ORDER BY PricelistDate DESC");
			self::bind(":project", str_replace("_"," ",$project)); 
			self::bind(":model", str_replace("_"," ",strtoupper($model))); 
			self::bind(":reserveDate", $reserveDate);  
		}
		$row = self::resultset();  
		return $row;
  }
  
  
  function generateSelect($name='', $options=array(), $id='', $cssClass='', $default='', $action='',$placeholder=''){
		$actionTask = ($action=="view") ? "disabled" : "";
		$html = '<select name="'.$name.'" class="select2 form-control '.$cssClass.'" id="'.$id.'" '.$actionTask.'>';
			if(!empty($placeholder)){
			$html .= '<option value="-">'.$placeholder.'</option>';
			}
		foreach ($options as $option => $value) {
			$option = html_entity_decode(stripslashes($option));
			if ($value == $default) {
				$value = html_entity_decode(stripslashes(str_replace(" ","_",$value)));
				$html .= '<option value='.$value.' selected="selected">'.$option.'</option>';
			} else {
				$value = html_entity_decode(stripslashes(str_replace(" ","_",$value)));
				$html .= '<option value='.$value.'>'.$option.'</option>';
			}
		}
		$html .= '</select>';
		return $html;
	}
	
	public function getOtherDetails($pricedate,$models,$project,$phase,$block,$lot){
		$rdetailTerms		=	"";
		$rpricelistStatus	=	"";
		$rselectPhase		=	"";
		$rselectBlock		=	"";
		$rselectLot			=	"";
		$rlotSize			=	"";
		$rlotType			=	"";
		$rfloorArea			=	"";
		$rcontractPrice		=	"";
		$rloanableAmount	=	"";
		$rpfNet				=	"";
		$rpfPayable			=	"";
		$ramortization		=	"";
		$rrequired_gross	=	"";
		$rpricelistId		=	"";
		$rdiscount 			= "";
		$rdiscount_amount 	= "";
		$rdiscount_type 	= "";
		$rdownpayment 		= "";
		$rdiscount_tcp 		= "";
		
	//GET other details
			/**********************/
			$priceDate	= date('Y-m-d', strtotime($pricedate)); 
	$action = "edit";
	$modelSelect = array("field"=>"PricelistDetails,ReservationFee,TermsDiscount,CashDiscount","field_value"=>"PricelistDetails","table"=>"pricelist","project"=>$project,"model"=>$models,"pricelist_date"=>$priceDate,"action"=>$action);
	$s = self::selectPricelistBox($modelSelect);		

			for($c=0; $c<count($s); $c++){
				$p = json_decode($s[$c]['PricelistDetails'],true);  
				if(count($p['details'])!=0){
					for($a=0; $a<count($p['details']); $a++){
						if($p['details'][$a]['phase']==$phase && $p['details'][$a]['block']==$block && $p['details'][$a]['lot']==$lot){ //28JUL24 to get the specific phase block lot details
							$rdetailTerms		=	$p['details'][$a]['detail_terms'];
							$rpricelistStatus	=	$p['details'][$a]['pricelist_status'];
							$rselectPhase		=	$p['details'][$a]['phase'];
							$rselectBlock		=	$p['details'][$a]['block'];
							$rselectLot			=	$p['details'][$a]['lot'];
							$rlotSize			=	$p['details'][$a]['lot_size'];
							$rlotType			=	$p['details'][$a]['lot_type'];
							$rfloorArea			=	$p['details'][$a]['floor_area'];
							$rcontractPrice		=	$p['details'][$a]['contract_price'];
							$rloanableAmount	=	$p['details'][$a]['loanable_amount'];
							$rpfNet				=	$p['details'][$a]['pf_net'];
							$rpfPayable			=	$p['details'][$a]['pf_payable'];
							$ramortization		=	$p['details'][$a]['amortization'];
							$rrequired_gross	=	$p['details'][$a]['required_gross'];
							$rpricelistId		=	$p['details'][$a]['pricelist_id'];


							$rdiscount_type 	=	$p['details'][$a]['discount_type']; 
							$rdiscount_amount	=	$p['details'][$a]['discount_amount'];
							$rdownpayment		=	$p['details'][$a]['downpayment'];
							$rdiscount_tcp 		=	$p['details'][$a]['discount_tcp'];
							
							if($p['details'][$a]['detail_terms']=='cash'){
								$rdiscount = $s[$c]['CashDiscount'];
							}else{
								$rdiscount = $s[$c]['TermsDiscount'];
							}	
 
							$rfee = $s[$c]['ReservationFee']; //28JUL24
						}else{
							//28JUL24
						} 
					} 
				}
			}
			
			$main		= array();
			$main['phase']				= $rselectPhase;
			$main['block']				= $rselectBlock;
			$main['lot']				= $rselectLot; 
			$main['discount']			= $rdiscount; 
			$main['detail_terms']		= $rdetailTerms; 
			$main['pricelist_status']	= $rpricelistStatus; 
			$main['lot']				= $rselectLot; 
			$main['lot_size']			= $rlotSize; 
			$main['lot_type']			= $rlotType; 
			$main['floor_area']			= $rfloorArea; 
			$main['contract_price']		= $rcontractPrice; 
			$main['loanable_amount']	= $rloanableAmount; 
			$main['pf_net']				= $rpfNet; 
			$main['pf_payable']			= $rpfPayable; 
			$main['required_gross']		= $rrequired_gross; 
			$main['amortization']		= $ramortization; 
			$main['pricelist_id']		= $rpricelistId;
			$main['discount_amount']	= $rdiscount_amount;
			$main['discount_tcp']		= $rdiscount_tcp;			
			$main['downpayment']		= $rdownpayment;
			$main['reservationfee']		= $rfee; //28JUL24
			
			return $main;

	}
}

 