Class: QuickTravel::Booking

Inherits:
Adapter
  • Object
show all
Defined in:
lib/quick_travel/booking.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Adapter

all, belongs_to, find, has_many, #to_s, update

Methods included from InitFromHash

#define_readers, #initialize, #to_hash

Class Method Details

.api_baseObject



11
12
13
# File 'lib/quick_travel/booking.rb', line 11

def self.api_base
  '/api/bookings'
end

.create(options = {}) ⇒ Object

Create an empty booking

Note, options pertain to initializing booking with some values:

options = {

:when => "28-04-2010" ,
:passengers => { "1" => nil, "2" => nil, "3" => nil, "4" => nil , "5" => nil } ,
:include_vehicle => nil , :vehicle => { :vehicle_type_id => nil , :length , :weight  } ,
:vehicle_return_weight  => nil ,
:include_trailer  => nil ,
:trailer => { :vehicle_type_id => nil , :length => nil  }

}



54
55
56
57
58
59
60
# File 'lib/quick_travel/booking.rb', line 54

def self.create(options = {})
  response = post_and_validate("#{api_base}.json", booking: options)
  fail AdapterError.new(response) unless response['id']

  return nil unless response['id']
  Booking.new(response)
end

.find_by_reference(reference) ⇒ Object



15
16
17
# File 'lib/quick_travel/booking.rb', line 15

def self.find_by_reference(reference)
  find_all!("#{api_base}/reference/#{URI.encode_www_form_component(reference)}.json").first
end

Instance Method Details

#accommodation_reserve(reservations_options = {}) ⇒ Object

Create an accommodation reservation from the given options

Returns current booking object after having added the reservation

reservations_options =

:id => nil ,
:first_travel_date => nil,
:last_travel_date => nil,
:resource_id => nil ,
:passenger_ids => { ,
:vehicle_ids => {} ,
:bed_configuration_id => nil ,
:tariff_level_id => nil

}

Example 2: reservations_options = {

:first_trave_date => "28-04-2010",
:last_travel_date => "28-04-2010",
:resource_id => 89,
:bed_configuration_id => 581,
:passenger_ids => {""=>nil},
:vehicle_ids => {""=>nil}

}



114
115
116
117
118
119
120
# File 'lib/quick_travel/booking.rb', line 114

def accommodation_reserve(reservations_options = {})
  if reservations_options[:last_travel_date].nil?
    fail AdapterError.new('No checkout date specified')
  end

  reserve('accommodations/create_or_update', reservations: reservations_options)
end

#activate!Object



280
281
282
# File 'lib/quick_travel/booking.rb', line 280

def activate!
  put_and_validate("#{api_base}/#{@id}/activate")
end

#calculate_existing_and_new_vehicles_for(required_vehicle_types) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/quick_travel/booking.rb', line 218

def calculate_existing_and_new_vehicles_for(required_vehicle_types)
  return [[], []] if required_vehicle_types.blank?

  vehicle_types_to_add = []
  existing_vehicle_ids_to_assign = []

  vehicles_yet_to_include = try(:vehicles)

  required_vehicle_types.each do |searched_vehicle_type_hash|
    matching_existing_vehicle = vehicles_yet_to_include.detect { |veh| veh.vehicle_type_id.to_i == searched_vehicle_type_hash[:vehicle_type_id].to_i }
    if matching_existing_vehicle
      existing_vehicle_ids_to_assign << matching_existing_vehicle.id.to_i
      vehicles_yet_to_include -= [matching_existing_vehicle]

    else
      vehicle_types_to_add << searched_vehicle_type_hash
    end
  end

  [existing_vehicle_ids_to_assign, vehicle_types_to_add]
end

#calculate_price_quote(params = {}) ⇒ Object

DEPRECATED: Please use PriceQuote.calculate(params.merge(booking_id: booking.id)) instead.



275
276
277
278
# File 'lib/quick_travel/booking.rb', line 275

def calculate_price_quote(params = {})
  response = post_and_validate("#{api_base}/#{@id}/price_quotes/calculate", params)
  Money.new(response['quoted_booking_gross_in_cents'])
end

#cancel!Object



284
285
286
# File 'lib/quick_travel/booking.rb', line 284

def cancel!
  put_and_validate("#{api_base}/#{@id}/cancel")
end

#clear_unfinished_reservations!Object



248
249
250
251
252
253
254
255
# File 'lib/quick_travel/booking.rb', line 248

def clear_unfinished_reservations!
  booking = self
  reservations.reject(&:complete).each do |reservation|
    # This will return updated booking..
    booking = delete_reservation(reservation)
  end
  booking
end

#clientObject



213
214
215
216
# File 'lib/quick_travel/booking.rb', line 213

def client
  return nil unless @client
  @client_object ||= Client.new(@client)
end

#client_addressObject



198
199
200
201
# File 'lib/quick_travel/booking.rb', line 198

def client_address
  return nil unless @client_address
  @client_address_object ||= Address.new(@client_address)
end

#client_contactObject



208
209
210
211
# File 'lib/quick_travel/booking.rb', line 208

def client_contact
  return nil unless @client_contact
  @client_contact_object ||= Contact.new(@client_contact)
end

#client_partyObject



203
204
205
206
# File 'lib/quick_travel/booking.rb', line 203

def client_party
  return nil unless @client_party
  @client_party_object ||= Party.new(@client_party)
end

#countryObject



38
39
40
# File 'lib/quick_travel/booking.rb', line 38

def country
  Country.find(@country_id) if @country_id
end

#customer_commentsObject



299
300
301
302
# File 'lib/quick_travel/booking.rb', line 299

def customer_comments
  comment = comments.detect{ |comment| comment['comment_type'] == 'customer' }
  comment.presence.try(:[], 'text') || ''
end

#delete_reservation(reservation) ⇒ Object

Delete a reservation

Returns current booking object after deleting the reservation



163
164
165
# File 'lib/quick_travel/booking.rb', line 163

def delete_reservation(reservation)
  delete_reservation_by_id(reservation.id)
end

#delete_reservation_by_id(reservation_id) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/quick_travel/booking.rb', line 167

def delete_reservation_by_id(reservation_id)
  if state != 'new'
    fail AdapterError.new('Reservation cannot be deleted unless the booking is new')
  end

  delete_and_validate("#{api_base}/#{@id}/reservations/#{reservation_id}.json")
  refresh!
end

#documents(regenerate = false) ⇒ Object



19
20
21
22
# File 'lib/quick_travel/booking.rb', line 19

def documents(regenerate = false)
  Document.find_all!("#{Booking.api_base}/#{@id}/documents.json",
                     last_group: true, regenerate: regenerate)
end

#finalised?Boolean

Returns:

  • (Boolean)


257
258
259
# File 'lib/quick_travel/booking.rb', line 257

def finalised?
  (balance_in_cents == 0 && state != 'new' && !reservations.empty?) || state == 'quote'
end

#find_passenger_by_id(pid) ⇒ Object



180
181
182
# File 'lib/quick_travel/booking.rb', line 180

def find_passenger_by_id(pid)
  passengers.detect { |p| p.id.to_i == pid.to_i }
end

#find_vehicle_by_id(vid) ⇒ Object



184
185
186
# File 'lib/quick_travel/booking.rb', line 184

def find_vehicle_by_id(vid)
  vehicles.detect { |v| v.id.to_i == vid.to_i }
end

#generics_reserve(options) ⇒ Object



148
149
150
# File 'lib/quick_travel/booking.rb', line 148

def generics_reserve(options)
  reserve(:generics, options)
end

#include_reservation_of?(product_type_id) ⇒ Boolean

Returns:

  • (Boolean)


240
241
242
# File 'lib/quick_travel/booking.rb', line 240

def include_reservation_of?(product_type_id)
  reservations.any? { |r| r.product_type_id.to_i == product_type_id }
end

#includes_resource_class?(resource_class_name) ⇒ Boolean

Returns:

  • (Boolean)


244
245
246
# File 'lib/quick_travel/booking.rb', line 244

def includes_resource_class?(resource_class_name)
  reservations.any? { |r| r.resource_class_name == resource_class_name }
end

#item_reserve(options) ⇒ Object



152
153
154
# File 'lib/quick_travel/booking.rb', line 152

def item_reserve(options)
  reserve(:items, options)
end

#on_account_payment_typeObject



30
31
32
33
34
35
36
# File 'lib/quick_travel/booking.rb', line 30

def 
  payment_types_by_code = payment_types.group_by(&:code)

  # Try on-account, or if not, on-account-with-reference
  payment_types_by_code['on_account_without_reference'].try(:first) ||
    payment_types_by_code['on_account_with_reference'].try(:first)
end

#passenger_types_countsObject



192
193
194
195
196
# File 'lib/quick_travel/booking.rb', line 192

def passenger_types_counts
  passengers.each_with_object(Hash.new(0)) do |passenger, hash|
    hash[passenger.passenger_type_id] += 1
  end
end

#passengersObject



188
189
190
# File 'lib/quick_travel/booking.rb', line 188

def passengers
  @passenger_objects ||= @passengers_attributes.map{ |p| Passenger.new(p) }
end

#price_changeObject



261
262
263
# File 'lib/quick_travel/booking.rb', line 261

def price_change
  @price_change ||= fetch_price_change
end

#price_change_on(reservation) ⇒ Object



265
266
267
# File 'lib/quick_travel/booking.rb', line 265

def price_change_on(reservation)
  price_change.price_change_on(reservation.id)
end

#refresh!Object



176
177
178
# File 'lib/quick_travel/booking.rb', line 176

def refresh!
  Booking.find(@id)  # refresh
end

#remove_unassigned_passengersObject



86
87
88
# File 'lib/quick_travel/booking.rb', line 86

def remove_unassigned_passengers
  put_and_validate("/api/bookings/#{@id}/remove_unassigned_passengers")
end

#scheduled_trips_reserve(segments = {}) ⇒ Object

Reserve a scheduled trips resource Returns current booking object after having added the reservation

Note: Forward_options and return_options look similar, but each is a different trip

Example: forward_options =

:passenger_ids => { ,
:vehicle_ids => {} ,
:first_travel_date => nil,
:tariff_level_id => nil ,
:resource_id => nil ,
:trip_id => nil

}



137
138
139
# File 'lib/quick_travel/booking.rb', line 137

def scheduled_trips_reserve(segments = {})
  reserve(:scheduled_trips, segments.merge(segments: segments.keys))
end

#scheduled_trips_update(segments = {}) ⇒ Object



141
142
143
144
145
146
# File 'lib/quick_travel/booking.rb', line 141

def scheduled_trips_update(segments = {})
  params = segments.merge(segments: segments.keys)
  params[:booking_id] = @id
  put_and_validate('/reservation_for/scheduled_trips/bulk_update.json', params)
  Booking.find(@id)
end

#secure_access_codeObject

secureAccessCodeString is simply a MAC algorithm (using SHA1) on the booking id string. it is using @id that is booking.id and configuration constant QUICK_TRAVEL_ACCESS_KEY



295
296
297
# File 'lib/quick_travel/booking.rb', line 295

def secure_access_code
  Encrypt.access_key(@id.to_s)
end

#subscribe_to_email_list(email = nil) ⇒ Object



288
289
290
291
# File 'lib/quick_travel/booking.rb', line 288

def subscribe_to_email_list(email = nil)
  params = email.nil? ? {} : { email: email }
  put_and_validate("#{api_base}/#{@id}/subscribe_to_email_list", params)
end

#total_price_change_on(reservation) ⇒ Object



269
270
271
# File 'lib/quick_travel/booking.rb', line 269

def total_price_change_on(reservation)
  price_change.total_price_change_on(reservation.id)
end

#tour_reserve(options) ⇒ Object



156
157
158
# File 'lib/quick_travel/booking.rb', line 156

def tour_reserve(options)
  reserve(:packages, options)
end

#update(attrs = {}, options = {}) ⇒ Object

Update an existing booking



63
64
65
66
67
68
69
70
# File 'lib/quick_travel/booking.rb', line 63

def update(attrs = {}, options = {})
  response_object = put_and_validate("#{api_base}/#{@id}.json", options.merge(booking: attrs), return_response_object: true)
  response = response_object.parsed_response
  # id is returned if other attributes change otherwise success: true
  fail AdapterError.new(response) unless response_object.no_content? || response['id'] || response['success']

  Booking.find(@id)
end

#update_with_nested_attributes!(booking_args = {}) ⇒ Object

### Updates:

- Client information
- Client contact
- Client address
- Pax details
- Vehicle details


79
80
81
82
83
84
# File 'lib/quick_travel/booking.rb', line 79

def update_with_nested_attributes!(booking_args = {})
  response = put_and_validate("#{api_base}/#{@id}/update_with_nested_attributes.json",
                              booking: booking_args)
  fail AdapterError.new(response) unless response['id']
  Booking.find(response['id'])
end