Class: Spree::FreightSummary::Line

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

Overview

One variant's contribution to a freight summary.

Built once from the live catalog and thereafter carried as plain data, so an order's frozen summary keeps reporting the cartons it actually shipped in even after the variant is repacked.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(variant, quantity) ⇒ Spree::FreightSummary::Line

Parameters:

Returns:



38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/spree/freight_summary/line.rb', line 38

def self.build(variant, quantity)
  quantity = quantity.to_i
  cartons = variant.cartons_for(quantity)
  carton_volume = variant.carton_volume

  if cartons && carton_volume
    from_cartons(variant, quantity, cartons, carton_volume)
  else
    from_units(variant, quantity)
  end
end

.combine(parts) ⇒ Spree::FreightSummary::Line

Adds together the lines the same variant produced in several consignments.

Cartons and pallets are re-derived from the combined units rather than summed, because each part was rounded up when it was built: a variant packed twelve to a carton, split three units and nine, is one carton in total and not two. The divisors ride on the line itself, so a frozen snapshot can be recombined years later without asking today's catalog — which is exactly what freezing it was meant to avoid.

Parameters:

Returns:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/models/spree/freight_summary/line.rb', line 105

def self.combine(parts)
  first = parts.first
  units = parts.sum(&:units)
  recombined = divide(units, first.units_per_carton)
  # `presence` is no help on an Integer — zero is present — so the
  # "no part counted cartons" case has to be spelled out, or a line
  # nobody packed reports zero cartons where every unmerged line
  # reports none at all.
  counted = parts.filter_map(&:cartons)
  cartons = recombined || (counted.sum if counted.any?)
  pallets = cartons && divide(cartons, first.cartons_per_pallet)

  new(
    variant_id: first.variant_id,
    sku: first.sku,
    name: first.name,
    units: units,
    units_per_carton: first.units_per_carton,
    cartons_per_pallet: first.cartons_per_pallet,
    cartons: cartons,
    pallets: pallets,
    # Volume follows the recombined carton count for the same reason
    # the count itself does: adding two part-full cartons' volumes
    # reports space the combined shipment does not take.
    volume: combined_volume(parts, recombined),
    weight: combined_weight(parts, cartons),
    weight_per_carton: parts.filter_map(&:weight_per_carton).first,
    complete: parts.all?(&:complete?)
  )
end

Instance Method Details

#as_json ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'app/models/spree/freight_summary/line.rb', line 239

def as_json(*)
  {
    'variant_id' => variant_id,
    'sku' => sku,
    'name' => name,
    'units' => units,
    'cartons' => cartons,
    'pallets' => pallets,
    'units_per_carton' => units_per_carton,
    'cartons_per_pallet' => cartons_per_pallet,
    'weight_per_carton' => weight_per_carton && BigDecimal(weight_per_carton.to_s).to_s('F'),
    'volume' => BigDecimal(volume.to_s).to_s('F'),
    'weight' => BigDecimal(weight.to_s).to_s('F'),
    'complete' => complete
  }
end

#cartons? ⇒ Boolean

Returns:

  • (Boolean)


230
231
232
# File 'app/models/spree/freight_summary/line.rb', line 230

def cartons?
  cartons.to_i.positive?
end

#complete? ⇒ Boolean

Returns:

  • (Boolean)


235
236
237
# File 'app/models/spree/freight_summary/line.rb', line 235

def complete?
  !!complete
end