mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-15 04:14:24 +00:00
Merge pull request #13049 from mkllnk/dfc-wholesale-stock
Calculate stock from DFC wholesale variants
This commit is contained in:
@@ -19,15 +19,12 @@ module Admin
|
||||
.find(params.require(:enterprise_id))
|
||||
|
||||
catalog_url = params.require(:catalog_url)
|
||||
broker = FdcOfferBroker.new(spree_current_user, catalog_url)
|
||||
catalog = DfcCatalog.load(spree_current_user, catalog_url)
|
||||
catalog.apply_wholesale_values!
|
||||
|
||||
# * First step: import all products for given enterprise.
|
||||
# * Second step: render table and let user decide which ones to import.
|
||||
imported = broker.catalog.map do |subject|
|
||||
next unless subject.is_a? DataFoodConsortium::Connector::SuppliedProduct
|
||||
|
||||
adjust_to_wholesale_price(broker, subject)
|
||||
|
||||
imported = catalog.products.map do |subject|
|
||||
existing_variant = enterprise.supplied_variants.linked_to(subject.semanticId)
|
||||
|
||||
if existing_variant
|
||||
@@ -44,24 +41,5 @@ 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
|
||||
|
||||
Reference in New Issue
Block a user