diff --git a/app/controllers/admin/subscription_line_items_controller.rb b/app/controllers/admin/subscription_line_items_controller.rb index c0b3f650eb..90a5005757 100644 --- a/app/controllers/admin/subscription_line_items_controller.rb +++ b/app/controllers/admin/subscription_line_items_controller.rb @@ -53,7 +53,11 @@ module Admin return unless @order_cycle fee_calculator = OpenFoodNetwork::EnterpriseFeeCalculator.new(@shop, @order_cycle) - OpenFoodNetwork::ScopeVariantToHub.new(@shop).scope(@variant) + + if OpenFoodNetwork::FeatureToggle.enabled?(:inventory, @shop) + OpenFoodNetwork::ScopeVariantToHub.new(@shop).scope(@variant) + end + @variant.price + fee_calculator.indexed_fees_for(@variant) end diff --git a/app/controllers/api/v0/shipments_controller.rb b/app/controllers/api/v0/shipments_controller.rb index afaea6a5b2..dc6be43345 100644 --- a/app/controllers/api/v0/shipments_controller.rb +++ b/app/controllers/api/v0/shipments_controller.rb @@ -112,7 +112,11 @@ module Api def scoped_variant(variant_id) variant = Spree::Variant.find(variant_id) - OpenFoodNetwork::ScopeVariantToHub.new(@order.distributor).scope(variant) + + if OpenFoodNetwork::FeatureToggle.enabled?(:inventory, @order.distributor) + OpenFoodNetwork::ScopeVariantToHub.new(@order.distributor).scope(variant) + end + variant end diff --git a/app/models/order_cycle.rb b/app/models/order_cycle.rb index 06bc7f2b5b..b8e70ae1eb 100644 --- a/app/models/order_cycle.rb +++ b/app/models/order_cycle.rb @@ -286,9 +286,15 @@ class OrderCycle < ApplicationRecord user_id: user, distributor_id: distributor, order_cycle_id: self) - scoper = OpenFoodNetwork::ScopeVariantToHub.new(distributor) - items = Spree::LineItem.includes(:variant).joins(:order).merge(orders).to_a - items.each { |li| scoper.scope(li.variant) } + + items = Spree::LineItem.includes(:variant).joins(:order).merge(orders) + + if OpenFoodNetwork::FeatureToggle.enabled?(:inventory, distributor) + scoper = OpenFoodNetwork::ScopeVariantToHub.new(distributor) + items.each { |li| scoper.scope(li.variant) } + end + + items end def distributor_payment_methods diff --git a/app/models/spree/line_item.rb b/app/models/spree/line_item.rb index fed86cd802..900bcdf8a1 100644 --- a/app/models/spree/line_item.rb +++ b/app/models/spree/line_item.rb @@ -164,7 +164,7 @@ module Spree return true if skip_stock_check return true if quantity <= 0 - scoper.scope(variant) + scope_variant variant.can_supply?(quantity) end @@ -177,7 +177,7 @@ module Spree end def cap_quantity_at_stock! - scoper.scope(variant) + scope_variant return if variant.on_demand update!(quantity: variant.on_hand) if quantity > variant.on_hand @@ -263,7 +263,7 @@ module Spree def update_inventory return unless changed? - scoper.scope(variant) + scope_variant Spree::OrderInventory.new(order).verify(self, target_shipment) end @@ -294,5 +294,13 @@ module Spree self.final_weight_volume = variant&.unit_value&.* quantity end end + + def scope_variant + if OpenFoodNetwork::FeatureToggle.enabled?(:inventory, order.distributor) + scoper.scope(variant) + end + + variant + end end end diff --git a/app/models/spree/shipment.rb b/app/models/spree/shipment.rb index 8288d564f5..f5fd7c9a47 100644 --- a/app/models/spree/shipment.rb +++ b/app/models/spree/shipment.rb @@ -194,7 +194,11 @@ module Spree inventory_units.group_by(&:variant).map do |variant, units| states = {} units.group_by(&:state).each { |state, iu| states[state] = iu.count } - scoper.scope(variant) + + if OpenFoodNetwork::FeatureToggle.enabled?(:inventory, order.distributor) + scoper.scope(variant) + end + OpenStruct.new(variant:, quantity: units.length, states:) end end diff --git a/app/services/cart_service.rb b/app/services/cart_service.rb index 3493a3f6d6..e61db51939 100644 --- a/app/services/cart_service.rb +++ b/app/services/cart_service.rb @@ -59,7 +59,10 @@ class CartService end def attempt_cart_add(variant, quantity, max_quantity = nil) - scoper.scope(variant) + if OpenFoodNetwork::FeatureToggle.enabled?(:inventory, order.distributor) + scoper.scope(variant) + end + return unless valid_variant?(variant) cart_add(variant, quantity, max_quantity) diff --git a/app/services/orders/factory_service.rb b/app/services/orders/factory_service.rb index eef377734b..55ae75cad5 100644 --- a/app/services/orders/factory_service.rb +++ b/app/services/orders/factory_service.rb @@ -51,7 +51,10 @@ module Orders attrs[:line_items].each do |li| next unless variant = Spree::Variant.find_by(id: li[:variant_id]) - scoper.scope(variant) + if OpenFoodNetwork::FeatureToggle.enabled?(:inventory, shop) + scoper.scope(variant) + end + li[:quantity] = stock_limited_quantity(variant.on_demand, variant.on_hand, li[:quantity]) li[:price] = variant.price build_item_from(li) diff --git a/app/services/variants_stock_levels.rb b/app/services/variants_stock_levels.rb index 454658b220..df725a0933 100644 --- a/app/services/variants_stock_levels.rb +++ b/app/services/variants_stock_levels.rb @@ -39,7 +39,9 @@ class VariantsStockLevels def scoped_variant(distributor, variant) return variant if distributor.blank? - scoper(distributor).scope(variant) + if OpenFoodNetwork::FeatureToggle.enabled?(:inventory, distributor) + scoper(distributor).scope(variant) + end variant end diff --git a/spec/controllers/admin/subscription_line_items_controller_spec.rb b/spec/controllers/admin/subscription_line_items_controller_spec.rb index 03955d13a3..62cc8ee25f 100644 --- a/spec/controllers/admin/subscription_line_items_controller_spec.rb +++ b/spec/controllers/admin/subscription_line_items_controller_spec.rb @@ -111,7 +111,7 @@ RSpec.describe Admin::SubscriptionLineItemsController do end end - context "where a relevant variant override exists" do + context "where a relevant variant override exists", feature: :inventory do let!(:override) { create(:variant_override, hub_id: shop.id, variant_id: variant.id, price: 12.00) } diff --git a/spec/controllers/api/v0/shipments_controller_spec.rb b/spec/controllers/api/v0/shipments_controller_spec.rb index e79c9dcec9..87d2d50003 100644 --- a/spec/controllers/api/v0/shipments_controller_spec.rb +++ b/spec/controllers/api/v0/shipments_controller_spec.rb @@ -67,7 +67,7 @@ RSpec.describe Api::V0::ShipmentsController do expect(order.reload.line_items.first.variant.price).to eq(variant.price) end - it 'updates existing shipment with variant override if an VO is sent' do + it 'updates existing shipment with variant override if an VO is sent', feature: :inventory do hub = create(:distributor_enterprise) order.update_attribute(:distributor, hub) shipment.shipping_method.distributors << hub @@ -346,7 +346,7 @@ RSpec.describe Api::V0::ShipmentsController do expect_error_response end - it 'adds a variant override to the shipment' do + it 'adds a variant override to the shipment', feature: :inventory do hub = create(:distributor_enterprise) order.update_attribute(:distributor, hub) variant_override = create(:variant_override, hub:, variant:) diff --git a/spec/controllers/cart_controller_spec.rb b/spec/controllers/cart_controller_spec.rb index 37b3be6c06..8edd6cce3e 100644 --- a/spec/controllers/cart_controller_spec.rb +++ b/spec/controllers/cart_controller_spec.rb @@ -42,7 +42,7 @@ RSpec.describe CartController do end end - context "handling variant overrides correctly" do + context "handling variant overrides correctly", feature: :inventory do let(:product) { create(:simple_product, supplier: producer) } let(:producer) { create(:supplier_enterprise) } let!(:variant_in_the_order) { create(:variant) } diff --git a/spec/models/order_cycle_spec.rb b/spec/models/order_cycle_spec.rb index 395be0a985..2977affb32 100644 --- a/spec/models/order_cycle_spec.rb +++ b/spec/models/order_cycle_spec.rb @@ -477,7 +477,7 @@ RSpec.describe OrderCycle do expect(items).to match_array order.reload.line_items end - it "returns items with scoped variants" do + it "returns items with scoped variants", feature: :inventory do overridden_variant = order.line_items.first.variant create(:variant_override, hub: shop, variant: overridden_variant, count_on_hand: 1000) diff --git a/spec/models/spree/line_item_spec.rb b/spec/models/spree/line_item_spec.rb index 13774597dc..885cbde7e8 100644 --- a/spec/models/spree/line_item_spec.rb +++ b/spec/models/spree/line_item_spec.rb @@ -259,7 +259,7 @@ RSpec.describe Spree::LineItem do expect(li.reload.quantity).to eq 0 end - context "when a variant override is in place" do + context "when a variant override is in place", feature: :inventory do let!(:hub) { create(:distributor_enterprise) } let!(:vo) { create(:variant_override, hub:, variant: v, count_on_hand: 2) } @@ -371,7 +371,7 @@ RSpec.describe Spree::LineItem do let!(:line_item) { order.reload.line_items.first } let!(:variant) { line_item.variant } - context "when a variant override applies" do + context "when a variant override applies", feature: :inventory do let!(:vo) { create(:variant_override, hub: shop, variant:, count_on_hand: 3 ) } it "restores stock to the variant override" do @@ -414,7 +414,7 @@ RSpec.describe Spree::LineItem do it { expect(li.sufficient_stock?).to be false } end - context "when a variant override is in place" do + context "when a variant override is in place", feature: :inventory do let!(:vo) { create(:variant_override, hub:, variant: v, count_on_hand: 5) } context "and stock on the variant override is sufficient" do diff --git a/spec/models/spree/shipment_spec.rb b/spec/models/spree/shipment_spec.rb index 2da2999d50..81acff4c45 100644 --- a/spec/models/spree/shipment_spec.rb +++ b/spec/models/spree/shipment_spec.rb @@ -86,6 +86,16 @@ RSpec.describe Spree::Shipment do end end end + + context "with variant override", feature: :inventory do + let(:order) { create(:order, distributor: variant.supplier) } + + it "returns the scoped variant" do + create(:variant_override, hub: variant.supplier, variant:, price: 25.00) + + expect(shipment.manifest.first.variant.price).to eq 25.00 + end + end end context 'shipping_rates' do @@ -276,7 +286,7 @@ RSpec.describe Spree::Shipment do .to change { variant.on_hand }.from(5).to(4) end - it "reduces stock of a variant override" do + it "reduces stock of a variant override", feature: :inventory do variant.on_hand = 5 variant_override = VariantOverride.create!( variant:, diff --git a/spec/services/cart_service_spec.rb b/spec/services/cart_service_spec.rb index 1116913b90..ef2307fe61 100644 --- a/spec/services/cart_service_spec.rb +++ b/spec/services/cart_service_spec.rb @@ -169,10 +169,12 @@ RSpec.describe CartService do describe "attempt_cart_add" do let!(:variant) { create(:variant, on_hand: 250) } let(:quantity) { 123 } + let(:distributor) { create(:distributor_enterprise) } before do allow(Spree::Variant).to receive(:find).and_return(variant) allow(VariantOverride).to receive(:for).and_return(nil) + allow(order).to receive(:distributor).and_return(distributor) end it "performs additional validations" do diff --git a/spec/services/orders/factory_service_spec.rb b/spec/services/orders/factory_service_spec.rb index aee3bea3cb..8693494797 100644 --- a/spec/services/orders/factory_service_spec.rb +++ b/spec/services/orders/factory_service_spec.rb @@ -104,7 +104,7 @@ RSpec.describe Orders::FactoryService do end end - context "when an override is present" do + context "when an override is present", feature: :inventory do let!(:override) { create(:variant_override, hub_id: shop.id, variant_id: variant1.id, count_on_hand: 3) } @@ -137,7 +137,7 @@ RSpec.describe Orders::FactoryService do end end - context "when an override is present" do + context "when an override is present", feature: :inventory do let!(:override) { create(:variant_override, hub_id: shop.id, variant_id: variant1.id, price: 3.0) } diff --git a/spec/services/variants_stock_levels_spec.rb b/spec/services/variants_stock_levels_spec.rb index 13b2056d96..9903469799 100644 --- a/spec/services/variants_stock_levels_spec.rb +++ b/spec/services/variants_stock_levels_spec.rb @@ -76,7 +76,7 @@ RSpec.describe VariantsStockLevels do end context "when the variant is in the order" do - it "returns the on_hand value of the override" do + it "returns the on_hand value of the override", feature: :inventory do expect(variant_stock_levels.call(order, [variant_in_the_order.id])).to eq( variant_in_the_order.id => { quantity: 2, max_quantity: 3, on_hand: 200, on_demand: false @@ -86,7 +86,7 @@ RSpec.describe VariantsStockLevels do end context "with variants that are not in the order" do - it "returns the on_hand value of the override" do + it "returns the on_hand value of the override", feature: :inventory do variant_ids = [variant_in_the_order.id, variant_not_in_the_order.id] expect(variant_stock_levels.call(order, variant_ids)).to eq( variant_in_the_order.id => {