mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-31 21:37:16 +00:00
Calculate wholesale price for imported retail variants
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user