Function that returns a room price, param: dates, room id, and # of persons

Hi Feriaz,

Based on the easy_reservations_short_code.php i created a function that returns a price if you give this param: dates, room id, and # of persons

The below function works well.

If you can help i would like you to check it just in case and to ask you if there is a way to optimize this a little bit more.

Is this what you do to calculate the price for each resource in the search_form short code?

Thanks for your help.


function easy_get_room_price($date_from = 0, $date_to = 0, $easy_room_id = 0, $no_beds = 0 ) {

$time_start = microtime(true);
/*return false if parameters are not ok*/
if($date_from == 0 || $date_to == 0 || $easy_room_id == 0 || $no_beds == 0) return false;
/*Change date FORMAT*/
$arrival = str_replace('/', '-', $date_from) . "12:00:01";
$departure = str_replace('/', '-', $date_to) . "11:59:59";

/*Arguments to create a fake reservation and GET PRICE */
$array = array(
'name' => 'Calcular Precio',
'email' => "some@email.com",
'arrival' => $arrival,
'departure' => $departure,
'resource' => (int) $easy_room_id,
'resourcenumber' => 0,
'country' => 'spain',
'adults' => $no_beds,
'childs' => '0',
'reservated' => date('Y-m-d H:i:s', time()),
'status' => '',
'user' => '1'
);
/*Create new Reservation and GET Price*/
$res = new Reservation(false, $array, false);
$res->Calculate();
$property_prices['total'] = $res->price;

/*Change Date Format to calculate number of nights*/
$arrival = strtotime($arrival);
$departure = strtotime($departure);

$datediff = $departure - $arrival;
$easy_nights = floor($datediff/(60*60*24))+1;

$property_prices['nights'] = $easy_nights;
$property_prices['paxnight'] = $property_prices['total'] / intval($easy_nights) / intval($no_beds);
// $property_prices['paxnight'] = easyreservations_format_money($property_prices['paxnight'],0);

$avail = $res->checkAvailability(1);
$property_prices['available']= true; // Available by default

//print_r($avail);
if($avail)
{
$property_prices['available'] = false;
if(!empty($atts['exclude']) && in_array($easy_room_id, explode(',', $atts['exclude']))) continue;
$errors = $res->Validate(false, 0, true);
$error = '';
if($errors){
if(!is_array($errors[0])) $errors = array($errors);
foreach($errors as $aerror){
if($aerror[0] == 'pers-min') $property_prices['error'] .= '| '.sprintf(__( 'min %s pers' , 'easyReservations' ), $aerror[1]).' ';
elseif($aerror[0] == 'pers-max') $property_prices['error'] .= '| '.sprintf(__( 'max %s pers' , 'easyReservations' ), $aerror[1]).' ';
elseif($aerror[0] == 'nights-min') $property_prices['error'] .= '| '.sprintf(__( 'min %1$s %2$s' , 'easyReservations' ), $aerror[1], easyreservations_interval_infos($the_rooms_intervals_array[$room->ID], 0, $aerror[1])).' ';
elseif($aerror[0] == 'nights-max') $property_prices['error'] .= '| '.sprintf(__( 'max %1$s %2$s' , 'easyReservations' ), $aerror[1], easyreservations_interval_infos($the_rooms_intervals_array[$room->ID], 0, $aerror[1])).' ';
elseif($aerror[0] == 'start-on') $property_prices['error'] .= '| '.sprintf(__( 'arrival only %s' , 'easyReservations' ), $aerror[1]).' ';
elseif($aerror[0] == 'end-on') $property_prices['error'] .= '| '.sprintf(__( 'departure only %s' , 'easyReservations' ), $aerror[1]).' ';
}
}
}

$time_end = microtime(true);
$property_prices['exectime'] = $time_end - $time_start;

/* try {
$res->admin = false;
if(isset($_POST['coupon'])) $res = apply_filters('easy-add-res-ajax', $res);
$save = $res->coupon;
$res->fake = false;
$res->coupon = false;
$theID = $res->addReservation();
if($theID){
foreach($theID as $key => $terror){
if($key%2==0) $error.= '';
else $error .= $terror.'';
}
}
} catch(easyException $e){
$error.= ''.$e->getMessage().'';
} */
return $property_prices;

/*Change Date Format to calculate number of nights*/
Use the $res->getTimes() function instead.

In the search form it’s just a loop for each resources. But yes it fakes a reservation and calculations the price very similar.
Regards

Thanks.

You must be logged in to reply in this thread.

Not Support
3 posts