You are here

public function AvailabilityAgentCommerceFilter::applyFilter in Rooms - Drupal Booking for Hotels, B&Bs and Vacation Rentals 7

Applies the filter operation to the units in the filter.

Return value

array|int Rooms remaining after the filter, error code otherwise.

Overrides AvailabilityAgentFilterInterface::applyFilter

File

modules/rooms_booking/includes/rooms_booking.availability_agent_filter.inc, line 304
Rooms Booking agent filter interfaces and base implementations.

Class

AvailabilityAgentCommerceFilter
Filter units if is in the commerce cart.

Code

public function applyFilter() {
  global $user;

  // Check parameters.
  $start_date = isset($this->parameters['start_date']) ? $this->parameters['start_date'] : date_create()
    ->setTimestamp(0);
  $end_date = isset($this->parameters['end_date']) ? $this->parameters['end_date'] : date_create()
    ->setTimestamp(2147483647);
  if (empty($this->units)) {
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', 'rooms_unit')
      ->propertyOrderBy('max_sleeps', 'ASC')
      ->propertyCondition('bookable', 1);

    // Execute the query and collect the results.
    $results = $query
      ->execute();
    if (count($results)) {
      $results = $results['rooms_unit'];
    }
  }
  else {
    $results = $this->units;
  }

  // Load all the current carts to
  $orders = array();
  $query = new EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', 'commerce_order')
    ->propertyCondition('status', array(
    'cart',
    'checkout_checkout',
    'checkout_review',
    'checkout_payment',
    'checkout_complete',
  ))
    ->execute();
  if (isset($result['commerce_order'])) {
    $orders = commerce_order_load_multiple(array_keys($result['commerce_order']));
  }
  foreach ($orders as $order) {
    $wrapper = entity_metadata_wrapper('commerce_order', $order);

    // See if there are any product line items.
    if (commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types()) > 0) {

      // Get the unit IDs already in the cart.
      foreach ($wrapper->commerce_line_items as $line_item) {

        // There are some line_item types as: coupon, fee, etc. that don't
        // have a referenced commerce_product.
        if (in_array($line_item->type
          ->value(), commerce_product_line_item_types())) {

          // Need to check if commerce_product_type = 'rooms_product'.
          if ($line_item->commerce_product
            ->value()->type == 'rooms_product') {
            if (isset($line_item->rooms_booking_dates)) {
              $rooms_booking_dates = $line_item->rooms_booking_dates
                ->value();

              // Current line_item start_date and end_date values.
              $s_date = new DateTime($rooms_booking_dates['value']);
              $e_date = new DateTime($rooms_booking_dates['value2']);

              // If current line_item date interval overlap parameters interval
              // remove unit from results.
              if ($this
                ->checkInRange($s_date, $e_date, $start_date, $end_date)) {
                if (isset($results[$line_item->rooms_booked_unit_id
                  ->value()])) {
                  unset($results[$line_item->rooms_booked_unit_id
                    ->value()]);
                }
              }
            }
          }
        }
      }
    }
  }
  if (empty($this->units)) {
    return $results;
  }
  else {

    // Computes the intersection of units and results.
    return $this
      ->intersectUnits($results);
  }
}