spree_cm_commissioner 2.13.7 → 2.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/app/assets/javascripts/spree_cm_commissioner/backend.js +0 -1
- data/app/assets/stylesheets/spree_cm_commissioner/backend/calendar.scss +0 -39
- data/app/controllers/concerns/spree/admin/rates_availability_cells.rb +16 -0
- data/app/controllers/concerns/spree/api/v2/product_list_includes_decorator.rb +1 -0
- data/app/controllers/spree/admin/guests_controller.rb +2 -1
- data/app/controllers/spree/admin/integrations_controller.rb +16 -7
- data/app/controllers/spree/admin/inventory_items_controller.rb +33 -62
- data/app/controllers/spree/admin/product_option_types_controller.rb +88 -0
- data/app/controllers/spree/admin/rate_plans_controller.rb +125 -0
- data/app/controllers/spree/admin/rates_availabilities_controller.rb +142 -0
- data/app/controllers/spree/admin/stock_managements_controller.rb +1 -53
- data/app/controllers/spree/api/switch_cm/base_controller.rb +109 -0
- data/app/controllers/spree/api/switch_cm/tokens_controller.rb +68 -0
- data/app/controllers/spree/api/switch_cm/xml/availabilities_controller.rb +39 -0
- data/app/controllers/spree/api/switch_cm/xml/base_controller.rb +37 -0
- data/app/controllers/spree/api/switch_cm/xml/reservation_summaries_controller.rb +28 -0
- data/app/controllers/spree/api/switch_cm/xml/reservations_controller.rb +57 -0
- data/app/controllers/spree/api/switch_cm/xml/room_rate_availabilities_controller.rb +56 -0
- data/app/controllers/spree/api/switch_cm/xml/room_rates_controller.rb +45 -0
- data/app/controllers/spree/api/v2/operator/guests_controller.rb +1 -1
- data/app/controllers/spree/api/v2/storefront/accommodations/variants_controller.rb +31 -5
- data/app/controllers/spree/api/v2/storefront/inventory_items_controller.rb +2 -2
- data/app/controllers/spree/api/v2/tenant/intercity_taxi/draft_orders_controller.rb +0 -1
- data/app/factory/spree_cm_commissioner/order_telegram_message_factory.rb +1 -1
- data/app/finders/spree_cm_commissioner/accommodations/find.rb +10 -8
- data/app/finders/spree_cm_commissioner/accommodations/find_variant.rb +5 -11
- data/app/finders/spree_cm_commissioner/rates_availabilities/find_bookings.rb +116 -0
- data/app/helpers/spree_cm_commissioner/admin/inventory_items_helper.rb +36 -0
- data/app/helpers/spree_cm_commissioner/admin/rates_availabilities_helper.rb +247 -0
- data/app/interactors/spree_cm_commissioner/intercity_taxi_order_creator.rb +0 -1
- data/app/jobs/spree_cm_commissioner/option_type_variants_public_metadata_updater_job.rb +1 -0
- data/app/jobs/spree_cm_commissioner/option_value_variants_public_metadata_updater_job.rb +1 -0
- data/app/jobs/spree_cm_commissioner/product_variants_public_metadata_updater_job.rb +12 -0
- data/app/models/concerns/spree_cm_commissioner/kyc_bitwise.rb +4 -0
- data/app/models/concerns/spree_cm_commissioner/line_item_durationable.rb +1 -10
- data/app/models/concerns/spree_cm_commissioner/line_item_guests_concern.rb +33 -3
- data/app/models/concerns/spree_cm_commissioner/line_item_rate_plannable.rb +55 -0
- data/app/models/concerns/spree_cm_commissioner/line_item_transitable.rb +0 -16
- data/app/models/concerns/spree_cm_commissioner/option_type_attr_type.rb +22 -4
- data/app/models/concerns/spree_cm_commissioner/rate_plan_options_concern.rb +62 -0
- data/app/models/concerns/spree_cm_commissioner/variant_options_concern.rb +12 -0
- data/app/models/concerns/spree_cm_commissioner/variant_rate_plannable.rb +81 -0
- data/app/models/spree_cm_commissioner/address_decorator.rb +2 -0
- data/app/models/spree_cm_commissioner/configuration.rb +0 -6
- data/app/models/spree_cm_commissioner/exports/import_order_guests_csv.rb +2 -1
- data/app/models/spree_cm_commissioner/exports/operator_guest_json_gzip.rb +8 -2
- data/app/models/spree_cm_commissioner/guest.rb +1 -1
- data/app/models/spree_cm_commissioner/integration.rb +15 -0
- data/app/models/spree_cm_commissioner/integrations/switch_cm.rb +96 -0
- data/app/models/spree_cm_commissioner/inventory_item.rb +32 -3
- data/app/models/spree_cm_commissioner/line_item_decorator.rb +7 -7
- data/app/models/spree_cm_commissioner/maintenance_tasks/cache_invalidation.rb +20 -4
- data/app/models/spree_cm_commissioner/maintenance_tasks/event.rb +3 -2
- data/app/models/spree_cm_commissioner/option_type_decorator.rb +2 -1
- data/app/models/spree_cm_commissioner/option_value_decorator.rb +3 -0
- data/app/models/spree_cm_commissioner/option_value_rate_plan.rb +12 -0
- data/app/models/spree_cm_commissioner/order_decorator.rb +1 -0
- data/app/models/spree_cm_commissioner/price_decorator.rb +13 -0
- data/app/models/spree_cm_commissioner/product_decorator.rb +18 -2
- data/app/models/spree_cm_commissioner/product_option_type_decorator.rb +42 -0
- data/app/models/spree_cm_commissioner/rate_plan.rb +139 -0
- data/app/models/spree_cm_commissioner/rate_plan_options.rb +36 -0
- data/app/models/spree_cm_commissioner/rate_restriction.rb +45 -0
- data/app/models/spree_cm_commissioner/taxon_decorator.rb +5 -2
- data/app/models/spree_cm_commissioner/variant_decorator.rb +36 -5
- data/app/models/spree_cm_commissioner/variant_options.rb +3 -2
- data/app/models/spree_cm_commissioner/vendor_decorator.rb +1 -0
- data/app/overrides/spree/admin/option_types/_form/checkbox.html.erb.deface +0 -8
- data/app/overrides/spree/admin/option_types/_form/name.html.erb.deface +1 -1
- data/app/overrides/spree/admin/products/_filters/vendor_filter.html.erb.deface +6 -0
- data/app/overrides/spree/admin/shared/_product_tabs/rates_availabilities.html.erb.deface +8 -0
- data/app/overrides/spree/admin/shared/_product_tabs/stock_managements.html.erb.deface +1 -1
- data/app/overrides/spree/admin/variants/index/discontinued_badge.html.erb.deface +13 -0
- data/app/overrides/spree/admin/variants/index/option_types_empty_state_link.html.erb.deface +3 -0
- data/app/overrides/spree/admin/variants/index/option_types_summary.html.erb.deface +24 -0
- data/app/presenters/spree/variants/visible_options_presenter.rb +34 -1
- data/app/queries/spree_cm_commissioner/multi_leg_trips_query.rb +40 -22
- data/app/queries/spree_cm_commissioner/single_leg_trips_query.rb +28 -14
- data/app/request_schemas/spree_cm_commissioner/intercity_taxi_draft_order_update_schema.rb +0 -1
- data/app/serializers/spree/v2/storefront/line_item_serializer_decorator.rb +2 -0
- data/app/serializers/spree/v2/storefront/option_type_serializer_decorator.rb +1 -1
- data/app/serializers/spree/v2/storefront/product_option_type_serializer.rb +15 -0
- data/app/serializers/spree/v2/storefront/product_serializer_decorator.rb +15 -0
- data/app/serializers/spree/v2/tenant/line_item_serializer.rb +1 -1
- data/app/serializers/spree/v2/tenant/option_type_serializer.rb +1 -1
- data/app/serializers/spree_cm_commissioner/v2/operator/dynamic_field_option_serializer.rb +9 -0
- data/app/serializers/spree_cm_commissioner/v2/operator/dynamic_field_serializer.rb +11 -0
- data/app/serializers/spree_cm_commissioner/v2/operator/guest_dynamic_field_serializer.rb +14 -0
- data/app/serializers/spree_cm_commissioner/v2/operator/guest_serializer.rb +1 -0
- data/app/serializers/spree_cm_commissioner/v2/storefront/accommodation_variant_serializer.rb +44 -0
- data/app/serializers/spree_cm_commissioner/v2/storefront/inventory_item_serializer.rb +4 -4
- data/app/serializers/spree_cm_commissioner/v2/storefront/rate_plan_serializer.rb +16 -0
- data/app/services/spree_cm_commissioner/integrations/book_me_bus_v1/polling/sync_item_availability.rb +1 -1
- data/app/services/spree_cm_commissioner/integrations/larryta/polling/sync_item_availability.rb +1 -1
- data/app/services/spree_cm_commissioner/integrations/switch_cm/inventory/accept_push.rb +111 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/inventory/accommodation_lock.rb +43 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/inventory/apply_updates.rb +133 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/inventory/availability_snapshot.rb +127 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/inventory/inventory_update.rb +35 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/inventory/pushed_nights.rb +82 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/inventory/unseen_room_nights.rb +49 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/inventory/validate_updates.rb +153 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/reservations/feed.rb +209 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/reservations/mark_seen.rb +62 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/reservations/reservation.rb +171 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/room_catalog.rb +88 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/sync_manager.rb +16 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/sync_strategies/full_sync_strategy.rb +39 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/sync_strategies/incremental_sync_strategy.rb +51 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/xml/availability_parser.rb +103 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/xml/push_response.rb +45 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/xml/reservation_serializer.rb +83 -0
- data/app/services/spree_cm_commissioner/integrations/switch_cm/xml/response_builder.rb +19 -0
- data/app/services/spree_cm_commissioner/integrations/vireak_buntham/polling/sync_item_availability.rb +1 -1
- data/app/services/spree_cm_commissioner/intercity_taxi_order/update.rb +1 -3
- data/app/services/spree_cm_commissioner/inventory_items/bulk_adjust_quantities_by_variant.rb +4 -0
- data/app/services/spree_cm_commissioner/inventory_items/reset.rb +4 -0
- data/app/services/spree_cm_commissioner/rate_plans/save.rb +65 -0
- data/app/services/spree_cm_commissioner/rates_availabilities/bulk_update.rb +225 -0
- data/app/services/spree_cm_commissioner/reschedule_requests/split_line_item.rb +5 -0
- data/app/services/spree_cm_commissioner/room_types/create.rb +87 -0
- data/app/services/spree_cm_commissioner/room_types/update_properties.rb +32 -0
- data/app/services/spree_cm_commissioner/room_types.rb +40 -0
- data/app/services/spree_cm_commissioner/route_prices/update_price_range.rb +3 -1
- data/app/services/spree_cm_commissioner/trips/search.rb +14 -7
- data/app/services/spree_cm_commissioner/trips/update_single_leg.rb +5 -1
- data/app/views/spree/admin/guests/_check_in_status.html.erb +28 -22
- data/app/views/spree/admin/guests/edit.html.erb +29 -26
- data/app/views/spree/admin/integrations/_form.html.erb +4 -0
- data/app/views/spree/admin/integrations/_switch_cm_fields.html.erb +53 -0
- data/app/views/spree/admin/inventory_items/_details.html.erb +56 -0
- data/app/views/spree/admin/product_option_types/index.html.erb +79 -0
- data/app/views/spree/admin/rate_plans/_form.html.erb +134 -0
- data/app/views/spree/admin/rate_plans/_order_form.html.erb +37 -0
- data/app/views/spree/admin/rates_availabilities/_block.html.erb +34 -0
- data/app/views/spree/admin/rates_availabilities/_bookings.html.erb +52 -0
- data/app/views/spree/admin/rates_availabilities/_filters.html.erb +48 -0
- data/app/views/spree/admin/rates_availabilities/_legend.html.erb +27 -0
- data/app/views/spree/admin/rates_availabilities/_list_pages.html.erb +14 -0
- data/app/views/spree/admin/rates_availabilities/_modal_header.html.erb +11 -0
- data/app/views/spree/admin/rates_availabilities/_names.html.erb +62 -0
- data/app/views/spree/admin/rates_availabilities/_script.html.erb +676 -0
- data/app/views/spree/admin/rates_availabilities/_styles.html.erb +159 -0
- data/app/views/spree/admin/rates_availabilities/index.html.erb +112 -0
- data/app/views/spree/admin/shared/sub_menu/_homepage.html.erb +0 -1
- data/app/views/spree/admin/stock_managements/_header.html.erb +0 -14
- data/app/views/spree/admin/stock_managements/_inventory_item_stats.html.erb +28 -0
- data/app/views/spree/admin/stock_managements/index.html.erb +1 -56
- data/config/initializers/spree_permitted_attributes.rb +1 -0
- data/config/locales/en.yml +241 -3
- data/config/locales/km.yml +241 -4
- data/config/routes.rb +36 -17
- data/db/migrate/20260928000000_add_oauth_application_id_to_cm_integrations.rb +5 -0
- data/db/migrate/20260929000000_create_cm_rate_plans.rb +22 -0
- data/db/migrate/20260929000001_add_rate_plan_to_spree_prices.rb +24 -0
- data/db/migrate/20260929000002_add_rate_plan_to_spree_line_items.rb +7 -0
- data/db/migrate/20260929000003_create_cm_option_value_rate_plans.rb +16 -0
- data/db/migrate/20261009000000_create_cm_rate_restrictions.rb +29 -0
- data/db/migrate/20261013000001_add_hidden_and_presentation_to_spree_product_option_types.rb +6 -0
- data/db/migrate/20261013000002_move_hidden_from_spree_option_types_to_product_option_types.rb +36 -0
- data/db/migrate/20261016000001_add_index_to_spree_line_items_number.rb +8 -0
- data/lib/spree_cm_commissioner/booking_dates.rb +43 -0
- data/lib/spree_cm_commissioner/calendar_event.rb +0 -25
- data/lib/spree_cm_commissioner/rate.rb +74 -0
- data/lib/spree_cm_commissioner/rate_closings.rb +48 -0
- data/lib/spree_cm_commissioner/rates_availability_grid.rb +113 -0
- data/lib/spree_cm_commissioner/stay_price.rb +30 -0
- data/lib/spree_cm_commissioner/stay_prices.rb +58 -0
- data/lib/spree_cm_commissioner/test_helper/factories/integration_factory.rb +6 -0
- data/lib/spree_cm_commissioner/test_helper/factories/option_type_factory.rb +6 -0
- data/lib/spree_cm_commissioner/test_helper/factories/option_value_rate_plan_factory.rb +6 -0
- data/lib/spree_cm_commissioner/test_helper/factories/rate_plan_factory.rb +14 -0
- data/lib/spree_cm_commissioner/test_helper/factories/variant_factory.rb +9 -0
- data/lib/spree_cm_commissioner/version.rb +1 -1
- data/lib/spree_cm_commissioner.rb +6 -0
- metadata +94 -32
- data/app/assets/javascripts/spree_cm_commissioner/calendar.js +0 -59
- data/app/controllers/spree/admin/calendars/base_controller.rb +0 -27
- data/app/controllers/spree/admin/calendars/orders_controller.rb +0 -27
- data/app/controllers/spree/admin/homepage_feed_controller.rb +0 -36
- data/app/controllers/spree/admin/metafields_controller.rb +0 -69
- data/app/models/concerns/spree_cm_commissioner/metafield.rb +0 -15
- data/app/models/spree_cm_commissioner/homepage_feed.rb +0 -4
- data/app/overrides/spree/admin/shared/_main_menu/sidebar_calendars.html.erb.deface +0 -5
- data/app/overrides/spree/admin/shared/_product_tabs/metafields.html.erb.deface +0 -15
- data/app/serializers/spree_cm_commissioner/v2/storefront/taxon_include_product_serializer.rb +0 -13
- data/app/serializers/spree_cm_commissioner/v2/storefront/vendor_include_product_serializer.rb +0 -14
- data/app/services/spree_cm_commissioner/feed/taxon_include_product.rb +0 -20
- data/app/services/spree_cm_commissioner/feed/taxon_product.rb +0 -54
- data/app/services/spree_cm_commissioner/feed/vendor_include_product.rb +0 -20
- data/app/services/spree_cm_commissioner/feed/vendor_product.rb +0 -52
- data/app/services/spree_cm_commissioner/homepage_data_loader.rb +0 -112
- data/app/services/spree_cm_commissioner/metafields/product_metadata_service.rb +0 -32
- data/app/views/shared/_calendar.html.erb +0 -27
- data/app/views/shared/calendar/_order_popover.html.erb +0 -18
- data/app/views/shared/calendar/_orders_sidebar.html.erb +0 -41
- data/app/views/spree/admin/calendars/orders/index.html.erb +0 -22
- data/app/views/spree/admin/homepage_feed/_form.html.erb +0 -24
- data/app/views/spree/admin/homepage_feed/edit.html.erb +0 -17
- data/app/views/spree/admin/inventory_items/prices.html.erb +0 -43
- data/app/views/spree/admin/inventory_items/stocks.html.erb +0 -34
- data/app/views/spree/admin/metafields/_private_metafields_form.html.erb +0 -71
- data/app/views/spree/admin/metafields/_public_metafields_form.html.erb +0 -64
- data/app/views/spree/admin/metafields/edit.html.erb +0 -27
- data/app/views/spree/admin/shared/sub_menu/_calendar.html.erb +0 -3
- data/app/views/spree/admin/stock_managements/calendar.html.erb +0 -310
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
class SpreeCmCommissioner::Integrations::SwitchCm
|
|
2
|
+
module Inventory
|
|
3
|
+
# Writes validated updates to the hotel's inventory rows. Callers hold the hotel's
|
|
4
|
+
# AccommodationLock, so this never logs; it returns what happened for the caller to log.
|
|
5
|
+
#
|
|
6
|
+
# Pushed rooms to sell is an absolute "rooms BookMe+ may still sell", applied as a delta:
|
|
7
|
+
#
|
|
8
|
+
# 1. Subtract rooms on BookMe+ reservations the channel manager hasn't seen (UnseenRoomNights),
|
|
9
|
+
# and the night's withheld (locked) pool, which is sold on top of public stock.
|
|
10
|
+
# 2. Never go below rooms held by guests already paying, so their holds can still convert;
|
|
11
|
+
# otherwise conversion would take Redis negative and its async DB sync would fail validation.
|
|
12
|
+
# When this clamps, we've sold more than the channel manager allowed: returned for an alert.
|
|
13
|
+
# 3. Set Redis stock in one Lua script that returns the delta, then add that delta to the row's
|
|
14
|
+
# quantity_available and max_capacity. Relative, so it commutes with BulkAdjustQuantities jobs
|
|
15
|
+
# still in flight for the row; an absolute write would be undone by them.
|
|
16
|
+
#
|
|
17
|
+
# Closing and prices are plain writes. A close without a rate is the row's `closed`, for every rate;
|
|
18
|
+
# one with a rate is that rate's RateRestriction. A price is the rate's per-night Spree::Price row:
|
|
19
|
+
# the variant's own for the base rate (which derived rate plans are priced from), else the rate
|
|
20
|
+
# plan's. A night's first availability push also opens it (PushedNights), unless the same update
|
|
21
|
+
# closes the room-night. A room-level close counts as the night's first push too, so availability
|
|
22
|
+
# pushed after it, in the same request or a later one, doesn't reopen what the channel manager closed.
|
|
23
|
+
class ApplyUpdates
|
|
24
|
+
# KEYS: stock, hold. ARGV: target, ttl. Returns [final - stock, final].
|
|
25
|
+
SET_STOCK_SCRIPT = <<~LUA.freeze
|
|
26
|
+
local stock = tonumber(redis.call('GET', KEYS[1]) or 0)
|
|
27
|
+
local held = tonumber(redis.call('GET', KEYS[2]) or 0)
|
|
28
|
+
local target = tonumber(ARGV[1])
|
|
29
|
+
local final = math.max(target, held)
|
|
30
|
+
redis.call('SET', KEYS[1], final, 'EX', tonumber(ARGV[2]))
|
|
31
|
+
return { final - stock, final }
|
|
32
|
+
LUA
|
|
33
|
+
|
|
34
|
+
def initialize(integration:)
|
|
35
|
+
@integration = integration
|
|
36
|
+
@unseen = UnseenRoomNights.new(integration)
|
|
37
|
+
@pushed_nights = PushedNights.new(integration)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def call(updates)
|
|
41
|
+
outcome = { room_nights: 0, missing_nights: 0, clamped_nights: [] }
|
|
42
|
+
|
|
43
|
+
updates.each do |update|
|
|
44
|
+
dates = update.dates
|
|
45
|
+
items = SpreeCmCommissioner::InventoryItem.where(variant_id: update.room_id, inventory_date: dates).order(:inventory_date).to_a
|
|
46
|
+
outcome[:missing_nights] += dates.size - items.size
|
|
47
|
+
next if items.empty?
|
|
48
|
+
|
|
49
|
+
write_closings(items, update) unless update.closed.nil?
|
|
50
|
+
write_prices(items, update) if update.price
|
|
51
|
+
outcome[:clamped_nights].concat(write_availability(items, update, dates)) if update.rooms_to_sell
|
|
52
|
+
mark_pushed(items, update) if update.rooms_to_sell || update.room_level_closed?
|
|
53
|
+
outcome[:room_nights] += items.size
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
outcome
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
def write_closings(items, update)
|
|
62
|
+
if update.room_level_closed?
|
|
63
|
+
write_closed(items.map(&:id), update.closed)
|
|
64
|
+
else
|
|
65
|
+
SpreeCmCommissioner::RateRestriction.set_closed!(items, rate_plan: update.rate.rate_plan, closed: update.closed)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def write_closed(item_ids, closed)
|
|
70
|
+
SpreeCmCommissioner::InventoryItem.where(id: item_ids).update_all( # rubocop:disable Rails/SkipsModelValidations
|
|
71
|
+
['closed = :closed, lock_version = lock_version + 1, updated_at = :now', { closed: closed, now: Time.current }]
|
|
72
|
+
)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def mark_pushed(items, update)
|
|
76
|
+
first_pushed = items.map(&:id) - @pushed_nights.pushed_at_by_item_id(items).keys
|
|
77
|
+
|
|
78
|
+
SpreeCmCommissioner::InventoryItem.transaction do
|
|
79
|
+
write_closed(first_pushed, false) if !update.room_level_closed? && first_pushed.any?
|
|
80
|
+
@pushed_nights.record!(items, at: Time.current)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def write_prices(items, update)
|
|
85
|
+
rate_plan_id = update.rate&.rate_plan&.id
|
|
86
|
+
items.each do |item|
|
|
87
|
+
price = Spree::Price.find_or_initialize_by(
|
|
88
|
+
variant_id: item.variant_id, rate_plan_id: rate_plan_id, inventory_item_id: item.id, currency: update.currency
|
|
89
|
+
)
|
|
90
|
+
price.update!(amount: update.price) if price.new_record? || price.amount != update.price
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Returns the nights where in-flight holds kept stock above the target.
|
|
95
|
+
def write_availability(items, update, dates)
|
|
96
|
+
# A cold key reads as 0 in the script; seed every pool from the row first (SET NX).
|
|
97
|
+
cached = SpreeCmCommissioner::RedisStock::CachedInventoryItemsBuilder.new(items).call.index_by(&:inventory_item_id)
|
|
98
|
+
unseen = @unseen.by_date(update.room_id, dates)
|
|
99
|
+
|
|
100
|
+
items.filter_map do |item|
|
|
101
|
+
target = [update.rooms_to_sell - unseen[item.inventory_date] - cached[item.id].quantity_locked, 0].max
|
|
102
|
+
delta, final = set_stock(item, target)
|
|
103
|
+
add_delta!(item, delta)
|
|
104
|
+
|
|
105
|
+
{ room_id: item.variant_id, date: item.inventory_date, target: target, held: final } if final > target
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def set_stock(item, target)
|
|
110
|
+
SpreeCmCommissioner.inventory_redis_pool.with do |redis|
|
|
111
|
+
redis.eval(SET_STOCK_SCRIPT, keys: [item.redis_key, item.redis_hold_key], argv: [target, item.redis_expired_in])
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Bumps lock_version so a writer holding a stale copy of the row fails instead of overwriting.
|
|
116
|
+
# If the row can't take the delta, Redis is put back by the same delta, which commutes with any
|
|
117
|
+
# sale that landed in between; the push then fails and the channel manager resends it.
|
|
118
|
+
def add_delta!(item, delta)
|
|
119
|
+
# A relative SQL update is the point here (see the class comment), so no model save.
|
|
120
|
+
SpreeCmCommissioner::InventoryItem.where(id: item.id).update_all( # rubocop:disable Rails/SkipsModelValidations
|
|
121
|
+
[
|
|
122
|
+
'quantity_available = quantity_available + :delta, max_capacity = max_capacity + :delta, ' \
|
|
123
|
+
'lock_version = lock_version + 1, updated_at = :now',
|
|
124
|
+
{ delta: delta, now: Time.current }
|
|
125
|
+
]
|
|
126
|
+
)
|
|
127
|
+
rescue StandardError
|
|
128
|
+
SpreeCmCommissioner.inventory_redis_pool.with { |redis| redis.incrby(item.redis_key, -delta) } unless delta.zero?
|
|
129
|
+
raise
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
data/app/services/spree_cm_commissioner/integrations/switch_cm/inventory/availability_snapshot.rb
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
class SpreeCmCommissioner::Integrations::SwitchCm
|
|
2
|
+
module Inventory
|
|
3
|
+
# What we currently hold per room, rate and night, for the channel manager to compare with what it
|
|
4
|
+
# pushed (B.XML roomrateavailability). rooms_to_sell is the room's, shared by its rates: what
|
|
5
|
+
# BookMe+ can still sell (Redis stock), which is below the pushed number by whatever sold since;
|
|
6
|
+
# booked and cancelled count BookMe+ reservations touching the night. A rate is closed when the
|
|
7
|
+
# room-night is or the rate is (RateClosings), and its price is what a guest would pay that night.
|
|
8
|
+
class AvailabilitySnapshot
|
|
9
|
+
Row = Struct.new(
|
|
10
|
+
:room_id, :rate_id, :date, :rooms_to_sell, :booked, :cancelled, :closed,
|
|
11
|
+
:price, :currency, :pushed_at,
|
|
12
|
+
keyword_init: true
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
def initialize(integration:, dates:, room_id: nil)
|
|
16
|
+
@integration = integration
|
|
17
|
+
@dates = dates
|
|
18
|
+
@room_id = room_id
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def rows
|
|
22
|
+
items = SpreeCmCommissioner::InventoryItem.where(variant_id: rooms.map(&:id), inventory_date: @dates)
|
|
23
|
+
.order(:variant_id, :inventory_date)
|
|
24
|
+
.to_a
|
|
25
|
+
cached = SpreeCmCommissioner::RedisStock::CachedInventoryItemsBuilder.new(items).call.index_by(&:inventory_item_id)
|
|
26
|
+
counts = reservation_counts
|
|
27
|
+
pushed_at = PushedNights.new(@integration).pushed_at_by_item_id(items)
|
|
28
|
+
rooms_by_id = rooms.index_by(&:id)
|
|
29
|
+
|
|
30
|
+
items.flat_map do |item|
|
|
31
|
+
room = rooms_by_id.fetch(item.variant_id)
|
|
32
|
+
room.rates.map { |rate| build_row(item, rate, cached[item.id], counts, pushed_at[item.id]) }
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
def rooms
|
|
39
|
+
@rooms ||= begin
|
|
40
|
+
all = RoomCatalog.new(@integration.vendor).rooms
|
|
41
|
+
@room_id.present? ? all.select { |room| room.id.to_s == @room_id.to_s } : all
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def build_row(item, rate, cached_item, counts, pushed_at)
|
|
46
|
+
currency = currency_for(item.variant_id)
|
|
47
|
+
|
|
48
|
+
Row.new(
|
|
49
|
+
room_id: item.variant_id,
|
|
50
|
+
rate_id: rate.id,
|
|
51
|
+
date: item.inventory_date,
|
|
52
|
+
rooms_to_sell: cached_item.quantity_available,
|
|
53
|
+
booked: counts[[item.variant_id, item.inventory_date, :confirmed]],
|
|
54
|
+
cancelled: counts[[item.variant_id, item.inventory_date, :cancelled]],
|
|
55
|
+
closed: item.closed || rate_closings.closed_on?(item.variant_id, rate.rate_plan, item.inventory_date),
|
|
56
|
+
price: currency && price(rate, item.inventory_date, currency),
|
|
57
|
+
currency: currency,
|
|
58
|
+
pushed_at: pushed_at
|
|
59
|
+
)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# The base rate's own price that night (its date row, else its base row); a rate plan's by its own rule,
|
|
63
|
+
# where a derived one adjusts the base rate's.
|
|
64
|
+
def price(rate, date, currency)
|
|
65
|
+
index = @dates.index(date)
|
|
66
|
+
stay_prices = stay_prices_in(currency)
|
|
67
|
+
return stay_prices.for_variant(rate.room_variant).amounts[index] if rate.base?
|
|
68
|
+
|
|
69
|
+
rate_plan_amounts(rate.rate_plan, currency)[index]
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def rate_plan_amounts(rate_plan, currency)
|
|
73
|
+
(@rate_plan_amounts ||= {})[[rate_plan.id, currency]] ||=
|
|
74
|
+
rate_plan.price_amounts(dates: @dates, currency: currency, stay_prices: stay_prices_in(currency))
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# One batch per currency for every room and rate plan in the snapshot.
|
|
78
|
+
def stay_prices_in(currency)
|
|
79
|
+
(@stay_prices ||= {})[currency] ||= SpreeCmCommissioner::StayPrices.new(
|
|
80
|
+
variants: rooms.map(&:variant),
|
|
81
|
+
rate_plans: rooms.flat_map { |room| room.rates.filter_map(&:rate_plan) },
|
|
82
|
+
dates: @dates,
|
|
83
|
+
currency: currency
|
|
84
|
+
)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def rate_closings
|
|
88
|
+
@rate_closings ||= SpreeCmCommissioner::RateClosings.new(variant_ids: rooms.map(&:id), dates: @dates)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# The store's currency when the room has a base price in it, else the room's first; nil for an unpriced room.
|
|
92
|
+
def currency_for(variant_id)
|
|
93
|
+
@base_currencies ||= Spree::Price.where(variant_id: rooms.map(&:id), rate_plan_id: nil, inventory_item_id: nil)
|
|
94
|
+
.pluck(:variant_id, :currency)
|
|
95
|
+
.group_by(&:first)
|
|
96
|
+
.transform_values { |rows| rows.map { |_, currency| currency.upcase }.sort }
|
|
97
|
+
currencies = @base_currencies.fetch(variant_id, [])
|
|
98
|
+
store_currency = Spree::Store.default.default_currency.upcase
|
|
99
|
+
currencies.include?(store_currency) ? store_currency : currencies.first
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# { [room_id, date, :confirmed | :cancelled] => rooms }
|
|
103
|
+
def reservation_counts
|
|
104
|
+
counts = Hash.new(0)
|
|
105
|
+
|
|
106
|
+
line_items.each do |line_item|
|
|
107
|
+
status = Reservations::Reservation::CANCELLED_STATES.include?(line_item.order_state) ? :cancelled : :confirmed
|
|
108
|
+
nights = line_item.from_date.to_date..line_item.to_date.to_date
|
|
109
|
+
@dates.each { |date| counts[[line_item.variant_id, date, status]] += line_item.quantity if nights.cover?(date) }
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
counts
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def line_items
|
|
116
|
+
Spree::LineItem.joins(:order)
|
|
117
|
+
.where(variant_id: rooms.map(&:id))
|
|
118
|
+
.where(spree_orders: { state: Reservations::Reservation::CONFIRMED_STATES + Reservations::Reservation::CANCELLED_STATES })
|
|
119
|
+
.where('spree_line_items.from_date <= ? AND spree_line_items.to_date >= ?', @dates.max.end_of_day, @dates.min.beginning_of_day)
|
|
120
|
+
.select(
|
|
121
|
+
'spree_line_items.variant_id', 'spree_line_items.quantity', 'spree_line_items.from_date',
|
|
122
|
+
'spree_line_items.to_date', 'spree_orders.state AS order_state'
|
|
123
|
+
)
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
class SpreeCmCommissioner::Integrations::SwitchCm
|
|
2
|
+
module Inventory
|
|
3
|
+
# One availability/price/restriction instruction for a room and rate over an inclusive range of
|
|
4
|
+
# nights, whichever XML family it arrived in, so validation and apply never see XML.
|
|
5
|
+
#
|
|
6
|
+
# nil means "not sent, leave as is", which is all that separates a change-only push from a full
|
|
7
|
+
# one. `weekdays` (0 = Sunday) limits the range to those days; nil means every day.
|
|
8
|
+
#
|
|
9
|
+
# `rate` is the RoomCatalog::Rate that `rate_id` names, set by ValidateUpdates; nil when no rate was
|
|
10
|
+
# sent. As at Booking.com, rooms to sell is always the room's, a close without a rate closes the
|
|
11
|
+
# room-night for every rate, and a close with one closes only that rate.
|
|
12
|
+
InventoryUpdate = Struct.new(
|
|
13
|
+
:index, :room_id, :rate_id, :date_from, :date_to, :weekdays,
|
|
14
|
+
:rooms_to_sell, :closed, :price, :currency, :unsupported, :rate,
|
|
15
|
+
keyword_init: true
|
|
16
|
+
) do
|
|
17
|
+
def dates
|
|
18
|
+
(date_from..date_to).select { |date| weekdays.nil? || weekdays.include?(date.wday) }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def nights
|
|
22
|
+
dates.size
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Closes or opens the room-night itself, rather than one rate.
|
|
26
|
+
def room_level_closed?
|
|
27
|
+
!closed.nil? && rate.nil?
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def changes_anything?
|
|
31
|
+
[rooms_to_sell, closed, price].any? { |value| !value.nil? }
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
class SpreeCmCommissioner::Integrations::SwitchCm
|
|
2
|
+
module Inventory
|
|
3
|
+
# Which of a hotel's room-nights switch.cm has pushed availability for, kept as IntegrationMappings
|
|
4
|
+
# (internal = the night's InventoryItem, date = its night, last_synced_at = the latest push) so
|
|
5
|
+
# inventory items carry nothing integration-specific.
|
|
6
|
+
#
|
|
7
|
+
# While switch.cm owns the rooms (Integration#inventory_external_wins?), a night it hasn't pushed
|
|
8
|
+
# stays closed: its count is only our seed from the physical room count. New nights are created
|
|
9
|
+
# closed (InventoryItem), existing ones are closed when the connection takes over and reopened
|
|
10
|
+
# when it lets go, and ApplyUpdates opens each night on its first push. A room-level close counts
|
|
11
|
+
# as a push, so a night switch.cm closed isn't reopened by either.
|
|
12
|
+
class PushedNights
|
|
13
|
+
INTERNAL_TYPE = 'SpreeCmCommissioner::InventoryItem'.freeze
|
|
14
|
+
|
|
15
|
+
def initialize(integration)
|
|
16
|
+
@integration = integration
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def mappings
|
|
20
|
+
SpreeCmCommissioner::IntegrationMapping.where(integration_id: @integration.id, internal_type: INTERNAL_TYPE)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# { inventory_item_id => last push time } for the pushed ones among items.
|
|
24
|
+
def pushed_at_by_item_id(items)
|
|
25
|
+
mappings.where(internal_id: items.map(&:id)).pluck(:internal_id, :last_synced_at).to_h
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def record!(items, at:)
|
|
29
|
+
return if items.empty?
|
|
30
|
+
|
|
31
|
+
rows = items.map do |item|
|
|
32
|
+
{
|
|
33
|
+
integration_id: @integration.id, internal_type: INTERNAL_TYPE, internal_id: item.id,
|
|
34
|
+
external_id: item.variant_id.to_s, date: item.inventory_date, last_synced_at: at
|
|
35
|
+
}
|
|
36
|
+
end
|
|
37
|
+
SpreeCmCommissioner::IntegrationMapping.upsert_all( # rubocop:disable Rails/SkipsModelValidations
|
|
38
|
+
rows, unique_by: :idx_external_internal_mapping, update_only: [:last_synced_at]
|
|
39
|
+
)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def close_unpushed!
|
|
43
|
+
unpushed_nights.where(closed: false).update_all(closed_update(true)) # rubocop:disable Rails/SkipsModelValidations
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def reopen_unpushed!
|
|
47
|
+
unpushed_nights.where(closed: true).update_all(closed_update(false)) # rubocop:disable Rails/SkipsModelValidations
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Puts unpushed nights back in the state ownership calls for; returns how many changed.
|
|
51
|
+
def reconcile_unpushed!
|
|
52
|
+
@integration.owns_inventory? ? close_unpushed! : reopen_unpushed!
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Returns how many were deleted.
|
|
56
|
+
def prune_past!
|
|
57
|
+
mappings.where('date < ?', Time.zone.today).delete_all
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Upcoming room-nights pushed, out of all we hold, for the admin integration page.
|
|
61
|
+
def progress
|
|
62
|
+
{ pushed: mappings.where(internal_id: upcoming_nights.select(:id)).count, total: upcoming_nights.count }
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
def upcoming_nights
|
|
68
|
+
room_ids = RoomCatalog.new(@integration.vendor).rooms.map(&:id)
|
|
69
|
+
SpreeCmCommissioner::InventoryItem.where(variant_id: room_ids).where('inventory_date >= ?', Time.zone.today)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def unpushed_nights
|
|
73
|
+
upcoming_nights.where.not(id: mappings.select(:internal_id))
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Bumps lock_version so a writer holding a stale copy of the row fails instead of overwriting.
|
|
77
|
+
def closed_update(closed)
|
|
78
|
+
['closed = ?, lock_version = lock_version + 1, updated_at = ?', closed, Time.current]
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
data/app/services/spree_cm_commissioner/integrations/switch_cm/inventory/unseen_room_nights.rb
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
class SpreeCmCommissioner::Integrations::SwitchCm
|
|
2
|
+
module Inventory
|
|
3
|
+
# Rooms per night on BookMe+ reservations the channel manager hasn't seen yet. Its pushed
|
|
4
|
+
# "rooms to sell" can't include them, so a push subtracts them; without this, every BookMe+ sale
|
|
5
|
+
# between two pulls would be sold again by the next push.
|
|
6
|
+
#
|
|
7
|
+
# Only confirmed reservations completed since the hotel connected count: older ones came in
|
|
8
|
+
# through the reservation summary and are already in the channel manager's numbers, and an unseen
|
|
9
|
+
# cancellation only makes its number too low, which is safe. An unseen modification counts its
|
|
10
|
+
# new nights; the channel manager still counts the old ones, which stay short until it sees the
|
|
11
|
+
# change, likewise safe.
|
|
12
|
+
class UnseenRoomNights
|
|
13
|
+
def initialize(integration)
|
|
14
|
+
@integration = integration
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# { date => rooms } over `dates` for one room.
|
|
18
|
+
def by_date(room_id, dates)
|
|
19
|
+
counts = Hash.new(0)
|
|
20
|
+
|
|
21
|
+
line_items(room_id, dates).each do |line_item|
|
|
22
|
+
nights = line_item.from_date.to_date..line_item.to_date.to_date
|
|
23
|
+
dates.each { |date| counts[date] += line_item.quantity if nights.cover?(date) }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
counts
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def line_items(room_id, dates)
|
|
32
|
+
Spree::LineItem.joins(:order)
|
|
33
|
+
.joins(Reservations::Feed.seen_join_sql(@integration))
|
|
34
|
+
.where(variant_id: room_id)
|
|
35
|
+
.where(spree_orders: { state: Reservations::Reservation::CONFIRMED_STATES })
|
|
36
|
+
.where(unseen_sql)
|
|
37
|
+
.where('spree_line_items.from_date <= ? AND spree_line_items.to_date >= ?', dates.max.end_of_day, dates.min.beginning_of_day)
|
|
38
|
+
.select(:id, :quantity, :from_date, :to_date)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def unseen_sql
|
|
42
|
+
new_sql = ActiveRecord::Base.sanitize_sql_array(
|
|
43
|
+
["spree_orders.completed_at >= ? AND #{Reservations::Feed::SEEN_STATUS_SQL} <> 'confirmed'", @integration.connected_at]
|
|
44
|
+
)
|
|
45
|
+
"(#{new_sql}) OR (#{Reservations::Feed.unseen_modification_sql(@integration)})"
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
class SpreeCmCommissioner::Integrations::SwitchCm
|
|
2
|
+
module Inventory
|
|
3
|
+
# Checks each pushed update against the hotel's rooms, before anything is applied. Everything
|
|
4
|
+
# the channel manager could fix is caught here and answered in the same response:
|
|
5
|
+
#
|
|
6
|
+
# - rejected, with an error: unknown room, a rate that isn't the room's, a missing, reversed or
|
|
7
|
+
# past date range, a negative or non-numeric value, min stay above max stay, a price of 0 or
|
|
8
|
+
# less, a price for a child rate (it follows the base rate's), or a currency the rate isn't
|
|
9
|
+
# priced in;
|
|
10
|
+
# - adjusted, with a warning: nights before today and past our inventory horizon are dropped
|
|
11
|
+
# (Booking.com accepts a day back and five years ahead), BookingLimit 255 ("unlimited") is
|
|
12
|
+
# capped at the room's physical count, and restrictions we don't store are ignored.
|
|
13
|
+
#
|
|
14
|
+
# Returns { accepted: [InventoryUpdate], errors: [{ code:, message:, index: }], warnings: [...] }.
|
|
15
|
+
class ValidateUpdates
|
|
16
|
+
prepend ::Spree::ServiceModule::Base
|
|
17
|
+
|
|
18
|
+
UNLIMITED_ROOMS = 255
|
|
19
|
+
MAX_PRICE = 50_000
|
|
20
|
+
|
|
21
|
+
def call(integration:, updates:)
|
|
22
|
+
@catalog = RoomCatalog.new(integration.vendor)
|
|
23
|
+
@errors = []
|
|
24
|
+
@warnings = []
|
|
25
|
+
|
|
26
|
+
accepted = updates.filter_map { |update| validate(update) }
|
|
27
|
+
|
|
28
|
+
success(accepted: accepted, errors: @errors, warnings: @warnings)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def validate(update)
|
|
34
|
+
room = @catalog.find(update.room_id)
|
|
35
|
+
error = room_error(update, room) || date_error(update) || value_error(update, room)
|
|
36
|
+
return reject(update, *error) if error
|
|
37
|
+
|
|
38
|
+
clip_dates(update)
|
|
39
|
+
return nil if update.date_from > update.date_to
|
|
40
|
+
|
|
41
|
+
normalize(update, room)
|
|
42
|
+
Array(update.unsupported).each { |restriction| add_warning(update, 'UNSUPPORTED_RESTRICTION', "Not supported, ignored: #{restriction}") }
|
|
43
|
+
return update if update.changes_anything?
|
|
44
|
+
|
|
45
|
+
add_warning(update, 'NOTHING_TO_UPDATE', 'Update carries no value we store')
|
|
46
|
+
nil
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def normalize(update, room)
|
|
50
|
+
update.room_id = room.id
|
|
51
|
+
update.rate = room.find_rate(update.rate_id) if update.rate_id.present?
|
|
52
|
+
update.rooms_to_sell = capped_rooms_to_sell(update, room)
|
|
53
|
+
update.currency = update.currency.presence&.upcase || default_currency(price_rate(update, room)) if update.price
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def room_error(update, room)
|
|
57
|
+
return ['INVALID_ROOM', "Room '#{update.room_id}' is not a room of this hotel"] if room.nil?
|
|
58
|
+
return nil if update.rate_id.blank? || room.find_rate(update.rate_id)
|
|
59
|
+
|
|
60
|
+
['RATE_NOT_ACTIVE_FOR_ROOM', "Rate '#{update.rate_id}' is not active for room '#{update.room_id}'"]
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def date_error(update)
|
|
64
|
+
if update.date_from.nil? || update.date_to.nil? || update.date_from > update.date_to
|
|
65
|
+
return ['NOT_A_VALID_DATE', 'Dates should be in YYYY-MM-DD format, with the start not after the end']
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
['NOT_A_VALID_DATE', "Dates should not be before '#{today.iso8601}'"] if update.date_to < today
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def value_error(update, room)
|
|
72
|
+
return ['INVALID_VALUE', 'Rooms to sell must be 0 or more'] if update.rooms_to_sell&.negative?
|
|
73
|
+
return nil if update.price.nil?
|
|
74
|
+
return ['INVALID_VALUE', 'Price must be above 0'] unless update.price.positive?
|
|
75
|
+
return ['INVALID_VALUE', "Price must not exceed #{MAX_PRICE}"] if update.price > MAX_PRICE
|
|
76
|
+
|
|
77
|
+
rate = price_rate(update, room)
|
|
78
|
+
return ['RATE_FOLLOWS_PARENT', "Rate '#{rate.id}' follows the price of rate '#{rate.parent_id}'; price that rate"] if rate.child?
|
|
79
|
+
|
|
80
|
+
return nil if update.currency.blank? || currencies(rate).include?(update.currency.upcase)
|
|
81
|
+
|
|
82
|
+
['INVALID_CURRENCY', "Rate '#{rate.id}' of room '#{room.id}' is not sold in #{update.currency}"]
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# A price without a rate is the base rate's, as before rate plans.
|
|
86
|
+
def price_rate(update, room)
|
|
87
|
+
room.find_rate(update.rate_id.presence || room.id)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Inventory rows exist from today to the accommodation horizon only.
|
|
91
|
+
def clip_dates(update)
|
|
92
|
+
if update.date_from < today
|
|
93
|
+
add_warning(update, 'DATES_BEFORE_TODAY_IGNORED', "Nights before #{today.iso8601} were ignored")
|
|
94
|
+
update.date_from = today
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
return if update.date_to <= horizon
|
|
98
|
+
|
|
99
|
+
add_warning(update, 'DATES_PAST_HORIZON_IGNORED', "Nights after #{horizon.iso8601} are not open for sale yet and were ignored")
|
|
100
|
+
update.date_to = horizon
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def capped_rooms_to_sell(update, room)
|
|
104
|
+
return update.rooms_to_sell unless update.rooms_to_sell.to_i >= UNLIMITED_ROOMS
|
|
105
|
+
|
|
106
|
+
physical = physical_count(room)
|
|
107
|
+
add_warning(update, 'ROOMS_TO_SELL_CAPPED', "Unlimited rooms to sell capped at the room's #{physical} rooms")
|
|
108
|
+
physical
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def reject(update, code, message)
|
|
112
|
+
@errors << { code: code, message: message, index: update.index }
|
|
113
|
+
nil
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def add_warning(update, code, message)
|
|
117
|
+
@warnings << { code: code, message: message, index: update.index }
|
|
118
|
+
true
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def today
|
|
122
|
+
@today ||= Time.zone.today
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def horizon
|
|
126
|
+
@horizon ||= today + SpreeCmCommissioner::ProductType::PRE_INVENTORY_DAYS.fetch('accommodation')
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def variants
|
|
130
|
+
@variants ||= Spree::Variant.where(id: @catalog.rooms.map(&:id)).includes(:prices, :stock_items).index_by(&:id)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# The currencies the rate has a base price in (per-night prices hang off inventory rows): the variant's
|
|
134
|
+
# own for the base rate; an independent rate plan's own, plus the variant's, which it may be priced in
|
|
135
|
+
# night by night without a base row.
|
|
136
|
+
def currencies(rate)
|
|
137
|
+
base_prices = variants[rate.room_id].prices.select { |price| price.inventory_item_id.nil? }
|
|
138
|
+
own = base_prices.select { |price| price.rate_plan_id.nil? }
|
|
139
|
+
own += base_prices.select { |price| price.rate_plan_id == rate.rate_plan.id } unless rate.base?
|
|
140
|
+
own.map { |price| price.currency.upcase }.uniq
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def default_currency(rate)
|
|
144
|
+
store_currency = Spree::Store.default.default_currency
|
|
145
|
+
currencies(rate).include?(store_currency) ? store_currency : currencies(rate).first || store_currency
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def physical_count(room)
|
|
149
|
+
variants[room.id].stock_items.sum(&:count_on_hand).clamp(0, UNLIMITED_ROOMS - 1)
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|