ProductOpener::EnvironmentalImpact - process and analyze products
ProductOpener::EnvironmentalImpact processes products to compute their environmental impact (see french environmental labeling Ecobalyse).
use ProductOpener::EnvironmentalImpact qw/:all/;
[..]
estimate_environmental_impact($product_ref);
[..]
Compute the environmental impact of a given product (see the french environmental environmental labeling Ecobalyse).
This function is a product service that can be run through ProductOpener::ApiProductServices
product object reference
reference to a hash of product fields that have been created or updated
reference to an array of error messages
Boolean flag indicating whether to skip the Ecobalyse API call, in which case we only prepare the request payload and store it in the product.
Set misc_tags for Ecobalyse ingredients coverage ranges.
Determine the quantity of product for which we send the ingredients and packaging to Ecobalyse.
Ecobalyse packaging entries are for a whole package (e.g. a 500 g tray), so we send the ingredients for the whole product, so that the returned impacts are consistent for the whole product. The impacts per 100 g or 100 ml are then computed from the impacts for the whole product.
The reference quantity is: - the product quantity (in g or ml) if it is known, - otherwise 100 g or 100 ml (depending on the nutrition data of the product) and in that case we do not send the packaging, and we compute only the impacts per 100 g or 100 ml.
($quantity, $unit, $source) with $unit = "g" or "ml", and $source = "product_quantity" or "default"
Round an Ecobalyse environmental cost (ECS) value for display: 1 decimal for values below 100, no decimals for values of 100 and more.
Returns an empty string if the value is not a number (e.g. unexpected value from the Ecobalyse API).
Return the leaf ingredients of a product (ingredients without sub-ingredients) with the quantity and Ecobalyse id that are used to compute the environmental impact.
The quantity is the quantity_estimate if available (not set by the current product opener % estimation), or the percent_estimate. Leaf ingredients without a quantity are not counted in the totals (they are not sent to Ecobalyse).
A hash reference with:
Reference to an array of hashes, one for each leaf ingredient: ingredient (reference to the ingredient), quantity (undef if no quantity), ecobalyse_id (ecobalyse_id or ecobalyse_proxy_id, undef if none).
Sum of the quantities of the leaf ingredients.
Sum of the quantities of the leaf ingredients that have an Ecobalyse id or proxy id.
Select the best-matching Ecobalyse packaging entry for a product, based on its categories, packaging materials, shapes and quantity.
The match is scored with the following weighted factors: - Material match: 100 (exact), 80 (is_a parent/child in either direction), 0 - Shape match: 70 (exact), 56 (is_a parent/child in either direction), 0 - Category specificity: raw taxonomy level (0-10) of the matched category, via get_taxonomy_tag_level() (0 for category-less entries) - Quantity distance: 0-5, only counted if material and shape both match - ECS tiebreaker: higher ecs wins when total scores are equal
The selected entry (or undef when no candidate matched) is returned as a hashref.
Return the list of Ecobalyse transformation entries applicable to the product, based on the ingredients_processing properties of its categories.
Each returned entry is a hashref with keys: id, name, name_fr, ecs, unit. Returns an empty list when no transformation applies.
The ingredients_processing:en property of a category overrides the property of its parent categories (e.g. xx:sashimi has the value en:none to cancel the en:cooked value of en:meals). Properties of categories that are not parents of each other are combined (e.g. en:canned-soups is both a canned food and a meal).
A transformation that includes another one (e.g. canning includes cooking) supersedes it.
Filter an Ecobalyse API response so that only the data suitable for the OFF open-data database is retained.
The following transformations are applied:
- The query key is removed (it is already stored separately in ecobalyse_input).
- Only the contents of the results key are kept; all other top-level keys (description, webUrl, …) are dropped.
- For every hash that contains an ecs key, all other keys are removed so that only ecs remains. This pruning is applied recursively throughout the results sub-structure.
The original hash is not modified; a new hash reference is returned.