From c64aa46812a38f516986f19deb3dcfd2e1df9769 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 13 Jan 2021 17:00:35 +0100 Subject: [PATCH] Bug 27426: Keep leading zeros for barcode plugin (incremental) If autoBarcode is set to incremental, we are generating the next barcode adding 1, but casting to integer. If you have "0001" "0002" the next barcode will be "3". Should not we keep the leading zeros and generate "0003" instead? This patch suggests to keep them. If this behaviour is not the expected one we could create another barcode plugin. Test plan: Create an item with barcode 0001, another one with 0002 Set autoBarcode=incremental Create another item and click the barcode input With this patch the new barcode will be 0003 Without this patch the barcode was cast to int and resulted in 3 --- C4/Barcodes/ValueBuilder.pm | 19 +++++++++++-------- t/db_dependent/Barcodes_ValueBuilder.t | 11 ++++++++++- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/C4/Barcodes/ValueBuilder.pm b/C4/Barcodes/ValueBuilder.pm index ea8bffafa3..ecb44d995b 100644 --- a/C4/Barcodes/ValueBuilder.pm +++ b/C4/Barcodes/ValueBuilder.pm @@ -24,16 +24,19 @@ use C4::Context; sub get_barcode { my ($args) = @_; - my $nextnum; # not the best, two catalogers could add the same barcode easily this way :/ - my $query = "select max(abs(barcode)) from items"; - my $sth = C4::Context->dbh->prepare($query); - $sth->execute(); - while (my ($count)= $sth->fetchrow_array) { - $nextnum = $count; + my $query = q| + SELECT ABS(barcode), barcode + FROM items + ORDER BY ABS(barcode) DESC LIMIT 1 + |; + my $dbh = C4::Context->dbh; + my ( $barcode, $max) = $dbh->selectrow_array($query); + my $next_barcode = ++$max; + if ( $barcode && $barcode =~ m|^0| ) { + $next_barcode = sprintf "%0" . length($barcode) . "d", $next_barcode; } - $nextnum++; - return $nextnum; + return $next_barcode; } 1; diff --git a/t/db_dependent/Barcodes_ValueBuilder.t b/t/db_dependent/Barcodes_ValueBuilder.t index ef0fa3a39b..bfa2760a73 100755 --- a/t/db_dependent/Barcodes_ValueBuilder.t +++ b/t/db_dependent/Barcodes_ValueBuilder.t @@ -16,7 +16,7 @@ use Modern::Perl; -use Test::More tests => 7; +use Test::More tests => 8; use Test::MockModule; use t::lib::TestBuilder; @@ -77,5 +77,14 @@ ok(length($scr) > 0, 'hbyymmincr javascript'); is($nextnum, '2012-0035', 'annual barcode'); is($scr, undef, 'annual javascript'); +my $item_5 = $builder->build_sample_item( + { + barcode => '0033333074344564' + } +); + +($nextnum, $scr) = C4::Barcodes::ValueBuilder::incremental::get_barcode(\%args); +is($nextnum, "0033333074344565", 'incremental barcode must keep leading zeros'); + $schema->storage->txn_rollback; -- 2.20.1