You are here

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

Submit callback for book_unit_form form.

1 string reference to 'book_unit_form_submit'
book_unit_form_builder in modules/rooms_booking_manager/rooms_booking_manager.module
The form builder builds the form (where visible is simply the purchase button) for individual bookable units.

File

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

Code

function book_unit_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'];
  $unit = rooms_unit_load($unit_id);
  $start_date = $form_state['values']['start_date'];
  $end_date = $form_state['values']['end_date'];
  $group_size = isset($form_state['values']['persons']) ? $form_state['values']['persons'] + $unit->min_sleeps : $form_state['values']['rooms_group_size'];
  $status = $form_state['values']['status'];
  $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) {
    if (variable_get('rooms_checkout_style', ROOMS_COMMERCE_CHECKOUT) == ROOMS_COMMERCE_CHECKOUT) {
      $unit = array_pop($available_units);
      $price_modifiers = array();
      if (isset($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($form_state['values']['options'][$option_name])) {
            if ($form_state['values']['options'][$option_name] == 1) {
              $quantity = 1;
              if (isset($form_state['values']['options'][$option_name . ':quantity']) && $option['operation'] != ROOMS_REPLACE) {
                $quantity = $form_state['values']['options'][$option_name . ':quantity'] + 1;
              }
              if ($option['type'] == ROOMS_OPTION_ONREQUEST) {
                $option['value'] = 0;
              }
              $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.
      module_load_include('inc', 'rooms_booking_manager', 'rooms_booking_manager.confirmation_override');
      $line_items = rooms_booking_manager_find_overlapping_line_items($sd, $ed, $unit['unit']->unit_id);
      if (!empty($line_items)) {

        // Redirect user to the override confirmation page.
        $booking_parameters = array(
          'unit_id' => $unit_id,
          'group_size' => $group_size,
        );
        $options = array();
        if (isset($form_state['values']['options'])) {
          foreach ($form_state['values']['options'] as $option => $value) {
            if (strpos($option, ':quantity') === FALSE && $value) {
              if (isset($form_state['values']['options'][$option . ':quantity'])) {
                $options[$option] = $form_state['values']['options'][$option . ':quantity'] + 1;
              }
              else {
                $options[$option] = 1;
              }
            }
          }
          $booking_parameters['options'] = $options;
        }
        $form_state['redirect'] = array(
          'booking-override-confirmation/' . $start_date . '/' . $end_date . '/' . $unit_id,
          array(
            'query' => $booking_parameters,
          ),
        );
      }
      else {

        // 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'] = variable_get('rooms_booking_manager_post_add_booking_path', 'bookings');
      }
    }
    elseif (variable_get('rooms_checkout_style', ROOMS_COMMERCE_CHECKOUT) == ROOMS_ENQ_CHECKOUT) {
      $booking_parameters = array();
      $options = array();
      if (isset($form_state['values']['options'])) {
        foreach ($form_state['values']['options'] as $option => $value) {
          if (strpos($option, ':quantity') === FALSE && $value) {
            if (isset($form_state['values']['options'][$option . ':quantity'])) {
              $options[$option] = $form_state['values']['options'][$option . ':quantity'] + 1;
            }
            else {
              $options[$option] = 1;
            }
          }
        }
        $booking_parameters['options'] = $options;
      }
      $form_state['redirect'] = array(
        'enquiry/' . $start_date . '/' . $end_date . '/' . $unit_id,
        array(
          'query' => $booking_parameters,
        ),
      );
    }
  }
  else {
    drupal_set_message(t('We apologize for the inconvenience; this unit is no longer available.'));
    $form_state['redirect'] = '<front>';
  }
}