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.

Phase P2.3: Subtract allocations everywhere availability is read

Łukasz Adamczykb9bdd4953a1f
diff --git a/spree/core/app/models/concerns/spree/product_scopes.rb b/spree/core/app/models/concerns/spree/product_scopes.rb
index 3a818c391f..75b45e2585 100644
--- a/spree/core/app/models/concerns/spree/product_scopes.rb
+++ b/spree/core/app/models/concerns/spree/product_scopes.rb
@@ -60,7 +60,7 @@ module Spree
# names (to pair with join_variants_and_stock_levels).
scope :in_stock_or_backorderable_condition, -> {
where(
- "#{Variant.table_name}.track_inventory = ? OR #{StockLevel.table_name}.count_on_hand > ? OR #{StockLevel.table_name}.backorderable = ?",
+ "#{Variant.table_name}.track_inventory = ? OR (#{StockLevel.table_name}.count_on_hand - #{StockLevel.table_name}.allocated_count) > ? OR #{StockLevel.table_name}.backorderable = ?",
false, 0, true
)
}
diff --git a/spree/core/app/models/spree/fulfillment_changer.rb b/spree/core/app/models/spree/fulfillment_changer.rb
index 48ecca19d1..4640a740b8 100644
--- a/spree/core/app/models/spree/fulfillment_changer.rb
+++ b/spree/core/app/models/spree/fulfillment_changer.rb
@@ -16,7 +16,7 @@ module Spree
@variant = params[:variant]
@quantity = params[:quantity]
@available_quantity = [
- desired_stock_location.try(:count_on_hand, variant).to_i,
+ desired_stock_location&.stock_level(variant)&.available_count.to_i,
current_quantity
].max
end
diff --git a/spree/core/app/models/spree/order.rb b/spree/core/app/models/spree/order.rb
index d028f9f893..d2728175d1 100644
--- a/spree/core/app/models/spree/order.rb
+++ b/spree/core/app/models/spree/order.rb
@@ -587,7 +587,8 @@ module Spree
variants.
where(track_inventory: true).
joins(:stock_levels, :product).
- where(Spree::StockLevel.table_name => { count_on_hand: ..0, backorderable: true })
+ where(Spree::StockLevel.table_name => { backorderable: true }).
+ where("#{Spree::StockLevel.table_name}.count_on_hand - #{Spree::StockLevel.table_name}.allocated_count <= ?", 0)
end
def can_ship?
diff --git a/spree/core/app/models/spree/order_routing/rules/minimize_splits.rb b/spree/core/app/models/spree/order_routing/rules/minimize_splits.rb
index b13f819bf1..d1a84646f7 100644
--- a/spree/core/app/models/spree/order_routing/rules/minimize_splits.rb
+++ b/spree/core/app/models/spree/order_routing/rules/minimize_splits.rb
@@ -36,8 +36,8 @@ module Spree
Spree::StockLevel
.where(stock_location_id: locations.map(&:id), variant_id: variant_ids)
- .pluck(:stock_location_id, :variant_id, :count_on_hand)
- .each_with_object({}) { |(loc_id, var_id, count), h| h[[loc_id, var_id]] = count }
+ .pluck(:stock_location_id, :variant_id, :count_on_hand, :allocated_count)
+ .each_with_object({}) { |(loc_id, var_id, on_hand, allocated), h| h[[loc_id, var_id]] = on_hand - allocated }
end
end
end
diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rb
index 3208296f38..0a5438f355 100644
--- a/spree/core/app/models/spree/product.rb
+++ b/spree/core/app/models/spree/product.rb
@@ -614,7 +614,7 @@ module Spree
if variants.loaded?
variants.sum(&:total_on_hand)
else
- stock_levels.loaded? ? stock_levels.sum(&:count_on_hand) : stock_levels.sum(:count_on_hand)
+ stock_levels.loaded? ? stock_levels.sum(&:available_count) : stock_levels.sum('count_on_hand - allocated_count')
end
end
end
diff --git a/spree/core/app/models/spree/stock/quantifier.rb b/spree/core/app/models/spree/stock/quantifier.rb
index df64e6a418..789b79ebc9 100644
--- a/spree/core/app/models/spree/stock/quantifier.rb
+++ b/spree/core/app/models/spree/stock/quantifier.rb
@@ -31,21 +31,14 @@ module Spree
end
# Physical pool minus already-allocated units, summed across the
- # variant's active stock items.
- #
- # In Spree 5.5 {Spree::StockLevel#allocated_count} is a Ruby shim that
- # always returns 0, so this equals +SUM(count_on_hand)+. In 6.0
- # (Typed Stock Movements) +allocated_count+ becomes a real column and
- # the SQL path subtracts it natively.
+ # variant's active stock levels.
#
# @return [Integer] units available before checkout reservations
def available_stock
if association_loaded?
stock_levels.sum(&:available_count)
- elsif self.class.allocated_count_column?
- stock_levels.sum('count_on_hand - allocated_count')
else
- stock_levels.sum(:count_on_hand)
+ stock_levels.sum('count_on_hand - allocated_count')
end
end
@@ -118,17 +111,6 @@ module Spree
@stock_levels ||= scope_to_location(variant.stock_levels)
end
- # Memoized schema check so {#available_stock} doesn't introspect the
- # column list on every call. Flips from false → true when 6.0 Typed
- # Stock Movements adds the `allocated_count` column.
- #
- # @return [Boolean]
- def self.allocated_count_column?
- return @allocated_count_column if defined?(@allocated_count_column)
-
- @allocated_count_column = Spree::StockLevel.connection.column_exists?(:spree_stock_levels, :allocated_count)
- end
-
private
# The stock location owns a store directly; otherwise the variant's
diff --git a/spree/core/app/models/spree/stock_location.rb b/spree/core/app/models/spree/stock_location.rb
index 6529e578e9..e265c3e822 100644
--- a/spree/core/app/models/spree/stock_location.rb
+++ b/spree/core/app/models/spree/stock_location.rb
@@ -195,11 +195,11 @@ module Spree
def fill_status(variant, quantity)
if item = stock_level_or_create(variant)
- if item.count_on_hand >= quantity
+ if item.available_count >= quantity
on_hand = quantity
backordered = 0
else
- on_hand = item.count_on_hand
+ on_hand = item.available_count
on_hand = 0 if on_hand < 0
# A pre-order oversells like a backorder: the shortfall becomes
# backordered inventory units (the backorder_limit cap is enforced
diff --git a/spree/core/app/models/spree/variant.rb b/spree/core/app/models/spree/variant.rb
index 38f0eb2ae1..cb94efa81b 100644
--- a/spree/core/app/models/spree/variant.rb
+++ b/spree/core/app/models/spree/variant.rb
@@ -100,7 +100,7 @@ module Spree
after_create :increment_product_variant_count
after_destroy :decrement_product_variant_count
- scope :in_stock, -> { left_joins(:stock_levels).where("#{Spree::Variant.table_name}.track_inventory = ? OR #{Spree::StockLevel.table_name}.count_on_hand > ?", false, 0) }
+ scope :in_stock, -> { left_joins(:stock_levels).where("#{Spree::Variant.table_name}.track_inventory = ? OR (#{Spree::StockLevel.table_name}.count_on_hand - #{Spree::StockLevel.table_name}.allocated_count) > ?", false, 0) }
scope :backorderable, -> { left_joins(:stock_levels).where(Spree::StockLevel.table_name => { backorderable: true }) }
scope :in_stock_or_backorderable, -> { in_stock.or(backorderable) }
diff --git a/spree/core/app/services/spree/stock_reservations/reserve.rb b/spree/core/app/services/spree/stock_reservations/reserve.rb
index 1090ef62c2..b90a043e9a 100644
--- a/spree/core/app/services/spree/stock_reservations/reserve.rb
+++ b/spree/core/app/services/spree/stock_reservations/reserve.rb
@@ -31,7 +31,7 @@ module Spree
targets.each do |line_item, stock_level|
stock_level = locked_stock_levels.fetch(stock_level.id)
- available = stock_level.count_on_hand - held.fetch(stock_level.id, 0) - this_order_used[stock_level.id]
+ available = stock_level.available_count - held.fetch(stock_level.id, 0) - this_order_used[stock_level.id]
if available < line_item.quantity
raise InsufficientStockError.new(
diff --git a/spree/core/spec/models/spree/product_spec.rb b/spree/core/spec/models/spree/product_spec.rb
index ca44674bc3..0589c07012 100644
--- a/spree/core/spec/models/spree/product_spec.rb
+++ b/spree/core/spec/models/spree/product_spec.rb
@@ -265,7 +265,7 @@ describe Spree::Product, type: :model do
let(:stock_level) { variant.stock_levels.first }
it 'doesnt raise ReadOnlyRecord error' do
- Spree::StockMovement.create!(stock_level: stock_level, quantity: 1)
+ Spree::StockMovement.create!(stock_level: stock_level, quantity: 1, kind: 'received')
expect { product.destroy }.not_to raise_error
end
end
diff --git a/spree/core/spec/models/spree/stock_level_spec.rb b/spree/core/spec/models/spree/stock_level_spec.rb
index 26a6e4bd04..e8f424fb45 100644
--- a/spree/core/spec/models/spree/stock_level_spec.rb
+++ b/spree/core/spec/models/spree/stock_level_spec.rb
@@ -267,7 +267,7 @@ describe Spree::StockLevel, type: :model do
end
context 'with stock movements' do
- before { Spree::StockMovement.create(stock_level: subject, quantity: 1) }
+ before { Spree::StockMovement.create(stock_level: subject, quantity: 1, kind: 'received') }
it 'doesnt raise ReadOnlyRecord error' do
expect { subject.destroy }.not_to raise_error