<?php

class Condo {	
	public $id;
	public $project;
	public $postal;
	public $street_name;
	public $subzone;
	public $district;
	public $website;
	
	public $top;
	public $enbloc;
	public $tenure_cat;
	public $tenure_short_desc;
	public $tenure_desc;
	public $units;
	
	public $mrt_near1;
	public $dist_mrt_near1;
	public $name_mrt_near1;
	
	public $train_stops_to_raffles;
	
	public $raffles_trainbus_time;
	public $raffles_car_time;
	public $raffles_taxi_cost;
	public $orchard_trainbus_time;
	public $orchard_car_time;
	public $orchard_taxi_cost;
	public $travel_details;
	
	public $desc_positive;
	public $desc_neutral;
	public $desc_negative;
	
	public $geolat;
	public $geolon;
	
	function __construct($params = null){
		date_default_timezone_set('Asia/Singapore');
		if($params!=null) { 
			if(is_array($params)) {
				$this->set_condo_parameters($params);
			}
		}
	}
	
	function set_condo_parameters($params) {
		if(isset($params['id'])){
			$this->id = $params['id'];
			//$this->get_condo($this->id);
		}
		if(isset($params['project'])){
			$this->project = $params['project'];
		}
		if(isset($params['postal'])){
			$this->postal = $params['postal'];
		}
		if(isset($params['streetname'])){
			$this->street_name = $params['streetname'];
		}
		if(isset($params['subzone'])){
			$this->subzone = $params['subzone'];
		}
		if(isset($params['district'])){
			$this->district = $params['district'];
		}
		if(isset($params['website'])){
			$this->website = $params['website'];
		}
		if(isset($params['top'])){
			$this->top = $params['top']>0? $params['top'] : 'NA';
		}
		if(isset($params['enbloc'])){
			$this->enbloc = $params['enbloc'];
		}
		if(isset($params['tenure_cat'])){
			$this->tenure_cat = $params['tenure_cat'];
		}
		if(isset($params['tenure_desc'])){
			$this->tenure_desc = $params['tenure_desc'];
			$this->tenure_short_desc = str_replace('lease commencing ', '', $params['tenure_desc']);
		}
		if(isset($params['units'])){
			$this->units = $params['units'];
		}
		if(isset($params['mrt_near1'])){
			$this->mrt_near1 = $params['mrt_near1'];
		}
		if(isset($params['dist_mrt_near1'])){
			$this->dist_mrt_near1 = $params['dist_mrt_near1'];
		}
		if(isset($params['name_mrt_near1'])){
			$this->name_mrt_near1 = $params['name_mrt_near1'];
		}
		if(isset($params['train_stops_to_raffles'])){
			$this->train_stops_to_raffles = $params['train_stops_to_raffles'];
		}
		
		
		if(isset($params['raffles_trainbus_time'])){
			$this->raffles_trainbus_time = $params['raffles_trainbus_time'];
		}
		if(isset($params['raffles_car_time'])){
			$this->raffles_car_time = $params['raffles_car_time'];
		}
		if(isset($params['raffles_taxi_cost'])){
			$this->raffles_taxi_cost = $params['raffles_taxi_cost'];
		}
		if(isset($params['orchard_trainbus_time'])){
			$this->orchard_trainbus_time = $params['orchard_trainbus_time'];
		}
		if(isset($params['orchard_car_time'])){
			$this->orchard_car_time = $params['orchard_car_time'];
		}
		if(isset($params['orchard_taxi_cost'])){
			$this->orchard_taxi_cost = $params['orchard_taxi_cost'];
		}
		if(isset($params['travel_details'])){
			$this->travel_details = $params['travel_details'];
		}
		
		
		if(isset($params['travel_details'])){
			$this->travel_details = $params['travel_details'];
		}
		if(isset($params['desc_positive'])){
			$this->desc_positive = $params['desc_positive'];
		}
		if(isset($params['desc_neutral'])){
			$this->desc_neutral = $params['desc_neutral'];
		}
		if(isset($params['desc_negative'])){
			$this->desc_negative = $params['desc_negative'];
		}
		if(isset($params['geolat'])){
			$this->geolat = $params['geolat'];
		}
		if(isset($params['geolon'])){
			$this->geolon = $params['geolon'];
		}
	}
	
	function get_condo_link() {
		if($this->id && $this->project) {
			return sprintf(default_url.'sg-property/condos/%s/%s', $this->id, $this->project);
		}
	}
	
	function get_condo($id = 0) {
		$db = new db();
		if($id == 0) {
			$id = $this->id;
		}
		if($id != null) {
			$sql = sprintf('SELECT * FROM condos
							LEFT JOIN train_network ON condos.mrt_near1 = train_network.train_station_code
							LEFT JOIN condo_area ON condos.id = condo_area.condo_id
							LEFT JOIN condo_desc ON condos.id = condo_desc.condo_id
							WHERE id = %s', $id);
			
			$result = $db->query($sql, true);
			//var_dump($result);
			$this->set_condo_parameters($result->result);
		}
	}
	
	function get_amenities() {
		$db = new db();
		$sql = sprintf("SELECT * FROM amenities WHERE condo_id = %s", $this->id);
		$result = $db->query($sql, true);
		
		return $result->result;
	}
	
	function get_prices() {
		$db = new db();
		
		$psf_list_500_print = "-";
		$psf_list_1000_print = "-";
		$psf_list_1500_print = "-";
		$psf_list_2000_print = "-";
		$psf_list_abv2000_print = "-";
		
		$price_list_500_print = "-";
		$price_list_1000_print = "-";
		$price_list_1500_print = "-";
		$price_list_2000_print = "-";
		$price_list_abv2000_print = "-";
		
		$psf_hist_500 = "-";
		$psf_hist_1000 = "-";
		$psf_hist_1500 = "-";
		$psf_hist_2000 = "-";
		$psf_hist_abv2000 = "-";

		$price_hist_500 = "-";
		$price_hist_1000 = "-";
		$price_hist_1500 = "-";
		$price_hist_2000 = "-";
		$price_hist_abv2000 = "-";
		
		$txncount_500 = "-";
		$txncount_1000 = "-";
		$txncount_1500 = "-";
		$txncount_2000 = "-";
		$txncount_abv2000 = "-";

		$txncount_500 = "-";
		$txncount_1000 = "-";
		$txncount_1500 = "-";
		$txncount_2000 = "-";
		$txncount_abv2000 = "-";
		

		$psf_list_500 = array();
		$price_list_500 = array();
		
		$psf_list_1000 = array();
		$price_list_1000 = array();
		
		$psf_list_1500 = array();
		$price_list_1500 = array();
		
		$psf_list_2000 = array();
		$price_list_2000 = array();
		
		$psf_list_abv2000 = array();
		$price_list_abv2000 = array();
		
		$price_hist_500_min = '';
		$price_hist_1000_min = '';
		$price_hist_1500_min = '';
		$price_hist_2000_min = '';
		$price_hist_abv2000_min = '';
		
		$price_hist_500_max = '';
		$price_hist_1000_max = '';
		$price_hist_1500_max = '';
		$price_hist_2000_max = '';
		$price_hist_abv2000_max = '';
		
		$psf_hist_500_min = '';
		$psf_hist_1000_min = '';
		$psf_hist_1500_min = '';
		$psf_hist_2000_min = '';
		$psf_hist_abv2000_min = '';
		
		$psf_hist_500_max = '';
		$psf_hist_1000_max = '';
		$psf_hist_1500_max = '';
		$psf_hist_2000_max = '';
		$psf_hist_abv2000_max = '';
		
		$txncount_500 = '-';
		$txncount_1000 = '-';
		$txncount_1500 = '-';
		$txncount_2000 = '-';
		$txncount_abv2000 = '-';
		
		$listings = $this->get_listings();
		foreach($listings as $listing) {
			$prep_price=intval($listing->listing_price/1000);
			$prep_psf=intval($listing->listing_psf);
			
				
			if($listing->listing_unitarea<501){
				$psf_list_500[] = $prep_psf;
				$price_list_500[] = $prep_price;
			}elseif($listing->listing_unitarea>500 AND $listing->listing_unitarea<1001){
				$psf_list_1000[] = $prep_psf;
				$price_list_1000[] = $prep_price;
			}elseif($listing->listing_unitarea>1000 AND $listing->listing_unitarea<1501){
				$psf_list_1500[] = $prep_psf;
				$price_list_1500[] = $prep_price;
			}elseif($listing->listing_unitarea>1500 AND $listing->listing_unitarea<2001){
				$psf_list_2000[] = $prep_psf;
				$price_list_2000[] = $prep_price;
			}elseif($listing->listing_unitarea>2000){
				$psf_list_abv2000[] = $prep_psf;
				$price_list_abv2000[] = $prep_price;
			}
		}
		
		if(count($price_list_500) == 1){
				$psf_list_500_print = $psf_list_500[0];		
				$price_list_500_print = $price_list_500[0];
		}elseif(count($price_list_500) > 1){
				$psf_list_500_print = min($psf_list_500)." - ".max($psf_list_500);		
				$price_list_500_print = min($price_list_500)." - ".max($price_list_500);
		}
		
		if(count($price_list_1000) == 1){
				$psf_list_1000_print = $psf_list_1000[0];		
				$price_list_1000_print = $price_list_1000[0];		
		}elseif(count($price_list_1000) > 1){
				$psf_list_1000_print = min($psf_list_1000)." - ".max($psf_list_1000);		
				$price_list_1000_print = min($price_list_1000)." - ".max($price_list_1000);
		}

		if(count($price_list_1500) == 1){
				$psf_list_1500_print = $psf_list_1500[0];		
				$price_list_1500_print = $price_list_1500[0];		
		}elseif(count($price_list_1500) > 1){
				$psf_list_1500_print = min($psf_list_1500)." - ".max($psf_list_1500);		
				$price_list_1500_print = min($price_list_1500)." - ".max($price_list_1500);
		}
		
		if(count($price_list_2000) == 1){
				$psf_list_2000_print = $psf_list_2000[0];		
				$price_list_2000_print = $price_list_2000[0];		
		}elseif(count($price_list_2000) > 1){
				$psf_list_2000_print = min($psf_list_2000)." - ".max($psf_list_2000);		
				$price_list_2000_print = min($price_list_2000)." - ".max($price_list_2000);
		}
		
		if(count($price_list_abv2000) == 1){
				$psf_list_abv2000_print = $psf_list_abv2000[0];		
				$price_list_abv2000_print = $price_list_abv2000[0];		
		}elseif(count($price_list_abv2000) > 1){
				$psf_list_abv2000_print = min($psf_list_abv2000)." - ".max($psf_list_abv2000);		
				$price_list_abv2000_print = min($price_list_abv2000)." - ".max($price_list_abv2000);
		}
		
		$sql = sprintf("SELECT date, AVG(psf), AVG(price), MIN(psf) AS minpsf, MAX(psf) AS maxpsf, MIN(price) AS minprice, MAX(price) as maxprice, COUNT(price) as txncount, sqft_range FROM Master WHERE date >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH) AND project = '%s' GROUP BY sqft_range ORDER BY date DESC",$db->real_escape_string($this->project));
		
		$result = $db->query($sql);
		if($result->num_rows > 0) {
			foreach($result->result as $row_prices) {
				switch($row_prices['sqft_range']){
					case "<500":
					$psf_hist_500 = intval($row_prices['AVG(psf)']);
					$psf_hist_500_min = intval($row_prices['minpsf']);
					$psf_hist_500_max = intval($row_prices['maxpsf']);
					$price_hist_500 = intval($row_prices['AVG(price)']/1000);
					$price_hist_500_min = intval($row_prices['minprice']/1000);
					$price_hist_500_max = intval($row_prices['maxprice']/1000);
					$txncount_500=intval($row_prices['txncount']);
					break;

					case "500-1000":
					$psf_hist_1000 = intval($row_prices['AVG(psf)']);
					$psf_hist_1000_min = intval($row_prices['minpsf']);
					$psf_hist_1000_max = intval($row_prices['maxpsf']);
					$price_hist_1000 = intval($row_prices['AVG(price)']/1000);
					$price_hist_1000_min = intval($row_prices['minprice']/1000);
					$price_hist_1000_max = intval($row_prices['maxprice']/1000);
					$txncount_1000=intval($row_prices['txncount']);
					break;

					case "1000-1500":
					$psf_hist_1500 = intval($row_prices['AVG(psf)']);
					$psf_hist_1500_min = intval($row_prices['minpsf']);
					$psf_hist_1500_max = intval($row_prices['maxpsf']);
					$price_hist_1500 = intval($row_prices['AVG(price)']/1000);
					$price_hist_1500_min = intval($row_prices['minprice']/1000);
					$price_hist_1500_max = intval($row_prices['maxprice']/1000);
					$txncount_1500=intval($row_prices['txncount']);
					break;

					case "1500-2000":
					$psf_hist_2000 = intval($row_prices['AVG(psf)']);
					$psf_hist_2000_min = intval($row_prices['minpsf']);
					$psf_hist_2000_max = intval($row_prices['maxpsf']);
					$price_hist_2000 = intval($row_prices['AVG(price)']/1000);
					$price_hist_2000_min = intval($row_prices['minprice']/1000);
					$price_hist_2000_max = intval($row_prices['maxprice']/1000);
					$txncount_2000=intval($row_prices['txncount']);
					break;

					case ">2000":
					$psf_hist_abv2000 = intval($row_prices['AVG(psf)']);
					$psf_hist_abv2000_min = intval($row_prices['minpsf']);
					$psf_hist_abv2000_max = intval($row_prices['maxpsf']);
					$price_hist_abv2000 = intval($row_prices['AVG(price)']/1000);
					$price_hist_abv2000_min = intval($row_prices['minprice']/1000);
					$price_hist_abv2000_max = intval($row_prices['maxprice']/1000);
					$txncount_abv2000=intval($row_prices['txncount']);
					break;

				}
			}
		}
		
		$unit_prices_asking = array(
							'500' => $price_list_500_print,
							'1000' => $price_list_1000_print,
							'1500' => $price_list_1500_print,
							'2000' => $price_list_2000_print,
							'abv2000' => $price_list_abv2000_print
							);
		$psf_prices_asking = array(
							'500' => $psf_list_500_print,
							'1000' => $psf_list_1000_print,
							'1500' => $psf_list_1500_print,
							'2000' => $psf_list_2000_print,
							'abv2000' => $psf_list_abv2000_print
							);
		$unit_prices_txn = array(
							'500' => array(
											'range'=>$price_hist_500_min.' - '.$price_hist_500_max,
											'count'=>$txncount_500
											),
							'1000' => array(
											'range'=>$price_hist_1000_min.' - '.$price_hist_1000_max,
											'count'=>$txncount_1000
											),
							'1500' => array(
											'range'=>$price_hist_1500_min.' - '.$price_hist_1500_max,
											'count'=>$txncount_1500
											),
							'2000' => array(
											'range'=>$price_hist_2000_min.' - '.$price_hist_2000_max,
											'count'=>$txncount_2000
											),
							'abv2000' => array(
											'range'=>$price_hist_abv2000_min.' - '.$price_hist_abv2000_max,
											'count'=>$txncount_abv2000
											)
							);
		$psf_prices_txn = array(
							'500' => array(
											'range'=>$psf_hist_500_min.' - '.$psf_hist_500_max,
											'count'=>$txncount_500
											),
							'1000' => array(
											'range'=>$psf_hist_1000_min.' - '.$psf_hist_1000_max,
											'count'=>$txncount_1000
											),
							'1500' => array(
											'range'=>$psf_hist_1500_min.' - '.$psf_hist_1500_max,
											'count'=>$txncount_1500
											),
							'2000' => array(
											'range'=>$psf_hist_2000_min.' - '.$psf_hist_2000_max,
											'count'=>$txncount_2000
											),
							'abv2000' => array(
											'range'=>$psf_hist_abv2000_min.' - '.$psf_hist_abv2000_max,
											'count'=>$txncount_abv2000
											)
							);
							
		return array(
					'unit'=>array(
						'asking'=>$unit_prices_asking,
						'transaction'=>$unit_prices_txn
					),
					'psf'=>array(
						'asking'=>$psf_prices_asking,
						'transaction'=>$psf_prices_txn
					)
				);
	}

	
	function get_supply($distance) {
		$db = new DB();
		
		$condo_lat = $this->geolat/10000000;
		$condo_lon = $this->geolon/10000000;
		
		$thisyear = date("Y");
		
		$sql = sprintf("
					SELECT
							units,
							top,
							( 3959.75587 * acos( cos( radians('$condo_lat') ) * 
							cos( radians( geolat/10000000) ) * 
							cos( radians( geolon/10000000 ) - 
							radians('$condo_lon') ) + 
							sin( radians('$condo_lat') ) * 
							sin( radians( geolat/10000000 ) ) ) ) 
							AS distance

					FROM condos

					WHERE

					top > '%s'

					HAVING
					distance < %s
					",$thisyear,$distance);
					
		$result = $db->query($sql);
		
		$supplies = array();
		for($i = $thisyear+1; $i <= $thisyear+4; $i++) {
			$supplies['years'][$i] = 0;
		}
		
		if($result->num_rows > 0) {
			foreach($result->result as $supply) {
				if($supply['top'] <= $thisyear+4) {
					$supplies['years'][$supply['top']] = $supplies['years'][$supply['top']] + $supply['units'];
				}
			}
		}
		if(array_key_exists ($this->top, $supplies['years'])) {
			$supplies['years'][$this->top] = $supplies['years'][$this->top] + $this->units;
		}
		
		$supplies['total'] = 0;
		foreach($supplies['years'] as $yearunits) {
			$supplies['total'] += $yearunits;
		}
		ksort($supplies['years']);
		return $supplies;
		//var_dump($supplies);
	}
	
	function get_nearby_condos($distance) {
		date_default_timezone_set('Asia/Singapore');
		$db = new DB();
		
		$condo_lat = $this->geolat/10000000;
		$condo_lon = $this->geolon/10000000;
		
		$sql = sprintf("SELECT
						project, id, top,
						( 3959.75587 * acos( cos( radians('$condo_lat') ) * 
						cos( radians( geolat/10000000) ) * 
						cos( radians( geolon/10000000 ) - 
						radians('$condo_lon') ) + 
						sin( radians('$condo_lat') ) * 
						sin( radians( geolat/10000000 ) ) ) ) 
						AS distance

				FROM condos
				HAVING distance < %s",$distance);
				
		$result = $db->query($sql);
		
		if($result->num_rows > 0) {
			return array('total'=>$result->num_rows, 'condos'=>$result->result);
		}
		return array('total'=>0,'condos'=>array());
	}
	
	function get_developer_sale($distance) {
		$thisyear = date("Y");
		$db = new DB();
		
		$nearby_condos = $this->get_nearby_condos($distance);
		$ds_condo_id_list = '';
		foreach($nearby_condos['condos'] as $condo) {
			if($condo['top'] > $thisyear) {
				$ds_condo_id_list .= "ds_condo_id =  '".$condo['id']."'". " OR ";
			}
		}
		$ds_condo_id_list = substr($ds_condo_id_list,0,-4);
		
		$sql = sprintf("
		SELECT * FROM(
		SELECT ds_date, ds_condo_id, ds_project, top, ds_totalunits, ds_cum_launched, ds_cum_sold FROM developer_sale LEFT JOIN condos on condos.id = developer_sale.ds_condo_id
		WHERE (%s) AND top >= ".date("Y")."
		ORDER BY ds_date DESC
		) as rs1
		GROUP BY ds_project",$ds_condo_id_list);
		
		$ds = array();
		for($i = $thisyear+1; $i <= $thisyear+4; $i++) {
			$ds['years'][$i] = 0;
		}
		$result = $db->query($sql);
		
		if($result->num_rows >0) {
			foreach($result->result as $sale) {
				$ds['years'][$sale['top']] += $sale['ds_cum_sold'];
			}
		}
		$ds['total'] = 0;
		foreach($ds['years'] as $yearunits) {
			$ds['total'] += $yearunits;
		}
		ksort($ds['years']);
			
		return $ds;
	}
	
	function get_nearby_txn($distance) {
		$thisyear = date("Y");
		$db = new DB();
		
		$nearby_condos = $this->get_nearby_condos($distance);
		
		$condo_count = 0;
		$condo_id_list = '';
		foreach($nearby_condos['condos'] as $condo) {
			if($condo['top'] > $thisyear) {
				$condo_id_list .= "condo_id =  '".$condo['id']."' OR ";
				$condo_count++;
			}
		}
		$condo_id_list = substr($condo_id_list,0,-4);
		
		if($this->top>$thisyear){
			$condo_count++;
		}
		
		$txns = array();
		for($i = $thisyear+1; $i <= $thisyear+4; $i++) {
			$txns['years'][$i] = 0;
		}
		if($condo_count > 0) {
			$sql = "SELECT
						SUM(Master.units) AS txn_units,
						condos.top
				FROM Master LEFT JOIN condos on Master.condo_id = condos.id
				WHERE ".$condo_id_list."
				GROUP BY condos.top
				";
			$result = $db->query($sql);
			if($result->num_rows >0) {
				foreach($result->result as $txn) {
					$txns['years'][$txn['top']] += $txn['txn_units'];
				}
			}
			$txns['total'] = 0;
			foreach($txns['years'] as $yearunits) {
				$txns['total'] += $yearunits;
			}
			ksort($txns['years']);
			return $txns;
		}
	}
	/*
	function get_rental() {
		$db = new DB();
		$rental_int=12;
		/*
		$geo_lat = $this->geolat;
		$geo_lon = $this->geolon;
		$miles_rental = 0.6;
		
		$sql = sprintf("SELECT
		rent_unitbr,
		rent_price,
		rent_sqft_max,
		rent_sqft_min,
		condos.tenure_cat,
		( 3959.75587 * acos( cos( radians('%s') ) * 
		cos( radians( geolat/10000000) ) * 
		cos( radians( geolon/10000000 ) - 
		radians('%s') ) + 
		sin( radians('%s') ) * 
		sin( radians( geolat/10000000 ) ) ) ) 
		AS distance
		FROM rental_txn LEFT JOIN condos ON rental_txn.rent_condo_id = condos.id
		WHERE date >= DATE_SUB(CURDATE(), INTERVAL %s MONTH)
		HAVING distance < %s
		", $geo_lat, $geo_lon, $geo_lat, $rental_int, $miles_rental);
		
		echo $sql;
		$result = $db->query($sql);
		
	*/
	
	function get_area_pricing($txn_int, $miles) {
		$db = new db();
		$condo_lat = $this->geolat / 10000000;
		$condo_lon = $this->geolon / 10000000;
		
		$sql = sprintf("SELECT
		psf,
		sqft_range, 
		( 3959.75587 * acos( cos( radians('".$condo_lat."') ) * 
		cos( radians( geolat/10000000) ) * 
		cos( radians( geolon/10000000 ) - 
		radians('".$condo_lon."') ) + 
		sin( radians('".$condo_lat."') ) * 
		sin( radians( geolat/10000000 ) ) ) ) 
		AS distance
		FROM Master INNER JOIN condos ON Master.project = condos.project
		WHERE
		Master.tenure_cat = '%s' AND
		date >= DATE_SUB(CURDATE(), INTERVAL %s MONTH)
		HAVING distance < '%s'
		",$this->tenure_cat, $txn_int, $miles);
		
		$txn_price=array();
		$result = $db->query($sql);
		//var_dump($result);
		$result_array = array();
		if($result->num_rows > 0) {
			foreach($result->result as $txn) {
				$txn_price[] = $txn['psf'];
			}
			sort($txn_price);
			$area_txn_count = $result->num_rows;
			$area_max_price = max($txn_price);
			$area_min_price = min($txn_price);
			$area_median_price=$txn_price[round($area_txn_count / 2)-1];
			$result_array = array(
								'area_txn_count'=>$area_txn_count,
								'area_min_price'=>$area_min_price,
								'area_max_price'=>$area_max_price,
								'area_median_price'=>$area_median_price
								);
		}
		
		return $result_array;
	}
	
	function get_listings($type = 'sale') {
		$db = new db();
		$sql = sprintf("SELECT * FROM listings WHERE listing_type = '%s' AND condo_id = '%s' AND active = 1", $type, $this->id);
		
		$result = $db->query($sql);
		$listings = array();
		if($result->num_rows) {
			foreach($result->result as $listing) {
				$listings[$listing['listing_id']] = new Listing($listing);
			}
		}
		
		return $listings;
	}
	
	function get_listing_counts() {
		
		$sale = array('s'=>0,'1'=>0,'2'=>0,'3'=>0,'4'=>0,'5'=>0);
		$rent = array('s'=>0,'r'=>0,'1'=>0,'2'=>0,'3'=>0,'4'=>0,'5'=>0);
		
		$sale_listings = $this->get_listings('sale');
		
		foreach($sale_listings as $listing) {
			if(!isset($sale[$listing->listing_unitbr])) {
				$sale[$listing->listing_unitbr] = 1;
			} else {
				$sale[$listing->listing_unitbr] = $sale[$listing->listing_unitbr]+1;
			}
		}
		
		$rent_listings = $this->get_listings('rent');
		
		foreach($rent_listings as $listing) {
			if(!isset($rent[$listing->listing_unitbr])) {
				$rent[$listing->listing_unitbr] = 1;
			} else {
				$rent[$listing->listing_unitbr] = $rent[$listing->listing_unitbr]+1;
			}
		}
		
		return array('sale'=>$sale, 'rent'=>$rent);
	}

}