Bug 10330 - Rename system preference authoritysep to AuthoritySeperator
[koha.git] / cataloguing / value_builder / barcode.pl
index e21105b..5aeae9f 100755 (executable)
@@ -1,8 +1,6 @@
 #!/usr/bin/perl
-
-# $Id: barcode.pl,v 1.1.2.2 2006/09/20 02:24:42 kados Exp $
-
 # Copyright 2000-2002 Katipo Communications
+# Parts copyright 2008-2010 Foundations Bible College
 #
 # This file is part of Koha.
 #
 # with Koha; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
-#use strict;
-#use warnings; FIXME - Bug 2505
+use strict;
+use warnings;
+no warnings 'redefine'; # otherwise loading up multiple plugins fills the log with subroutine redefine warnings
+
 use C4::Context;
+require C4::Barcodes::ValueBuilder;
 require C4::Dates;
+
+use Algorithm::CheckDigits;
+
 my $DEBUG = 0;
 
 =head1
@@ -30,6 +34,7 @@ my $DEBUG = 0;
 plugin_parameters : other parameters added when the plugin is called by the dopop function
 
 =cut
+
 sub plugin_parameters {
 #   my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
     return "";
@@ -49,17 +54,18 @@ returns :
 the 3 scripts are inserted after the <input> in the html code
 
 =cut
+
 sub plugin_javascript {
        my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
        my $function_name= "barcode".(int(rand(100000))+1);
+    my %args;
 
        # find today's date
-       my ($year, $mon, $day) = split('-', C4::Dates->today('iso'));
-       my ($tag,$subfield)       =  GetMarcFromKohaField("items.barcode", '');
-       my ($loctag,$locsubfield) =  GetMarcFromKohaField("items.homebranch", '');
+    ($args{year}, $args{mon}, $args{day}) = split('-', C4::Dates->today('iso'));
+    ($args{tag},$args{subfield})       =  GetMarcFromKohaField("items.barcode", '');
+    ($args{loctag},$args{locsubfield}) =  GetMarcFromKohaField("items.homebranch", '');
 
        my $nextnum;
-       my $query;
     my $scr;
        my $autoBarcodeType = C4::Context->preference("autoBarcode");
     warn "Barcode type = $autoBarcodeType" if $DEBUG;
@@ -74,50 +80,34 @@ sub plugin_javascript {
         </script>");
     }
        if ($autoBarcodeType eq 'annual') {
-               $query = "select max(cast( substring_index(barcode, '-',-1) as signed)) from items where barcode like ?";
-               my $sth=$dbh->prepare($query);
-               $sth->execute("$year%");
-               while (my ($count)= $sth->fetchrow_array) {
-            warn "Examining Record: $count" if $DEBUG;
-               $nextnum = $count if $count;
-               }
-               $nextnum++;
-               $nextnum = sprintf("%0*d", "4",$nextnum);
-               $nextnum = "$year-$nextnum";
+        ($nextnum, $scr) = C4::Barcodes::ValueBuilder::annual::get_barcode(\%args);
        }
        elsif ($autoBarcodeType eq 'incremental') {
-               # not the best, two catalogers could add the same barcode easily this way :/
-               $query = "select max(abs(barcode)) from items";
-        my $sth = $dbh->prepare($query);
-               $sth->execute();
-               while (my ($count)= $sth->fetchrow_array) {
-                       $nextnum = $count;
-               }
-               $nextnum++;
+        ($nextnum, $scr) = C4::Barcodes::ValueBuilder::incremental::get_barcode(\%args);
     }
     elsif ($autoBarcodeType eq 'hbyymmincr') {      # Generates a barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number, reset yearly -fbcit
-        $year = substr($year, -2);
-        $query = "SELECT MAX(CAST(SUBSTRING(barcode,7,4) AS signed)) FROM items WHERE barcode REGEXP ?";
+        ($nextnum, $scr) = C4::Barcodes::ValueBuilder::hbyymmincr::get_barcode(\%args);
+    }
+    elsif ($autoBarcodeType eq 'EAN13') {
+        # not the best, two catalogers could add the same barcode easily this way :/
+        my $query = "select max(abs(barcode)) from items";
         my $sth = $dbh->prepare($query);
-        $sth->execute("^[a-zA-Z]{1,}$year");
-        while (my ($count)= $sth->fetchrow_array) {
-            $nextnum = $count if $count;
-            warn "Existing incremental number = $nextnum" if $DEBUG;
+        $sth->execute();
+        while (my ($last)= $sth->fetchrow_array) {
+            $nextnum = $last;
         }
-        $nextnum++;
-        $nextnum = sprintf("%0*d", "4",$nextnum);
-        $nextnum = $year . $mon . $nextnum;
-        warn "New hbyymmincr Barcode = $nextnum" if $DEBUG;
-        $scr = " 
-        for (i=0 ; i<document.f.field_value.length ; i++) {
-            if (document.f.tag[i].value == '$loctag' && document.f.subfield[i].value == '$locsubfield') {
-                fnum = i;
-            }
+        my $ean = CheckDigits('ean');
+        if ( $ean->is_valid($nextnum) ) {
+            my $next = $ean->basenumber( $nextnum ) + 1;
+            $nextnum = $ean->complete( $next );
+            $nextnum = '0' x ( 13 - length($nextnum) ) . $nextnum; # pad zeros
+        } else {
+            warn "ERROR: invalid EAN-13 $nextnum, using increment";
+            $nextnum++;
         }
-        if (\$('#' + id).val() == '' || force) {
-            \$('#' + id).val(document.f.field_value[fnum].value + '$nextnum');
-        }
-        ";
+    }
+    else {
+        warn "ERROR: unknown autoBarcode: $autoBarcodeType";
     }
 
     # default js body (if not filled by hbyymmincr)