From a1887bdc7684cfc65384715d4c2d44e6d5d97ea9 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 20 Mar 2024 16:53:39 +1100 Subject: [PATCH] Update stock levels of on-demand items 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. --- app/models/concerns/variant_stock.rb | 3 +-- lib/open_food_network/scope_variant_to_hub.rb | 4 ---- spec/lib/open_food_network/scope_variant_to_hub_spec.rb | 2 ++ spec/models/spree/line_item_spec.rb | 4 ++-- spec/system/consumer/shopping/variant_overrides_spec.rb | 2 ++ 5 files changed, 7 insertions(+), 8 deletions(-) diff --git a/app/models/concerns/variant_stock.rb b/app/models/concerns/variant_stock.rb index f39ab38dc9..10ea8f136e 100644 --- a/app/models/concerns/variant_stock.rb +++ b/app/models/concerns/variant_stock.rb @@ -112,8 +112,7 @@ module VariantStock # # 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 diff --git a/lib/open_food_network/scope_variant_to_hub.rb b/lib/open_food_network/scope_variant_to_hub.rb index d7a29cbb9c..90bfef39d4 100644 --- a/lib/open_food_network/scope_variant_to_hub.rb +++ b/lib/open_food_network/scope_variant_to_hub.rb @@ -43,11 +43,7 @@ module OpenFoodNetwork # - 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 diff --git a/spec/lib/open_food_network/scope_variant_to_hub_spec.rb b/spec/lib/open_food_network/scope_variant_to_hub_spec.rb index cbff22dc35..705f4ba159 100644 --- a/spec/lib/open_food_network/scope_variant_to_hub_spec.rb +++ b/spec/lib/open_food_network/scope_variant_to_hub_spec.rb @@ -182,6 +182,8 @@ module OpenFoodNetwork end it "doesn't reduce variant's stock" do + pending "updating override stock" + v2.move(-2) expect(Spree::Variant.find(v2.id).on_hand).to eq 5 end diff --git a/spec/models/spree/line_item_spec.rb b/spec/models/spree/line_item_spec.rb index d715fec503..b6fecf1f33 100644 --- a/spec/models/spree/line_item_spec.rb +++ b/spec/models/spree/line_item_spec.rb @@ -313,8 +313,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 diff --git a/spec/system/consumer/shopping/variant_overrides_spec.rb b/spec/system/consumer/shopping/variant_overrides_spec.rb index 01dd56c349..2bbc679604 100644 --- a/spec/system/consumer/shopping/variant_overrides_spec.rb +++ b/spec/system/consumer/shopping/variant_overrides_spec.rb @@ -223,6 +223,8 @@ RSpec.describe "shopping with variant overrides defined" do end it "does not subtract stock from variants where the override has on_demand: true" do + pending "update override stock" + click_add_to_cart product4_variant1, 2 click_checkout expect do