Skip to content

Commit

Permalink
Move wholesale calculation for re-use
Browse files Browse the repository at this point in the history
  • Loading branch information
mkllnk committed Jan 7, 2025
1 parent ddaeff7 commit faad7fa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
23 changes: 1 addition & 22 deletions app/controllers/admin/dfc_product_imports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ def index

catalog_url = params.require(:catalog_url)
catalog = DfcCatalog.load(spree_current_user, catalog_url)
broker = FdcOfferBroker.new(catalog)
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 = catalog.products.map do |subject|
adjust_to_wholesale_price(broker, subject)

existing_variant = enterprise.supplied_variants.linked_to(subject.semanticId)

if existing_variant
Expand All @@ -43,24 +41,5 @@ def index
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
26 changes: 26 additions & 0 deletions engines/dfc_provider/app/services/dfc_catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,30 @@ def item(semantic_id)
def select_type(semantic_type)
@graph.select { |i| i.semanticType == semantic_type }
end

def apply_wholesale_values!
broker = FdcOfferBroker.new(self)
products.each do |product|
adjust_to_wholesale_price(broker, product)
end
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

0 comments on commit faad7fa

Please sign in to comment.