From edc4d680fead7b678c50df120cb194c88c9d417b Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 18 Dec 2024 17:04:55 +1100 Subject: [PATCH] Calculate stock from wholesale products --- app/jobs/stock_sync_job.rb | 12 +++---- .../dfc_provider/app/services/dfc_catalog.rb | 36 +++++++++++++++---- .../spec/services/dfc_catalog_spec.rb | 21 +++++++++++ 3 files changed, 55 insertions(+), 14 deletions(-) diff --git a/app/jobs/stock_sync_job.rb b/app/jobs/stock_sync_job.rb index 1572731e0ce..565d6425d4a 100644 --- a/app/jobs/stock_sync_job.rb +++ b/app/jobs/stock_sync_job.rb @@ -40,7 +40,10 @@ def self.catalog_ids(order) end def perform(user, catalog_id) - products = load_products(user, catalog_id) + catalog = DfcCatalog.load(user, catalog_id) + catalog.apply_wholesale_values! + + products = catalog.products products_by_id = products.index_by(&:semanticId) product_ids = products_by_id.keys variants = linked_variants(user.enterprises, product_ids) @@ -58,13 +61,6 @@ def perform(user, catalog_id) end end - def load_products(user, catalog_id) - json_catalog = DfcRequest.new(user).call(catalog_id) - graph = DfcIo.import(json_catalog) - - DfcCatalog.new(graph).products - end - def linked_variants(enterprises, product_ids) Spree::Variant.where(supplier: enterprises) .includes(:semantic_links).references(:semantic_links) diff --git a/engines/dfc_provider/app/services/dfc_catalog.rb b/engines/dfc_provider/app/services/dfc_catalog.rb index 7e9fed7009b..f068bdc3cf8 100644 --- a/engines/dfc_provider/app/services/dfc_catalog.rb +++ b/engines/dfc_provider/app/services/dfc_catalog.rb @@ -31,17 +31,18 @@ def select_type(semantic_type) def apply_wholesale_values! broker = FdcOfferBroker.new(self) products.each do |product| - adjust_to_wholesale_price(broker, product) + transformation = broker.best_offer(product.semanticId) + + next if transformation.factor == 1 + + adjust_to_wholesale_price(product, transformation) + adjust_to_wholesale_stock(product, transformation) end end private - def adjust_to_wholesale_price(broker, product) - transformation = broker.best_offer(product.semanticId) - - return if transformation.factor == 1 - + def adjust_to_wholesale_price(product, transformation) wholesale_variant_price = transformation.offer.price return unless wholesale_variant_price @@ -53,4 +54,27 @@ def adjust_to_wholesale_price(broker, product) offer.price = wholesale_variant_price.dup offer.price.value = offer.price.value.to_f / transformation.factor end + + def adjust_to_wholesale_stock(product, transformation) + adjust_item_stock(product, transformation) + adjust_offer_stock(product, transformation) + end + + def adjust_item_stock(product, transformation) + item = product.catalogItems&.first + wholesale_item = transformation.product.catalogItems&.first + + return unless item && wholesale_item&.stockLimitation.present? + + item.stockLimitation = wholesale_item.stockLimitation.to_i * transformation.factor + end + + def adjust_offer_stock(product, transformation) + offer = product.catalogItems&.first&.offers&.first + wholesale_offer = transformation.offer + + return unless offer && wholesale_offer&.stockLimitation.present? + + offer.stockLimiation = wholesale_offer.stockLimitation.to_i * transformation.factor + end end diff --git a/engines/dfc_provider/spec/services/dfc_catalog_spec.rb b/engines/dfc_provider/spec/services/dfc_catalog_spec.rb index 35b4a1082d1..15e59f1d2ec 100644 --- a/engines/dfc_provider/spec/services/dfc_catalog_spec.rb +++ b/engines/dfc_provider/spec/services/dfc_catalog_spec.rb @@ -21,4 +21,25 @@ expect(products.map(&:semanticType).uniq).to eq ["dfc-b:SuppliedProduct"] end end + + describe "#apply_wholesale_values!" do + let(:offer) { beans.catalogItems.first.offers.first } + let(:catalog_item) { beans.catalogItems.first } + let(:beans) { catalog.item(beans_id) } + let(:beans_id) { + "https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts/44519466467635" + } + + it "changes price of retail variants" do + expect { catalog.apply_wholesale_values! }.to change { + offer.price.value.to_f.round(2) + }.from(2.09).to(1.57) # 18.85 wholesale price divided by 12 + end + + it "changes stock level of retail variants" do + expect { catalog.apply_wholesale_values! }.to change { + catalog_item.stockLimitation + }.from("-1").to(-12) + end + end end