Koha/C4/Barcodes/hbyymmincr.pm
Jonathan Druart 9d6d641d1f Bug 17600: Standardize our EXPORT_OK
On bug 17591 we discovered that there was something weird going on with
the way we export and use subroutines/modules.
This patch tries to standardize our EXPORT to use EXPORT_OK only.

That way we will need to explicitely define the subroutine we want to
use from a module.

This patch is a squashed version of:
Bug 17600: After export.pl
Bug 17600: After perlimport
Bug 17600: Manual changes
Bug 17600: Other manual changes after second perlimports run
Bug 17600: Fix tests

And a lot of other manual changes.

export.pl is a dirty script that can be found on bug 17600.

"perlimport" is:
git clone https://github.com/oalders/App-perlimports.git
cd App-perlimports/
cpanm --installdeps .
export PERL5LIB="$PERL5LIB:/kohadevbox/koha/App-perlimports/lib"
find . \( -name "*.pl" -o -name "*.pm" \) -exec perl App-perlimports/script/perlimports --inplace-edit --no-preserve-unused --filename {} \;

The ideas of this patch are to:
* use EXPORT_OK instead of EXPORT
* perltidy the EXPORT_OK list
* remove '&' before the subroutine names
* remove some uneeded use statements
* explicitely import the subroutines we need within the controllers or
modules

Note that the private subroutines (starting with _) should not be
exported (and not used from outside of the module except from tests).

EXPORT vs EXPORT_OK (from
https://www.thegeekstuff.com/2010/06/perl-exporter-examples/)
"""
Export allows to export the functions and variables of modules to user’s namespace using the standard import method. This way, we don’t need to create the objects for the modules to access it’s members.

@EXPORT and @EXPORT_OK are the two main variables used during export operation.

@EXPORT contains list of symbols (subroutines and variables) of the module to be exported into the caller namespace.

@EXPORT_OK does export of symbols on demand basis.
"""

If this patch caused a conflict with a patch you wrote prior to its
push:
* Make sure you are not reintroducing a "use" statement that has been
removed
* "$subroutine" is not exported by the C4::$MODULE module
means that you need to add the subroutine to the @EXPORT_OK list
* Bareword "$subroutine" not allowed while "strict subs"
means that you didn't imported the subroutine from the module:
  - use $MODULE qw( $subroutine list );
You can also use the fully qualified namespace: C4::$MODULE::$subroutine

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2021-07-16 08:58:47 +02:00

152 lines
5.4 KiB
Perl

package C4::Barcodes::hbyymmincr;
# Copyright 2008 LibLime
#
# 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>.
use Modern::Perl;
use Carp qw( carp );
use C4::Context;
use Koha::DateUtils qw( dt_from_string output_pref );
use constant WIDTH => 4; # FIXME: too small for sizeable or multi-branch libraries?
use vars qw(@ISA);
BEGIN {
@ISA = qw(C4::Barcodes);
}
# Generates barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number,
# increment resets yearly -fbcit
sub db_max {
my $self = shift;
my $width = WIDTH;
my $query = "SELECT SUBSTRING(barcode,-$width) AS chunk, barcode FROM items WHERE barcode REGEXP ? ORDER BY chunk DESC LIMIT 1";
my $sth = C4::Context->dbh->prepare($query);
my ($iso);
if (@_) {
my $input = shift;
$iso = output_pref({ dt => dt_from_string( $input, 'iso' ), dateformat => 'iso', dateonly => 1 }); # try to set the date w/ 2nd arg
unless ($iso) {
warn "Failed to create 'iso' Dates object with input '$input'. Reverting to today's date.";
$iso = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }); # failover back to today
}
} else {
$iso = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
}
my $year = substr($iso,2,2); # i.e. "08" for 2008
my $andtwo = $width+2;
$sth->execute("^[a-zA-Z]{1,}" . $year . "[0-9]{$andtwo}"); # the extra two digits are the month. we don't care what they are, just that they are there.
unless ($sth->rows) {
warn "No existing hbyymmincr barcodes found. Reverting to initial value.";
return $self->initial;
}
my ($row) = $sth->fetchrow_hashref;
my $max = $row->{barcode};
return ($max || 0);
}
sub initial {
my $self = shift;
# FIXME: populated branch?
my $iso = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }); # like "2008-07-02"
warn "HBYYMM Barcode was not passed a branch, default is blank" if ( $self->branch eq '' );
my $width = WIDTH;
return $self->branch . substr($iso,2,2) . substr($iso,5,2) . sprintf('%' . "$width.$width" . 'd',1);
}
sub parse { # return 3 parts of barcode: non-incrementing, incrementing, non-incrementing
my $self = shift;
my $barcode = (@_) ? shift : $self->value;
my $branch = $self->branch;
unless ($barcode =~ /($branch\d{4})(\d+)$/) {
carp "Barcode '$barcode' has no incrementing part!";
return ($barcode,undef,undef);
}
return ($1,$2,''); # the third part is in anticipation of barcodes that include checkdigits
}
sub branch {
my $self = shift;
(@_) and $self->{branch} = shift;
return $self->{branch};
}
# Commented out (BZ 16635)
#sub width {
# my $self = shift;
# (@_) and $width = shift; # hitting the class variable.
# return $width;
#}
sub process_head { # (self,head,whole,specific)
my ($self,$head,$whole,$specific) = @_;
$specific and return $head; # if this is built off an existing barcode, just return the head unchanged.
$head =~ s/\d{4}$//; # else strip the old yymm
my $iso = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }); # like "2008-07-02"
return $head . substr($iso,2,2) . substr($iso,5,2);
}
sub new_object {
my $class_or_object = shift;
my $type = ref($class_or_object) || $class_or_object;
my $from_obj =
ref($class_or_object)
? 1
: 0; # are we building off another Barcodes object?
my $self = $class_or_object->default_self('hbyymmincr');
bless $self, $type;
$self->branch( @_ ? shift : $from_obj ? $class_or_object->branch : '' );
warn "HBYYMM Barcode created with no branchcode, default is blank" if ( $self->branch() eq '' );
return $self;
}
1;
__END__
=head1 NOTICE
This format is deprecated and SHOULD NOT BE USED.
It is fairly clear the originator of the format did not intend to accommodate
multiple branch libraries, given that the format caps the available namespace to
10,000 barcodes per year TOTAL.
Also, the question of what to do with an item that changes branch is unsettled.
Nothing prevents the barcode from working fine, but it will look out of place
with the old branchcode in it. Rebarcoding a single item is trivial, but if you
consider the scenario of branches being consolidated, it is an unnecessary
burden to force the rebarcoding of thousands of items, especially when the format
will limit you to under 10,000 on the year!
The main purpose of the format seems to be to get the branch code into the barcode.
This is wholly unnecessary, since the barcodes can be printed with the branchcode
directly on it, without it being part of the barcode itself.
The API for this module should exist almost exclusively through C4::Barcodes.
One novel aspect of this format is the fact that the barcode is tied to a branch.
=cut