Class: Delivery::SetProperShippingCost

Inherits:
BaseService show all
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:

  1. skip guards (nothing relevant changed / detection disabled / no costs)
  2. reset an unintentional override selection on address/LTL change
  3. build the auto-select pool (no beta, no item-less options)
  4. recover the explicit selection (live/orphaned beta + freight picks)
  5. default the shipping_option (selection's option, else preferred)
  6. LTL: preselect the absolute cheapest freight before heuristics
  7. heuristic ladder: option → carrier+service → service → carrier → override
  8. LTL flag flip: re-cost the override placeholder
  9. LTL guard: cheapest non-beta freight from the www hash
  10. delivery-deadline policy (incl. AMZBS cheapest-on-time optimization)
  11. resource fallbacks: cart cheapest / dropship supplier carrier / customer default
  12. modality fallbacks: economy override / cheapest LTL / first non-postal
  13. store the chosen option back (never clobbering an intentional override)
  14. 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.

Examples:

Delivery::SetProperShippingCost.new(delivery).process

Instance Attribute Summary

Attributes inherited from BaseService

#options

Instance Method Summary collapse

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

#processBoolean?

Returns true when applied or skipped; nil when no
ShippingCost could be chosen (mirrors the original method's returns).

Returns:

  • (Boolean, nil)

    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