mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-09 03:20:21 +00:00
Calculate stock from wholesale products
This commit is contained in:
@@ -40,7 +40,10 @@ class StockSyncJob < ApplicationJob
|
||||
end
|
||||
|
||||
def perform(user, catalog_id)
|
||||
products = load_products(user, catalog_id)
|
||||
catalog = DfcCatalog.load(user, catalog_id)
|
||||
catalog.apply_wholesale_values!
|
||||
|
||||
products = catalog.products
|
||||
products_by_id = products.index_by(&:semanticId)
|
||||
product_ids = products_by_id.keys
|
||||
variants = linked_variants(user.enterprises, product_ids)
|
||||
@@ -58,13 +61,6 @@ class StockSyncJob < ApplicationJob
|
||||
end
|
||||
end
|
||||
|
||||
def load_products(user, catalog_id)
|
||||
json_catalog = DfcRequest.new(user).call(catalog_id)
|
||||
graph = DfcIo.import(json_catalog)
|
||||
|
||||
DfcCatalog.new(graph).products
|
||||
end
|
||||
|
||||
def linked_variants(enterprises, product_ids)
|
||||
Spree::Variant.where(supplier: enterprises)
|
||||
.includes(:semantic_links).references(:semantic_links)
|
||||
|
||||
@@ -31,17 +31,18 @@ class DfcCatalog
|
||||
def apply_wholesale_values!
|
||||
broker = FdcOfferBroker.new(self)
|
||||
products.each do |product|
|
||||
adjust_to_wholesale_price(broker, product)
|
||||
transformation = broker.best_offer(product.semanticId)
|
||||
|
||||
next if transformation.factor == 1
|
||||
|
||||
adjust_to_wholesale_price(product, transformation)
|
||||
adjust_to_wholesale_stock(product, transformation)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def adjust_to_wholesale_price(broker, product)
|
||||
transformation = broker.best_offer(product.semanticId)
|
||||
|
||||
return if transformation.factor == 1
|
||||
|
||||
def adjust_to_wholesale_price(product, transformation)
|
||||
wholesale_variant_price = transformation.offer.price
|
||||
|
||||
return unless wholesale_variant_price
|
||||
@@ -53,4 +54,27 @@ class DfcCatalog
|
||||
offer.price = wholesale_variant_price.dup
|
||||
offer.price.value = offer.price.value.to_f / transformation.factor
|
||||
end
|
||||
|
||||
def adjust_to_wholesale_stock(product, transformation)
|
||||
adjust_item_stock(product, transformation)
|
||||
adjust_offer_stock(product, transformation)
|
||||
end
|
||||
|
||||
def adjust_item_stock(product, transformation)
|
||||
item = product.catalogItems&.first
|
||||
wholesale_item = transformation.product.catalogItems&.first
|
||||
|
||||
return unless item && wholesale_item&.stockLimitation.present?
|
||||
|
||||
item.stockLimitation = wholesale_item.stockLimitation.to_i * transformation.factor
|
||||
end
|
||||
|
||||
def adjust_offer_stock(product, transformation)
|
||||
offer = product.catalogItems&.first&.offers&.first
|
||||
wholesale_offer = transformation.offer
|
||||
|
||||
return unless offer && wholesale_offer&.stockLimitation.present?
|
||||
|
||||
offer.stockLimiation = wholesale_offer.stockLimitation.to_i * transformation.factor
|
||||
end
|
||||
end
|
||||
|
||||
@@ -21,4 +21,25 @@ RSpec.describe DfcCatalog do
|
||||
expect(products.map(&:semanticType).uniq).to eq ["dfc-b:SuppliedProduct"]
|
||||
end
|
||||
end
|
||||
|
||||
describe "#apply_wholesale_values!" do
|
||||
let(:offer) { beans.catalogItems.first.offers.first }
|
||||
let(:catalog_item) { beans.catalogItems.first }
|
||||
let(:beans) { catalog.item(beans_id) }
|
||||
let(:beans_id) {
|
||||
"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts/44519466467635"
|
||||
}
|
||||
|
||||
it "changes price of retail variants" do
|
||||
expect { catalog.apply_wholesale_values! }.to change {
|
||||
offer.price.value.to_f.round(2)
|
||||
}.from(2.09).to(1.57) # 18.85 wholesale price divided by 12
|
||||
end
|
||||
|
||||
it "changes stock level of retail variants" do
|
||||
expect { catalog.apply_wholesale_values! }.to change {
|
||||
catalog_item.stockLimitation
|
||||
}.from("-1").to(-12)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user