You are here

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

Submit callback for book_units_per_type_form form.

@todo - check that original availability still holds @todo - fix the user ownership of products

2 string references to 'book_units_per_type_form_submit'
book_units_per_type_form in modules/rooms_booking_manager/rooms_booking_manager.module
Book units per type booking form callback.
_rooms_booking_manager_display_book_button in modules/rooms_booking_manager/rooms_booking_manager.units_per_type_form.inc
Add "place booking" button to form.

File

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

Code

function book_units_per_type_form_submit(&$form, &$form_state) {
  module_load_include('inc', 'rooms_booking_manager', 'rooms_booking_manager.commerce');
  global $user;
  $start_date = $form_state['values']['start_date'];
  $end_date = $form_state['values']['end_date'];
  $booking_parameters = unserialize($form_state['values']['booking_parameters']);
  $b_units = $form_state['values']['b_units'];

  // Create an "order" based on the form submitted.
  $order = array();
  foreach ($form_state['values'] as $type => $value) {
    if (is_array($value)) {
      foreach ($value as $value2) {
        if (!empty($value2['quantity'])) {
          $order[$type] = $value;
        }
      }
    }
  }
  if (variable_get('rooms_checkout_style', ROOMS_COMMERCE_CHECKOUT) == ROOMS_ENQ_CHECKOUT) {
    $sd = $start_date
      ->format('Y-m-d');
    $ed = $end_date
      ->format('Y-m-d');
    $form_state['redirect'] = array(
      "enquiry/{$sd}/{$ed}",
      array(
        'query' => array(
          'order' => $order,
        ),
      ),
    );
    return;
  }

  // Check the rooms availability and match the order against actual rooms.
  $agent = new AvailabilityAgent($start_date, $end_date, $booking_parameters, $b_units, array());
  $agent
    ->setValidStates(array_keys(array_filter(variable_get('rooms_valid_availability_states', drupal_map_assoc(array(
    ROOMS_AVAILABLE,
    ROOMS_ON_REQUEST,
  ))))));
  $units_per_type = $agent
    ->checkAvailability();

  // We are going to check that this is still true.
  foreach ($order as $type => $price_serving) {
    foreach ($price_serving as $price_level => $unit_order) {
      if ($unit_order['quantity'] > 0) {
        for ($i = 1; $i <= $unit_order['quantity']; $i++) {
          $unit = array_shift($units_per_type[$type][$price_level]);
          $childrens_age = array();
          if (isset($form_state['values'][$type][$price_level]['fieldset'][$i]['children'])) {
            for ($t = 1; $t <= $form_state['values'][$type][$price_level]['fieldset'][$i]['children']; $t++) {
              $childrens_age[] = $form_state['values'][$type][$price_level]['fieldset'][$i]['childrens_age'][$t];
            }
          }
          $persons = isset($form_state['values'][$type][$price_level]['fieldset'][$i]['persons']) ? $form_state['values'][$type][$price_level]['fieldset'][$i]['persons'] : (isset($booking_parameters[$i]['adults']) ? $booking_parameters[$i]['adults'] : $unit['unit']->min_sleeps);
          $children = isset($form_state['values'][$type][$price_level]['fieldset'][$i]['children']) ? $form_state['values'][$type][$price_level]['fieldset'][$i]['children'] + $unit['unit']->min_children : (isset($booking_parameters[$i]['children']) ? $booking_parameters[$i]['children'] : 0);

          // Fill group_size with unit size.
          $group_size = array(
            'adults' => $persons,
            'children' => $children,
            'childrens_age' => $childrens_age,
          );

          // Convert Price options in Price modifiers.
          $price_modifiers = array();
          foreach (rooms_unit_get_unit_options($unit['unit']) as $option) {
            if (isset($form_state['values'][$type][$price_level]['fieldset'][$i][rooms_options_machine_name($option['name'])])) {
              if ($form_state['values'][$type][$price_level]['fieldset'][$i][rooms_options_machine_name($option['name'])] == 1) {
                $quantity = 1;
                if (isset($form_state['values'][$type][$price_level]['fieldset'][$i][rooms_options_machine_name($option['name']) . ':quantity']) && $option['operation'] != ROOMS_REPLACE) {
                  $quantity = $form_state['values'][$type][$price_level]['fieldset'][$i][rooms_options_machine_name($option['name']) . ':quantity'] + 1;
                }
                if ($option['type'] == ROOMS_OPTION_ONREQUEST) {
                  $option['value'] = 0;
                }
                $price_modifiers[rooms_options_machine_name($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($start_date, $end_date, $unit['unit']->unit_id);
          if (!empty($line_items)) {
            if (isset($form_state['storage']['confirm'])) {
              $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);
              }
            }
            else {

              // Save the current form_state.
              $form_state['storage']['values'] = $form_state['values'];

              // Rebuild this form to show the confirmation form.
              $form_state['storage']['confirm'] = TRUE;
              $form_state['rebuild'] = TRUE;
              return;
            }
          }

          // Create line item.
          $line_item = rooms_create_line_item($unit, $agent, $group_size, $price_modifiers);

          // Add line item to cart.
          if (!empty($line_item)) {
            $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');
}