From 66ed58dfa04e14f5a74b4e28b103b31517c6851e Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Fri, 29 Jan 2010 15:42:45 +0000 Subject: [PATCH] new function add_codabar_checkdigit -- add_codabar_checkdigit -- $barcode source barcode -- -- If the source string is 13 or 14 characters long and contains only digits, adds or replaces the 14 -- character with a checkdigit computed according to the usual algorithm for library barcodes -- using the Codabar symbology - see . If the -- input string does not meet those requirements, it is returned unchanged. --- sql/base/base.sql | 21 +++++++++++++++++++++ 1 files changed, 21 insertions(+), 0 deletions(-) diff --git a/sql/base/base.sql b/sql/base/base.sql index 06bebff..fa60773 100644 --- a/sql/base/base.sql +++ b/sql/base/base.sql @@ -671,3 +671,24 @@ CREATE OR REPLACE FUNCTION migration_tools.attempt_money (TEXT,TEXT) RETURNS NUM END; $$ LANGUAGE PLPGSQL STRICT STABLE; +-- add_codabar_checkdigit +-- $barcode source barcode +-- +-- If the source string is 13 or 14 characters long and contains only digits, adds or replaces the 14 +-- character with a checkdigit computed according to the usual algorithm for library barcodes +-- using the Codabar symbology - see . If the +-- input string does not meet those requirements, it is returned unchanged. +-- +CREATE OR REPLACE FUNCTION migration_tools.add_codabar_checkdigit (TEXT) RETURNS TEXT AS $$ + my $barcode = shift; + + return $barcode if $barcode !~ /^\d{13,14}$/; + $barcode = substr($barcode, 0, 13); # ignore 14th digit + my @digits = split //, $barcode; + my $total = 0; + $total += $digits[$_] foreach (1, 3, 5, 7, 9, 11); + $total += (2 * $digits[$_] > 10) ? (2 * $digits[$_] - 9) : (2 * $digits[$_]) foreach (0, 2, 4, 6, 8, 10, 12); + my $remainder = $total % 10; + my $checkdigit = ($remainder == 0) ? $remainder : 10 - $remainder; + return $barcode . $checkdigit; +$$ LANGUAGE PLPERL STRICT STABLE; -- 1.7.2.5