Class: Spree::PackageType

Inherits:
Object
  • Object
show all
Includes:
Metadata, SingleStoreResource
Defined in:
app/models/spree/package_type.rb

Overview

A kind of packaging the store uses: the box it ships parcels in, the cartons its products are packed into, the pallets those cartons stack on, the containers a wholesale order fills.

Geometry lives here rather than on the things packed into it because merchants reuse a handful of standard sizes across hundreds of products — one edit to a carton row fixes every product packed in that carton. What varies per product (how many units fit, what a packed carton weighs) stays on the variant.

See docs/plans/6.0-b2b-wholesale-shipping.md.

Constant Summary collapse

KINDS =
%w[box envelope carton pallet container].freeze

Instance Method Summary collapse

Methods included from Metadata

#private_metadata, #private_metadata=

Instance Method Details

#available_to_seller?(owner_seller_id) ⇒ Boolean

Whether the given owner may pack into this row: their own, or the marketplace's shared vocabulary. The Ruby twin of available_to_seller, so the validation and the scope cannot drift apart on what "mine or the marketplace's" means.

Parameters:

  • owner_seller_id (Integer, nil) —

    nil is the operator

Returns:

  • (Boolean)


82
83
84
# File 'app/models/spree/package_type.rb', line 82

def available_to_seller?(owner_seller_id)
  seller_id.nil? || seller_id == owner_seller_id
end

#carton? ⇒ Boolean

The one kind anything branches on: a variant may only be packed into a carton. The rest of the vocabulary is the merchant's to name and read.

Returns:

  • (Boolean)


90
91
92
# File 'app/models/spree/package_type.rb', line 90

def carton?
  kind == 'carton'
end

#dimensions_in(unit) ⇒ Hash{Symbol => Float}?

Geometry expressed in the unit the given store works in, so a carton a merchant measured in centimetres is still usable by a store that quotes in inches. Nil unless all three sides are recorded.

Parameters:

Returns:

  • (Hash{Symbol => Float}, nil)


127
128
129
130
131
132
# File 'app/models/spree/package_type.rb', line 127

def dimensions_in(unit)
  sides = { length: length, width: width, height: height }
  return if sides.values.any? { |side| side.to_f.zero? }

  sides.transform_values { |side| Spree::Measurement.convert_length(side, from: dimensions_unit, to: unit).to_f }
end

#dimensions_unit ⇒ String

The unit the geometry is expressed in, falling back to what the store's unit system implies — the same fallback a variant's dimensions take.

Returns:

  • (String)


104
105
106
# File 'app/models/spree/package_type.rb', line 104

def dimensions_unit
  self[:dimensions_unit].presence || Spree::Variant.store_dimensions_unit(store)
end

#volume ⇒ BigDecimal?

The cubic meters this package occupies. Nil until all three dimensions are recorded — a partially measured box has no volume to report.

Returns:

  • (BigDecimal, nil)


117
118
119
# File 'app/models/spree/package_type.rb', line 117

def volume
  Spree::Measurement.cubic_meters(length, width, height, unit: dimensions_unit)
end

#weight_in(unit) ⇒ BigDecimal

The empty package's own weight in the given unit — the tare added to every quote.

Parameters:

Returns:

  • (BigDecimal)


139
140
141
# File 'app/models/spree/package_type.rb', line 139

def weight_in(unit)
  Spree::Measurement.convert_weight(weight, from: weight_unit, to: unit) || 0
end

#weight_unit ⇒ String

Returns:

  • (String)


109
110
111
# File 'app/models/spree/package_type.rb', line 109

def weight_unit
  self[:weight_unit].presence || store&.preferred_weight_unit || Spree::Measurement::DEFAULT_WEIGHT_UNIT
end