Skip to content

Commit

Permalink
Calculate stock from wholesale products
Browse files Browse the repository at this point in the history
  • Loading branch information
mkllnk committed Dec 20, 2024
1 parent 2fd21ff commit edc4d68
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
12 changes: 4 additions & 8 deletions app/jobs/stock_sync_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ def self.catalog_ids(order)
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)
Expand All @@ -58,13 +61,6 @@ def perform(user, catalog_id)
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)
Expand Down
36 changes: 30 additions & 6 deletions engines/dfc_provider/app/services/dfc_catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@ def select_type(semantic_type)
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
Expand All @@ -53,4 +54,27 @@ def adjust_to_wholesale_price(broker, product)
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 changes: 21 additions & 0 deletions engines/dfc_provider/spec/services/dfc_catalog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,25 @@
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

0 comments on commit edc4d68

Please sign in to comment.