Koha/C4/Barcodes/ValueBuilder.pm
Julian Maurice b168f4a2e9 Bug 21395: Make perlcritic happy
This patch adds a .perlcriticrc (copied from qa-test-tools) and fixes
almost all perlcrictic violations according to this .perlcriticrc
The remaining violations are silenced out by appending a '## no critic'
to the offending lines. They can still be seen by using the --force
option of perlcritic
This patch also modify t/00-testcritic.t to check all Perl files using
the new .perlcriticrc.
I'm not sure if this test script is still useful as it is now equivalent
to `perlcritic --quiet .` and it looks like it is much slower
(approximatively 5 times slower on my machine)

Test plan:
1. Run `perlcritic --quiet .` from the root directory. It should output
   nothing
2. Run `perlcritic --quiet --force .`. It should output 7 errors (6
   StringyEval, 1 BarewordFileHandles)
3. Run `TEST_QA=1 prove t/00-testcritic.t`
4. Read the patch. Check that all changes make sense and do not
   introduce undesired behaviour

Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2020-06-29 12:37:02 +02:00

119 lines
3.7 KiB
Perl

#!/usr/bin/perl
#
# 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;
my $DEBUG = 0;
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;
}
$nextnum++;
return $nextnum;
}
1;
package C4::Barcodes::ValueBuilder::hbyymmincr;
use C4::Context;
my $DEBUG = 0;
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-Z]{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
warn "Existing incremental number = $nextnum" if $DEBUG;
}
$nextnum++;
$nextnum = sprintf("%0*d", "4",$nextnum);
$nextnum = $year . $month . $nextnum;
warn "New hbyymmincr Barcode = $nextnum" if $DEBUG;
my $scr = "
var form = document.getElementById('f');
if ( !form ) {
form = document.getElementById('serials_edit');
}
if ( !form ) {
form = document.getElementById('Aform');
}
for (i=0 ; i<form.field_value.length ; i++) {
if (form.tag[i].value == '$args->{loctag}' && form.subfield[i].value == '$args->{locsubfield}') {
fnum = i;
}
}
if (\$('#' + id).val() == '') {
\$('#' + id).val(form.field_value[fnum].value + '$nextnum');
}
";
return $nextnum, $scr;
}
package C4::Barcodes::ValueBuilder::annual;
use C4::Context;
my $DEBUG = 0;
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) {
warn "Examining Record: $count" if $DEBUG;
$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;