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
Let only one receive at a time credit a delivery
Łukasz Adamczyk521a215db6b5
diff --git a/spree/core/app/workflows/spree/purchase_orders/receive.rb b/spree/core/app/workflows/spree/purchase_orders/receive.rbindex c32e730562..98866c053a 100644--- a/spree/core/app/workflows/spree/purchase_orders/receive.rb+++ b/spree/core/app/workflows/spree/purchase_orders/receive.rb@@ -21,11 +21,22 @@ module Spreedef perform(purchase_order:, items: nil, received_by: nil)super- step :ensure_receivable- step :normalize_items- run_hooks :validate+ # One receive at a time per document. Each line's delta is measured+ # against its running total, so two callers reading the same total both+ # credit the whole delivery — three concurrent receives of ten units put+ # thirty on the shelf and leave the line saying ten. The lock also opens+ # the transaction the writes below need, and it is always taken+ # document-then-level, never the other way, so it cannot cross with the+ # lock `StockLevel#adjust_count_on_hand` takes underneath.+ purchase_order.with_lock do+ # The caller resolved these before the lock, so their running totals+ # may predate a receive that has since committed.+ items&.each { |item| item[:item]&.reload }++ step :ensure_receivable+ step :normalize_items+ run_hooks :validate- ApplicationRecord.transaction dostep :record_receiptrun_hooks :before_restockstep :restock_deltasdiff --git a/spree/core/app/workflows/spree/stock_transfers/receive.rb b/spree/core/app/workflows/spree/stock_transfers/receive.rbindex f06b9be5f8..43d37251c0 100644--- a/spree/core/app/workflows/spree/stock_transfers/receive.rb+++ b/spree/core/app/workflows/spree/stock_transfers/receive.rb@@ -17,11 +17,22 @@ module Spreedef perform(stock_transfer:, items: nil, received_by: nil)super- step :ensure_receivable- step :normalize_items- run_hooks :validate+ # One receive at a time per document. Each line's delta is measured+ # against its running total, so two callers reading the same total both+ # credit the whole delivery — three concurrent receives of ten units put+ # thirty on the shelf and leave the line saying ten. The lock also opens+ # the transaction the writes below need, and it is always taken+ # document-then-level, never the other way, so it cannot cross with the+ # lock `StockLevel#adjust_count_on_hand` takes underneath.+ stock_transfer.with_lock do+ # The caller resolved these before the lock, so their running totals+ # may predate a receive that has since committed.+ items&.each { |item| item[:item]&.reload }++ step :ensure_receivable+ step :normalize_items+ run_hooks :validate- ApplicationRecord.transaction dostep :record_receiptrun_hooks :before_restockstep :restock_deltasdiff --git a/spree/core/spec/workflows/spree/purchase_orders/lifecycle_spec.rb b/spree/core/spec/workflows/spree/purchase_orders/lifecycle_spec.rbindex 60c2703bd4..fd6ee691d4 100644--- a/spree/core/spec/workflows/spree/purchase_orders/lifecycle_spec.rb+++ b/spree/core/spec/workflows/spree/purchase_orders/lifecycle_spec.rb@@ -104,6 +104,23 @@ describe 'purchase order lifecycle', type: :model doexpect(movement.display_unit_cost.to_s).to eq('$12.50')end+ # Two receivers booking the same pallet in at once: both resolve the line,+ # both compute a delta from the total they read, and both credit the whole+ # delivery. The workflow re-reads the line under its lock, so the second+ # caller measures against what actually committed.+ it 'ignores a running total that was read before another receive committed' do+ stale_line = purchase_order.reload.items.sole+ Spree::PurchaseOrders::Receive.call(purchase_order: purchase_order,+ items: [{ item: purchase_order.items.sole, quantity_received: 60 }])++ result = Spree::PurchaseOrders::Receive.call(purchase_order: purchase_order.reload,+ items: [{ item: stale_line, quantity_received: 60 }])++ expect(result).to be_failure+ expect(result.error.to_s).to eq(Spree.t('purchase_order.errors.no_items_received'))+ expect(on_hand).to eq(60)+ end+it 'stays open when the supplier under-ships' doitem = purchase_order.reload.items.solediff --git a/spree/core/spec/workflows/spree/stock_transfers/lifecycle_spec.rb b/spree/core/spec/workflows/spree/stock_transfers/lifecycle_spec.rbindex 4cfe0b22cd..305abf3bfd 100644--- a/spree/core/spec/workflows/spree/stock_transfers/lifecycle_spec.rb+++ b/spree/core/spec/workflows/spree/stock_transfers/lifecycle_spec.rb@@ -219,6 +219,24 @@ describe 'stock transfer lifecycle', type: :model doexpect(destination_on_hand).to eq(8)end+ # Two operators receiving the same box at once, which the destination's+ # tablet makes as easy as a double-tap: both resolve the line, both compute+ # a delta from the total they read, and both credit the whole delivery. The+ # workflow re-reads the line under its lock, so the second caller measures+ # against what actually committed and finds nothing left to receive.+ it 'ignores a running total that was read before another receive committed' do+ stale_line = transfer.reload.items.sole+ Spree::StockTransfers::Receive.call(stock_transfer: transfer,+ items: [{ item: transfer.items.sole, quantity_received: 6 }])++ result = Spree::StockTransfers::Receive.call(stock_transfer: transfer.reload,+ items: [{ item: stale_line, quantity_received: 6 }])++ expect(result).to be_failure+ expect(result.error.to_s).to eq(Spree.t('stock_transfer.errors.no_items_received'))+ expect(destination_on_hand).to eq(6)+ end+it 'refuses a line belonging to another transfer' doother_item = create(:stock_transfer, store: store).items.first