From b35c2a48ae2f6dc7908b4ef2457833ed4f1e88e1 Mon Sep 17 00:00:00 2001
From: Gerhard Gonter <ggonter@gmail.com>
Date: Sun, 10 Feb 2019 19:31:09 +0100
Subject: [PATCH] added barcode checking

---
 modules/util/Util/Barcode.pm | 56 ++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)
 create mode 100755 modules/util/Util/Barcode.pm

diff --git a/modules/util/Util/Barcode.pm b/modules/util/Util/Barcode.pm
new file mode 100755
index 0000000..998d99e
--- /dev/null
+++ b/modules/util/Util/Barcode.pm
@@ -0,0 +1,56 @@
+#!/usr/bin/perl
+
+package Util::Barcode;
+
+# see Business::Barcode::EAN13;
+
+__PACKAGE__->test() unless caller();
+
+sub test
+{
+  my @test_codes=
+  (
+    [ '4006381333931', 'see https://en.wikipedia.org/wiki/International_Article_Number#Calculation_of_checksum_digit' ],
+    [ '4006381333937', 'invalid ean13 code for testing', ],
+  );
+
+  foreach my $x (@test_codes)
+  {
+    my ($code, $note)= @$x;
+    my $type= validate($code);
+    print "validate: type=[$type] code=[$code] note=[$note]\n";
+  }
+}
+
+sub validate
+{
+  my $code= shift;
+
+  # print __LINE__, " code=[$code]\n";
+  my @digits= split ('', $code);
+
+  my $scheme;
+     if ($#digits eq 12) { $scheme= 'EAN13'; }
+  elsif ($#digits eq 11) { $scheme= 'UPC'; }
+  else { return undef; }
+
+  my $weight= 3;
+  my $sum= 0;
+  for (my $i= $#digits-1; $i >= 0; $i--)
+  {
+    $sum += $digits[$i] * $weight;
+    $weight= ($weight == 3) ? 1 : 3;
+  }
+  my $mod= $sum % 10;
+  my $check_digit= 10-$mod;
+  # print __LINE__, " sum=[$sum] mod=[$mod] check_digit=[$check_digit]\n";
+
+  $digits[$#digits]= $check_digit;
+  my $checked_code= join ('', @digits);
+  my $valid= ($code eq $checked_code) ? 1 : 0;
+  # print __LINE__, " checked_code=[$checked_code] valid=[$valid]\n";
+
+  return ($valid) ? $scheme : undef;
+}
+
+1;
-- 
GitLab