Skip to content

Commit

Permalink
Merge pull request #2908 from nebulab/fix_fullfilment_charger_allocation
Browse files Browse the repository at this point in the history
Spree::FulfilmentChanger stock allocation fix
  • Loading branch information
kennyadsl authored Nov 26, 2018
2 parents 5707a64 + 0921e00 commit 9b6ad06
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
10 changes: 9 additions & 1 deletion core/app/models/spree/fulfilment_changer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def run!
desired_shipment.save! if desired_shipment.new_record?

# Retrieve how many on hand items we can take from desired stock location
available_quantity = [desired_shipment.stock_location.count_on_hand(variant), 0].max
available_quantity = [desired_shipment.stock_location.count_on_hand(variant), default_on_hand_quantity].max

new_on_hand_quantity = [available_quantity, quantity].min
unstock_quantity = desired_shipment.stock_location.backorderable?(variant) ? quantity : new_on_hand_quantity
Expand Down Expand Up @@ -114,6 +114,14 @@ def handle_stock_counts?
current_shipment.order.completed? && current_stock_location != desired_stock_location
end

def default_on_hand_quantity
if current_stock_location != desired_stock_location
0
else
current_shipment.inventory_units.where(variant: variant).on_hand.count
end
end

def current_shipment_not_already_shipped
if current_shipment.shipped?
errors.add(:current_shipment, :has_already_been_shipped)
Expand Down
33 changes: 33 additions & 0 deletions core/spec/models/spree/fulfilment_changer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,39 @@
variant.stock_items.first.update_column(:count_on_hand, 100)
end

context "when the current shipment stock location is the same of the target shipment" do
let(:current_shipment_inventory_unit_count) { 1 }
let(:quantity) { current_shipment_inventory_unit_count }

context "when the stock location is empty" do
before do
variant.stock_items.first.update_column(:count_on_hand, 0)
end

context "when the inventory unit is backordered" do
before do
current_shipment.inventory_units.first.update state: :backordered
end

it "creates a new backordered inventory unit" do
subject
expect(desired_shipment.inventory_units.first).to be_backordered
end
end

context "when the inventory unit is on hand" do
before do
current_shipment.inventory_units.first.update state: :on_hand
end

it "creates a new on hand inventory unit" do
subject
expect(desired_shipment.inventory_units.first).to be_on_hand
end
end
end
end

context "when the current shipment has enough inventory units" do
let(:current_shipment_inventory_unit_count) { 2 }
let(:quantity) { 1 }
Expand Down

0 comments on commit 9b6ad06

Please sign in to comment.