Import prices and stock levels from DFC catalog

We were already importing stock levels from offers but now we are
looking at catalog items as well.
This commit is contained in:
Maikel Linke
2024-09-26 08:53:08 +10:00
parent 9f43244312
commit 2465780c1c
4 changed files with 33 additions and 8 deletions

View File

@@ -6,10 +6,6 @@ class CatalogItemBuilder < DfcBuilder
return if limit.blank?
# Negative stock means "on demand".
# And we are only interested in that for now.
return unless limit.to_i.negative?
if variant.stock_items.empty?
variant.stock_items << Spree::StockItem.new(
stock_location: DefaultStockLocation.find_or_create,
@@ -17,6 +13,10 @@ class CatalogItemBuilder < DfcBuilder
)
end
variant.stock_items[0].backorderable = true
if limit.to_i.negative?
variant.stock_items[0].backorderable = true
else
variant.stock_items[0].count_on_hand = limit
end
end
end

View File

@@ -15,7 +15,21 @@ class OfferBuilder < DfcBuilder
end
def self.apply(offer, variant)
variant.on_hand = offer.stockLimitation
variant.price = offer.price
return if offer.nil?
CatalogItemBuilder.apply_stock(offer, variant)
return if offer.price.nil?
variant.price = price(offer)
end
def self.price(offer)
# We assume same currency here:
if offer.price.respond_to?(:value)
offer.price.value
else
offer.price
end
end
end

View File

@@ -42,8 +42,11 @@ class SuppliedProductBuilder < DfcBuilder
product.variants.first
end.tap do |variant|
link = supplied_product.semanticId
catalog_item = supplied_product&.catalogItems&.first
offer = catalog_item&.offers&.first
variant.semantic_links.new(semantic_id: link) if link.present?
CatalogItemBuilder.apply_stock(supplied_product&.catalogItems&.first, variant)
CatalogItemBuilder.apply_stock(catalog_item, variant)
OfferBuilder.apply(offer, variant)
end
end

View File

@@ -157,6 +157,13 @@ RSpec.describe SuppliedProductBuilder do
# On-demand is expressed as negative stock.
# And some APIs send strings instead of numbers...
stockLimitation: "-1",
offers: [offer],
)
}
let(:offer) {
DataFoodConsortium::Connector::Offer.new(
nil,
price: DataFoodConsortium::Connector::Price.new(value: "15.50"),
)
}
@@ -184,6 +191,7 @@ RSpec.describe SuppliedProductBuilder do
# Stock can only be checked when persisted:
imported_product.save!
expect(imported_variant.price).to eq 15.50
expect(imported_variant.on_demand).to eq true
expect(imported_variant.on_hand).to eq 0
end