<<

NAME

ProductOpener::EnvironmentalImpact - process and analyze products

SYNOPSIS

ProductOpener::EnvironmentalImpact processes products to compute their environmental impact (see french environmental labeling Ecobalyse).

    use ProductOpener::EnvironmentalImpact qw/:all/;

        [..]

        estimate_environmental_impact($product_ref);

DESCRIPTION

[..]

FUNCTIONS

estimate_environmental_impact_service ( $product_ref, $updated_product_fields_ref, $errors_ref, $skip_ecobalyse_call = 0 )

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

Arguments

$product_ref

product object reference

$updated_product_fields_ref

reference to a hash of product fields that have been created or updated

$errors_ref

reference to an array of error messages

$skip_ecobalyse_call

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_ecobalyse_coverage_misc_tags ($product_ref)

Set misc_tags for Ecobalyse ingredients coverage ranges.

get_ecobalyse_reference_quantity ($product_ref)

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.

Return values

($quantity, $unit, $source) with $unit = "g" or "ml", and $source = "product_quantity" or "default"

format_ecs_value ($value)

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).

get_ecobalyse_ingredients_quantities ($product_ref)

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).

Arguments

$product_ref

Return value

A hash reference with:

ingredients

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).

total_quantity

Sum of the quantities of the leaf ingredients.

total_quantity_with_ecobalyse_id

Sum of the quantities of the leaf ingredients that have an Ecobalyse id or proxy id.

get_ecobalyse_packaging_entry ($product_ref)

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.

get_ecobalyse_transformation_entries ($product_ref)

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_ecobalyse_response_for_open_data ( $response_ref )

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.

<<