You are here

public function UnitPricingCalendar::calculatePrice in Rooms - Drupal Booking for Hotels, B&Bs and Vacation Rentals 7

Given a date range determine the cost of the room over that period.

Parameters

DateTime $start_date: The starting date for the search.

DateTime $end_date: The end date for the search.

int $persons: The number of persons staying in this room.

int $children: The number of children staying in this room.

array $children_ages: Children ages.

Return value

array Array holding full price and booking price of the room for that period.

Overrides UnitPricingCalendarInterface::calculatePrice

File

modules/rooms_pricing/includes/rooms_pricing.unit_pricing_calendar.inc, line 57
Contains UnitPricingCalendar.

Class

UnitPricingCalendar
Handles querying and updating the pricing information relative to a single bookable unit.

Code

public function calculatePrice(DateTime $start_date, DateTime $end_date, $persons = 0, $children = 0, $children_ages = array()) {
  if ($persons == 0) {
    $persons = $this->unit->max_sleeps;
  }
  $price = 0;
  $booking_price = 0;
  $booking_days = 0;

  // Setup pricing reply and log
  $reply = array(
    'full_price' => $price,
    'booking_price' => $booking_price,
    'log' => array(),
  );

  // Get settings to add to log
  $reply['log']['rooms_children_discount_options'] = variable_get('rooms_children_discount_options', array());
  $reply['log']['rooms_price_calculation'] = variable_get('rooms_price_calculation', ROOMS_PER_NIGHT);
  $pricing_events = $this
    ->getEvents($start_date, $end_date);
  $reply['log']['pricing_events'] = $pricing_events;
  foreach ($pricing_events as $event) {
    $days = $event
      ->diff()->days + 1;
    $booking_days += $days;
    if (variable_get('rooms_price_calculation', ROOMS_PER_NIGHT) == ROOMS_PER_PERSON) {
      $children_discount_options = variable_get('rooms_children_discount_options', array());
      $price = $price + $days * $event->amount * ($persons - $children);
      foreach ($children_ages as $age) {
        $reply['log']['children'][$age]['pre'] = $price;
        if (is_array($age)) {
          $age = $age['value'];
        }
        $discount = 0;
        foreach ($children_discount_options as $option) {
          if ($age >= $option['start'] && $age <= $option['end']) {
            $discount = $option['discount'];
            break;
          }
        }
        $price = $price + $days * $event->amount * (100 - $discount) / 100;
        $reply['log']['children'][$age]['post'] = $price;
      }
    }
    else {
      $price = $price + $days * $event->amount;
    }
  }
  $booking_info = array(
    'unit' => $this->unit,
    'start_date' => $start_date,
    'end_date' => $end_date,
    'booking_parameters' => array(
      'group_size' => $persons,
      'group_size_children' => $children,
      'childrens_age' => $children_ages,
    ),
  );
  drupal_alter('rooms_booking_amount_before_modifiers', $price, $booking_info);
  $price = $this
    ->applyPriceModifiers($price, $booking_days, $reply);
  $payment_option = variable_get('rooms_payment_options', FULL_PAYMENT);
  $reply['rooms_payment_option'] = variable_get('rooms_payment_options', FULL_PAYMENT);
  switch ($payment_option) {
    case FULL_PAYMENT:
      $booking_price = $price;
      break;
    case PERCENT_PAYMENT:
      $reply['rooms_payment_option'][PERCENT_PAYMENT] = variable_get('rooms_payment_options_percentual');
      $booking_price = $price / 100 * variable_get('rooms_payment_options_percentual');
      break;
    case FIRST_NIGHT_PAYMENT:
      $booking_price = $pricing_events[0]->amount;
      $reply['rooms_payment_option'][FIRST_NIGHT_PAYMENT] = $booking_price;
      break;
  }
  $reply['full_price'] = $price;
  $reply['booking_price'] = $booking_price;
  return $reply;
}