From 48e3b5b05e3728005cd7f58b4154cc1f3888eb90 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 27 Mar 2025 13:33:52 +1100 Subject: [PATCH] Move more reset variant code for re-use --- .../admin/dfc_product_imports_controller.rb | 20 +---------------- app/services/dfc_catalog_importer.rb | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/app/controllers/admin/dfc_product_imports_controller.rb b/app/controllers/admin/dfc_product_imports_controller.rb index 9b98598275..9b7c15a4e6 100644 --- a/app/controllers/admin/dfc_product_imports_controller.rb +++ b/app/controllers/admin/dfc_product_imports_controller.rb @@ -86,26 +86,8 @@ module Admin end end - # Reset stock for any variants that were removed from the catalog. - # - # When variants are removed from the remote catalog, we can't place - # backorders for them anymore. If our copy of the product has limited - # stock then we need to set the stock to zero to prevent any more sales. - # - # But if our product is on-demand/backorderable then our stock level is - # a representation of remaining local stock. We then need to limit sales - # to this local stock and set on-demand to false. - # - # We don't delete the variant because it may come back at a later time and - # we don't want to lose the connection to previous orders. def reset_absent_variants(catalog) - absent_variants(catalog).map do |variant| - if variant.on_demand - variant.on_demand = false - else - variant.on_hand = 0 - end - end + DfcCatalogImporter.new(@enterprise.supplied_variants, catalog).reset_absent_variants end def absent_variants(catalog) diff --git a/app/services/dfc_catalog_importer.rb b/app/services/dfc_catalog_importer.rb index c411353674..ab5837caee 100644 --- a/app/services/dfc_catalog_importer.rb +++ b/app/services/dfc_catalog_importer.rb @@ -8,6 +8,28 @@ class DfcCatalogImporter @catalog = catalog end + # Reset stock for any variants that were removed from the catalog. + # + # When variants are removed from the remote catalog, we can't place + # backorders for them anymore. If our copy of the product has limited + # stock then we need to set the stock to zero to prevent any more sales. + # + # But if our product is on-demand/backorderable then our stock level is + # a representation of remaining local stock. We then need to limit sales + # to this local stock and set on-demand to false. + # + # We don't delete the variant because it may come back at a later time and + # we don't want to lose the connection to previous orders. + def reset_absent_variants + absent_variants.map do |variant| + if variant.on_demand + variant.on_demand = false + else + variant.on_hand = 0 + end + end + end + def absent_variants present_ids = catalog.products.map(&:semanticId) catalog_url = FdcUrlBuilder.new(present_ids.first).catalog_url