diff --git a/lib/open_food_network/scope_variant_to_hub.rb b/lib/open_food_network/scope_variant_to_hub.rb index 2cf79ee28d..80396ffa17 100644 --- a/lib/open_food_network/scope_variant_to_hub.rb +++ b/lib/open_food_network/scope_variant_to_hub.rb @@ -25,6 +25,16 @@ module OpenFoodNetwork @variant_override.andand.count_on_hand || super end + def on_demand + if @variant_override.andand.count_on_hand.present? + # If we're overriding the stock level of an on_demand variant, show it as not + # on_demand, so our stock control can take effect. + false + else + super + end + end + def decrement!(attribute, by=1) if attribute == :count_on_hand && @variant_override.andand.stock_overridden? @variant_override.decrement_stock! by diff --git a/spec/features/consumer/shopping/variant_overrides_spec.rb b/spec/features/consumer/shopping/variant_overrides_spec.rb index 44e8e38732..a6855fc6d1 100644 --- a/spec/features/consumer/shopping/variant_overrides_spec.rb +++ b/spec/features/consumer/shopping/variant_overrides_spec.rb @@ -15,18 +15,23 @@ feature "shopping with variant overrides defined", js: true do let(:pm) { hub.payment_methods.first } let(:p1) { create(:simple_product, supplier: producer) } let(:p2) { create(:simple_product, supplier: producer) } + let(:p3) { create(:simple_product, supplier: producer, on_demand: true) } let(:v1) { create(:variant, product: p1, price: 11.11, unit_value: 1) } let(:v2) { create(:variant, product: p1, price: 22.22, unit_value: 2) } let(:v3) { create(:variant, product: p2, price: 33.33, unit_value: 3) } let(:v4) { create(:variant, product: p1, price: 44.44, unit_value: 4) } + let(:v5) { create(:variant, product: p3, price: 55.55, unit_value: 5, on_demand: true) } + let(:v6) { create(:variant, product: p3, price: 66.66, unit_value: 6, on_demand: true) } let!(:vo1) { create(:variant_override, hub: hub, variant: v1, price: 55.55, count_on_hand: nil) } let!(:vo2) { create(:variant_override, hub: hub, variant: v2, count_on_hand: 0) } let!(:vo3) { create(:variant_override, hub: hub, variant: v3, count_on_hand: 0) } let!(:vo4) { create(:variant_override, hub: hub, variant: v4, count_on_hand: 3) } + let!(:vo5) { create(:variant_override, hub: hub, variant: v5, count_on_hand: 0) } + let!(:vo6) { create(:variant_override, hub: hub, variant: v6, count_on_hand: 6) } let(:ef) { create(:enterprise_fee, enterprise: hub, fee_type: 'packing', calculator: Spree::Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10)) } before do - outgoing_exchange.variants = [v1, v2, v3, v4] + outgoing_exchange.variants = [v1, v2, v3, v4, v5, v6] outgoing_exchange.enterprise_fees << ef sm.calculator.preferred_amount = 0 visit shops_path @@ -46,6 +51,9 @@ feature "shopping with variant overrides defined", js: true do # Entire product should not appear - no stock page.should_not have_content p2.name page.should_not have_content v3.options_text + + # On-demand product with VO of no stock should NOT appear + page.should_not have_content v5.options_text end it "calculates fees correctly" do @@ -127,6 +135,19 @@ feature "shopping with variant overrides defined", js: true do end.to change { vo4.reload.count_on_hand }.by(-2) end + it "subtracts stock from stock-overridden on_demand variants" do + fill_in "variants[#{v6.id}]", with: "2" + show_cart + wait_until_enabled 'li.cart a.button' + click_link 'Checkout now' + + expect do + expect do + complete_checkout + end.to change { v6.reload.count_on_hand }.by(0) + end.to change { vo6.reload.count_on_hand }.by(-2) + end + it "does not subtract stock from overrides that do not override count_on_hand" do fill_in "variants[#{v1.id}]", with: "2" show_cart 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 dbdf5c074e..2a249f9c7a 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 @@ -5,6 +5,7 @@ module OpenFoodNetwork let(:hub) { create(:distributor_enterprise) } let(:v) { create(:variant, price: 11.11, count_on_hand: 1) } let(:vo) { create(:variant_override, hub: hub, variant: v, price: 22.22, count_on_hand: 2) } + let(:vo_price_only) { create(:variant_override, hub: hub, variant: v, price: 22.22, count_on_hand: nil) } let(:scoper) { ScopeVariantToHub.new(hub) } describe "overriding price" do @@ -44,6 +45,27 @@ module OpenFoodNetwork scoper.scope v v.count_on_hand.should == 1 end + + describe "overriding stock on an on_demand variant" do + let(:v) { create(:variant, price: 11.11, on_demand: true) } + + it "clears on_demand when the stock is overridden" do + vo + scoper.scope v + v.on_demand.should be_false + end + + it "does not clear on_demand when only the price is overridden" do + vo_price_only + scoper.scope v + v.on_demand.should be_true + end + + it "does not clear on_demand when there is no override" do + scoper.scope v + v.on_demand.should be_true + end + end end end end