From: Galen Charlton Date: Mon, 22 Sep 2014 18:44:03 +0000 (-0400) Subject: add routine to calculate Code 39 mod 43 barcode checkdigits X-Git-Url: http://git.equinoxoli.org/?p=migration-tools.git;a=commitdiff_plain;h=41eb73e7b28d8b05c6f9a150e7e497badae8cdd4 add routine to calculate Code 39 mod 43 barcode checkdigits migration_tools.add_code39mod43_checkdigit($barcode); If the source string is 13 or 14 characters long and contains only valid Code 39 mod 43 characters, adds or replaces the 14th character with a checkdigit computed according to the usual algorithm for library barcodes using the Code 39 mod 43 symbology - see . If the input string does not meet those requirements, it is returned unchanged. Signed-off-by: Galen Charlton --- diff --git a/sql/base/base.sql b/sql/base/base.sql index 751ce72..916686a 100644 --- a/sql/base/base.sql +++ b/sql/base/base.sql @@ -724,6 +724,31 @@ CREATE OR REPLACE FUNCTION migration_tools.add_codabar_checkdigit (TEXT) RETURNS return $barcode . $checkdigit; $$ LANGUAGE PLPERLU STRICT STABLE; +-- add_code39mod43_checkdigit +-- $barcode source barcode +-- +-- If the source string is 13 or 14 characters long and contains only valid +-- Code 39 mod 43 characters, adds or replaces the 14th +-- character with a checkdigit computed according to the usual algorithm for library barcodes +-- using the Code 39 mod 43 symbology - see . If the +-- input string does not meet those requirements, it is returned unchanged. +-- +CREATE OR REPLACE FUNCTION migration_tools.add_code39mod43_checkdigit (TEXT) RETURNS TEXT AS $$ + my $barcode = shift; + + return $barcode if $barcode !~ /^[0-9A-Z. $\/+%-]{13,14}$/; + $barcode = substr($barcode, 0, 13); # ignore 14th character + + my @valid_chars = split //, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%'; + my %nums = map { $valid_chars[$_] => $_ } (0..42); + + my $total = 0; + $total += $nums{$_} foreach split(//, $barcode); + my $remainder = $total % 43; + my $checkdigit = $valid_chars[$remainder]; + return $barcode . $checkdigit; +$$ LANGUAGE PLPERLU STRICT STABLE; + CREATE OR REPLACE FUNCTION migration_tools.attempt_phone (TEXT,TEXT) RETURNS TEXT AS $$ DECLARE phone TEXT := $1;