You are here

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

Submit hanlder for the confirmation form.

File

modules/rooms_booking_manager/rooms_booking_manager.confirmation_override.inc, line 93
Override confirmation page logic.

Code

function rooms_booking_manager_override_confirmation_form_submit($form, &$form_state) {
  module_load_include('inc', 'rooms_booking_manager', 'rooms_booking_manager.commerce');
  global $user;
  $unit_id = $form_state['values']['unit_id'];
  $start_date = $form_state['values']['start_date'];
  $end_date = $form_state['values']['end_date'];
  $group_size = $form_state['values']['rooms_group_size'];
  $booking_parameters = array(
    'adults' => $group_size,
    'children' => 0,
  );

  // This is very inefficient right now but we need to create date objects
  // reconvert them back to strings to recreate them in the Availability Agent.
  $sd = start_date_load($start_date);
  $ed = end_date_load($end_date);

  // Let us get available rooms again and match the order against actual rooms.
  $agent = new AvailabilityAgent($sd, $ed, $booking_parameters);
  $agent
    ->setValidStates(array_keys(array_filter(variable_get('rooms_valid_availability_states', drupal_map_assoc(array(
    ROOMS_AVAILABLE,
    ROOMS_ON_REQUEST,
  ))))));

  // Let us make sure our bookable unit is still available.
  $available_units = $agent
    ->checkAvailabilityForUnit($unit_id);
  if (count($available_units) > 0) {
    $unit = array_pop($available_units);
    $price_modifiers = array();
    if (isset($form_state['values']['options'])) {
      $options = unserialize($form_state['values']['options']);

      // Convert Price options in Price modifiers.
      foreach (rooms_unit_get_unit_options($unit['unit']) as $option) {
        $option_name = rooms_options_machine_name($option['name']);
        if (isset($options[$option_name])) {
          if ($options[$option_name] == 1) {
            $quantity = 1;
            if (isset($options[$option_name . ':quantity']) && $option['operation'] != ROOMS_REPLACE) {
              $quantity = $options[$option_name . ':quantity'] + 1;
            }
            $price_modifiers[$option_name] = array(
              '#name' => $option['name'],
              '#type' => ROOMS_DYNAMIC_MODIFIER,
              '#op_type' => $option['operation'],
              '#amount' => $option['value'],
              '#quantity' => $quantity,
            );
          }
        }
      }
    }

    // Check if there are line_items with overlapping dates.
    $line_items = rooms_booking_manager_find_overlapping_line_items($sd, $ed, $unit['unit']->unit_id);
    if (!empty($line_items)) {
      $commerce_order = commerce_cart_order_load($user->uid);
      foreach ($line_items as $line_item_id) {
        commerce_cart_order_product_line_item_delete($commerce_order, $line_item_id);
      }
    }

    // Create line item.
    if (isset($form_state['values']['children'])) {
      $children = $form_state['values']['children'];
      if (isset($form_state['values']['childrens_age'])) {
        $childrens_age = $form_state['values']['childrens_age'];
        $line_item = rooms_create_line_item($unit, $agent, array(
          'adults' => $group_size,
          'children' => $children,
          'childrens_age' => $childrens_age,
        ), $price_modifiers);
      }
      else {
        $line_item = rooms_create_line_item($unit, $agent, array(
          'adults' => $group_size,
          'children' => $children,
        ), $price_modifiers);
      }
    }
    else {
      $line_item = rooms_create_line_item($unit, $agent, array(
        'adults' => $group_size,
        'children' => 0,
      ), $price_modifiers);
    }

    // Add line item to cart.
    $line_item = commerce_cart_product_add($user->uid, $line_item, FALSE);

    // Refresh line items price and redirect to bookings page.
    commerce_cart_order_refresh(commerce_cart_order_load($user->uid));
    $form_state['redirect'] = 'bookings';
  }
  else {
    drupal_set_message(t('We apologize for the inconvenience; this unit is no longer available.'));
    $form_state['redirect'] = '<front>';
  }
}