X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=cataloguing%2Fvalue_builder%2Fbarcode.pl;h=5aeae9f61e8360d584d1c2222ac38935afd266a1;hb=ad6d36776362a398a46acef0d00ca67a7d7a11c7;hp=c05ec48b266c6989530c8f50c26a98036b2686d5;hpb=8ca5d5ff85c429ee972b3f80f91f5b27a58cf5d5;p=koha.git diff --git a/cataloguing/value_builder/barcode.pl b/cataloguing/value_builder/barcode.pl index c05ec48b26..5aeae9f61e 100755 --- a/cataloguing/value_builder/barcode.pl +++ b/cataloguing/value_builder/barcode.pl @@ -22,8 +22,11 @@ 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 @@ -55,14 +58,14 @@ the 3 scripts are inserted after the in the html code 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; @@ -77,51 +80,34 @@ sub plugin_javascript { "); } 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,-4) AS signed)) AS number 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; - $nextnum = 0 if $nextnum == 9999; # this sequence only allows for cataloging 10000 books per month - 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 ; iis_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)