You are here

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

Given an array of RoomEvents the calendar is updated with regards to the events that are relevant to the Unit this calendar refers to

Parameters

RoomsEventInterface[] $events: An array of events to update the calendar with

Return value

array An array of response on whether event updates were successful or not

Overrides RoomsCalendar::updateCalendar

File

modules/rooms_pricing/includes/rooms_pricing.unit_pricing_calendar.inc, line 458
Contains UnitPricingCalendar.

Class

UnitPricingCalendar
Handles querying and updating the pricing information relative to a single bookable unit.

Code

public function updateCalendar($events) {
  foreach ($events as $event) {

    // Make sure event refers to the unit for this calendar.
    if ($event->unit_id == $this->unit_id) {

      // Get all the pricing events that fit within this event.
      $affected_events = $this
        ->getEvents($event->start_date, $event->end_date);
      $monthly_events = array();
      foreach ($affected_events as $a_event) {

        /** @var PricingEventInterface $a_event */

        // Apply the operation.
        $a_event
          ->applyOperation($event->amount, $event->operation);

        // If the event is in the same month span just queue to be added.
        if ($a_event
          ->sameMonth()) {
          $monthly_events[] = $a_event;
        }
        else {

          // Check if multi-year - if not just create monthly events.
          if ($a_event
            ->sameYear()) {
            $monthly_events_tmp = $a_event
              ->transformToMonthlyEvents();
            $monthly_events = array_merge($monthly_events, $monthly_events_tmp);
          }
          else {

            // Else transform to single years and then to monthly.
            $yearly_events = $a_event
              ->transformToYearlyEvents();
            foreach ($yearly_events as $ye) {
              $monthly_events_tmp = $ye
                ->transformToMonthlyEvents();
              $monthly_events = array_merge($monthly_events, $monthly_events_tmp);
            }
          }
        }
      }
      foreach ($monthly_events as $event) {
        $this
          ->addMonthEvent($event);
      }
    }
  }
}