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.

Let only a forced dispatch send goods the shelf cannot cover

Łukasz Adamczyk5c775861ed3e
diff --git a/docs/plans/6.0-typed-stock-movements.md b/docs/plans/6.0-typed-stock-movements.md
index d410763bb1..46a985b886 100644
--- a/docs/plans/6.0-typed-stock-movements.md
+++ b/docs/plans/6.0-typed-stock-movements.md
@@ -146,11 +146,13 @@ end
The `min_quantity` validation goes away. It exists to stop a movement from driving a non-backorderable item negative, a check that belongs at allocation time against `available_count` (where `Stock::Quantifier` and `StockReservations::Reserve` already do it) rather than at ship time, when the units have physically gone whatever the ledger says.
-**Shipping from an empty shelf.** Three ways a dispatch can meet `count_on_hand: 0`, and they resolve differently. A backordered unit is stopped before any of this by `Fulfillments::Fulfill#ensure_ready_to_hand_over`, which already refuses backordered items and stays as it is; the unit ships once a `received` movement arrives and `process_backorders` fills the backorder. A shelf that really does hold the goods is a ledger error, fixed with an `adjusted` movement and then shipped normally. And a genuinely forced dispatch — `force: true` or a `validate` hook waving the guard through — writes `shipped` and takes `count_on_hand` to −1, because a parcel that physically left must be recordable whatever the ledger says (`6.0-fulfillment-and-delivery.md`, the same rule that lets a carrier outage not block a dispatch).
+**Shipping from an empty shelf.** Three ways a dispatch can meet `count_on_hand: 0`, and they resolve differently. A backordered unit is stopped before any of this by `Fulfillments::Fulfill#ensure_ready_to_hand_over`, which already refuses backordered items and stays as it is; the unit ships once a `received` movement arrives and `process_backorders` fills the backorder. A shelf that really does hold the goods is a ledger error, fixed with an `adjusted` movement and then shipped normally. And a genuinely forced dispatch — `force: true`, which is the only route: a `validate` hook can only `reject!`, never wave `ensure_ready_to_hand_over` through — writes `shipped` and takes `count_on_hand` to −1, because a parcel that physically left must be recordable whatever the ledger says (`6.0-fulfillment-and-delivery.md`, the same rule that lets a carrier outage not block a dispatch).
A negative `count_on_hand` therefore stops meaning "oversold" — that is `allocated_count > count_on_hand` now — and comes to mean something narrower: goods left that Spree never saw arrive. A receiving gap, visible as a number, and self-healing the moment the missing `received` movement lands. Availability is unaffected on the way through: on-hand 0 / allocated 1 / available −1 becomes on-hand −1 / allocated 0 / available −1, because the promise had already been subtracted and only the shelf changed.
-This is why the non-negative guard keys off the movement kind rather than off `backorderable?` / `preorder?` as `verify_count_on_hand?` does today: `shipped` may go negative (departure is a fact), `received` cannot by construction, and `adjusted` keeps the guard — a hand-typed count landing below zero is a typo, not a fact.
+This is why the non-negative guard keys off the write rather than off `backorderable?` / `preorder?` as `verify_count_on_hand?` does today. Specifically it keys off `force` (revised 2026-08-17): the `shipped` kind alone would hand the same licence to a stock transfer, which moves goods nobody promised and cannot send what the warehouse does not have — and did, until the transfer's own precondition was tightened to compare against the quantity being moved rather than merely against zero. `received` cannot go negative by construction, and `adjusted` keeps the guard — a hand-typed count landing below zero is a typo, not a fact.
+
+`force` reaches the write as a transient attribute on the movement, threaded `Fulfill(force:)` → `StockLocation#unstock(force:)` → `move(force:)`, never persisted — the shape `Fulfillment#notify_customer` already uses for an instruction about one dispatch. And because a merchant needs to be told rather than 500'd, `Fulfillments::Fulfill` refuses a dispatch the shelf cannot cover before writing anything, with a message saying to correct the count or force it. Note the ordering that makes this work: a canceled fulfillment re-allocates on its way out, so the check reads its manifest rather than the nothing it currently holds.
### StockLevel
diff --git a/spree/core/app/models/spree/stock_level.rb b/spree/core/app/models/spree/stock_level.rb
index b98bacb83f..f712677057 100644
--- a/spree/core/app/models/spree/stock_level.rb
+++ b/spree/core/app/models/spree/stock_level.rb
@@ -32,11 +32,6 @@ module Spree
delegate :name, to: :variant, prefix: true
delegate :product, to: :variant
- # Which movement kind is currently writing count_on_hand, set by
- # {Spree::StockMovement#apply_to_stock_level} so the non-negative guard
- # can key off the kind rather than off the variant's backorder settings.
- attr_accessor :applying_movement_kind
-
after_save :conditional_variant_touch, if: :saved_changes?
after_touch { variant.touch }
after_destroy { variant.touch }
@@ -66,17 +61,27 @@ module Spree
# {Spree::StockLocation} verb rather than this, so the change leaves an
# audit row. Keeps the locked read-modify-write that
# {#process_backorders} needs the delta for.
- def adjust_count_on_hand(value)
+ #
+ # @param force [Boolean] let the write leave the shelf below zero. Only a
+ # forced dispatch asks for this: the merchant has decided the parcel
+ # left whatever the ledger claims.
+ def adjust_count_on_hand(value, force: false)
with_lock do
- set_count_on_hand(count_on_hand + value)
+ set_count_on_hand(count_on_hand + value, force: force)
end
end
- def set_count_on_hand(value)
+ # @param force [Boolean] see {#adjust_count_on_hand}
+ def set_count_on_hand(value, force: false)
+ @forced_count_on_hand = force
self.count_on_hand = value
process_backorders(count_on_hand - count_on_hand_was)
save!
+ ensure
+ # Scoped to this write: the exemption must not outlive the call that
+ # asked for it.
+ @forced_count_on_hand = false
end
# Units that are physically here and not already promised to a placed
@@ -131,14 +136,14 @@ module Spree
private
- # A shelf can only be driven below zero by goods physically leaving, and
- # then it means Spree never saw those goods arrive — a receiving gap that
- # heals itself when the missing `received` movement lands. Every other
- # writer, a correction above all, is stopped: a hand-typed count below
- # zero is a typo, not a fact.
+ # A shelf can only be driven below zero by a write that asked to, and then
+ # it means Spree never saw those goods arrive — a receiving gap that heals
+ # itself when the missing `received` movement lands. Every other writer, a
+ # correction above all, is stopped: a hand-typed count below zero is a
+ # typo, not a fact.
def verify_count_on_hand?
count_on_hand_changed? && count_on_hand.negative? &&
- count_on_hand < count_on_hand_was && applying_movement_kind != 'shipped'
+ count_on_hand < count_on_hand_was && !@forced_count_on_hand
end
# Process backorders based on amount of stock received
diff --git a/spree/core/app/models/spree/stock_location.rb b/spree/core/app/models/spree/stock_location.rb
index e265c3e822..772273229a 100644
--- a/spree/core/app/models/spree/stock_location.rb
+++ b/spree/core/app/models/spree/stock_location.rb
@@ -144,12 +144,14 @@ module Spree
move(variant, quantity, kind: 'received', cause: cause, persist: persist)
end
- # Goods left. Departure is a physical fact, so this is allowed to take the
- # shelf below zero.
+ # Goods left.
#
+ # @param force [Boolean] record the departure even if it leaves the shelf
+ # below zero. A merchant forcing a dispatch has decided the parcel left
+ # whatever the ledger claims.
# @return [Spree::StockMovement]
- def unstock(variant, quantity, cause = nil, persist: true)
- move(variant, quantity, kind: 'shipped', cause: cause, persist: persist)
+ def unstock(variant, quantity, cause = nil, persist: true, force: false)
+ move(variant, quantity, kind: 'shipped', cause: cause, persist: persist, force: force)
end
# Promises stock to a placed order. The cause is the fulfillment, never
@@ -180,16 +182,19 @@ module Spree
move(variant, quantity, kind: 'adjusted', reason: reason)
end
- def move(variant, quantity, kind:, cause: nil, reason: nil, persist: true)
+ def move(variant, quantity, kind:, cause: nil, reason: nil, persist: true, force: false)
stock_level = stock_level_or_create(variant)
attributes = { quantity: quantity, kind: kind, reason: reason, **cause_attributes(cause) }
if persist
- stock_level.stock_movements.create!(attributes)
+ stock_level.stock_movements.create!(attributes) { |movement| movement.force = force }
else
# StockTransfer builds its movements before it is saved, so they ride
# along on its own association rather than being created here.
- cause.stock_movements << stock_level.stock_movements.build(attributes)
+ built = stock_level.stock_movements.build(attributes)
+ built.force = force
+ cause.stock_movements << built
+ built
end
end
diff --git a/spree/core/app/models/spree/stock_movement.rb b/spree/core/app/models/spree/stock_movement.rb
index 5e2340374c..a2505f5fde 100644
--- a/spree/core/app/models/spree/stock_movement.rb
+++ b/spree/core/app/models/spree/stock_movement.rb
@@ -30,6 +30,11 @@ module Spree
alias_attribute :stock_item_id, :stock_level_id
+ # Whether this write may leave the shelf below zero. An instruction about
+ # this one movement rather than a property of it, so it is never
+ # persisted — the same shape as {Spree::Fulfillment#notify_customer}.
+ attr_accessor :force
+
after_create :apply_to_stock_level
validates :stock_level, :quantity, :kind, presence: true
@@ -106,20 +111,17 @@ module Spree
end
end
- # Departure is a physical fact, so it is allowed to leave the shelf
- # negative — see the plan's "shipping from an empty shelf". The stock
- # level needs to know that to relax its own non-negative guard.
+ # The shelf may only be driven below zero when the caller said so: a
+ # merchant forcing a dispatch has decided the parcel left whatever the
+ # ledger claims. Nothing else — a transfer least of all — may send goods a
+ # warehouse does not have.
#
- # Only a dispatch retires a promise, and only its own: a stock transfer
- # moves goods nobody had promised, so consuming an allocation there would
- # take a different order's units and leave the level looking more
- # available than it is.
+ # Retiring the promise keys off the cause instead, because only a dispatch
+ # has one to retire: consuming an allocation on a transfer would take a
+ # different order's units.
def apply_departure
- stock_level.applying_movement_kind = 'shipped'
- stock_level.adjust_count_on_hand(-quantity.abs)
+ stock_level.adjust_count_on_hand(-quantity.abs, force: force.present?)
stock_level.release_allocated_count(quantity.abs) if fulfillment_id.present?
- ensure
- stock_level.applying_movement_kind = nil
end
end
end
diff --git a/spree/core/app/models/spree/stock_transfer.rb b/spree/core/app/models/spree/stock_transfer.rb
index c65beac257..3a1ccc18d2 100644
--- a/spree/core/app/models/spree/stock_transfer.rb
+++ b/spree/core/app/models/spree/stock_transfer.rb
@@ -89,11 +89,17 @@ module Spree
errors.add(:base, Spree.t('stock_transfer.errors.must_have_variant')) if stock_movements.empty?
end
+ # Each variant needs enough available stock for the quantity being moved,
+ # not merely some. Checking only for a positive balance let a transfer take
+ # more than the shelf held and leave it negative.
def variants_available_in_source_location?(source_location, variants)
return true if source_location.nil?
- available = Spree::StockLevel.arel_table[:count_on_hand] - Spree::StockLevel.arel_table[:allocated_count]
- source_location.stock_levels.where(variant: variants.keys).where(available.gt(0)).size == variants.keys.size
+ variants.all? do |variant, quantity|
+ stock_level = source_location.stock_level(variant)
+
+ stock_level.present? && stock_level.available_count >= quantity.to_i
+ end
end
end
end
diff --git a/spree/core/app/workflows/spree/fulfillments/fulfill.rb b/spree/core/app/workflows/spree/fulfillments/fulfill.rb
index 1ba8220992..a930b880e1 100644
--- a/spree/core/app/workflows/spree/fulfillments/fulfill.rb
+++ b/spree/core/app/workflows/spree/fulfillments/fulfill.rb
@@ -104,6 +104,7 @@ module Spree
ensure_order_placed
ensure_ready_to_hand_over
+ ensure_shelf_can_cover_dispatch
ensure_requested_units_available
end
@@ -147,6 +148,66 @@ module Spree
failure(@source, Spree.t('fulfillments.errors.order_not_paid'))
end
+ # The shelf has to be able to cover what is about to leave it. A dispatch
+ # that would take it below zero is one of two things, and only the
+ # merchant can say which: a ledger error, fixed with a stock adjustment
+ # and then shipped normally, or goods the warehouse knows it does not
+ # have, which is what force is for.
+ #
+ # Refused here rather than at the stock write so it renders as a 422 the
+ # merchant can act on, instead of a validation exception surfacing from
+ # inside the dispatch transaction.
+ def ensure_shelf_can_cover_dispatch
+ return if force
+
+ quantities = dispatch_quantity_by_variant
+ return if quantities.empty?
+
+ variants = Spree::Variant.with_deleted.where(id: quantities.keys).index_by(&:id)
+
+ quantities.each do |variant_id, quantity|
+ stock_level = @source.stock_location.stock_level(variant_id)
+ next if stock_level.nil? || stock_level.count_on_hand >= quantity
+
+ failure(
+ @source,
+ Spree.t('fulfillments.errors.insufficient_stock_on_hand',
+ item: variants[variant_id]&.name,
+ on_hand: stock_level.count_on_hand,
+ requested: quantity)
+ )
+ end
+ end
+
+ # What each variant will actually ship: its outstanding promise, capped
+ # by what this dispatch covers. A partial dispatch splits first and the
+ # split carries min(promise, moved quantity), so the two agree.
+ #
+ # @return [Hash{Integer => Integer}]
+ def dispatch_quantity_by_variant
+ held = @source.fulfillment_items.group(:variant_id).sum(:quantity)
+
+ # A canceled fulfillment gets its promise back on the way out (see
+ # #reallocate_if_resuming_from_canceled), so what it is about to take
+ # is its whole manifest rather than the nothing it holds right now.
+ allocated = @source.canceled? ? held : @source.allocated_quantities
+ return {} if allocated.empty?
+
+ wanted =
+ if @requested.empty?
+ held
+ else
+ @requested.each_with_object(Hash.new(0)) do |item, totals|
+ totals[item[:line_item].variant_id] += item[:quantity]
+ end
+ end
+
+ allocated.each_with_object({}) do |(variant_id, promised), totals|
+ quantity = [promised, wanted[variant_id].to_i].min
+ totals[variant_id] = quantity if quantity.positive?
+ end
+ end
+
# Each requested quantity has to exist in *this* fulfillment. Without
# this the split would silently pull units out of a sibling fulfillment,
# shipping goods the merchant did not select.
@@ -225,7 +286,7 @@ module Spree
quantity = [allocated[item.variant.id].to_i, item.quantity].min
next unless quantity.positive?
- @fulfillment.stock_location.unstock(item.variant, quantity, @fulfillment)
+ @fulfillment.stock_location.unstock(item.variant, quantity, @fulfillment, force: force)
end
end
diff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.yml
index e8b6728c0a..38b5ad6025 100644
--- a/spree/core/config/locales/en.yml
+++ b/spree/core/config/locales/en.yml
@@ -970,6 +970,7 @@ en:
cannot_purchase_label: Labels can only be bought before the fulfillment ships
cannot_resume: This fulfillment cannot be resumed in its current status
insufficient_quantity: Requested quantity (%{requested}) for item %{item} exceeds its unfulfilled quantity (%{available})
+ insufficient_stock_on_hand: This dispatch needs %{requested} of %{item} but only %{on_hand} are on hand at this location — correct the stock count or force the dispatch
invalid_cost: cost must be a non-negative number
invalid_quantity: Quantity for item %{item} must be a positive integer
invalid_status: status must be 'shipped' when provided
diff --git a/spree/core/spec/models/spree/shipment_spec.rb b/spree/core/spec/models/spree/shipment_spec.rb
index ff70ffc1c2..e6827bb4b2 100644
--- a/spree/core/spec/models/spree/shipment_spec.rb
+++ b/spree/core/spec/models/spree/shipment_spec.rb
@@ -760,7 +760,15 @@ describe Spree::Shipment, type: :model do
let(:paid_order) { create(:order_ready_to_ship) }
let(:fulfillment) { paid_order.fulfillments.first }
- before { fulfillment.update_column(:status, status) }
+ before do
+ # A placed order's goods were on the shelf when it was placed; the
+ # factory never puts them there, and a dispatch the shelf cannot
+ # cover is refused unless it is forced.
+ fulfillment.manifest.each do |item|
+ fulfillment.stock_location.stock_level_or_create(item.variant).update_column(:count_on_hand, 10)
+ end
+ fulfillment.update_column(:status, status)
+ end
it 'updates fulfilled_at timestamp' do
Spree.fulfillment_fulfill_workflow.call(fulfillment: fulfillment)
diff --git a/spree/core/spec/models/spree/stock_level_spec.rb b/spree/core/spec/models/spree/stock_level_spec.rb
index e8f424fb45..bdb4783b4d 100644
--- a/spree/core/spec/models/spree/stock_level_spec.rb
+++ b/spree/core/spec/models/spree/stock_level_spec.rb
@@ -196,13 +196,12 @@ describe Spree::StockLevel, type: :model do
expect(subject.count_on_hand).to eq(-1)
end
- # Test for #3755. Only a departure may drive the shelf further below
- # zero now, so this states the kind that is doing the writing.
+ # Test for #3755. Only a dispatch may drive the shelf further below zero
+ # now, so this says so at the call.
it 'does not process backorders when stock is adjusted negatively' do
expect(inventory_unit).not_to receive(:fill_backorder)
expect(inventory_unit_2).not_to receive(:fill_backorder)
- subject.applying_movement_kind = 'shipped'
- subject.adjust_count_on_hand(-1)
+ subject.adjust_count_on_hand(-1, force: true)
expect(subject.count_on_hand).to eq(-3)
end
@@ -238,11 +237,7 @@ describe Spree::StockLevel, type: :model do
let(:inventory_unit) { double('InventoryUnit') }
let(:inventory_unit_2) { double('InventoryUnit2') }
- before do
- subject.applying_movement_kind = 'shipped'
- subject.set_count_on_hand(-2)
- subject.applying_movement_kind = nil
- end
+ before { subject.set_count_on_hand(-2, force: true) }
it "doesn't process backorders" do
expect(subject).not_to receive(:backordered_inventory_units)
@@ -425,10 +420,26 @@ describe Spree::StockLevel, type: :model do
it_behaves_like 'not valid count_on_hand'
end
- context 'and a departure is what is writing it' do
- before { subject.applying_movement_kind = 'shipped' }
+ context 'and a dispatch is what is writing it' do
+ it 'is let through' do
+ subject.update_column(:count_on_hand, 3)
+
+ subject.set_count_on_hand(-3, force: true)
+
+ expect(subject.reload.count_on_hand).to eq(-3)
+ end
+
+ # The exemption belongs to the write that asked for it, not to the
+ # record — the next save must be policed again.
+ it 'does not outlive the call that asked for it' do
+ subject.update_column(:count_on_hand, 3)
+ subject.set_count_on_hand(-3, force: true)
+
+ subject.count_on_hand = -4
+ subject.save
- it_behaves_like 'valid count_on_hand'
+ expect(subject.errors[:count_on_hand]).to include 'must be greater than or equal to 0'
+ end
end
end
end
diff --git a/spree/core/spec/models/spree/stock_location_spec.rb b/spree/core/spec/models/spree/stock_location_spec.rb
index 9c488f6a5f..1fd6cf19ae 100644
--- a/spree/core/spec/models/spree/stock_location_spec.rb
+++ b/spree/core/spec/models/spree/stock_location_spec.rb
@@ -180,10 +180,16 @@ module Spree
# departure is written positive like every other order-driven kind.
it 'unstocks a variant with a shipped movement' do
cause = double
- expect(subject).to receive(:move).with(variant, 5, kind: 'shipped', cause: cause, persist: true)
+ expect(subject).to receive(:move).with(variant, 5, kind: 'shipped', cause: cause, persist: true, force: false)
subject.unstock(variant, 5, cause)
end
+ it 'passes force through so a merchant can record a departure the shelf cannot cover' do
+ cause = double
+ expect(subject).to receive(:move).with(variant, 5, kind: 'shipped', cause: cause, persist: true, force: true)
+ subject.unstock(variant, 5, cause, force: true)
+ end
+
it 'allocates a variant to a fulfillment' do
fulfillment = double
expect(subject).to receive(:move).with(variant, 2, kind: 'allocated', cause: fulfillment)
diff --git a/spree/core/spec/models/spree/stock_movement_spec.rb b/spree/core/spec/models/spree/stock_movement_spec.rb
index 64b2159884..9c6f10af8a 100644
--- a/spree/core/spec/models/spree/stock_movement_spec.rb
+++ b/spree/core/spec/models/spree/stock_movement_spec.rb
@@ -255,13 +255,35 @@ describe Spree::StockMovement, type: :model do
expect(stock_level.available_count).to eq(5)
end
- it 'may leave the shelf negative' do
+ # A parcel that physically left has to be recordable whatever the
+ # ledger says — the shelf then reads as goods Spree never saw arrive.
+ it 'may leave the shelf negative when the caller forced it' do
stock_level.update_column(:count_on_hand, 0)
- create(:stock_movement, kind: 'shipped', quantity: 1, stock_level: stock_level)
+ create(:stock_movement, kind: 'shipped', quantity: 1, stock_level: stock_level, force: true)
expect(stock_level.reload.count_on_hand).to eq(-1)
end
+
+ # Nobody may send goods a warehouse does not have without saying so —
+ # a stock transfer least of all.
+ it 'refuses to leave the shelf negative otherwise' do
+ stock_level.update_column(:count_on_hand, 0)
+
+ expect {
+ create(:stock_movement, kind: 'shipped', quantity: 1, stock_level: stock_level)
+ }.to raise_error(ActiveRecord::RecordInvalid)
+
+ expect(stock_level.reload.count_on_hand).to eq(0)
+ end
+
+ # An instruction about one write, not a property of the row — so a
+ # movement read back later carries no trace of it.
+ it 'does not persist the force instruction' do
+ movement = create(:stock_movement, kind: 'shipped', quantity: 1, stock_level: stock_level, force: true)
+
+ expect(described_class.find(movement.id).force).to be_nil
+ end
end
context 'adjusted' do
diff --git a/spree/core/spec/models/spree/stock_transfer_spec.rb b/spree/core/spec/models/spree/stock_transfer_spec.rb
index 2e72ea0a5a..b0afbedff1 100644
--- a/spree/core/spec/models/spree/stock_transfer_spec.rb
+++ b/spree/core/spec/models/spree/stock_transfer_spec.rb
@@ -67,6 +67,25 @@ module Spree
expect(stock_transfer.source_movements.first.stock_transfer).to eq(stock_transfer)
end
+ # Checking only for a positive balance let a transfer take more than the
+ # shelf held and leave it negative.
+ context 'when the source holds some of the variant but not enough' do
+ let(:variants) { { variant => 5 } }
+
+ before { source_location.stock_level_or_create(variant).update_column(:count_on_hand, 1) }
+
+ it 'does not transfer the variants' do
+ expect(subject).to be false
+ expect(stock_transfer.errors[:base]).to include(Spree.t('stock_transfer.errors.variants_unavailable'))
+ end
+
+ it 'leaves the source shelf alone' do
+ subject
+
+ expect(source_location.stock_level(variant).reload.count_on_hand).to eq(1)
+ end
+ end
+
context 'when variants are not available in the source location' do
let(:variants) { { variant => 5, other_variant => 5 } }
let(:other_variant) { create(:variant) }
diff --git a/spree/core/spec/workflows/spree/fulfillments/fulfill_spec.rb b/spree/core/spec/workflows/spree/fulfillments/fulfill_spec.rb
index 4e243de903..59287bfca4 100644
--- a/spree/core/spec/workflows/spree/fulfillments/fulfill_spec.rb
+++ b/spree/core/spec/workflows/spree/fulfillments/fulfill_spec.rb
@@ -9,6 +9,14 @@ module Spree
let(:fulfillment) { order.fulfillments.first }
let(:line_items) { order.line_items.sort_by(&:id) }
+ # A placed order's goods were on the shelf when it was placed; the
+ # order_ready_to_ship factory never puts them there.
+ def stock_the_shelf(fulfillment, count: 10)
+ fulfillment.manifest.each do |item|
+ fulfillment.stock_location.stock_level_or_create(item.variant).update_column(:count_on_hand, count)
+ end
+ end
+
describe 'fulfilling everything (items omitted)' do
let(:execute) { subject.call(fulfillment: fulfillment) }
@@ -97,6 +105,7 @@ module Spree
# Shipping a canceled fulfillment is deliberate — the goods went out
# anyway — so the workflow must not second-guess it.
it 'fulfills a canceled fulfillment' do
+ stock_the_shelf(fulfillment)
fulfillment.update!(status: 'canceled')
result = subject.call(fulfillment: fulfillment)
@@ -154,6 +163,7 @@ module Spree
before { fulfillment.update!(status: 'canceled') }
it 're-promises the units and then ships them' do
+ stock_the_shelf(fulfillment)
variant = fulfillment.fulfillment_items.first.variant
stock_level = fulfillment.stock_location.stock_level(variant)
@@ -167,6 +177,7 @@ module Spree
# Resuming already re-promised the units, so fulfilling afterwards
# takes them off the shelf exactly once rather than twice.
it 'ships the re-promised units only once when the fulfillment was resumed' do
+ stock_the_shelf(fulfillment)
Spree.fulfillment_resume_workflow.call(fulfillment: fulfillment)
variant = fulfillment.fulfillment_items.first.variant
quantity = fulfillment.fulfillment_items.where(variant_id: variant.id).sum(:quantity)
@@ -185,6 +196,7 @@ module Spree
let(:stock_level) { fulfillment.stock_location.stock_level(variant) }
it 'ships only the units the fulfillment holds a promise for' do
+ stock_the_shelf(fulfillment)
fulfillment.stock_location.allocate(variant, quantity, fulfillment)
expect { subject.call(fulfillment: fulfillment) }.
@@ -200,6 +212,19 @@ module Spree
not_to change { stock_level.reload.count_on_hand }
end
+ # The merchant has to say which it is: a ledger error to correct, or
+ # goods the warehouse knows it does not have.
+ it 'refuses a dispatch the shelf cannot cover' do
+ fulfillment.stock_location.allocate(variant, quantity, fulfillment)
+
+ result = subject.call(fulfillment: fulfillment)
+
+ expect(result.success?).to eq(false)
+ expect(result.error.to_s).to match(/only 0 are on hand/)
+ expect(fulfillment.reload).not_to be_fulfilled
+ expect(stock_level.reload.count_on_hand).to eq(0)
+ end
+
# Departure is a physical fact: a forced dispatch records the parcel even
# when the shelf says the goods were never there. Availability is
# untouched on the way through — the promise had already been subtracted.