Sha256: 259b2d2327cea62763c0798693587949fec92fa1cc30ed587937dd28fef53c5e

Contents?: true

Size: 1.91 KB

Versions: 13

Compression:

Stored size: 1.91 KB

Contents

require 'quick_travel/adapter'
require 'quick_travel/adjustment'
require 'quick_travel/resource'

module QuickTravel
  class Reservation < Adapter
    has_many :adjustments
    has_many :sub_reservations, class_name: 'Reservation'
    belongs_to :to_route_stop, class_name: 'RouteStop'
    belongs_to :from_route_stop, class_name: 'RouteStop'

    def self.create(options)
      json = post_and_validate('/api/reservations.json', options, expect: :json)
      new(json)
    end

    def self.destroy(id)
      delete_and_validate("/api/reservations/#{id}.json")
    end

    def resource
      @resource ||= QuickTravel::Resource.find(resource_id) if resource_id
    end

    def start_date_time
      start_time.to_time.on(first_travel_date) if start_time
    end

    def end_date_time
      end_time.to_time.on(last_travel_date) if end_time
    end

    def passenger_splits
      @_passenger_splits ||= @passenger_splits.map{ |split| PassengerSplit.new(split) }
    end

    def passengers_count_string(booking)
      passengers_count(booking).join(', ')
    end

    def passengers_count(booking)
      passenger_type_count = {}
      if passenger_splits.present?
        passenger_splits.each do |p|
          passenger = booking.find_passenger_by_id(p.consumer_id)

          if passenger.present?
            passenger_type_count[passenger.passenger_type_id] ||= 0
            passenger_type_count[passenger.passenger_type_id] += 1
          end
        end
      end
      PassengerType.passenger_counts(passenger_type_count)
    end

    def vehicle_count_string(booking)
      vehicles_count(booking).to_sentence
    end

    def vehicles_count(booking)
      vehicle_splits.map do|v|
        vehicle = booking.find_vehicle_by_id(v['consumer_id'])
        "#{vehicle.length}m #{vehicle.vehicle_type.name}"
      end
    end

    def adjusted?
      pre_adjusted_gross_including_packaged_item != gross_including_packaged_item
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
quicktravel_client-4.7.0 lib/quick_travel/reservation.rb
quicktravel_client-4.6.0 lib/quick_travel/reservation.rb
quicktravel_client-4.5.0 lib/quick_travel/reservation.rb
quicktravel_client-4.4.0 lib/quick_travel/reservation.rb
quicktravel_client-4.3.2 lib/quick_travel/reservation.rb
quicktravel_client-4.3.1 lib/quick_travel/reservation.rb
quicktravel_client-4.3.0 lib/quick_travel/reservation.rb
quicktravel_client-4.2.0 lib/quick_travel/reservation.rb
quicktravel_client-4.1.0 lib/quick_travel/reservation.rb
quicktravel_client-4.0.0 lib/quick_travel/reservation.rb
quicktravel_client-3.9.0 lib/quick_travel/reservation.rb
quicktravel_client-3.8.1 lib/quick_travel/reservation.rb
quicktravel_client-3.8.0 lib/quick_travel/reservation.rb