Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load stock_items with a deterministic order in OrderInventory#determine_target_shipment #5288

Merged
merged 2 commits into from
Aug 7, 2023
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: 6 additions & 4 deletions core/app/models/spree/order_inventory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ def determine_target_shipment(quantity)

potential_shipments.detect do |shipment|
shipment.include?(variant)
end || potential_shipments.detect do |shipment|
stock_item = variant.stock_items.detect { |stock_item| stock_item.stock_location == shipment.stock_location }
if stock_item
stock_item.backorderable? || stock_item.count_on_hand >= quantity
end || begin
stock_items = variant.stock_items.sort_by(&:id) # cache stock items to avoid N+1

potential_shipments.detect do |shipment|
stock_item = stock_items.detect { |stock_item| stock_item.stock_location == shipment.stock_location }
stock_item.backorderable? || stock_item.count_on_hand >= quantity if stock_item
end
end || potential_shipments.detect do |shipment|
variant.stock_location_ids.include?(shipment.stock_location_id)
Expand Down
19 changes: 13 additions & 6 deletions core/spec/models/spree/order_inventory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
end

context 'when availability should be considered' do
let(:stock_item) { variant.stock_items.last }
let(:stock_item) { variant.stock_items.max_by(&:id) }

before do
variant.stock_items.update_all backorderable: false
Expand All @@ -171,14 +171,21 @@
end

context 'when there is not enough availability at any stock location' do
before { stock_item.set_count_on_hand 0 }

it 'falls-back selecting first non-shipped shipment that leaves from same stock_location' do
shipment = subject.send(:determine_target_shipment, 1)
required_quantity = 1
shipment = subject.send(:determine_target_shipment, required_quantity)
shipment.reload

expect(shipment.shipped?).to be false
expect(shipment.inventory_units_for(variant)).to be_empty
expect(variant.stock_location_ids.include?(shipment.stock_location_id)).to be true
expect(shipment.stock_location).not_to eql stock_item.stock_location
aggregate_failures do
expect(stock_item.count_on_hand).to eq(0)
expect(stock_item.backorderable?).to eq(false)
expect(shipment.shipped?).to be false
expect(shipment.inventory_units_for(variant)).to be_empty
expect(variant.stock_location_ids.include?(shipment.stock_location_id)).to be true
expect(shipment.stock_location).not_to eql stock_item.stock_location
end
end
end
end
Expand Down