Calculate wholesale price for imported retail variants

This commit is contained in:
Maikel Linke
2024-12-12 16:08:45 +11:00
parent e47a57fa3c
commit 13e008e91e
2 changed files with 23 additions and 1 deletions

View File

@@ -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

View File

@@ -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)