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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Type an open fulfillment's placement row as its allocation
Łukasz Adamczykf7292af3e59b
diff --git a/docs/plans/6.0-typed-stock-movements.md b/docs/plans/6.0-typed-stock-movements.mdindex dc9960ec8d..b8aa396a87 100644--- a/docs/plans/6.0-typed-stock-movements.md+++ b/docs/plans/6.0-typed-stock-movements.md@@ -430,7 +430,8 @@ Two jobs in one idempotent task, added to the 5.6 → 6.0 manifest.| Legacy row | Becomes ||---|---|-| `Spree::Fulfillment` (or `Spree::Shipment`) originator, negative quantity | `shipped`, `fulfillment_id` + the fulfillment's `order_id` |+| `Spree::Fulfillment` (or `Spree::Shipment`) originator, negative quantity, fulfillment **closed** | `shipped`, `fulfillment_id` + the fulfillment's `order_id` |+| `Spree::Fulfillment` (or `Spree::Shipment`) originator, negative quantity, fulfillment still **open** | `allocated`, same FKs — and the counters move with it (see below) || `Spree::Fulfillment` originator, positive quantity | `released`, same FKs (a cancellation or relocation restock) || `Spree::ReturnAuthorization` originator | `received`, `return_id` or `exchange_id` || `Spree::StockTransfer` originator, positive quantity | `received`, `stock_transfer_id` |@@ -439,7 +440,11 @@ Two jobs in one idempotent task, added to the 5.6 → 6.0 manifest.Ordering is load-bearing in two directions. The task runs **after** `migrate_shipping_to_delivery`, which rewrites `'Spree::Shipment'` originator strings, and **after** `migrate_returns`, because a legacy `ReturnAuthorization` id says nothing about the new record: the returns migrator preserves the `number`, not the id, and can turn one authorization into either a `Return` or an `Exchange`. Resolving `return_id`/`exchange_id` therefore means reading the legacy authorization table by name (the same anonymous-reader pattern `Spree::ReturnsMigrator` uses) and matching on `number`. Rows whose cause cannot be resolved are typed by sign and left with null FKs rather than skipped — a movement with no cause is still a true statement about stock.-**Reconcile open fulfillments.** Under the old model, an unfulfilled fulfillment on a placed order has already taken its units off the shelf. Under the new one, those units are on the shelf and allocated. For every open fulfillment on a placed order the task therefore adds its quantity back to `count_on_hand`, adds the same quantity to `allocated_count`, and writes the matching `allocated` movement so the jump has a visible cause. Availability is unchanged by construction — both sides of `count_on_hand - allocated_count` move together — and the write is deliberately a direct column update rather than a `received` movement, because no goods arrived.+**Whether a fulfillment had shipped decides how its history reads** (2026-08-19, after Damian's review of the implementation). A closed fulfillment's departure was a departure. An open one's departure was the promise it still holds — under the old model, taking the units off the shelf at placement *was* the allocation — so it types as `allocated`. Typing it `shipped` instead is not a cosmetic mislabel: `allocated_quantities` subtracts `shipped` from `allocated`, so the reconciliation below would net to zero for every open fulfillment, leaving it reporting no promise at all. The consequences are permanent and silent in both directions — dispatch never takes the units off the shelf, and cancelling never returns them to availability, stranding real stock at zero.++Because the typing is a `update_all` relabel that skips callbacks, an open fulfillment's row carries the counters itself: `count_on_hand` and `allocated_count` each move by the row's negated legacy quantity, applied as database-side increments. Both counters moving together is what leaves availability unchanged, and doing it per row rather than per fulfillment is what makes a partially restocked fulfillment come out right — legacy rows conserve `unstock − restock = held quantity`, so the sum lands on the quantity still held.++**Reconcile open fulfillments.** That leaves reconciliation holding only the open fulfillments that recorded no movement at all: for each it adds the quantity back to `count_on_hand`, adds the same to `allocated_count`, and writes the matching `allocated` movement so the jump has a visible cause. Availability is unchanged by construction here too, and the write is deliberately a direct column update rather than a `received` movement, because no goods arrived. Fulfillments whose rows were typed above already hold an allocation, so the existing idempotency predicate skips them.Backordered units need no special case: the pair of increments cancels out the negative on-hand that used to represent them, which is precisely the representation change this plan makes.diff --git a/spree/core/lib/tasks/typed_stock_movements_migration.rake b/spree/core/lib/tasks/typed_stock_movements_migration.rakeindex 471b8a908a..a345651564 100644--- a/spree/core/lib/tasks/typed_stock_movements_migration.rake+++ b/spree/core/lib/tasks/typed_stock_movements_migration.rake@@ -14,6 +14,13 @@ module Spree# makes the deploy window safe: a fulfillment shipped before the task runs# never gets an allocation, and dispatch only ships allocated units.#+ # Whether a fulfillment had shipped decides how its history reads. A closed+ # one's departure was a departure. An open one's was the promise it still+ # holds, so it types as the allocation and carries the counters with it —+ # which also means reconciliation finds an allocation already there and+ # leaves it alone. Reconciliation is left holding the open fulfillments that+ # recorded no movement at all.+ ## Runs after spree:migrate_shipping_to_delivery, which rewrites the# 'Spree::Shipment' originator strings, and after# spree:upgrade:migrate_returns, because a legacy authorization id says@@ -45,12 +52,50 @@ module Spreedef type_historySpree::StockMovement.unscoped.where(kind: nil).find_each(batch_size: batch_size) do |movement|+ # Read before the update rewrites it: the legacy sign is what says which+ # way the stock went, and a typed row keeps only the magnitude.+ legacy_quantity = movement.quantity.to_iattributes = attributes_for(movement)- Spree::StockMovement.unscoped.where(id: movement.id).update_all(attributes.merge(updated_at: Time.current))++ ActiveRecord::Base.transaction do+ Spree::StockMovement.unscoped.where(id: movement.id).update_all(attributes.merge(updated_at: Time.current))+ carry_promise(movement.stock_level_id, legacy_quantity) if open_fulfillment_row?(attributes)+ end+@typed += 1endend+ def open_fulfillment_row?(attributes)+ attributes[:fulfillment_id].present? && open_fulfillment_ids.include?(attributes[:fulfillment_id])+ end++ # Turning an open fulfillment's placement row into its allocation has to+ # move the counters with it, because the relabelling above skips callbacks.+ # Legacy placement took the units off the shelf; under the allocation model+ # they sit on the shelf and are promised, so both counters move by the same+ # amount and availability comes out unchanged — which is what makes a+ # pre-upgrade fulfillment safe to ship or cancel afterwards.+ def carry_promise(stock_level_id, legacy_quantity)+ delta = -legacy_quantity+ return if delta.zero?++ Spree::StockLevel.update_counters(+ stock_level_id,+ count_on_hand: delta,+ allocated_count: delta,+ touch: true+ )+ end++ # Fulfillments still open when the upgrade runs. Read once: every+ # fulfillment-originated row asks about them, and the set does not change+ # while the task runs — typing and reconciliation both leave the status+ # alone.+ def open_fulfillment_ids+ @open_fulfillment_ids ||= open_fulfillments.pluck(:id).to_set+ end+def attributes_for(movement)return { kind: 'adjusted', reason: LEGACY_ADJUSTMENT_REASON } if movement.originator_type.blank?@@ -65,11 +110,19 @@ module Spree# A departure, or the restock of a cancellation or relocation, carrying# the order the fulfillment belonged to so "which order was this for?"# costs no join.+ #+ # A fulfillment still open when the upgrade runs never shipped: the+ # departure it recorded at placement was the promise, and a promise is an+ # allocation now — typing it 'shipped' would retire an allocation the+ # fulfillment still holds, leaving it unable to ship or be cancelled. One+ # that has already gone out keeps the departure.def fulfillment_attributes(movement)fulfillment = Spree::Fulfillment.find_by(id: movement.originator_id)return by_sign(movement) if fulfillment.nil?- by_sign(movement, negative: 'shipped', positive: 'released').+ departure = open_fulfillment_ids.include?(fulfillment.id) ? 'allocated' : 'shipped'++ by_sign(movement, negative: departure, positive: 'released').merge(fulfillment_id: fulfillment.id, order_id: fulfillment.order_id)enddiff --git a/spree/core/spec/lib/tasks/six_zero_data_migrations_spec.rb b/spree/core/spec/lib/tasks/six_zero_data_migrations_spec.rbindex ee9e494af0..a1f27760a6 100644--- a/spree/core/spec/lib/tasks/six_zero_data_migrations_spec.rb+++ b/spree/core/spec/lib/tasks/six_zero_data_migrations_spec.rb@@ -683,9 +683,17 @@ describe '6.0 data migration tasks' docreate(:order_ready_to_ship, store: store, line_items_count: 1)end+ # A fulfillment that already went out before the upgrade. Its departure was+ # a real departure, so it types differently from an open one's.+ def shipped_order+ order = open_order+ order.fulfillments.each { |fulfillment| fulfillment.update_columns(status: 'fulfilled') }+ order+ end+describe 'typing the history' do- it 'types a fulfillment departure and carries its order' do- order = open_order+ it 'types a shipped fulfillment departure and carries its order' do+ order = shipped_orderfulfillment = order.fulfillments.firstmovement = legacy_movement(-3, originator_type: 'Spree::Fulfillment', originator_id: fulfillment.id)@@ -698,10 +706,27 @@ describe '6.0 data migration tasks' doexpect(movement.order_id).to eq(order.id)end+ # An open fulfillment never shipped, so its placement row is the promise+ # it still holds — typing it as a departure would retire that promise and+ # leave the fulfillment unable to ship or be cancelled.+ it 'types an open fulfillment departure as the allocation it was' do+ order = open_order+ fulfillment = order.fulfillments.first+ movement = legacy_movement(-3, originator_type: 'Spree::Fulfillment', originator_id: fulfillment.id)++ run_task('spree:migrate_stock_movements_to_typed_rows')++ movement.reload+ expect(movement.kind).to eq('allocated')+ expect(movement.quantity).to eq(3)+ expect(movement.fulfillment_id).to eq(fulfillment.id)+ expect(movement.order_id).to eq(order.id)+ end+# Installs whose rows were never rewritten by migrate_shipping_to_delivery# still say Spree::Shipment.it 'accepts the pre-rename originator string' do- fulfillment = open_order.fulfillments.first+ fulfillment = shipped_order.fulfillments.firstmovement = legacy_movement(-1, originator_type: 'Spree::Shipment', originator_id: fulfillment.id)run_task('spree:migrate_stock_movements_to_typed_rows')@@ -711,7 +736,7 @@ describe '6.0 data migration tasks' doendit 'types a fulfillment restock as a release' do- fulfillment = open_order.fulfillments.first+ fulfillment = shipped_order.fulfillments.firstmovement = legacy_movement(2, originator_type: 'Spree::Fulfillment', originator_id: fulfillment.id)run_task('spree:migrate_stock_movements_to_typed_rows')@@ -831,6 +856,57 @@ describe '6.0 data migration tasks' doexpect { run_task('spree:migrate_stock_movements_to_typed_rows') }.not_to change { [level.reload.count_on_hand, level.allocated_count, Spree::StockMovement.count] }end++ # The shape the two halves of this task meet in production, and the one+ # neither half was exercised against alone: a placed but unfulfilled order+ # whose units left the shelf at placement. A real 5.6 row carries no kind+ # and no allocation, so this strips what modern placement wrote and leaves+ # only the legacy departure behind.+ def legacy_placement!+ Spree::StockMovement.unscoped.where(stock_level_id: level.id).delete_all+ level.update_columns(allocated_count: 0)+ legacy_movement(-quantity, originator_type: 'Spree::Fulfillment', originator_id: fulfillment.id, level: level)+ end++ context 'when the fulfillment carries a legacy placement row' do+ it 'leaves it holding its promise, with availability unchanged' do+ legacy_placement!+ shelf_before = level.reload.count_on_hand+ available_before = level.available_count++ run_task('spree:migrate_stock_movements_to_typed_rows')++ level.reload+ expect(fulfillment.reload.allocated_quantities[reconciled_variant.id]).to eq(quantity)+ expect(level.count_on_hand).to eq(shelf_before + quantity)+ expect(level.allocated_count).to eq(quantity)+ expect(level.available_count).to eq(available_before)+ end++ # Typing the placement row as a departure used to retire the promise, so+ # dispatch found nothing to ship and the units stayed on the shelf.+ it 'can still ship its units off the shelf afterwards' do+ legacy_placement!+ run_task('spree:migrate_stock_movements_to_typed_rows')++ expect { Spree::Fulfillments::Fulfill.call(fulfillment: fulfillment.reload, notify_customer: false) }.+ to change { level.reload.count_on_hand }.by(-quantity)++ expect(level.allocated_count).to eq(0)+ end++ # And cancelling used to release nothing, stranding real stock at zero+ # availability for good.+ it 'can still be cancelled back into availability afterwards' do+ legacy_placement!+ run_task('spree:migrate_stock_movements_to_typed_rows')++ expect { Spree::Fulfillments::Cancel.call(fulfillment: fulfillment.reload, notify_provider: false) }.+ to change { level.reload.available_count }.by(quantity)++ expect(level.allocated_count).to eq(0)+ end+ endendendend