Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Added package types for boxes, cartons, pallets and containers

Damian Legawiec8ff9013de61c
diff --git a/spree/core/app/models/spree/package_type.rb b/spree/core/app/models/spree/package_type.rb
new file mode 100644
index 0000000000..52a9214a2e
--- /dev/null
+++ b/spree/core/app/models/spree/package_type.rb
@@ -0,0 +1,140 @@
+module Spree
+ # 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.
+ class PackageType < Spree.base_class
+ KINDS = %w[box envelope carton pallet container].freeze
+
+ has_prefix_id :pkgtype
+
+ include Spree::SingleStoreResource
+ include Spree::Metadata
+
+ belongs_to :store, class_name: 'Spree::Store'
+
+ has_many :variants, class_name: 'Spree::Variant', foreign_key: :carton_package_type_id,
+ inverse_of: :carton_package_type, dependent: :restrict_with_error
+
+ validates :name, presence: true, uniqueness: { scope: spree_base_uniqueness_scope + [:store_id] }
+ validates :kind, inclusion: { in: KINDS }
+ validates :dimensions_unit, inclusion: { in: Spree::Variant::DIMENSION_UNITS }, allow_blank: true
+ validates :weight_unit, inclusion: { in: Spree::Variant::WEIGHT_UNITS }, allow_blank: true
+ validates :length, :width, :height, :weight, :max_weight,
+ numericality: { greater_than_or_equal_to: 0, allow_nil: true }
+
+ # Demote any prior default in the same transaction so the partial unique
+ # index ("one default per store") never sees two TRUE rows, and MySQL —
+ # which cannot enforce that index — arrives at the same place.
+ validate :kind_cannot_leave_the_products_packed_in_it, on: :update
+ before_save :demote_other_defaults, if: -> { default? && will_save_change_to_default? }
+ validate :default_cannot_be_given_up, on: :update
+ # prepend so this runs before the association callbacks Rails registers,
+ # and is skipped when the store itself is going away.
+ before_destroy :ensure_not_default, prepend: true
+
+ scope :default, -> { where(default: true) }
+
+ scope :cartons, -> { where(kind: 'carton') }
+
+ # 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.
+ #
+ # @return [Boolean]
+ def carton?
+ kind == 'carton'
+ end
+
+ self.whitelisted_ransackable_attributes = %w[name kind default]
+
+ # 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.
+ #
+ # @return [String]
+ def dimensions_unit
+ self[:dimensions_unit].presence || Spree::Variant.store_dimensions_unit(store)
+ end
+
+ # @return [String]
+ def weight_unit
+ self[:weight_unit].presence || store&.preferred_weight_unit || Spree::Measurement::DEFAULT_WEIGHT_UNIT
+ end
+
+ # The cubic meters this package occupies. Nil until all three dimensions
+ # are recorded — a partially measured box has no volume to report.
+ #
+ # @return [BigDecimal, nil]
+ def volume
+ Spree::Measurement.cubic_meters(length, width, height, unit: dimensions_unit)
+ end
+
+ # 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.
+ #
+ # @param unit [String] one of {Spree::Variant::DIMENSION_UNITS}
+ # @return [Hash{Symbol => Float}, nil]
+ 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
+
+ # The empty package's own weight in the given unit — the tare added to
+ # every quote.
+ #
+ # @param unit [String] one of {Spree::Variant::WEIGHT_UNITS}
+ # @return [BigDecimal]
+ def weight_in(unit)
+ Spree::Measurement.convert_weight(weight, from: weight_unit, to: unit) || 0
+ end
+
+ private
+
+ # Deleting the store's box would leave every quote with no tare and no
+ # dimensions, which under-prices bulky shipments silently. Name another
+ # default first — unless the store itself is being destroyed, where
+ # refusing would abort that too and strand the row.
+ def ensure_not_default
+ return unless default?
+ return if destroyed_by_association.present?
+
+ errors.add(:base, Spree.t('errors.messages.cannot_delete_default_package_type'))
+ throw(:abort)
+ end
+
+ # A variant may only be packed into a carton, and that is checked when the
+ # variant is saved — so letting a carton become a pallet later would leave
+ # every product already packed in it pointing at a row it could no longer
+ # choose, its geometry still feeding their freight rollups. Repack them
+ # first.
+ def kind_cannot_leave_the_products_packed_in_it
+ return unless kind_changed? && kind_was == 'carton' && kind != 'carton'
+ return unless variants.exists?
+
+ errors.add(:kind, :cannot_stop_being_a_carton)
+ end
+
+ # Clearing the flag on the only default leaves the store with no box at
+ # all, which is the same silent loss as deleting it. Promote another row
+ # instead; that demotes this one in the same save.
+ def default_cannot_be_given_up
+ return unless default_changed? && default_was && !default?
+ return if self.class.where(store_id: store_id, default: true).where.not(id: id).exists?
+
+ errors.add(:default, :cannot_be_given_up)
+ end
+
+ def demote_other_defaults
+ self.class.where(store_id: store_id, default: true).where.not(id: id).update_all(default: false)
+ end
+ end
+end
diff --git a/spree/core/app/models/spree/store.rb b/spree/core/app/models/spree/store.rb
index f57d0ee4a3..7646754dda 100644
--- a/spree/core/app/models/spree/store.rb
+++ b/spree/core/app/models/spree/store.rb
@@ -44,16 +44,6 @@ module Spree
preference :admin_locale, :string
preference :timezone, :string, default: Time.zone.name
preference :weight_unit, :string, default: 'lb'
- # Default package (Shopify-style): the box a store usually ships in.
- # Weight is the packaging tare (box + filler) added to every package's
- # content weight for rate calculation, in the store's weight unit; the
- # dimensions are the box itself, used verbatim by carrier rate providers
- # for dimensional-weight pricing — inches when the unit system is
- # imperial, centimeters when metric. Zeros keep historical behavior.
- preference :default_package_weight, :decimal, default: 0
- preference :default_package_length, :decimal, default: 0
- preference :default_package_width, :decimal, default: 0
- preference :default_package_height, :decimal, default: 0
preference :unit_system, :string, default: 'imperial'
# email preferences
preference :send_consumer_transactional_emails, :boolean, default: true
@@ -242,6 +232,14 @@ module Spree
Spree::DeliveryProfile.default_for(self)
end
has_many :stock_locations, class_name: 'Spree::StockLocation', dependent: :nullify
+
+ # The store's packaging vocabulary. The default row is the box it usually
+ # ships in — the tare and dimensions every parcel quote is built on, read
+ # through an association so it is loaded once per store rather than once
+ # per package quoted.
+ has_many :package_types, class_name: 'Spree::PackageType', dependent: :destroy, inverse_of: :store
+ has_one :default_package_type, -> { where(default: true) }, class_name: 'Spree::PackageType',
+ inverse_of: :store
has_many :promotions, class_name: 'Spree::Promotion', dependent: :nullify
has_many :tax_categories, class_name: 'Spree::TaxCategory', dependent: :destroy, inverse_of: :store
diff --git a/spree/core/app/services/spree/stores/provision_defaults.rb b/spree/core/app/services/spree/stores/provision_defaults.rb
index c9b4fddd80..92f4788e18 100644
--- a/spree/core/app/services/spree/stores/provision_defaults.rb
+++ b/spree/core/app/services/spree/stores/provision_defaults.rb
@@ -34,6 +34,7 @@ module Spree
update_default_market
provision_stock_location
provision_delivery_zones
+ provision_package_type
provision_pickup
restate_seeded_calculators
end
@@ -189,6 +190,38 @@ module Spree
delivery_method.save!
end
+ # The box the store ships parcels in. Its tare rides on every quote and
+ # its dimensions feed dimensional-weight pricing, so without one a
+ # carrier is asked to price a parcel with no size — which under-charges
+ # anything bulky but light, silently.
+ #
+ # Sized in the unit the store already implies rather than converted, so
+ # the numbers read as a merchant would write them. Skipped when the
+ # store already has a default, so a merchant's own box is never
+ # replaced and re-running is harmless.
+ def provision_package_type
+ return if store.default_package_type.present?
+
+ metric = store.metric_unit_system?
+ weight_unit = store.preferred_weight_unit
+
+ store.package_types.create!(
+ name: Spree.t('package_types.default_name'),
+ kind: 'box',
+ default: true,
+ length: metric ? 30 : 12,
+ width: metric ? 23 : 9,
+ height: metric ? 10 : 4,
+ dimensions_unit: metric ? 'cm' : 'in',
+ # The unit system and the weight unit are separate preferences, so a
+ # metric store can still weigh in pounds. Stating half a pound as
+ # `0.5` under a `kg` label — or the reverse — would misreport the
+ # tare on every quote, so the figure is converted rather than assumed.
+ weight: Spree::Measurement.convert_weight(0.5, from: 'lb', to: weight_unit).round(3),
+ weight_unit: weight_unit
+ )
+ end
+
# Collection at a merchant counter. Rides the store's default profile
# (the same physical goods ship or get collected) with no zone — pickup
# has no destination, only an origin, and the Pickup provider decides
diff --git a/spree/core/db/migrate/20260831100001_create_spree_package_types.rb b/spree/core/db/migrate/20260831100001_create_spree_package_types.rb
new file mode 100644
index 0000000000..9a4f36d99d
--- /dev/null
+++ b/spree/core/db/migrate/20260831100001_create_spree_package_types.rb
@@ -0,0 +1,38 @@
+class CreateSpreePackageTypes < ActiveRecord::Migration[8.1]
+ def change
+ # The store's packaging vocabulary: the boxes it ships in, and the
+ # cartons, pallets and containers wholesale orders leave on.
+ create_table :spree_package_types do |t|
+ t.references :store, null: false
+ t.string :name, null: false
+ t.string :kind, null: false
+ t.decimal :length, precision: 8, scale: 2
+ t.decimal :width, precision: 8, scale: 2
+ t.decimal :height, precision: 8, scale: 2
+ t.string :dimensions_unit
+ # The empty package's own weight (box plus filler), added to content
+ # weight on every quote — distinct from max_weight, what it can hold.
+ t.decimal :weight, precision: 10, scale: 2
+ t.decimal :max_weight, precision: 10, scale: 2
+ t.string :weight_unit
+ t.boolean :default, null: false, default: false
+ if t.respond_to?(:jsonb)
+ t.jsonb :metadata
+ else
+ t.json :metadata
+ end
+ t.timestamps
+ end
+
+ add_index :spree_package_types, [:store_id, :name], unique: true,
+ name: 'index_spree_package_types_on_store_and_name'
+
+ # One default package type per store — the box the store usually ships
+ # in. MySQL ignores +where:+, so the model validation carries it there.
+ unless ActiveRecord::Base.connection.adapter_name.match?(/mysql/i)
+ add_index :spree_package_types, :store_id, unique: true,
+ where: '"default" = TRUE',
+ name: 'index_spree_package_types_default_per_store'
+ end
+ end
+end
diff --git a/spree/core/db/sample_data/fulfillment.rb b/spree/core/db/sample_data/fulfillment.rb
index 4be06a9c99..8284cd7a07 100644
--- a/spree/core/db/sample_data/fulfillment.rb
+++ b/spree/core/db/sample_data/fulfillment.rb
@@ -19,22 +19,32 @@ end
# A realistic shipping box (imperial units — the demo store is US): its weight
# rides on every parcel and its dimensions feed dimensional-weight pricing,
-# without which carrier quotes under-price bulky-but-light items. Applied only
-# when nothing is configured, so a half-filled box is never completed with
-# values the merchant did not choose.
-package_preferences = %i[
- preferred_default_package_weight
- preferred_default_package_length
- preferred_default_package_width
- preferred_default_package_height
-]
+# without which carrier quotes under-price bulky-but-light items. Created only
+# when the store has no default box, so a merchant's own is never replaced.
+if store.default_package_type.nil?
+ store.package_types.create!(
+ name: 'Standard box',
+ kind: 'box',
+ default: true,
+ weight: 0.5,
+ length: 12,
+ width: 9,
+ height: 4,
+ dimensions_unit: 'in',
+ weight_unit: 'lb'
+ )
+end
-if package_preferences.all? { |preference| store.public_send(preference).to_f.zero? }
- store.preferred_default_package_weight = 0.5
- store.preferred_default_package_length = 12
- store.preferred_default_package_width = 9
- store.preferred_default_package_height = 4
- store.save!
+# Wholesale packaging: the carton products are packed into, and the pallet
+# those cartons stack onto. A merchant configuring freight starts from rows
+# like these.
+[
+ { name: 'Master carton', kind: 'carton', length: 40, width: 30, height: 25, weight: 0.4, max_weight: 20 },
+ { name: 'Euro pallet', kind: 'pallet', length: 120, width: 80, height: 15, weight: 25, max_weight: 1_500 }
+].each do |attributes|
+ store.package_types.where(name: attributes[:name]).first_or_create! do |package_type|
+ package_type.assign_attributes(attributes.merge(dimensions_unit: 'cm', weight_unit: 'kg'))
+ end
end
domestic = store.delivery_zones.find_by(name: 'Domestic')
diff --git a/spree/core/lib/spree/testing_support/factories/package_type_factory.rb b/spree/core/lib/spree/testing_support/factories/package_type_factory.rb
new file mode 100644
index 0000000000..b602016c12
--- /dev/null
+++ b/spree/core/lib/spree/testing_support/factories/package_type_factory.rb
@@ -0,0 +1,26 @@
+FactoryBot.define do
+ factory :package_type, class: Spree::PackageType do
+ store { Spree::Store.default || association(:store) }
+ sequence(:name) { |n| "Package type #{n}" }
+ kind { 'box' }
+ dimensions_unit { 'cm' }
+ weight_unit { 'kg' }
+
+ factory :carton_package_type do
+ kind { 'carton' }
+ sequence(:name) { |n| "Carton #{n}" }
+ length { 40 }
+ width { 30 }
+ height { 25 }
+ max_weight { 20 }
+ end
+
+ factory :pallet_package_type do
+ kind { 'pallet' }
+ sequence(:name) { |n| "Pallet #{n}" }
+ length { 120 }
+ width { 80 }
+ height { 15 }
+ end
+ end
+end
diff --git a/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml b/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml
index c947e30332..5ce1ea43bf 100644
--- a/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml
+++ b/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml
@@ -616,3 +616,21 @@ steps:
filter_key another row in the same store already owns — only one of them
can be addressed as a sort or filter param. Rename one and re-run.
Idempotent.
+
+ - id: package_types
+ name: "Turn the store's default package into a package type row"
+ task: "spree:upgrade:package_types"
+ notes: |
+ The box a store ships parcels in used to be four loose preferences on
+ the store. It is now a row in the store's packaging vocabulary, beside
+ the cartons, pallets and containers a wholesale order leaves on, so the
+ same geometry can be named, reused, and referenced by the products
+ packed into it.
+
+ Until this runs, quotes for stores that had configured a box lose their
+ packaging tare and dimensions — carrier rates then under-price bulky
+ light items, which is what the dimensions were there to prevent. Stores
+ still on the seeded zeros are unaffected either way.
+
+ Idempotent — a store that already has a default package type is left
+ alone, so a box edited since the upgrade is never overwritten.
diff --git a/spree/core/lib/tasks/package_types.rake b/spree/core/lib/tasks/package_types.rake
new file mode 100644
index 0000000000..cc26361cc0
--- /dev/null
+++ b/spree/core/lib/tasks/package_types.rake
@@ -0,0 +1,62 @@
+namespace :spree do
+ namespace :upgrade do
+ desc <<~DESC
+ Turns each store's default package preferences into a default
+ Spree::PackageType row (Spree 6.0).
+
+ The box a store ships in used to be four loose preferences on the store
+ (default_package_weight/length/width/height). It is now a row in the
+ store's packaging vocabulary, alongside the cartons, pallets and
+ containers wholesale orders leave on, so the same geometry can be named,
+ reused and referenced by a product.
+
+ Stores that never configured a box — the preferences still at their
+ seeded zeros — get nothing, exactly as before: quoting falls back to
+ content weight with no tare and no dimensions.
+
+ Idempotent. A store that already has a default package type is skipped,
+ so this never overwrites a box the merchant has since edited.
+ DESC
+ task package_types: :environment do
+ created = 0
+ skipped = 0
+
+ Spree::Store.find_each do |store|
+ if store.default_package_type.present?
+ skipped += 1
+ next
+ end
+
+ # Read the raw preferences hash: the four keys no longer have
+ # declarations on Spree::Store, so there are no readers to call.
+ # Indifferent access because how the YAML column keyed them depends on
+ # what wrote it, and a symbol-only read would silently answer zero and
+ # discard a merchant's configured box.
+ preferences = (store.preferences || {}).with_indifferent_access
+ weight, length, width, height = %i[
+ default_package_weight default_package_length default_package_width default_package_height
+ ].map { |key| preferences[key].to_f }
+
+ if [weight, length, width, height].all?(&:zero?)
+ skipped += 1
+ next
+ end
+
+ store.package_types.create!(
+ name: Spree.t('package_types.default_name'),
+ kind: 'box',
+ default: true,
+ weight: weight.positive? ? weight : nil,
+ length: length.positive? ? length : nil,
+ width: width.positive? ? width : nil,
+ height: height.positive? ? height : nil,
+ dimensions_unit: store.metric_unit_system? ? 'cm' : 'in',
+ weight_unit: store.preferred_weight_unit
+ )
+ created += 1
+ end
+
+ puts " Created #{created} default package type(s); skipped #{skipped} store(s)."
+ end
+ end
+end
diff --git a/spree/core/spec/models/spree/package_type_spec.rb b/spree/core/spec/models/spree/package_type_spec.rb
new file mode 100644
index 0000000000..cfc9a2b38d
--- /dev/null
+++ b/spree/core/spec/models/spree/package_type_spec.rb
@@ -0,0 +1,173 @@
+require 'spec_helper'
+
+RSpec.describe Spree::PackageType, type: :model do
+ let(:store) { @default_store }
+
+ describe 'validations' do
+ it 'refuses a kind outside the vocabulary' do
+ package_type = build(:package_type, kind: 'crate')
+
+ expect(package_type).not_to be_valid
+ expect(package_type.errors[:kind]).to be_present
+ end
+
+ it 'refuses a duplicate name within the store' do
+ create(:package_type, store: store, name: 'Master carton')
+ duplicate = build(:package_type, store: store, name: 'Master carton')
+
+ expect(duplicate).not_to be_valid
+ end
+
+ it 'allows the same name in a different store' do
+ create(:package_type, store: store, name: 'Master carton')
+ other = build(:package_type, store: create(:store), name: 'Master carton')
+
+ expect(other).to be_valid
+ end
+ end
+
+ describe 'the store default' do
+ it 'demotes the previous default so only one survives' do
+ first = create(:package_type, store: store, default: true)
+ second = create(:package_type, store: store, default: true)
+
+ expect(first.reload).not_to be_default
+ expect(second.reload).to be_default
+ expect(store.default_package_type).to eq(second)
+ end
+
+ it 'leaves another store default alone' do
+ other_store = create(:store)
+ other_default = create(:package_type, store: other_store, default: true)
+ create(:package_type, store: store, default: true)
+
+ expect(other_default.reload).to be_default
+ end
+ end
+
+ describe '#volume' do
+ it 'reports cubic meters from the recorded unit' do
+ package_type = build(:package_type, length: 100, width: 100, height: 100, dimensions_unit: 'cm')
+
+ expect(package_type.volume).to eq(BigDecimal('1'))
+ end
+
+ it 'is nil until every side is measured' do
+ expect(build(:package_type, length: 100, width: 100, height: nil).volume).to be_nil
+ end
+ end
+
+ describe '#dimensions_unit' do
+ it 'reads centimeters on a metric store' do
+ stub_store_preferences(store, unit_system: 'metric')
+
+ expect(build(:package_type, store: store, dimensions_unit: nil).dimensions_unit).to eq('cm')
+ end
+
+ it 'reads inches on an imperial store' do
+ stub_store_preferences(store, unit_system: 'imperial')
+
+ expect(build(:package_type, store: store, dimensions_unit: nil).dimensions_unit).to eq('in')
+ end
+ end
+
+ describe 'reading geometry in another unit' do
+ subject(:package_type) do
+ build(:package_type, length: 30.48, width: 22.86, height: 10.16,
+ dimensions_unit: 'cm', weight: 1, weight_unit: 'kg')
+ end
+
+ it 'converts the sides' do
+ expect(package_type.dimensions_in('in')).to eq(length: 12.0, width: 9.0, height: 4.0)
+ end
+
+ it 'converts the tare' do
+ expect(package_type.weight_in('lb')).to be_within(0.001).of(2.2046)
+ end
+
+ it 'has no dimensions until every side is recorded' do
+ package_type.height = nil
+
+ expect(package_type.dimensions_in('cm')).to be_nil
+ end
+
+ it 'reads an unrecorded tare as nothing rather than nil' do
+ expect(build(:package_type, weight: nil).weight_in('kg')).to eq(0)
+ end
+ end
+
+ describe 'giving up the default' do
+ # Clearing the flag leaves the store with no box at all, which is the
+ # same silent loss as deleting it.
+ it 'refuses to turn the flag off on the only default' do
+ package_type = create(:package_type, store: store, default: true)
+
+ expect(package_type.update(default: false)).to be(false)
+ expect(package_type.errors[:default]).to be_present
+ expect(store.reload.default_package_type).to eq(package_type)
+ end
+
+ it 'allows it once another row holds the flag' do
+ first = create(:package_type, store: store, default: true)
+ create(:package_type, store: store, default: true)
+
+ expect(first.reload).not_to be_default
+ end
+ end
+
+ describe 'changing kind' do
+ # The variant validation only runs when the variant is saved, so a carton
+ # that turned into a pallet would leave its products pointing at a row
+ # they could no longer choose.
+ it 'refuses to stop being a carton while products are packed into it' do
+ carton = create(:carton_package_type, store: store)
+ create(:variant, carton_package_type: carton)
+
+ expect(carton.update(kind: 'pallet')).to be(false)
+ expect(carton.errors[:kind]).to be_present
+ expect(carton.reload.kind).to eq('carton')
+ end
+
+ it 'allows the change once nothing is packed into it' do
+ carton = create(:carton_package_type, store: store)
+
+ expect(carton.update(kind: 'pallet')).to be(true)
+ end
+
+ it 'leaves other edits to a carton in use alone' do
+ carton = create(:carton_package_type, store: store)
+ create(:variant, carton_package_type: carton)
+
+ expect(carton.update(name: 'Renamed carton', length: 50)).to be(true)
+ end
+ end
+
+ describe 'deletion' do
+ # Deleting it would leave every quote with no tare and no dimensions,
+ # which under-prices bulky shipments with nothing to notice.
+ it 'refuses the store default' do
+ package_type = create(:package_type, store: store, default: true)
+
+ expect(package_type.destroy).to be_falsey
+ expect(package_type.errors[:base]).to be_present
+ expect(store.reload.default_package_type).to eq(package_type)
+ end
+
+ # Refusing here would abort the store's own destroy and strand the row.
+ it 'goes with the store it belongs to' do
+ other_store = create(:store)
+ package_type = create(:package_type, store: other_store, default: true)
+
+ expect { other_store.destroy }.not_to raise_error
+ expect(Spree::PackageType.where(id: package_type.id)).not_to exist
+ end
+
+ it 'refuses while a variant is packed into it' do
+ carton = create(:carton_package_type, store: store)
+ create(:variant, carton_package_type: carton)
+
+ expect(carton.destroy).to be_falsey
+ expect(carton.errors[:base]).to be_present
+ end
+ end
+end
diff --git a/spree/core/spec/services/spree/stores/provision_defaults_spec.rb b/spree/core/spec/services/spree/stores/provision_defaults_spec.rb
index 2a1bdf9fc7..d07bde33a8 100644
--- a/spree/core/spec/services/spree/stores/provision_defaults_spec.rb
+++ b/spree/core/spec/services/spree/stores/provision_defaults_spec.rb
@@ -7,6 +7,74 @@ RSpec.describe Spree::Stores::ProvisionDefaults do
let(:country) { Spree::Country.by_iso('DE') }
let(:locale) { 'de' }
+ describe 'the default package type' do
+ # Nothing errors without a box — a quote just goes out with no size and
+ # no tare, and a carrier under-charges anything bulky but light.
+ it 'gives the store a box to ship parcels in' do
+ subject
+
+ box = store.reload.default_package_type
+ expect(box).to be_present
+ expect(box.kind).to eq('box')
+ expect(box.weight).to be_positive
+ expect([box.length, box.width, box.height]).to all(be_positive)
+ end
+
+ it 'states it in the unit the store already implies' do
+ stub_store_preferences(store, unit_system: 'metric', weight_unit: 'kg')
+
+ subject
+
+ box = store.reload.default_package_type
+ expect(box.dimensions_unit).to eq('cm')
+ expect(box.weight_unit).to eq('kg')
+ end
+
+ # The two preferences are independent: a metric store can still weigh in
+ # pounds, and stating the wrong number under that label misreports the tare
+ # on every quote.
+ it 'states the tare in the weight unit the store actually uses' do
+ stub_store_preferences(store, unit_system: 'metric', weight_unit: 'lb')
+
+ subject
+
+ box = store.reload.default_package_type
+ expect(box.weight_unit).to eq('lb')
+ expect(box.weight.to_f).to be_within(0.01).of(0.5)
+ end
+
+ it 'converts that tare for a store weighing in kilograms' do
+ stub_store_preferences(store, unit_system: 'metric', weight_unit: 'kg')
+
+ subject
+
+ box = store.reload.default_package_type
+ expect(box.weight_unit).to eq('kg')
+ expect(box.weight.to_f).to be_within(0.01).of(0.227)
+ end
+
+ it 'measures an imperial store in inches' do
+ stub_store_preferences(store, unit_system: 'imperial', weight_unit: 'lb')
+
+ subject
+
+ box = store.reload.default_package_type
+ expect(box.dimensions_unit).to eq('in')
+ expect(box.weight_unit).to eq('lb')
+ end
+
+ # Re-provisioning must never replace packaging a merchant measured
+ # themselves.
+ it 'leaves a box the merchant already chose alone' do
+ existing = create(:package_type, store: store, kind: 'box', name: 'Our own box', default: true)
+
+ subject
+
+ expect(store.reload.default_package_type).to eq(existing)
+ expect(store.package_types.count).to eq(1)
+ end
+ end
+
describe 'the default market' do
it 're-points the bootstrap market at the chosen country' do
subject