You are here

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

Loads the price information for a given unit type/price level combination.

Parameters

$form:

$type_obj:

$type:

$price:

$currency_code:

$units_per_price:

$units:

Return value

mixed

1 call to _rooms_booking_manager_load_price_info()
book_units_per_type_form in modules/rooms_booking_manager/rooms_booking_manager.module
Book units per type booking form callback.

File

modules/rooms_booking_manager/rooms_booking_manager.units_per_type_form.inc, line 99
Helper functions to clear up the units per type form and make the logic easier to follow.

Code

function _rooms_booking_manager_load_price_info($form, $type_obj, $type, $price, $currency_code, $units_per_price, $units) {
  $currency_setting = commerce_currency_load($currency_code);
  $currency_symbol = $currency_setting['symbol'];

  // Calculate the period.
  $nights = $form['end_date']['#value']
    ->diff($form['start_date']['#value'])->days;

  // Element to display price.
  $form[$type][$price]['price'] = array(
    '#prefix' => '<tr class="rooms-search-result__unit_summary_wrapper"><td class="rooms-search-result__base_price">',
    '#markup' => rooms_string(format_plural(count($units), '1 @unit_type ' . variable_get_value('rooms_booking_manager_unit_available_from') . ' @price', '@count @unit_type ' . variable_get_value('rooms_booking_manager_unit_available_from') . ' @price', array(
      '@unit_type' => $type_obj->label,
      '@price' => commerce_currency_format($price * 100, $currency_code),
    )), $context = array(
      '#component' => 'units_per_type_form',
      '#purpose' => 'display_base_price',
      '#data' => array(
        'unit_type' => $type_obj->label,
        'price' => $price,
        'currency_symbol' => $currency_symbol,
        'currency_code' => $currency_code,
        'amount' => $price,
        'units' => $units_per_price,
        'nights' => $nights,
        'arrival' => $form['start_date']['#value'],
        'departure' => $form['end_date']['#value'],
      ),
    )),
  );
  if (variable_get('rooms_price_calculation', ROOMS_PER_NIGHT) == ROOMS_PER_PERSON) {
    $units_keys = array_keys($units);

    // Calculate the price per person for the booking period.
    $base_price = $units[$units_keys[0]]['price'] / $units[$units_keys[0]]['unit']->max_sleeps * 100;
    if (module_exists('commerce_multicurrency')) {
      $base_price = commerce_currency_convert($base_price, commerce_default_currency(), $currency_code);
    }
    $form[$type][$price]['price']['#markup'] = format_plural(count($units), '1 @unit_type unit available starting at @price per
        person', '@count @unit_type units available starting at @price per person', array(
      '@unit_type' => $type_obj->label,
      '@price' => commerce_currency_format($base_price, $currency_code),
    ));
  }

  // Add form element to hold the price.
  $form[$type][$price]['price_value'] = array(
    '#type' => 'hidden',
    '#value' => $price,
    '#suffix' => '</td>',
  );

  // Dropdown element to select unit quantity.
  $options = array();
  for ($i = 0; $i <= count($units); $i++) {
    $options[$i] = $i;
  }

  // AJAX wrapper id.
  $id = 'rooms_' . $type . '_' . $price;
  $form[$type][$price]['quantity'] = array(
    '#prefix' => '<td class="rooms-search-result__select-units">',
    '#field_suffix' => variable_get_value('rooms_booking_manager_units_chosen'),
    '#type' => 'select',
    '#options' => $options,
    '#suffix' => '</td>',
    '#ajax' => array(
      'callback' => 'rooms_booking_manager_quantity_change_callback',
      'wrapper' => $type . '_' . $price . '_container',
    ),
    '#attributes' => array(
      'onchange' => "document.getElementById('" . $id . "').style.display = (this.value == 0) ? 'none' : 'table-row';",
    ),
  );
  return $form;
}