Skip to content

Commit

Permalink
feat: add support for 2 GS1 quantityContained field in nutrientDetail #…
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanegigandet authored Apr 22, 2022
1 parent bbe0ef2 commit f6c2678
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 30 deletions.
67 changes: 37 additions & 30 deletions lib/ProductOpener/GS1.pm
Original file line number Diff line number Diff line change
Expand Up @@ -960,42 +960,49 @@ sub gs1_to_off ($$$) {
my $nutrient_value;
my $nutrient_unit;

# quantityContained may be an array with a single hash
if ((defined $nutrient_detail_ref->{quantityContained}) and (ref($nutrient_detail_ref->{quantityContained}) eq "ARRAY")) {
$nutrient_detail_ref->{quantityContained} = $nutrient_detail_ref->{quantityContained}[0];
}

if (defined $nutrient_detail_ref->{quantityContained}{'#'}) {
$nutrient_value = $nutrient_detail_ref->{quantityContained}{'#'};
$nutrient_unit = $gs1_maps{measurementUnitCode}{$nutrient_detail_ref->{quantityContained}{'@'}{measurementUnitCode}};
}
elsif (defined $nutrient_detail_ref->{quantityContained}{'$t'}) {
$nutrient_value = $nutrient_detail_ref->{quantityContained}{'$t'};
$nutrient_unit = $gs1_maps{measurementUnitCode}{$nutrient_detail_ref->{quantityContained}{measurementUnitCode}};
}
else {
$log->error("gs1_to_off - unrecognized quantity contained",
{ quantityContained => $nutrient_detail_ref->{quantityContained} }) if $log->is_error();
}

# less than < modifier
if ((defined $nutrient_detail_ref->{measurementPrecisionCode})
and ($nutrient_detail_ref->{measurementPrecisionCode} eq "LESS_THAN")) {
$nutrient_value = "< " . $nutrient_value;
# quantityContained may be a single hash, or an array of hashes
# e.g. for the energy ENER- field, there are values in kJ and kcal that can be given in different ways:
# - Equadis has 2 ENER- nutrientDetail, each with a single quantityContained hash
# - Agena3000 has 1 ENER- nutrientDetail with an array of 2 quantityContained
# --> convert a single hash to an array with a hash
if ((defined $nutrient_detail_ref->{quantityContained}) and (ref($nutrient_detail_ref->{quantityContained}) ne "ARRAY")) {
$nutrient_detail_ref->{quantityContained} = [$nutrient_detail_ref->{quantityContained}];
}

foreach my $quantity_contained_ref (@{$nutrient_detail_ref->{quantityContained}}) {

# energy: based on the nutrient unit, assign the energy-kj or energy-kcal field
if ($nid eq "energy") {
if ($nutrient_unit eq "kcal") {
$nutrient_field = "energy-kcal" . $type . "_" . $per;
if (defined $quantity_contained_ref->{'#'}) {
$nutrient_value = $quantity_contained_ref->{'#'};
$nutrient_unit = $gs1_maps{measurementUnitCode}{$quantity_contained_ref->{'@'}{measurementUnitCode}};
}
elsif (defined $quantity_contained_ref->{'$t'}) {
$nutrient_value = $quantity_contained_ref->{'$t'};
$nutrient_unit = $gs1_maps{measurementUnitCode}{$quantity_contained_ref->{measurementUnitCode}};
}
else {
$nutrient_field = "energy-kj" . $type . "_" . $per;
$log->error("gs1_to_off - unrecognized quantity contained",
{ quantityContained => $quantity_contained_ref }) if $log->is_error();
}

# less than < modifier
if ((defined $nutrient_detail_ref->{measurementPrecisionCode})
and ($nutrient_detail_ref->{measurementPrecisionCode} eq "LESS_THAN")) {
$nutrient_value = "< " . $nutrient_value;
}

# energy: based on the nutrient unit, assign the energy-kj or energy-kcal field
if ($nid eq "energy") {
if ($nutrient_unit eq "kcal") {
$nutrient_field = "energy-kcal" . $type . "_" . $per;
}
else {
$nutrient_field = "energy-kj" . $type . "_" . $per;
}
}

assign_field($results_ref, $nutrient_field . "_value", $nutrient_value);
assign_field($results_ref, $nutrient_field . "_unit", $nutrient_unit);
}

assign_field($results_ref, $nutrient_field . "_value", $nutrient_value);
assign_field($results_ref, $nutrient_field . "_unit", $nutrient_unit);
}
else {
$log->error("gs1_to_off - unrecognized nutrient",
Expand Down
2 changes: 2 additions & 0 deletions t/expected_test_results/import_gs1/agena3000_andros.off.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"conservation_conditions_fr" : "A conserver à l'abri de la chaleur et de la lumière. Ne pas mettre au réfrigérateur.",
"countries" : "en:france",
"customer_service_fr" : "RELATION SERVICE CONSOMMATEUR BONNE MAMAN, ZI",
"energy-kcal_100g_unit" : "kcal",
"energy-kcal_100g_value" : "551",
"energy-kj_100g_unit" : "kJ",
"energy-kj_100g_value" : "2299",
"fat_100g_unit" : "g",
Expand Down
2 changes: 2 additions & 0 deletions t/expected_test_results/import_gs1/agena3000_aoste.off.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"conservation_conditions_fr" : "À conserver entre 0°C et 4°C.",
"countries" : "en:france",
"customer_service_fr" : "Service Consommateurs Justin Bridou, CS 50152 - 69792 Saint Priest Cedex",
"energy-kcal_100g_unit" : "kcal",
"energy-kcal_100g_value" : "418",
"energy-kj_100g_unit" : "kJ",
"energy-kj_100g_value" : "1737",
"fat_100g_unit" : "g",
Expand Down

0 comments on commit f6c2678

Please sign in to comment.