From 91e3bf9a2e2a007fa11c56131f8ff6f3546d5931 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Mon, 27 Feb 2023 17:57:22 +0000 Subject: [PATCH] Bug 31798: (follow-up) Handle all values of autoBarcode --- Koha/REST/V1/Biblios.pm | 55 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/Koha/REST/V1/Biblios.pm b/Koha/REST/V1/Biblios.pm index 3e278fae52..3f50df9841 100644 --- a/Koha/REST/V1/Biblios.pm +++ b/Koha/REST/V1/Biblios.pm @@ -305,9 +305,58 @@ sub add_item { my $item = Koha::Item->new_from_api($body); - if ( ! defined $item->barcode && C4::Context->preference('autoBarcode') eq 'incremental' ) { - my ( $barcode ) = C4::Barcodes::ValueBuilder::incremental::get_barcode; - $item->barcode($barcode); + if ( !defined $item->barcode ) { + + # FIXME This should be moved to Koha::Item->store + my $autoBarcode = C4::Context->preference('autoBarcode'); + my $barcode = ''; +## Please see file perltidy.ERR + if ( !$autoBarcode || $autoBarcode eq 'OFF' ) { + + #We do nothing + } + elsif ( $autobarcode eq 'incremental' ) { + ($barcode) = + C4::Barcodes::ValueBuilder::incremental::get_barcode; + } + elsif ( $autoBarcodeType eq 'annual' ) { + ($barcode) = + C4::Barcodes::ValueBuilder::annual::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 + my $homebranch = $item->homebranch // ''; + ($barcode) = + C4::Barcodes::ValueBuilder::hbyymmincr::get_barcode( \%args ); + $barcode = $homebranch . $barcode; + } + 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 $dbh = $params->{dbh}; + my $sth = $dbh->prepare($query); + $sth->execute(); + while ( my ($last) = $sth->fetchrow_array ) { + $nextnum = $last; + } + 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++; + } + } + else { + warn "ERROR: unknown autoBarcode: $autoBarcodeType"; + } + $item->barcode($barcode) if $barcode; } $item->store->discard_changes; -- 2.30.2