diff --git a/C4/Barcodes.pm b/C4/Barcodes.pm index 957d60f283..dfca945a33 100644 --- a/C4/Barcodes.pm +++ b/C4/Barcodes.pm @@ -95,7 +95,7 @@ sub max { } sub db_max { my $self = shift; - my $query = "SELECT max(abs(barcode)) FROM items LIMIT 1"; # Possible problem if multiple barcode types populated + my $query = "SELECT max(abs(CAST(barcode AS UNSIGNED))) FROM items LIMIT 1"; # Possible problem if multiple barcode types populated my $sth = C4::Context->dbh->prepare($query); $sth->execute(); return $sth->fetchrow_array || $self->initial; diff --git a/t/db_dependent/Barcodes.t b/t/db_dependent/Barcodes.t index 2846f4fe89..b5a976b841 100755 --- a/t/db_dependent/Barcodes.t +++ b/t/db_dependent/Barcodes.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 75; +use Test::More tests => 76; use Test::Warn; use Test::MockModule; use t::lib::TestBuilder; @@ -132,6 +132,28 @@ subtest 'Test generation of hbyymmincr barcodes from DB values' => sub { $schema->storage->txn_rollback; }; +subtest 'Test generation of incremental barcodes from DB values' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + $builder->schema->resultset('Issue')->delete_all; + $builder->schema->resultset('Item')->delete_all; + my $barcodeobj; + + my $item_1 = $builder->build_sample_item( + { + barcode => '1000000000000000' + } + ); + + $barcodeobj = C4::Barcodes->new('incremental'); + + is( $barcodeobj->db_max(), '1000000000000000', "(hbyymmincr) First barcode saved to db is equal to db_max" ); + is( $barcodeobj->value(), '1000000000000001', 'incremental barcode' ); + + $schema->storage->txn_rollback; +}; $schema->storage->txn_begin;