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
Refuse a receive that lists the same line twice
Łukasz Adamczyk5c756a448a64
diff --git a/spree/core/app/workflows/spree/purchase_orders/receive.rb b/spree/core/app/workflows/spree/purchase_orders/receive.rbindex 98866c053a..8b030b61e5 100644--- a/spree/core/app/workflows/spree/purchase_orders/receive.rb+++ b/spree/core/app/workflows/spree/purchase_orders/receive.rb@@ -63,6 +63,7 @@ module Spree{ item: line, quantity_received: line.quantity_ordered, delta: line.outstanding }endelse+ reject_repeated_linesArray(items).map { |item| normalize_item(item) }end@@ -87,6 +88,21 @@ module Spree{ item: line, quantity_received: quantity, delta: quantity - line.quantity_received.to_i }end+ # Every delta is measured against the line's running total before any of+ # them is written, so a line named twice contributes its delta once per+ # entry: entries of four and six against a fresh line put ten on the+ # shelf while the line records six. Refused rather than merged, because+ # those two entries are genuinely ambiguous — ten in total, or six+ # correcting four? A scanner that appends an entry per carton has to send+ # the total instead.+ def reject_repeated_lines+ lines = Array(items).map { |item| item[:item] }.compact+ repeated = lines.group_by(&:id).values.find { |group| group.length > 1 }+ return if repeated.nil?++ failure(purchase_order, Spree.t('purchase_order.errors.repeated_item', variant: repeated.first.variant_name))+ end+def record_receipt@normalized_items.each do |item|item[:item].update!(quantity_received: item[:quantity_received])diff --git a/spree/core/app/workflows/spree/stock_transfers/receive.rb b/spree/core/app/workflows/spree/stock_transfers/receive.rbindex 43d37251c0..f409be32e1 100644--- a/spree/core/app/workflows/spree/stock_transfers/receive.rb+++ b/spree/core/app/workflows/spree/stock_transfers/receive.rb@@ -60,6 +60,7 @@ module Spreedelta: line.outstanding }endelse+ reject_repeated_linesArray(items).map { |item| normalize_item(item) }end@@ -93,6 +94,21 @@ module Spreenormalizedend+ # Every delta is measured against the line's running total before any of+ # them is written, so a line named twice contributes its delta once per+ # entry: entries of four and six against a fresh line put ten on the+ # shelf while the line records six. Refused rather than merged, because+ # those two entries are genuinely ambiguous — ten in total, or six+ # correcting four? A scanner that appends an entry per carton has to send+ # the total instead.+ def reject_repeated_lines+ lines = Array(items).map { |item| item[:item] }.compact+ repeated = lines.group_by(&:id).values.find { |group| group.length > 1 }+ return if repeated.nil?++ failure(stock_transfer, Spree.t('stock_transfer.errors.repeated_item', variant: repeated.first.variant_name))+ end+def record_receipt@normalized_items.each do |item|attributes = { quantity_received: item[:quantity_received] }diff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.ymlindex f7e146d41f..95f6c3366b 100644--- a/spree/core/config/locales/en.yml+++ b/spree/core/config/locales/en.yml@@ -1628,6 +1628,7 @@ en:not_editable: Only a draft purchase order can be editednot_ordered: Only a purchase order that has been placed can be receivedonly_draft_can_be_deleted: Only a draft purchase order can be deleted — cancel it instead+ repeated_item: "%{variant} was listed more than once. Send one entry per line, with the total received so far"qty: Qtyquantity: Quantityquery: Query@@ -1898,6 +1899,7 @@ en:not_in_transit: Only a transfer in transit can be receivednot_shippable: Only a draft or ready-to-ship transfer can be marked in transitonly_draft_can_be_deleted: Only a draft transfer can be deleted — cancel it instead+ repeated_item: "%{variant} was listed more than once. Send one entry per line, with the total received so far"same_location: cannot be the same as the destination locationvariants_unavailable: Some variants are not available on %{stock}stop: Stopdiff --git a/spree/core/spec/workflows/spree/purchase_orders/lifecycle_spec.rb b/spree/core/spec/workflows/spree/purchase_orders/lifecycle_spec.rbindex fd6ee691d4..acf5917310 100644--- a/spree/core/spec/workflows/spree/purchase_orders/lifecycle_spec.rb+++ b/spree/core/spec/workflows/spree/purchase_orders/lifecycle_spec.rb@@ -108,6 +108,25 @@ describe 'purchase order lifecycle', type: :model do# 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.+ # Every delta is measured before any of them is written, so a line named+ # twice would contribute its delta once per entry — the shape a scanner+ # app produces when it appends an entry per pallet.+ it 'refuses a payload that names the same line twice' do+ item = purchase_order.reload.items.sole++ result = Spree::PurchaseOrders::Receive.call(+ purchase_order: purchase_order,+ items: [{ item: item, quantity_received: 40 }, { item: item, quantity_received: 60 }]+ )++ expect(result).to be_failure+ expect(result.error.to_s).to eq(+ Spree.t('purchase_order.errors.repeated_item', variant: item.variant_name)+ )+ expect(on_hand).to eq(0)+ expect(item.reload.quantity_received).to eq(0)+ end+it 'ignores a running total that was read before another receive committed' dostale_line = purchase_order.reload.items.soleSpree::PurchaseOrders::Receive.call(purchase_order: purchase_order,diff --git a/spree/core/spec/workflows/spree/stock_transfers/lifecycle_spec.rb b/spree/core/spec/workflows/spree/stock_transfers/lifecycle_spec.rbindex 305abf3bfd..81832fddc0 100644--- a/spree/core/spec/workflows/spree/stock_transfers/lifecycle_spec.rb+++ b/spree/core/spec/workflows/spree/stock_transfers/lifecycle_spec.rb@@ -237,6 +237,25 @@ describe 'stock transfer lifecycle', type: :model doexpect(destination_on_hand).to eq(6)end+ # Every delta is measured before any of them is written, so a line named+ # twice would contribute its delta once per entry — the shape a scanner+ # app produces when it appends an entry per carton.+ it 'refuses a payload that names the same line twice' do+ item = transfer.reload.items.sole++ result = Spree::StockTransfers::Receive.call(+ stock_transfer: transfer,+ items: [{ item: item, quantity_received: 4 }, { item: item, quantity_received: 6 }]+ )++ expect(result).to be_failure+ expect(result.error.to_s).to eq(+ Spree.t('stock_transfer.errors.repeated_item', variant: item.variant_name)+ )+ expect(destination_on_hand).to eq(0)+ expect(item.reload.quantity_received).to eq(0)+ end+it 'refuses a line belonging to another transfer' doother_item = create(:stock_transfer, store: store).items.first