Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions core/app/models/spree/stock/availability_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ def is_valid?(line_item)
if line_item.inventory_units.empty?
Spree::Config.stock.quantifier_class.new(line_item.variant).can_supply?(line_item.quantity)
else
quantity_by_stock_location_id = line_item.inventory_units.pending.joins(:shipment).group(:stock_location_id).count
quantity_by_stock_location_id.all? do |stock_location_id, quantity|
Spree::Config.stock.quantifier_class.new(line_item.variant, stock_location_id).can_supply?(quantity)
quantity_by_variant_and_stock_location = line_item.inventory_units.pending.joins(:shipment)
.group(:variant_id, :stock_location_id).count
variant_ids = quantity_by_variant_and_stock_location.keys.map(&:first)
variants = Spree::Variant.with_discarded.where(id: variant_ids).index_by(&:id)

quantity_by_variant_and_stock_location.all? do |(variant_id, stock_location_id), quantity|
Spree::Config.stock.quantifier_class.new(variants.fetch(variant_id), stock_location_id).can_supply?(quantity)
end
end
end
Expand Down
36 changes: 36 additions & 0 deletions core/spec/models/spree/stock/availability_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,42 @@ module Stock
include_examples "passes validation"
end
end

context "when an inventory unit has a different variant than the line item" do
let(:line_item) { order.line_items.first }
let(:other_variant) { create(:variant) }

before do
create(:inventory_unit, variant: other_variant, line_item:, shipment: order.shipments.first)
line_item.variant.stock_items.update_all(count_on_hand: 10, backorderable: false)
end

context "and the other variant has no stock" do
before do
other_variant.stock_items.update_all(count_on_hand: 0, backorderable: false)
end

include_examples "fails validation"
end

context "and the other variant has stock" do
before do
other_variant.stock_items.update_all(count_on_hand: 1, backorderable: false)
end

include_examples "passes validation"
end

context "and only the other variant's units are pending" do
before do
line_item.inventory_units.where(variant: line_item.variant).update_all(pending: false)
line_item.variant.stock_items.update_all(count_on_hand: 0, backorderable: false)
other_variant.stock_items.update_all(count_on_hand: 1, backorderable: false)
end

include_examples "passes validation"
end
end
end

context "line_item is split across two shipments" do
Expand Down
Loading