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
Promise the stock an exchange or claim sends as a replacement
Łukasz Adamczyk3409df4bdd44
diff --git a/spree/core/app/workflows/spree/claims/resolve.rb b/spree/core/app/workflows/spree/claims/resolve.rbindex fd28f09cf2..a72f45a85c 100644--- a/spree/core/app/workflows/spree/claims/resolve.rb+++ b/spree/core/app/workflows/spree/claims/resolve.rb@@ -113,7 +113,18 @@ module Spreeclaim.order.fulfillments += @fulfillmentsclaim.order.save!- @fulfillments.each { |fulfillment| fulfillment.update!(claim.order) }+ @fulfillments.each { |fulfillment| allocate_replacement_stock(fulfillment) }+ end++ # See Spree::Exchanges::Fulfill — a replacement that holds no promise+ # ships without writing a movement, so the shelf never follows the goods.+ def allocate_replacement_stock(fulfillment)+ fulfillment.manifest.each do |item|+ next unless item.variant.track_inventory?+ next unless item.quantity.positive?++ fulfillment.stock_location.allocate(item.variant, item.quantity, fulfillment)+ endenddef issue_store_creditdiff --git a/spree/core/app/workflows/spree/exchanges/fulfill.rb b/spree/core/app/workflows/spree/exchanges/fulfill.rbindex 043ed479f0..9bb42e33d4 100644--- a/spree/core/app/workflows/spree/exchanges/fulfill.rb+++ b/spree/core/app/workflows/spree/exchanges/fulfill.rb@@ -92,7 +92,21 @@ module Spreeexchange.order.fulfillments += @fulfillmentsexchange.order.save!- @fulfillments.each { |fulfillment| fulfillment.update!(exchange.order) }+ @fulfillments.each { |fulfillment| allocate_replacement_stock(fulfillment) }+ end++ # The replacement is promised the moment it exists, exactly as placement+ # promises an order's own fulfillments. Without this the fulfillment holds+ # nothing, and dispatch then writes no movement at all — an unallocated+ # fulfillment is indistinguishable from one created before typed+ # movements, so the goods would leave the shelf untouched and unrecorded.+ def allocate_replacement_stock(fulfillment)+ fulfillment.manifest.each do |item|+ next unless item.variant.track_inventory?+ next unless item.quantity.positive?++ fulfillment.stock_location.allocate(item.variant, item.quantity, fulfillment)+ endenddef issue_store_creditdiff --git a/spree/core/spec/workflows/spree/claims/lifecycle_spec.rb b/spree/core/spec/workflows/spree/claims/lifecycle_spec.rbindex f90485b66e..984a82556e 100644--- a/spree/core/spec/workflows/spree/claims/lifecycle_spec.rb+++ b/spree/core/spec/workflows/spree/claims/lifecycle_spec.rb@@ -85,6 +85,22 @@ RSpec.describe 'Spree::Claims workflows' doexpect(result).to be_successend+ # Unallocated, the replacement is indistinguishable from a fulfillment made+ # before typed movements: dispatch writes nothing and the goods leave the+ # shelf untouched.+ it 'promises the replacement stock so dispatch has something to convert' do+ replacing = create(:approved_claim, store: store, order: order, send_replacement: true)+ replacing.claim_line_items.each { |line| (line.replacement_variant || line.variant).stock_levels.first&.set_count_on_hand(10) }+ # A replacement often reuses the same variant, so the new fulfillment is+ # identified by being new — not by what it carries.+ existing_ids = replacing.order.fulfillments.ids++ result = Spree::Claims::Resolve.call(claim: replacing, resolution: 'replacement')+ replacement = result.value.order.fulfillments.reload.find { |f| existing_ids.exclude?(f.id) }++ expect(replacement.stock_movements.allocated.sum(:quantity)).to be_positive+ end+it 'refunds to store credit' doresult = Spree::Claims::Resolve.call(claim: claim, resolution: 'refund')diff --git a/spree/core/spec/workflows/spree/exchanges/lifecycle_spec.rb b/spree/core/spec/workflows/spree/exchanges/lifecycle_spec.rbindex 75265fbac3..b96903d920 100644--- a/spree/core/spec/workflows/spree/exchanges/lifecycle_spec.rb+++ b/spree/core/spec/workflows/spree/exchanges/lifecycle_spec.rb@@ -106,6 +106,20 @@ RSpec.describe 'Spree::Exchanges workflows' doexpect(result.value).to be_fulfilledend+ # An unallocated replacement looks exactly like a fulfillment created before+ # typed movements, so dispatch ships it without writing anything and the+ # goods leave the shelf untouched.+ it 'promises the replacement stock so dispatch has something to convert' do+ line = exchange.exchange_line_items.first+ line.new_variant.stock_levels.first&.set_count_on_hand(10)++ result = Spree::Exchanges::Fulfill.call(exchange: exchange)+ replacement = result.value.order.fulfillments.detect { |f| f.variants.include?(line.new_variant) }++ expect(replacement.allocated_quantities[line.new_variant.id]).to eq(1)+ expect(replacement.stock_movements.allocated.sum(:quantity)).to eq(1)+ end+# The guard only matters when money moves: a cheaper replacement owes the# customer the difference, and an unrecognised method would otherwise fall# through to the gateway instead of store credit.