Skip to content

Commit

Permalink
Update stock levels of on-demand items
Browse files Browse the repository at this point in the history
We weren't bothering with stock when items were on demand anyway. But we
want to track stock now so that we can backorder more when local stock
levels become negative.
  • Loading branch information
mkllnk committed May 31, 2024
1 parent d391faa commit ecbba89
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 12 deletions.
3 changes: 1 addition & 2 deletions app/models/concerns/variant_stock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ def fill_status(quantity)
#
# This enables us to override this behaviour for variant overrides
def move(quantity, originator = nil)
# Don't change variant stock if variant is on_demand or has been deleted
return if on_demand || deleted_at
return if deleted_at

raise_error_if_no_stock_item_available

Expand Down
4 changes: 0 additions & 4 deletions lib/open_food_network/scope_variant_to_hub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ def on_demand
# - updates variant_override.count_on_hand
# - does not create stock_movement
# - does not update stock_item.count_on_hand
# If it is a variant override with on_demand:
# - don't change stock or call super (super would change the variant's stock)
def move(quantity, originator = nil)
return if @variant_override&.on_demand

if @variant_override&.stock_overridden?
@variant_override.move_stock! quantity
else
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/open_food_network/scope_variant_to_hub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ module OpenFoodNetwork
scoper.scope v2
end

it "doesn't reduce variant's stock" do
it "does reduce variant's stock" do
v2.move(-2)
expect(Spree::Variant.find(v2.id).on_hand).to eq 5
expect(Spree::Variant.find(v2.id).on_hand).to eq 3
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/models/spree/line_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ module Spree
expect(order.shipment.manifest.first.variant).to eq line_item.variant
end

it "does not reduce the variant's stock level" do
expect(variant_on_demand.reload.on_hand).to eq 1
it "reduces the variant's stock level" do
expect(variant_on_demand.reload.on_hand).to eq(-9)
end

it "does not mark inventory units as backorderd" do
Expand Down
4 changes: 2 additions & 2 deletions spec/system/consumer/shopping/variant_overrides_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,12 @@
expect(product1_variant1_override.reload.count_on_hand).to be_nil
end

it "does not subtract stock from variants where the override has on_demand: true" do
it "does subtract stock from variants where the override has on_demand: true" do
click_add_to_cart product4_variant1, 2
click_checkout
expect do
complete_checkout
end.to change { product4_variant1.reload.on_hand }.by(0)
end.to change { product4_variant1.reload.on_hand }.by(-2)
expect(product4_variant1_override.reload.count_on_hand).to be_nil
end

Expand Down

0 comments on commit ecbba89

Please sign in to comment.