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
12 changes: 9 additions & 3 deletions core/app/models/spree/fulfilment_changer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,18 @@ def run_tracking_inventory
available_quantity = get_available_quantity
new_on_hand_quantity = [available_quantity, quantity].min
backordered_quantity = get_backordered_quantity(available_quantity, new_on_hand_quantity)
unstock_quantity = desired_shipment.stock_location.backorderable?(variant) ? quantity : new_on_hand_quantity

# Determine how many backordered and on_hand items we'll need to move. We
# don't want to move more than what's being asked. And we can't move a
# negative amount, which is why we need to perform our min/max logic here.
backordered_quantity_to_move = [backordered_quantity, quantity].min
on_hand_quantity_to_move = [quantity - backordered_quantity_to_move, 0].max

ActiveRecord::Base.transaction do
if handle_stock_counts?
# We only run this query if we need it.
current_on_hand_quantity = [current_shipment.inventory_units.pre_shipment.size, quantity].min
unstock_quantity = desired_shipment.stock_location.backorderable?(variant) ? quantity : new_on_hand_quantity

# Restock things we will not fulfil from the current shipment anymore
current_stock_location.restock(variant, current_on_hand_quantity, current_shipment)
Expand All @@ -111,14 +117,14 @@ def run_tracking_inventory
inventory_units.
where(variant:).
order(state: :asc).
limit(backordered_quantity).
limit(backordered_quantity_to_move).
update_all(shipment_id: desired_shipment.id, state: :backordered)

current_shipment.
inventory_units.
where(variant:).
order(state: :asc).
limit(quantity - backordered_quantity).
limit(on_hand_quantity_to_move).
update_all(shipment_id: desired_shipment.id, state: :on_hand)
end
end
Expand Down
23 changes: 23 additions & 0 deletions core/spec/models/spree/fulfilment_changer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,29 @@

it_behaves_like "moves inventory units between shipments"
it_behaves_like "properly manages inventory units"

context "and we're only moving some backordered units" do
let(:stock_item) { variant.stock_items.find_by!(stock_location: current_shipment.stock_location) }
let(:current_shipment_inventory_unit_count) { 10 }
let(:quantity) { 4 }

before do
current_shipment.inventory_units.limit(6).update!(state: :backordered)
stock_item.update_column(:count_on_hand, -6)
end

it_behaves_like "moves inventory units between shipments"

it "only removes the desired quantity of backordered items" do
subject

expect(current_shipment.inventory_units.on_hand.count).to eq(4)
expect(current_shipment.inventory_units.backordered.count).to eq(2)

expect(desired_shipment.inventory_units.on_hand.count).to eq(0)
expect(desired_shipment.inventory_units.backordered.count).to eq(4)
end
end
end

context "when tracking inventory is not set (same as false)" do
Expand Down
Loading