diff --git a/app/controllers/admin/dfc_product_imports_controller.rb b/app/controllers/admin/dfc_product_imports_controller.rb index fbb854595d..ab3b819c4f 100644 --- a/app/controllers/admin/dfc_product_imports_controller.rb +++ b/app/controllers/admin/dfc_product_imports_controller.rb @@ -26,6 +26,8 @@ module Admin imported = broker.catalog.map do |subject| next unless subject.is_a? DataFoodConsortium::Connector::SuppliedProduct + adjust_to_wholesale_price(broker, subject) + existing_variant = enterprise.supplied_variants.linked_to(subject.semanticId) if existing_variant @@ -42,5 +44,24 @@ module Admin flash[:error] = e.message redirect_to admin_product_import_path end + + private + + def adjust_to_wholesale_price(broker, product) + transformation = broker.best_offer(product.semanticId) + + return if transformation.factor == 1 + + wholesale_variant_price = transformation.offer.price + + return unless wholesale_variant_price + + offer = product.catalogItems&.first&.offers&.first + + return unless offer + + offer.price = wholesale_variant_price.dup + offer.price.value = offer.price.value.to_f / transformation.factor + end end end diff --git a/spec/system/admin/dfc_product_import_spec.rb b/spec/system/admin/dfc_product_import_spec.rb index a6f2518779..bae9881278 100644 --- a/spec/system/admin/dfc_product_import_spec.rb +++ b/spec/system/admin/dfc_product_import_spec.rb @@ -62,7 +62,8 @@ RSpec.describe "DFC Product Import" do }.to change { enterprise.supplied_products.count } .and change { linked_variant.display_name } .and change { linked_variant.unit_value } - .and change { linked_variant.price }.to(2.09) + # 18.85 wholesale variant price divided by 12 cans in the slab. + .and change { linked_variant.price }.to(1.57) .and change { linked_variant.on_demand }.to(true) .and change { linked_variant.on_hand }.by(0)