You are here

private function FeatureContext::createBookingsForUnit in Rooms - Drupal Booking for Hotels, B&Bs and Vacation Rentals 7

1 call to FeatureContext::createBookingsForUnit()
FeatureContext::bookingsOfTypeForAllUnits in test/features/bootstrap/FeatureContext.php
@Given /^(\d+) bookings of type "([^"]*)" for all "([^"]*)" units$/

File

test/features/bootstrap/FeatureContext.php, line 438

Class

FeatureContext
Features context.

Code

private function createBookingsForUnit($count, $type, $unit_id) {
  $now = new DateTime();
  $end_date = clone $now;
  $end_date
    ->sub(new DateInterval('P4D'));
  $start_date = clone $now;
  $start_date
    ->sub(new DateInterval('P7D'));
  for ($i = 0; $i < $count; $i++) {
    $profile_id = reset($this->customerProfiles);
    $profile = commerce_customer_profile_load($profile_id);
    $client_name = isset($profile->commerce_customer_address[LANGUAGE_NONE][0]['name_line']) ? $profile->commerce_customer_address[LANGUAGE_NONE][0]['name_line'] : '';

    // Save customer in rooms_customers table.
    db_merge('rooms_customers')
      ->key(array(
      'name' => $client_name,
    ))
      ->fields(array(
      'name' => $client_name,
      'commerce_customer_id' => $profile_id,
    ))
      ->execute();

    // Get customer id from rooms_customers table.
    $client_id = db_select('rooms_customers')
      ->fields('rooms_customers', array(
      'id',
    ))
      ->condition('name', $client_name, '=')
      ->execute()
      ->fetchField();
    $unit = rooms_unit_load($unit_id);
    $unit_type = $unit->type;
    $data = array(
      'type' => $type,
      'name' => $client_name,
      'customer_id' => $client_id,
      'unit_id' => $unit_id,
      'unit_type' => $unit_type,
      'start_date' => $start_date
        ->format('Y-m-d'),
      'end_date' => $end_date
        ->format('Y-m-d'),
      'price' => $unit->base_price * 300,
      'booking_status' => 1,
    );
    $booking = rooms_booking_create($data);
    $booking
      ->save();
    $booking_parameters = array(
      'adults' => 2,
      'children' => 0,
    );
    $order = rooms_booking_manager_create_order($start_date, $end_date, $booking_parameters, $unit, $booking, $client_id);
    $booking->order_id = $order->order_number;
    $booking
      ->save();
    $this->bookings[] = $booking->booking_id;
    if ($i + 1 == floor($count / 2)) {
      $end_date = clone $now;
      $end_date
        ->add(new DateInterval('P7D'));
      $start_date = clone $now;
      $start_date
        ->add(new DateInterval('P4D'));
    }
    elseif ($i + 1 < floor($count / 2)) {
      $end_date
        ->sub(new DateInterval('P7D'));
      $start_date
        ->sub(new DateInterval('P7D'));
    }
    else {
      $end_date
        ->add(new DateInterval('P7D'));
      $start_date
        ->add(new DateInterval('P7D'));
    }
  }
}