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.

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.rb
index 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 }
end
else
+ reject_repeated_lines
Array(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.rb
index 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 Spree
delta: line.outstanding }
end
else
+ reject_repeated_lines
Array(items).map { |item| normalize_item(item) }
end
@@ -93,6 +94,21 @@ module Spree
normalized
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(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.yml
index 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 edited
not_ordered: Only a purchase order that has been placed can be received
only_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: Qty
quantity: Quantity
query: Query
@@ -1898,6 +1899,7 @@ en:
not_in_transit: Only a transfer in transit can be received
not_shippable: Only a draft or ready-to-ship transfer can be marked in transit
only_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 location
variants_unavailable: Some variants are not available on %{stock}
stop: Stop
diff --git a/spree/core/spec/workflows/spree/purchase_orders/lifecycle_spec.rb b/spree/core/spec/workflows/spree/purchase_orders/lifecycle_spec.rb
index 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' do
stale_line = purchase_order.reload.items.sole
Spree::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.rb
index 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 do
expect(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' do
other_item = create(:stock_transfer, store: store).items.first