Koha/C4/Barcodes/ValueBuilder.pm
William Lavoie db067d48ee
Bug 19113: Barcode value builder not working with numeric branchcode
The regexp for the value builder 'hbyymmincr' doesn't allow for numerics in the branchcode,
so the nextnum found is always 0 and multiple Add always generate the same barcode.
This simply allow for numerics within the regex.

Test
- It's a bit complicated.  You need
   - all your branchcodes to be numerics (01,02,71...)
   - all barcodes to already be a bunch of numbers
- Modify the syspref 'autobarcode' to hbyymmincr, the home branch + date one.
- Find an item in the pro-search,
- New -> New Item
- Click on the 'p' to have the value builder create the next barcode.
   - Without the patch, the query won't have found anything, so it'll suggest xxxxxx0001
- Click Add.  It reprensent the form to enter another one.
   - Click on the 'p' edit box, it'll offer you again xxxxxx0001
With the patch, it'll offer valid ones everytime.

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
2025-02-17 15:06:03 +01:00

108 lines
3.4 KiB
Perl

package C4::Barcodes::ValueBuilder;
# Copyright 2008-2010 Foundations Bible College
# Parts copyright 2012 C & P Bibliography Services
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
package C4::Barcodes::ValueBuilder::incremental;
use Modern::Perl;
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(cast(barcode as unsigned)) from items";
my $sth = C4::Context->dbh->prepare($query);
$sth->execute();
while ( my ($count) = $sth->fetchrow_array ) {
$nextnum = $count;
}
$nextnum++;
return $nextnum;
}
1;
package C4::Barcodes::ValueBuilder::hbyymmincr;
use C4::Context;
sub get_barcode {
my ($args) = @_;
my $nextnum = 0;
my $year = substr( $args->{year}, -2 );
my $month = $args->{mon};
my $query = "SELECT MAX(CAST(SUBSTRING(barcode,-4) AS signed)) AS number FROM items WHERE barcode REGEXP ?";
my $sth = C4::Context->dbh->prepare($query);
$sth->execute("^[-a-zA-Z0-9]{1,}$year$month");
while ( my ($count) = $sth->fetchrow_array ) {
$nextnum = $count if $count;
$nextnum = 0 if $nextnum == 9999; # this sequence only allows for cataloging 9999 items per month
}
$nextnum++;
$nextnum = sprintf( "%0*d", "4", $nextnum );
$nextnum = $year . $month . $nextnum;
my $scr = qq~
let elt = \$("#"+id);
let homebranch = elt.parents('fieldset.rows:first')
.find('input[name="kohafield"][value="items.homebranch"]')
.siblings("select")
.val();
if(typeof offset == 'undefined'){
var offset = 0;
}
if ( \$(elt).val() == '' ) {
\$(elt).val(homebranch + ($nextnum + offset));
}
~;
return $nextnum, $scr;
}
package C4::Barcodes::ValueBuilder::annual;
use C4::Context;
sub get_barcode {
my ($args) = @_;
my $nextnum;
my $query = "select max(cast( substring_index(barcode, '-',-1) as signed)) from items where barcode like ?";
my $sth = C4::Context->dbh->prepare($query);
$sth->execute( $args->{year} . '-%' );
while ( my ($count) = $sth->fetchrow_array ) {
$nextnum = $count if $count;
}
$nextnum++;
$nextnum = sprintf( "%0*d", "4", $nextnum );
$nextnum = "$args->{year}-$nextnum";
return $nextnum;
}
1;
=head1 Barcodes::ValueBuilder
This module is intended as a shim to ease the eventual transition from
having all barcode-related code in the value builder plugin .pl file
to using C4::Barcodes. Since the shift will require a rather significant
amount of refactoring, this module will return value builder-formatted
results, at first by merely running the code that was formerly in the
barcodes.pl value builder, but later by using C4::Barcodes.
=cut
1;