You are here

function rooms_booking_manager_price_apply in Rooms - Drupal Booking for Hotels, B&Bs and Vacation Rentals 7

Calculates the price for rooms_booking line item types.

Parameters

object $line_item: Line item to modify.

1 string reference to 'rooms_booking_manager_price_apply'
rooms_booking_manager_rules_action_info in modules/rooms_booking_manager/rooms_booking_manager.rules.inc
Implements hook_rules_action_info().

File

modules/rooms_booking_manager/rooms_booking_manager.module, line 3107
Rooms Booking Manager brings together all the pieces required to find a room and book it - including the DrupalCommerce integration

Code

function rooms_booking_manager_price_apply($line_item) {
  if ($line_item->type == 'rooms_booking') {
    if ($item = field_get_items('commerce_line_item', $line_item, 'commerce_product')) {
      $product = commerce_product_load($item[0]['product_id']);
      if ($product->sku != 'ROOMS-BASIC-BOOKING') {
        return;
      }
    }
    $amount = $line_item->commerce_unit_price[LANGUAGE_NONE][0]['amount'];
    $start_date = new DateTime($line_item->rooms_booking_dates[LANGUAGE_NONE][0]['value']);
    $end_date = new DateTime($line_item->rooms_booking_dates[LANGUAGE_NONE][0]['value2']);
    $unit_id = $line_item->rooms_booked_unit_id[LANGUAGE_NONE][0]['value'];
    $unit = rooms_unit_load($unit_id);

    // First set up price modifiers.
    $price_options = is_array(field_get_items('commerce_line_item', $line_item, 'rooms_booking_options')) ? field_get_items('commerce_line_item', $line_item, 'rooms_booking_options') : array();

    // Convert Price options in Price modifiers.
    $price_modifiers = array();
    foreach ($price_options as $option) {
      $price_modifiers[rooms_options_machine_name($option['name'])] = array(
        '#name' => $option['name'],
        '#type' => ROOMS_DYNAMIC_MODIFIER,
        '#op_type' => $option['operation'],
        '#amount' => $option['value'],
        '#quantity' => $option['quantity'],
      );
    }
    $adults = $line_item->rooms_booking_number_people[LANGUAGE_NONE][0]['value'];
    $children = $line_item->rooms_booking_number_people[LANGUAGE_NONE][1]['value'];
    $childrens_age = isset($line_item->rooms_booking_children_ages[LANGUAGE_NONE]) ? $line_item->rooms_booking_children_ages[LANGUAGE_NONE] : array();
    $booking_info = array(
      'start_date' => clone $start_date,
      'end_date' => clone $end_date,
      'unit' => $unit,
      'booking_parameters' => array(
        'group_size' => $adults,
        'group_size_children' => $children,
        'childrens_age' => $childrens_age,
      ),
    );

    // Give other modules a chance to change the price modifiers.
    drupal_alter('rooms_price_modifier', $price_modifiers, $booking_info);
    $price_calendar = new UnitPricingCalendar($unit->unit_id, $price_modifiers);
    $temp_end_date = clone $end_date;
    $temp_end_date
      ->sub(new DateInterval('P1D'));
    $price = $price_calendar
      ->calculatePrice($start_date, $temp_end_date, $adults, $children, $childrens_age);
    $full_price = $price['full_price'] * 100;
    $line_item->commerce_unit_price[LANGUAGE_NONE][0]['amount'] = $full_price;
    $line_item->commerce_unit_price[LANGUAGE_NONE][0]['currency_code'] = commerce_default_currency();
    foreach ($line_item->commerce_unit_price[LANGUAGE_NONE][0]['data']['components'] as $key => $component) {
      $line_item->commerce_unit_price[LANGUAGE_NONE][0]['data']['components'][$key]['price']['amount'] = $full_price / $amount * $component['price']['amount'];
      $line_item->commerce_unit_price[LANGUAGE_NONE][0]['data']['components'][$key]['price']['currency_code'] = commerce_default_currency();
    }
  }
}