Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / SL / Helper / UNECERecommendation20.pm
diff --git a/SL/Helper/UNECERecommendation20.pm b/SL/Helper/UNECERecommendation20.pm
new file mode 100644 (file)
index 0000000..e79991c
--- /dev/null
@@ -0,0 +1,59 @@
+package SL::Helper::UNECERecommendation20;
+
+use strict;
+use warnings;
+use utf8;
+
+use Exporter qw(import);
+our @EXPORT_OK = qw(map_name_to_alpha_2_code);
+
+use List::Util qw(first);
+
+my @mappings = (
+  # space and time
+  # areas
+  [ 'MTK', qr{^(?:m²|qm|quadrat *meter|quadrat *metre)$}i ],
+
+  # distances
+  [ 'CMT', qr{^(?:cm|centi *meter|centi *metre)$}i ],
+  [ 'MTR', qr{^(?:m|meter|metre)$}i ],
+  [ 'KMT', qr{^(?:km|kilo *meter|kilo *metre)$}i ],
+
+  # durations
+  [ 'SEC', qr{^(?:s|sec|second|sek|sekunde)$}i ],
+  [ 'MIN', qr{^min(?:ute)?$}i ],
+  [ 'HUR', qr{^(?:h(?:our)?|std(?:unde)?)$}i ],
+  [ 'DAY', qr{^(?:day|tag)$}i ],
+  [ 'MON', qr{^mon(?:th|at|atlich)?$}i ],
+  [ 'QAN', qr{^quart(?:er|al|alsweise)?$}i ],
+  [ 'ANN', qr{^(?:yearly|annually|jährlich|Jahr)?$}i ],
+
+  # mass
+  [ 'MGM', qr{^(?:mg|milli *gramm?)$}i ],
+  [ 'GRM', qr{^(?:g|gramm?)$}i ],
+  [ 'KGM', qr{^(?:kg|kilo *gramm?)$}i ],
+  [ 'KTN', qr{^(?:t|tonne|kilo *tonne)$}i ],
+
+  # volumes
+  [ 'MLT', qr{^(?:ml|milli *liter|milli *litre)$}i ],
+  [ 'LTR', qr{^(?:l|liter|litre)$}i ],
+
+  # miscellaneous
+  [ 'C62', qr{^(?:stck|stück|pieces?|pc|psch|pauschal|licenses?|lizenz(?:en)?)$}i ],
+);
+
+sub map_name_to_code {
+  my ($unit) = @_;
+
+  return undef if ($unit // '') eq '';
+
+  my $code = first { $unit =~ $_->[1] } @mappings;
+  return $code->[0] if $code;
+
+  no warnings 'once';
+  $::lxdebug->message(LXDebug::WARN(), "UNECERecommendation20::map_name_code: no mapping found for '$unit'");
+
+  return undef;
+}
+
+1;