From 2cc751cb30c8ffb6661641d9277226fb0077dc8d Mon Sep 17 00:00:00 2001 From: Cillian O'Ruanaidh Date: Fri, 16 Oct 2020 11:38:49 +0100 Subject: [PATCH 1/2] When resetting stock to 0 on absent products in product import also reset the on demand setting Before when you imported products and clicked the 'Set stock to zero for all existing products not present in the file' option it would set the on hand stock to 0 but if the variant was also set to be on demand the product would still be available for sale. This change makes sure the on demand setting is turned off too. Fixes #6064. --- .../product_import/products_reset_strategy.rb | 12 +++++++----- .../product_import/products_reset_strategy_spec.rb | 10 ++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/engines/catalog/app/services/catalog/product_import/products_reset_strategy.rb b/engines/catalog/app/services/catalog/product_import/products_reset_strategy.rb index c4cf28cdf1..da963717e7 100644 --- a/engines/catalog/app/services/catalog/product_import/products_reset_strategy.rb +++ b/engines/catalog/app/services/catalog/product_import/products_reset_strategy.rb @@ -12,7 +12,7 @@ module Catalog return 0 if enterprise_ids.blank? - reset_variants_on_hand(enterprise_variants_relation) + reset_variants_on_hand_and_on_demand(enterprise_variants_relation) end private @@ -32,17 +32,19 @@ module Catalog relation.where('spree_variants.id NOT IN (?)', excluded_items_ids) end - def reset_variants_on_hand(variants) + def reset_variants_on_hand_and_on_demand(variants) updated_records_count = 0 variants.each do |variant| - updated_records_count += 1 if reset_variant_on_hand(variant) + updated_records_count += 1 if reset_variant_on_hand_and_on_demand(variant) end updated_records_count end - def reset_variant_on_hand(variant) + def reset_variant_on_hand_and_on_demand(variant) + was_on_demand = variant.on_demand + variant.on_demand = false variant.on_hand = 0 - variant.on_hand.zero? + variant.on_hand.zero? || was_on_demand end end end diff --git a/engines/catalog/spec/services/catalog/product_import/products_reset_strategy_spec.rb b/engines/catalog/spec/services/catalog/product_import/products_reset_strategy_spec.rb index a2fd01d01b..823ed5d6b1 100644 --- a/engines/catalog/spec/services/catalog/product_import/products_reset_strategy_spec.rb +++ b/engines/catalog/spec/services/catalog/product_import/products_reset_strategy_spec.rb @@ -91,6 +91,16 @@ module Catalog expect { products_reset.reset(supplier_ids) }.to raise_error RuntimeError end end + + context 'and the variant is on demand' do + before { variant.on_demand = true } + + it 'turns off the on demand setting on the variant' do + products_reset.reset(supplier_ids) + + expect(variant.reload.on_demand).to eq(false) + end + end end end From c62f1bd550aeb45df9c758c5145a110f6c911d97 Mon Sep 17 00:00:00 2001 From: Cillian O'Ruanaidh Date: Fri, 16 Oct 2020 12:13:55 +0100 Subject: [PATCH 2/2] Exclude products_reset_strategy_spec.rb from Rubocop's Metrics/ModuleLength check --- .rubocop_manual_todo.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.rubocop_manual_todo.yml b/.rubocop_manual_todo.yml index 5c55f6ef9e..a919e0dedc 100644 --- a/.rubocop_manual_todo.yml +++ b/.rubocop_manual_todo.yml @@ -814,6 +814,7 @@ Metrics/ModuleLength: - app/helpers/spree/admin/navigation_helper.rb - app/models/spree/order/checkout.rb - app/models/spree/payment/processing.rb + - engines/catalog/spec/services/catalog/product_import/products_reset_strategy_spec.rb - engines/order_management/spec/services/order_management/order/updater_spec.rb - engines/order_management/spec/services/order_management/stock/package_spec.rb - engines/order_management/spec/services/order_management/subscriptions/estimator_spec.rb