diff --git a/app/services/products_renderer.rb b/app/services/products_renderer.rb index 1aa62737d2..2dbf12076b 100644 --- a/app/services/products_renderer.rb +++ b/app/services/products_renderer.rb @@ -44,7 +44,6 @@ class ProductsRenderer paginated_products = paginate(results) - # TODO test this ? if options[:inventory_enabled] # Scope results with variant_overrides paginated_products.each { |product| product_scoper.scope(product) } diff --git a/lib/open_food_network/scope_variants_for_search.rb b/lib/open_food_network/scope_variants_for_search.rb index d8e96b2a69..e40f1c5e34 100644 --- a/lib/open_food_network/scope_variants_for_search.rb +++ b/lib/open_food_network/scope_variants_for_search.rb @@ -81,16 +81,25 @@ module OpenFoodNetwork end def scope_to_in_stock_only - @variants = @variants.joins( - "INNER JOIN spree_stock_items ON spree_stock_items.variant_id = spree_variants.id - LEFT JOIN variant_overrides ON variant_overrides.variant_id = spree_variants.id AND - variant_overrides.hub_id = #{distributor.id}" - ).where(" - variant_overrides.on_demand IS TRUE OR - variant_overrides.count_on_hand > 0 OR - (variant_overrides.on_demand IS NULL AND spree_stock_items.backorderable IS TRUE) OR - (variant_overrides.count_on_hand IS NULL AND spree_stock_items.count_on_hand > 0) - ") + if OpenFoodNetwork::FeatureToggle.enabled?(:inventory, distributor) + @variants = @variants.joins( + "INNER JOIN spree_stock_items ON spree_stock_items.variant_id = spree_variants.id + LEFT JOIN variant_overrides ON variant_overrides.variant_id = spree_variants.id AND + variant_overrides.hub_id = #{distributor.id}" + ).where(" + variant_overrides.on_demand IS TRUE OR + variant_overrides.count_on_hand > 0 OR + (variant_overrides.on_demand IS NULL AND spree_stock_items.backorderable IS TRUE) OR + (variant_overrides.count_on_hand IS NULL AND spree_stock_items.count_on_hand > 0) + ") + else + @variants = @variants.joins( + "INNER JOIN spree_stock_items ON spree_stock_items.variant_id = spree_variants.id" + ).where(" + spree_stock_items.backorderable IS TRUE OR + spree_stock_items.count_on_hand > 0 + ") + end end def scope_to_in_stock_only? diff --git a/lib/reporting/reports/orders_and_fulfillment/base.rb b/lib/reporting/reports/orders_and_fulfillment/base.rb index f3a9052610..b0bcff9511 100644 --- a/lib/reporting/reports/orders_and_fulfillment/base.rb +++ b/lib/reporting/reports/orders_and_fulfillment/base.rb @@ -80,10 +80,16 @@ module Reporting def variant_scoper_for(distributor_id) @variant_scopers_by_distributor_id ||= {} + variant_overrides = {} + if OpenFoodNetwork::FeatureToggle.enabled?(:inventory, + Enterprise.find_by(id: distributor_id)) + variant_overrides = report_variant_overrides[distributor_id] + end + @variant_scopers_by_distributor_id[distributor_id] ||= OpenFoodNetwork::ScopeVariantToHub.new( distributor_id, - report_variant_overrides[distributor_id] || {}, + variant_overrides, ) end diff --git a/spec/lib/open_food_network/scope_variants_for_search_spec.rb b/spec/lib/open_food_network/scope_variants_for_search_spec.rb index bbed2af712..b0fc09d51d 100644 --- a/spec/lib/open_food_network/scope_variants_for_search_spec.rb +++ b/spec/lib/open_food_network/scope_variants_for_search_spec.rb @@ -126,20 +126,42 @@ RSpec.describe OpenFoodNetwork::ScopeVariantsForSearch do context "when :include_out_of_stock is not specified" do let(:params) { { distributor_id: d1.id } } - it "returns variants for the given distributor if they have a variant override which is - in stock, or if they have a variant override with no stock level set but the producer - has stock, or if they don't have a variant override and the producer has stock" do + context "with inventory enabled", feature: :inventory do + it "returns variants for the given distributor if they have a variant override + which is in stock, or if they have a variant override with no stock level set + but the producer has stock, or if they don't have a variant override + and the producer has stock" do + expect(result).to include( + distributor1_variant_on_hand_but_not_backorderable, + distributor1_variant_backorderable_but_not_on_hand, + distributor1_variant_with_override_on_demand_but_not_on_hand, + distributor1_variant_with_override_on_hand_but_not_on_demand, + distributor1_variant_with_override_without_stock_level_set_but_producer_in_stock + ) + expect(result).not_to include( + distributor1_variant_not_backorderable_and_not_on_hand, + distributor1_variant_with_override_not_on_demand_and_not_on_hand, + distributor1_variant_with_override_not_in_stock_but_producer_in_stock, + distributor1_variant_with_override_without_stock_level_set_and_no_producer_stock, + distributor2_variant_with_override_in_stock + ) + end + end + + it "returns variants for the given distributor if the producer has stock" do + # variant with override are returned here because the associated variant has stock expect(result).to include( distributor1_variant_on_hand_but_not_backorderable, distributor1_variant_backorderable_but_not_on_hand, - distributor1_variant_with_override_on_demand_but_not_on_hand, - distributor1_variant_with_override_on_hand_but_not_on_demand, - distributor1_variant_with_override_without_stock_level_set_but_producer_in_stock + distributor1_variant_with_override_without_stock_level_set_but_producer_in_stock, + distributor1_variant_with_override_not_in_stock_but_producer_in_stock, ) + expect(result).not_to include( distributor1_variant_not_backorderable_and_not_on_hand, + distributor1_variant_with_override_on_demand_but_not_on_hand, + distributor1_variant_with_override_on_hand_but_not_on_demand, distributor1_variant_with_override_not_on_demand_and_not_on_hand, - distributor1_variant_with_override_not_in_stock_but_producer_in_stock, distributor1_variant_with_override_without_stock_level_set_and_no_producer_stock, distributor2_variant_with_override_in_stock ) diff --git a/spec/lib/reports/orders_and_fulfillment/order_cycle_customer_totals_report_spec.rb b/spec/lib/reports/orders_and_fulfillment/order_cycle_customer_totals_report_spec.rb index d09e63a9ea..ac9179c2ce 100644 --- a/spec/lib/reports/orders_and_fulfillment/order_cycle_customer_totals_report_spec.rb +++ b/spec/lib/reports/orders_and_fulfillment/order_cycle_customer_totals_report_spec.rb @@ -132,7 +132,7 @@ RSpec.describe Reporting::Reports::OrdersAndFulfillment::OrderCycleCustomerTotal end end - context 'when a variant override applies' do + context 'when a variant override applies', feature: :inventory do let!(:order) do create( :completed_order_with_totals,