Class: Delivery::GroupWwwShippingCosts

Inherits:
BaseService show all
Defined in:
app/services/delivery/group_www_shipping_costs.rb

Overview

Groups a delivery's ShippingCost rows into the service-level buckets the
WWW shipping picker renders: :economy (the override placeholder),
:ground, :faster (expedited + rush), and :freight (cheapest only).
PO-box destinations are restricted to postal carriers; otherwise the store
country's WWW carrier list applies (its order breaks cost ties when
sort_by_price is off).

Extracted from Delivery#sorted_shipping_costs_www_hash (god-object
decomposition); the model keeps a thin delegator.

Examples:

Delivery::GroupWwwShippingCosts.new(delivery).process
Delivery::GroupWwwShippingCosts.new(delivery, sort_by_price: false).process

Constant Summary collapse

GROUND =

Ground = slower commitments; Speedee is always ground regardless of its
committed days. Uses shipping_option.days_commitment for categorization,
NOT sc.days_commitment, which includes dynamic carrier estimates that vary
by shipment.

->(sc) { sc.shipping_option.days_commitment >= 3.5 || sc.shipping_option.carrier == 'SpeedeeDelivery' }
EXPEDITED =
->(sc) { sc.shipping_option.days_commitment >= 2 && sc.shipping_option.days_commitment < 3.5 && sc.shipping_option.carrier != 'SpeedeeDelivery' }
RUSH =
->(sc) { sc.shipping_option.days_commitment < 2 && sc.shipping_option.carrier != 'SpeedeeDelivery' }

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, sort_by_price: true) ⇒ GroupWwwShippingCosts

Returns a new instance of GroupWwwShippingCosts.



26
27
28
29
30
# File 'app/services/delivery/group_www_shipping_costs.rb', line 26

def initialize(delivery, sort_by_price: true)
  super()
  @delivery = delivery
  @sort_by_price = sort_by_price
end

Instance Method Details

#processHash{Symbol => Array<ShippingCost>}

Returns:



33
34
35
36
37
38
39
40
41
42
# File 'app/services/delivery/group_www_shipping_costs.rb', line 33

def process
  res = delivery.shipping_costs.skip_3rd_party.includes(:shipping_option)
  freight = res.select { |sp| sp.shipping_option.is_freight && !sp.hide? }.min_by { |a| a.cost.to_f.round(2) }
  {
    economy: delivery.shipping_costs.override_only.uniq,
    ground: bucket(res, GROUND).compact.uniq,
    faster: (bucket(res, EXPEDITED) + bucket(res, RUSH)).compact.uniq,
    freight: [freight].compact.uniq
  }
end