Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.3.3: add support for "free of charge" UnpricedItemType #113

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion im_onix.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

Gem::Specification.new do |spec|
spec.name = "im_onix"
spec.version = "1.3.2"
spec.version = "1.3.3"
spec.authors = ["Julien Boulnois"]
spec.email = ["[email protected]"]

Expand Down
1 change: 1 addition & 0 deletions lib/onix/onix21.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ class SupplyDetail < SubsetDSL
:shortcut => :availability_date,
:parse_lambda => lambda { |v| Date.strptime(v, "%Y%m%d") }
}
element "UnpricedItemType", :subset, :cardinality => 0..1
elements "Price", :subset

def available?
Expand Down
84 changes: 54 additions & 30 deletions lib/onix/product_supplies_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,31 @@ module ProductSuppliesMethods
# :tax=>{:amount=>int, :rate_percent=>float}}]}]
def supplies(keep_all_prices_dates = false)
supplies = []
free_items = []

# add territories if missing
if self.product_supplies
self.product_supplies.each do |product_supply|
product_supply.supply_details.each do |supply_detail|

availability_date = supply_detail.availability_date
unless availability_date
if product_supply.availability_date
availability_date = product_supply.market_publishing_detail.availability_date
end
end
unless availability_date
if @publishing_detail
availability_date = @publishing_detail.publication_date
end
end

supply_detail.prices.each do |price|
supply = {}
supply[:suppliers] = supply_detail.suppliers.map(&:name)
supply[:available] = supply_detail.available?
supply[:availability_date] = supply_detail.availability_date
supply[:availability_date] = availability_date

unless supply[:availability_date]
if product_supply.availability_date
supply[:availability_date] = product_supply.market_publishing_detail.availability_date
end
end
supply[:price] = price.amount
supply[:qualifier] = price.qualifier.human if price.qualifier
supply[:including_tax] = price.including_tax?
Expand All @@ -58,18 +67,33 @@ def supplies(keep_all_prices_dates = false)
supply[:currency] = price.currency
supply[:tax] = price.tax

unless supply[:availability_date]
if @publishing_detail
supply[:availability_date] = @publishing_detail.publication_date
end
end

supplies << supply
end

if supply_detail.unpriced_item_type && supply_detail.unpriced_item_type.human == "FreeOfCharge"
free_items << {
:suppliers => supply_detail.suppliers,
:available => supply_detail.available?,
:availability_date => availability_date,
:currency => "EUR",
:including_tax => true,
:price => 0,
:from_date => nil,
:until_date => nil,
:territory => product_supply.markets ? product_supply.markets.map { |m| m.territory.countries }.flatten.uniq : []
}
end
end
end
end

# filter on availability, date, type and territories because suppliers are always the same
free_items.uniq! do |i|
i.select { |k, v| [:available, :availability_date, :territory].include?(k) }.hash
end

supplies += free_items

grouped_supplies = {}
supplies.each do |supply|
supply[:territory].each do |territory|
Expand Down Expand Up @@ -130,22 +154,22 @@ def supplies(keep_all_prices_dates = false)

grouped_territories_supplies.values.each do |supplies|
fsupply = supplies.first.first
final_supplies << {:including_tax => fsupply[:including_tax], :currency => fsupply[:currency],
:territory => supplies.map { |fs| fs.map { |s| s[:territory] } }.flatten.uniq,
:available => fsupply[:available],
:availability_date => fsupply[:availability_date],
:suppliers => fsupply[:suppliers],
:prices => supplies.first.map { |s|
s[:amount] = s[:price]
s.delete(:price)
s.delete(:available)
s.delete(:currency)
s.delete(:availability_date)
s.delete(:including_tax)
s.delete(:territory)
s.delete(:suppliers)
s
}}
final_supplies << { :including_tax => fsupply[:including_tax], :currency => fsupply[:currency],
:territory => supplies.map { |fs| fs.map { |s| s[:territory] } }.flatten.uniq,
:available => fsupply[:available],
:availability_date => fsupply[:availability_date],
:suppliers => fsupply[:suppliers],
:prices => supplies.first.map { |s|
s[:amount] = s[:price]
s.delete(:price)
s.delete(:available)
s.delete(:currency)
s.delete(:availability_date)
s.delete(:including_tax)
s.delete(:territory)
s.delete(:suppliers)
s
} }
end

final_supplies
Expand Down Expand Up @@ -245,7 +269,7 @@ def at_time_price_amount_for(time, currency, country = nil)
# exclusive
sup = sups.first[:prices].select { |p|
(!p[:from_date] or p[:from_date].to_date <= time.to_date) and
(!p[:until_date] or p[:until_date].to_date > time.to_date)
(!p[:until_date] or p[:until_date].to_date > time.to_date)
}.first

if sup
Expand All @@ -254,7 +278,7 @@ def at_time_price_amount_for(time, currency, country = nil)
# or inclusive
sup = sups.first[:prices].select { |p|
(!p[:from_date] or p[:from_date].to_date <= time.to_date) and
(!p[:until_date] or p[:until_date].to_date >= time.to_date)
(!p[:until_date] or p[:until_date].to_date >= time.to_date)
}.first

if sup
Expand Down
207 changes: 207 additions & 0 deletions test/fixtures/test_prices_unpriced.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
<Product>
<RecordReference>xxx</RecordReference>
<NotificationType>03</NotificationType>
<ProductIdentifier>
<ProductIDType>03</ProductIDType>
<IDValue>978123456789</IDValue>
</ProductIdentifier>
<DescriptiveDetail>
<ProductComposition>00</ProductComposition>
<ProductForm>EA</ProductForm>
<ProductFormDetail>E107</ProductFormDetail>
<EpubTechnicalProtection>03</EpubTechnicalProtection>
<TitleDetail>
<TitleType>01</TitleType>
<TitleElement>
<TitleElementLevel>01</TitleElementLevel>
<TitleText>XXX</TitleText>
</TitleElement>
</TitleDetail>
<Contributor>
<SequenceNumber>1</SequenceNumber>
<ContributorRole>A01</ContributorRole>
<PersonName>XXX XXX</PersonName>
</Contributor>
<NoEdition/>
<Language>
<LanguageRole>01</LanguageRole>
<LanguageCode>fre</LanguageCode>
</Language>
</DescriptiveDetail>
<CollateralDetail>
<TextContent>
<TextType>03</TextType>
<ContentAudience>00</ContentAudience>
<Text>XXX
</Text>
</TextContent>
</CollateralDetail>
<PublishingDetail>
<Publisher>
<PublishingRole>01</PublishingRole>
<PublisherIdentifier>
<PublisherIDType>06</PublisherIDType>
<IDValue>xxx</IDValue>
</PublisherIdentifier>
<PublisherName>XXX</PublisherName>
</Publisher>
<PublishingStatus>04</PublishingStatus>
<PublishingDate>
<PublishingDateRole>01</PublishingDateRole>
<DateFormat>14</DateFormat>
<Date>20110601T000000+0200</Date>
</PublishingDate>
<SalesRights>
<SalesRightsType>01</SalesRightsType>
<Territory>
<CountriesIncluded>FR GF GP MC MQ NC PF PM ES IT PT</CountriesIncluded>
</Territory>
</SalesRights>
</PublishingDetail>
<ProductSupply>
<Market>
<Territory>
<CountriesIncluded>FR GF GP MC MQ NC PF PM</CountriesIncluded>
</Territory>
</Market>
<MarketPublishingDetail>
<MarketPublishingStatus>04</MarketPublishingStatus>
<MarketDate>
<MarketDateRole>11</MarketDateRole>
<Date>20150401</Date>
</MarketDate>
</MarketPublishingDetail>
<SupplyDetail>
<Supplier>
<SupplierRole>01</SupplierRole>
<SupplierName>Vendu Livre</SupplierName>
</Supplier>
<ProductAvailability>20</ProductAvailability>
<SupplyDate>
<SupplyDateRole>08</SupplyDateRole>
<Date>20150401</Date>
</SupplyDate>
<UnpricedItemType>01</UnpricedItemType>
</SupplyDetail>
<SupplyDetail>
<Supplier>
<SupplierRole>01</SupplierRole>
<SupplierName>Vendu Livre</SupplierName>
</Supplier>
<ProductAvailability>20</ProductAvailability>
<SupplyDate>
<SupplyDateRole>08</SupplyDateRole>
<Date>20150401</Date>
</SupplyDate>
<UnpricedItemType>01</UnpricedItemType>
</SupplyDetail>
<SupplyDetail>
<Supplier>
<SupplierRole>01</SupplierRole>
<SupplierName>Vendu Livre</SupplierName>
</Supplier>
<ProductAvailability>20</ProductAvailability>
<SupplyDate>
<SupplyDateRole>08</SupplyDateRole>
<Date>20150401</Date>
</SupplyDate>
<UnpricedItemType>01</UnpricedItemType>
</SupplyDetail>
<SupplyDetail>
<Supplier>
<SupplierRole>01</SupplierRole>
<SupplierName>Vendu Livre</SupplierName>
</Supplier>
<ProductAvailability>20</ProductAvailability>
<SupplyDate>
<SupplyDateRole>08</SupplyDateRole>
<Date>20150401</Date>
</SupplyDate>
<UnpricedItemType>01</UnpricedItemType>
</SupplyDetail>
<SupplyDetail>
<Supplier>
<SupplierRole>01</SupplierRole>
<SupplierName>Vendu Livre</SupplierName>
</Supplier>
<ProductAvailability>20</ProductAvailability>
<SupplyDate>
<SupplyDateRole>08</SupplyDateRole>
<Date>20150401</Date>
</SupplyDate>
<UnpricedItemType>01</UnpricedItemType>
</SupplyDetail>
<SupplyDetail>
<Supplier>
<SupplierRole>01</SupplierRole>
<SupplierName>Vendu Livre</SupplierName>
</Supplier>
<ProductAvailability>20</ProductAvailability>
<SupplyDate>
<SupplyDateRole>08</SupplyDateRole>
<Date>20150401</Date>
</SupplyDate>
<UnpricedItemType>01</UnpricedItemType>
</SupplyDetail>
<SupplyDetail>
<Supplier>
<SupplierRole>01</SupplierRole>
<SupplierName>Vendu Livre</SupplierName>
</Supplier>
<ProductAvailability>20</ProductAvailability>
<SupplyDate>
<SupplyDateRole>08</SupplyDateRole>
<Date>20150401</Date>
</SupplyDate>
<UnpricedItemType>01</UnpricedItemType>
</SupplyDetail>
<SupplyDetail>
<Supplier>
<SupplierRole>01</SupplierRole>
<SupplierName>Vendu Livre</SupplierName>
</Supplier>
<ProductAvailability>20</ProductAvailability>
<SupplyDate>
<SupplyDateRole>08</SupplyDateRole>
<Date>20150401</Date>
</SupplyDate>
<UnpricedItemType>01</UnpricedItemType>
</SupplyDetail>
</ProductSupply>
<ProductSupply>
<Market>
<Territory>
<CountriesIncluded>ES IT PT</CountriesIncluded>
</Territory>
</Market>
<MarketPublishingDetail>
<MarketPublishingStatus>04</MarketPublishingStatus>
<MarketDate>
<MarketDateRole>11</MarketDateRole>
<Date>20150401</Date>
</MarketDate>
</MarketPublishingDetail>
<SupplyDetail>
<Supplier>
<SupplierRole>01</SupplierRole>
<SupplierName>Vendu Livre</SupplierName>
</Supplier>
<ProductAvailability>20</ProductAvailability>
<Price>
<PriceType>04</PriceType>
<PriceAmount>10.99</PriceAmount>
<Tax>
<TaxType>01</TaxType>
<TaxRatePercent>5.5</TaxRatePercent>
<TaxAmount>1.09</TaxAmount>
</Tax>
<CurrencyCode>EUR</CurrencyCode>
<PriceDate>
<PriceDateRole>14</PriceDateRole>
<DateFormat>14</DateFormat>
<Date>20131001</Date>
</PriceDate>
</Price>
</SupplyDetail>
</ProductSupply>
</Product>
Loading