Class: Spree::FreightSummary

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, ActiveModel::Model
Defined in:
app/models/spree/freight_summary.rb,
app/models/spree/freight_summary/line.rb

Overview

What a cart or a package looks like to a freight forwarder: how many units, how many cartons those fill, how many pallets those stack onto, the cubic meters they occupy and what the whole thing weighs.

These are the numbers a wholesale shipment is quoted and tiered on, and they are not derivable from the retail shipping data — a case of 48 bottles is one carton, not 48 parcels. A variant declares its carton (docs/plans/6.0-b2b-wholesale-shipping.md) and the rollup follows the chain Unit -> Carton -> Pallet -> CBM / weight.

Lines whose variant declares no carton fall back to unit volume and weight and mark the summary #complete? false, so the numbers stay useful while saying plainly that some of the catalog is unmeasured.

A purchase reads the copy the freight provider froze onto its selected delivery rate, and never re-derives it from the live catalog. No freight rate, no summary.

Defined Under Namespace

Classes: Line

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(contents) ⇒ Spree::FreightSummary

Rolls up stock package contents.

Parameters:

Returns:



30
31
32
33
34
35
# File 'app/models/spree/freight_summary.rb', line 30

def build(contents)
  from_pairs(Array(contents).filter_map do |item|
    variant = item.variant
    [variant, item.quantity] if variant
  end)
end

.from_metadata(payload) ⇒ Spree::FreightSummary?

Rebuilds a summary from its serialized form — the frozen copy carried in a delivery rate's metadata.

Parameters:

  • payload (Hash, nil)

Returns:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/spree/freight_summary.rb', line 61

def (payload)
  return if payload.blank?

  payload = payload.with_indifferent_access
  # Only keys this version of Line declares. A frozen snapshot outlives
  # the code that wrote it by design, so an attribute renamed or dropped
  # later must read as absent rather than raise on every historical
  # order the moment it is serialized.
  known = Spree::FreightSummary::Line.attribute_names.map(&:to_sym)
  lines = Array(payload[:lines]).map do |line|
    Spree::FreightSummary::Line.new(line.symbolize_keys.slice(*known))
  end

  new(lines: lines)
end

.merge(summaries) ⇒ Spree::FreightSummary

Combines the summaries several fulfillments froze into the one load the forwarder sees.

Lines for the same variant are added together rather than listed twice, because carton and pallet counts were each rounded up when the line was built: three cartons in one consignment and three in another is six cartons on one pallet, not two pallets.

Parameters:

Returns:



48
49
50
51
52
53
54
# File 'app/models/spree/freight_summary.rb', line 48

def merge(summaries)
  lines = Array(summaries).flat_map(&:lines).
          group_by(&:variant_id).
          map { |_variant_id, grouped| grouped.length == 1 ? grouped.first : Line.combine(grouped) }

  new(lines: lines)
end

Instance Method Details

#as_json ⇒ Hash

The serialized form frozen onto a delivery rate at completion.

Returns:

  • (Hash)


156
157
158
159
160
161
162
163
164
165
166
# File 'app/models/spree/freight_summary.rb', line 156

def as_json(*)
  {
    'total_units' => total_units,
    'total_cartons' => total_cartons,
    'total_pallets' => total_pallets,
    'total_volume' => decimal_string(total_volume),
    'total_weight' => decimal_string(total_weight),
    'complete' => complete?,
    'lines' => lines.map(&:as_json)
  }
end

#complete? ⇒ Boolean

True when every line was measured from its carton. False means some of the figures above came from unit dimensions instead, which under-reports the space packaging takes.

Returns:

  • (Boolean)


138
139
140
# File 'app/models/spree/freight_summary.rb', line 138

def complete?
  lines.any? && lines.all?(&:complete?)
end

#empty? ⇒ Boolean

True when there is nothing here worth reporting as freight — no lines, or lines that measured to nothing because the catalog records no dimensions. A retail cart of unmeasured goods has no freight summary rather than one full of zeros, which would read as a shipment that takes up no space.

Returns:

  • (Boolean)


149
150
151
# File 'app/models/spree/freight_summary.rb', line 149

def empty?
  lines.empty? || (total_volume.zero? && total_weight.zero? && total_cartons.zero?)
end

#total_cartons ⇒ Integer

Returns:

  • (Integer)


105
106
107
# File 'app/models/spree/freight_summary.rb', line 105

def total_cartons
  @total_cartons ||= sum_of(:cartons).to_i
end

#total_pallets ⇒ Integer?

Pallets are only meaningful when every carton-bearing line says how it stacks; one silent line would understate the load.

Returns:

  • (Integer, nil)


113
114
115
116
117
118
119
120
121
# File 'app/models/spree/freight_summary.rb', line 113

def total_pallets
  return @total_pallets if defined?(@total_pallets)

  carton_lines = lines.select(&:cartons?)
  @total_pallets =
    unless carton_lines.empty? || carton_lines.any? { |line| line.pallets.nil? }
      carton_lines.sum(&:pallets)
    end
end

#total_units ⇒ Integer

Returns:

  • (Integer)


100
101
102
# File 'app/models/spree/freight_summary.rb', line 100

def total_units
  @total_units ||= sum_of(:units).to_i
end

#total_volume ⇒ BigDecimal

Returns cubic meters.

Returns:

  • (BigDecimal) —

    cubic meters



124
125
126
# File 'app/models/spree/freight_summary.rb', line 124

def total_volume
  @total_volume ||= sum_of(:volume)
end

#total_weight ⇒ BigDecimal

Returns kilograms.

Returns:

  • (BigDecimal) —

    kilograms



129
130
131
# File 'app/models/spree/freight_summary.rb', line 129

def total_weight
  @total_weight ||= sum_of(:weight)
end