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.

Fix seven gaps found reviewing the typed stock movements work

Łukasz Adamczykbc0403a273fa
diff --git a/packages/admin-sdk/src/params.ts b/packages/admin-sdk/src/params.ts
index da9525fee6..7ff98a40f1 100644
--- a/packages/admin-sdk/src/params.ts
+++ b/packages/admin-sdk/src/params.ts
@@ -1109,6 +1109,8 @@ export interface StockLocationUpdateParams {
export interface StockLevelUpdateParams {
count_on_hand?: number
backorderable?: boolean
+ /** Labels the correction in the stock history. Defaults to "Manual adjustment". */
+ reason?: string
metadata?: Record<string, unknown>
}
diff --git a/spree/api/app/controllers/spree/api/v3/store/delivery_methods_controller.rb b/spree/api/app/controllers/spree/api/v3/store/delivery_methods_controller.rb
index 78212ae42e..316754d365 100644
--- a/spree/api/app/controllers/spree/api/v3/store/delivery_methods_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/delivery_methods_controller.rb
@@ -98,7 +98,7 @@ module Spree
next true unless line_item.variant.should_track_inventory?
stock_level = location.stock_level(line_item.variant)
- stock_level.present? && stock_level.count_on_hand >= line_item.quantity
+ stock_level.present? && stock_level.available_count >= line_item.quantity
end
end
end
diff --git a/spree/api/lib/spree/api.rb b/spree/api/lib/spree/api.rb
index 518f2e1bd9..5a8d1fbb47 100644
--- a/spree/api/lib/spree/api.rb
+++ b/spree/api/lib/spree/api.rb
@@ -51,8 +51,13 @@ module Spree
def api_dependency?(name)
return false unless defined?(Spree::Api::Dependencies)
- # Check both V3 and dynamically added V2 dependencies
- Spree::Api::Dependencies.class::INJECTION_POINTS.include?(name)
+ dependencies = Spree::Api::Dependencies.class
+
+ # Check V3, dynamically added V2, and the pre-rename names that forward
+ # to their replacement for one release.
+ dependencies::INJECTION_POINTS.include?(name) ||
+ (dependencies.const_defined?(:LEGACY_SERIALIZER_KEYS) &&
+ dependencies::LEGACY_SERIALIZER_KEYS.key?(name))
end
end
end
diff --git a/spree/api/spec/serializers/spree/api/v3/stock_movement_serializer_spec.rb b/spree/api/spec/serializers/spree/api/v3/stock_movement_serializer_spec.rb
index f29c8aefe6..e20fbe4e2c 100644
--- a/spree/api/spec/serializers/spree/api/v3/stock_movement_serializer_spec.rb
+++ b/spree/api/spec/serializers/spree/api/v3/stock_movement_serializer_spec.rb
@@ -11,10 +11,17 @@ RSpec.describe Spree::Api::V3::StockMovementSerializer do
it 'includes all expected attributes' do
expect(subject.keys).to match_array(%w[
- id quantity action originator_type originator_id stock_level_id created_at updated_at
+ id quantity kind reason stock_level_id created_at updated_at
])
end
+ # The polymorphic originator left the response with the typed rows.
+ it 'says what happened to stock' do
+ expect(subject['kind']).to eq('received')
+ expect(subject).not_to have_key('originator_type')
+ expect(subject).not_to have_key('originator_id')
+ end
+
it 'returns the prefixed id' do
expect(subject['id']).to eq(stock_movement.prefixed_id)
end
diff --git a/spree/core/app/models/spree/fulfillment.rb b/spree/core/app/models/spree/fulfillment.rb
index c2583c5fbb..b420f011a6 100644
--- a/spree/core/app/models/spree/fulfillment.rb
+++ b/spree/core/app/models/spree/fulfillment.rb
@@ -787,9 +787,10 @@ module Spree
# comes back, so on-hand and backordered units go back the same way and
# the old backordered special case is gone.
def manifest_restock(item)
- return unless item.quantity.positive?
+ quantity = [item.quantity, allocated_quantities[item.variant.id].to_i].min
+ return unless quantity.positive?
- stock_location.release(item.variant, item.quantity, self)
+ stock_location.release(item.variant, quantity, self)
end
def manifest_unstock(item)
diff --git a/spree/core/app/models/spree/order_inventory.rb b/spree/core/app/models/spree/order_inventory.rb
index c38559fb01..fbb0171467 100644
--- a/spree/core/app/models/spree/order_inventory.rb
+++ b/spree/core/app/models/spree/order_inventory.rb
@@ -149,7 +149,9 @@ module Spree
# and backordered units go back the same way now — nothing physical
# moved either way.
if order.completed? && removed_quantity.positive? && variant.should_track_inventory?
- shipment.stock_location.release(variant, removed_quantity, shipment)
+ # Only this fulfillment's own promise can be withdrawn.
+ released = [removed_quantity, shipment.allocated_quantities[variant.id].to_i].min
+ shipment.stock_location.release(variant, released, shipment) if released.positive?
end
removed_quantity
diff --git a/spree/core/app/models/spree/stock_movement.rb b/spree/core/app/models/spree/stock_movement.rb
index 598d34ce66..5e2340374c 100644
--- a/spree/core/app/models/spree/stock_movement.rb
+++ b/spree/core/app/models/spree/stock_movement.rb
@@ -43,6 +43,10 @@ module Spree
validates :reason, presence: true, if: :adjusted?
scope :recent, -> { order(created_at: :desc) }
+ KINDS.each do |movement_kind|
+ scope movement_kind, -> { where(kind: movement_kind) }
+ define_method("#{movement_kind}?") { kind == movement_kind }
+ end
# Movements for products assigned to `store`, walking
# `stock level → variant → product → store`. The table carries no store of
@@ -50,10 +54,6 @@ module Spree
scope :for_store, ->(store) {
joins(stock_level: { variant: :product }).where(spree_products: { store_id: store.id })
}
- KINDS.each do |movement_kind|
- scope movement_kind, -> { where(kind: movement_kind) }
- define_method("#{movement_kind}?") { kind == movement_kind }
- end
delegate :variant, :variant_id, to: :stock_level, allow_nil: true
delegate :product, to: :variant
@@ -109,10 +109,15 @@ module Spree
# Departure is a physical fact, so it is allowed to leave the shelf
# negative — see the plan's "shipping from an empty shelf". The stock
# level needs to know that to relax its own non-negative guard.
+ #
+ # Only a dispatch retires a promise, and only its own: a stock transfer
+ # moves goods nobody had promised, so consuming an allocation there would
+ # take a different order's units and leave the level looking more
+ # available than it is.
def apply_departure
stock_level.applying_movement_kind = 'shipped'
stock_level.adjust_count_on_hand(-quantity.abs)
- stock_level.release_allocated_count(quantity.abs)
+ stock_level.release_allocated_count(quantity.abs) if fulfillment_id.present?
ensure
stock_level.applying_movement_kind = nil
end
diff --git a/spree/core/app/models/spree/variant.rb b/spree/core/app/models/spree/variant.rb
index 01e9cc95d4..a488e9b443 100644
--- a/spree/core/app/models/spree/variant.rb
+++ b/spree/core/app/models/spree/variant.rb
@@ -543,6 +543,29 @@ module Spree
self.stock_levels = value
end
+ # The v3 API sends `stock_levels: [...]`, which the params normalizer
+ # rewrites to nested attributes — and Rails would then write count_on_hand
+ # straight onto the row. Each entry is routed through {#set_stock} instead,
+ # so a count typed in the dashboard lands in the stock history like every
+ # other change. Entries are upserted and never removed, which is what
+ # accepts_nested_attributes_for did here.
+ #
+ # @param rows [Array<Hash>, Hash]
+ # @return [void]
+ def stock_levels_attributes=(rows)
+ rows = rows.values if rows.respond_to?(:values) && !rows.is_a?(Array)
+
+ Array.wrap(rows).each do |row|
+ row = (row.respond_to?(:to_unsafe_h) ? row.to_unsafe_h : row.to_h).with_indifferent_access
+ next if row[:stock_location_id].blank? || row[:count_on_hand].blank?
+
+ stock_location = Spree::StockLocation.find_by_param(row[:stock_location_id])
+ next if stock_location.nil?
+
+ set_stock(row[:count_on_hand], row[:backorderable], stock_location)
+ end
+ end
+
# @deprecated Use +stock_levels_attributes=+; removed in 6.1.
def stock_items_attributes=(value)
Spree::Deprecation.warn('Spree::Variant#stock_items_attributes= is deprecated and will be removed in Spree 6.1. Use #stock_levels_attributes= instead.')
@@ -622,7 +645,8 @@ module Spree
def set_stock(count_on_hand, backorderable = nil, stock_location = nil)
stock_location ||= default_stock_location
stock_level = stock_levels.find_or_initialize_by(stock_location: stock_location)
- stock_level.backorderable = backorderable if backorderable.present?
+ # `unless nil?`, not `if present?` — a caller passing false means it.
+ stock_level.backorderable = backorderable unless backorderable.nil?
# Nothing is saved yet, so the count rides on the in-memory record and
# the autosave association persists it with the variant.
diff --git a/spree/core/app/workflows/spree/fulfillments/cancel.rb b/spree/core/app/workflows/spree/fulfillments/cancel.rb
index 9bbc4ed72d..4cfb8249bf 100644
--- a/spree/core/app/workflows/spree/fulfillments/cancel.rb
+++ b/spree/core/app/workflows/spree/fulfillments/cancel.rb
@@ -53,11 +53,19 @@ module Spree
# units release the same way and the old backordered special case is
# gone with the negative-on-hand representation it served.
def release_units
+ # Capped at what this fulfillment itself still holds: a fulfillment
+ # created before typed movements holds no promise, and withdrawing one
+ # it never made would take another order's units.
+ outstanding = fulfillment.allocated_quantities
+
fulfillment.manifest.each do |item|
next unless item.variant.track_inventory?
- next unless item.quantity.positive?
- stock_location.release(item.variant, item.quantity, fulfillment)
+ quantity = [item.quantity, outstanding[item.variant.id].to_i].min
+ next unless quantity.positive?
+
+ outstanding[item.variant.id] -= quantity
+ stock_location.release(item.variant, quantity, fulfillment)
end
end
diff --git a/spree/core/spec/models/spree/shipment_spec.rb b/spree/core/spec/models/spree/shipment_spec.rb
index be19176d25..ff70ffc1c2 100644
--- a/spree/core/spec/models/spree/shipment_spec.rb
+++ b/spree/core/spec/models/spree/shipment_spec.rb
@@ -659,11 +659,21 @@ describe Spree::Shipment, type: :model do
it 'releases the items through the deprecated shell' do
allow(shipment).to receive(:fulfillment_items).and_return([inventory_unit])
allow(shipment).to receive(:provider).and_return(instance_double(Spree::FulfillmentProvider::Manual, cancel_fulfillment: true))
+ # Only a promise the fulfillment actually holds can be withdrawn.
+ allow(shipment).to receive(:allocated_quantities).and_return(variant.id => 1)
shipment.stock_location = create(:stock_location)
expect(shipment.stock_location).to receive(:release).with(variant, 1, shipment)
shipment.after_cancel
end
+ it 'withdraws nothing through the shell when it holds no promise' do
+ allow(shipment).to receive(:fulfillment_items).and_return([inventory_unit])
+ allow(shipment).to receive(:provider).and_return(instance_double(Spree::FulfillmentProvider::Manual, cancel_fulfillment: true))
+ shipment.stock_location = create(:stock_location)
+ expect(shipment.stock_location).not_to receive(:release)
+ shipment.after_cancel
+ end
+
context 'with backordered inventory units' do
let(:order) { create(:order) }
let(:variant) { create(:variant) }
diff --git a/spree/core/spec/models/spree/stock_movement_spec.rb b/spree/core/spec/models/spree/stock_movement_spec.rb
index 6d6611a943..64b2159884 100644
--- a/spree/core/spec/models/spree/stock_movement_spec.rb
+++ b/spree/core/spec/models/spree/stock_movement_spec.rb
@@ -224,8 +224,9 @@ describe Spree::StockMovement, type: :model do
context 'shipped' do
it 'takes the units off the shelf and retires their allocation' do
- create(:stock_movement, kind: 'allocated', quantity: 3, stock_level: stock_level)
- create(:stock_movement, kind: 'shipped', quantity: 3, stock_level: stock_level)
+ fulfillment = create(:fulfillment, stock_location: stock_location)
+ create(:stock_movement, kind: 'allocated', quantity: 3, stock_level: stock_level, fulfillment: fulfillment)
+ create(:stock_movement, kind: 'shipped', quantity: 3, stock_level: stock_level, fulfillment: fulfillment)
stock_level.reload
expect(stock_level.count_on_hand).to eq(7)
@@ -240,6 +241,20 @@ describe Spree::StockMovement, type: :model do
expect(stock_level.allocated_count).to eq(0)
end
+ # A transfer moves goods nobody promised. Taking somebody else's
+ # allocation here would leave the level looking more available than
+ # it is.
+ it 'leaves other promises alone when the departure has no fulfillment' do
+ create(:stock_movement, kind: 'allocated', quantity: 3, stock_level: stock_level)
+
+ create(:stock_movement, kind: 'shipped', quantity: 2, stock_level: stock_level)
+ stock_level.reload
+
+ expect(stock_level.count_on_hand).to eq(8)
+ expect(stock_level.allocated_count).to eq(3)
+ expect(stock_level.available_count).to eq(5)
+ end
+
it 'may leave the shelf negative' do
stock_level.update_column(:count_on_hand, 0)
diff --git a/spree/core/spec/models/spree/stock_transfer_spec.rb b/spree/core/spec/models/spree/stock_transfer_spec.rb
index d050784e9e..2e72ea0a5a 100644
--- a/spree/core/spec/models/spree/stock_transfer_spec.rb
+++ b/spree/core/spec/models/spree/stock_transfer_spec.rb
@@ -40,6 +40,17 @@ module Spree
let(:variants) { { variant => 5 } }
+ # A transfer moves goods, not promises: units another order is waiting
+ # for stay promised at the source.
+ it 'leaves the source location\'s promises intact' do
+ level = source_location.stock_level_or_create(variant)
+ level.update_column(:allocated_count, 3)
+
+ subject
+
+ expect(level.reload.allocated_count).to eq(3)
+ end
+
it 'transfers variants between 2 locations' do
subject
diff --git a/spree/core/spec/workflows/spree/fulfillments/cancel_spec.rb b/spree/core/spec/workflows/spree/fulfillments/cancel_spec.rb
index aadd03cd8e..39f30fde48 100644
--- a/spree/core/spec/workflows/spree/fulfillments/cancel_spec.rb
+++ b/spree/core/spec/workflows/spree/fulfillments/cancel_spec.rb
@@ -30,6 +30,17 @@ module Spree
expect(stock_level.stock_movements.released.last.fulfillment).to eq(fulfillment)
end
+ # A fulfillment created before typed movements holds no promise, so there
+ # is nothing to withdraw — and withdrawing one it never made would take
+ # units another order is waiting for.
+ it 'withdraws nothing when the fulfillment holds no promise' do
+ variant = fulfillment.fulfillment_items.first.variant
+ stock_level = fulfillment.stock_location.stock_level(variant)
+ stock_level.update_column(:allocated_count, 4)
+
+ expect { execute }.not_to change { stock_level.reload.allocated_count }
+ end
+
# Fulfillment#provider builds a fresh Manual provider per call, so the
# double is injected rather than stubbed on an instance.
let(:provider) { instance_double(Spree::FulfillmentProvider::Manual, cancel_fulfillment: true) }
diff --git a/spree/lib/generators/spree/install/templates/config/initializers/spree.rb b/spree/lib/generators/spree/install/templates/config/initializers/spree.rb
index bc1da7f9d8..8478dfdf94 100644
--- a/spree/lib/generators/spree/install/templates/config/initializers/spree.rb
+++ b/spree/lib/generators/spree/install/templates/config/initializers/spree.rb
@@ -22,7 +22,7 @@ end
# Spree.queues.default = :default
# Spree.queues.events = :default # Event subscribers (Spree::Events::SubscriberJob)
# Spree.queues.variants = :default
-# Spree.queues.stock_location_stock_items = :default
+# Spree.queues.stock_location_stock_levels = :default
# Spree.queues.coupon_codes = :default
# Use a CDN host for images, eg. Cloudfront
@@ -107,7 +107,7 @@ end
# Spree.queues.reports = :default
# Spree.queues.variants = :default
# Spree.queues.taxons = :default
-# Spree.queues.stock_location_stock_items = :default
+# Spree.queues.stock_location_stock_levels = :default
# Spree.queues.coupon_codes = :default
# Spree.queues.themes = :default
# Spree.queues.addresses = :default