Class: Delivery::SetProperShippingCost
- Inherits:
-
BaseService
- Object
- BaseService
- Delivery::SetProperShippingCost
- Defined in:
- app/services/delivery/set_proper_shipping_cost.rb
Overview
before_save driver that picks the right ShippingCost for a delivery and
syncs it onto the delivery (shipping_option, selected_shipping_cost,
shipping_cost) and the shipping LineItem. Honours user-intentional
overrides, EDI shipping options, delivery deadlines, LTL switches, and falls
back through preferred service / carrier / cheapest. The bulk of
carrier-selection policy lives here, as a pipeline of stages:
- skip guards (nothing relevant changed / detection disabled / no costs)
- reset an unintentional override selection on address/LTL change
- build the auto-select pool (no beta, no item-less options)
- recover the explicit selection (live/orphaned beta + freight picks)
- default the shipping_option (selection's option, else preferred)
- LTL: preselect the absolute cheapest freight before heuristics
- heuristic ladder: option → carrier+service → service → carrier → override
- LTL flag flip: re-cost the override placeholder
- LTL guard: cheapest non-beta freight from the www hash
- delivery-deadline policy (incl. AMZBS cheapest-on-time optimization)
- resource fallbacks: cart cheapest / dropship supplier carrier / customer default
- modality fallbacks: economy override / cheapest LTL / first non-postal
- store the chosen option back (never clobbering an intentional override)
- apply via Delivery#apply_selected_shipping_cost!
Extracted from Delivery#set_proper_shipping_cost (god-object decomposition);
the model keeps a thin delegator wired into its before_save chain.
Instance Attribute Summary
Attributes inherited from BaseService
Instance Method Summary collapse
-
#initialize(delivery) ⇒ SetProperShippingCost
constructor
A new instance of SetProperShippingCost.
-
#process ⇒ Boolean?
True when applied or skipped; nil when no ShippingCost could be chosen (mirrors the original method's returns).
Methods inherited from BaseService
#log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger
Constructor Details
#initialize(delivery) ⇒ SetProperShippingCost
Returns a new instance of SetProperShippingCost.
32 33 34 35 |
# File 'app/services/delivery/set_proper_shipping_cost.rb', line 32 def initialize(delivery) super() @delivery = delivery end |
Instance Method Details
#process ⇒ Boolean?
Returns true when applied or skipped; nil when no
ShippingCost could be chosen (mirrors the original method's returns).
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/services/delivery/set_proper_shipping_cost.rb', line 39 def process shipping_line = delivery.line_items.select(&:is_shipping?).pop Rails.logger.debug { "set_proper_shipping_cost, shipping_line: #{shipping_line ? shipping_line.id : 'none'}" } return true if skip_selection?(shipping_line) && delivery.force_shipping_cost_update != true reset_unintentional_override! @sorted_costs = build_sorted_costs entry = recover_explicit_selection default_shipping_option! Rails.logger.debug { "set_proper_shipping_cost, sorted_costs: #{sorted_costs.map { |sc| [sc.shipping_option.name, sc.id] }.inspect}" } entry = preselect_cheapest_freight(entry) entry = match_by_option_heuristics(entry) recost_override_for_ltl_flip!(entry) entry ||= ltl_guard_candidate if delivery.ships_ltl_freight? entry = apply_delivery_deadline_policy(entry) entry = apply_resource_fallbacks(entry) entry = apply_modality_fallbacks(entry) store_back_shipping_option!(entry) return unless entry delivery.apply_selected_shipping_cost!(entry) true end |