-
-
Notifications
You must be signed in to change notification settings - Fork 729
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13049 from mkllnk/dfc-wholesale-stock
Calculate stock from DFC wholesale variants
- Loading branch information
Showing
10 changed files
with
169 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# frozen_string_literal: true | ||
|
||
class DfcCatalog | ||
def self.load(user, catalog_url) | ||
api = DfcRequest.new(user) | ||
catalog_json = api.call(catalog_url) | ||
graph = DfcIo.import(catalog_json) | ||
|
||
new(graph) | ||
end | ||
|
||
def initialize(graph) | ||
@graph = graph | ||
end | ||
|
||
def products | ||
@products ||= @graph.select do |subject| | ||
subject.is_a? DataFoodConsortium::Connector::SuppliedProduct | ||
end | ||
end | ||
|
||
def item(semantic_id) | ||
@items ||= @graph.index_by(&:semanticId) | ||
@items[semantic_id] | ||
end | ||
|
||
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| | ||
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(product, transformation) | ||
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 | ||
|
||
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.stockLimitation = wholesale_offer.stockLimitation.to_i * transformation.factor | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../spec_helper" | ||
|
||
RSpec.describe DfcCatalog do | ||
subject(:catalog) { | ||
VCR.use_cassette(:fdc_catalog) { | ||
DfcCatalog.load(user, catalog_url) | ||
} | ||
} | ||
let(:user) { build(:testdfc_user) } | ||
let(:catalog_url) { | ||
"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts" | ||
} | ||
|
||
describe "#products" do | ||
let(:products) { catalog.products } | ||
|
||
it "returns only products" do | ||
expect(products.count).to eq 4 | ||
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 variant's catalog item" do | ||
expect { catalog.apply_wholesale_values! }.to change { | ||
catalog_item.stockLimitation | ||
}.from("-1").to(-12) | ||
end | ||
|
||
it "changes stock level of retail variant's offer" do | ||
wholesale_offer = catalog.item( | ||
"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts/44519466500403/Offer" | ||
) | ||
wholesale_offer.stockLimitation = 2 | ||
|
||
expect { catalog.apply_wholesale_values! }.to change { | ||
offer.stockLimitation | ||
}.from(nil).to(24) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters