From 2c6b67229a29049776aa47fb758ea336c828c8fd Mon Sep 17 00:00:00 2001 From: Mason James Date: Mon, 27 Aug 2012 13:14:02 +1200 Subject: [PATCH] Bug 8691 - add a new 'hbincr' barcode plugin, for cataloging Content-Type: text/plain; charset="utf-8" http://koha-community.org --- C4/Barcodes.pm | 3 + C4/Barcodes/hbincr.pm | 129 ++++++++++++++++++++ cataloguing/value_builder/barcode.pl | 40 +++++- .../en/modules/admin/preferences/cataloguing.pref | 1 + 4 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 C4/Barcodes/hbincr.pm diff --git a/C4/Barcodes.pm b/C4/Barcodes.pm index 2e81da6..5fa0a68 100644 --- a/C4/Barcodes.pm +++ b/C4/Barcodes.pm @@ -1,6 +1,7 @@ package C4::Barcodes; # Copyright 2008 LibLime +# Copyright 2012 KohaAloha # # This file is part of Koha. # @@ -26,6 +27,7 @@ use C4::Context; use C4::Debug; use C4::Dates; use C4::Barcodes::hbyymmincr; +use C4::Barcodes::hbincr; use C4::Barcodes::annual; use C4::Barcodes::incremental; use C4::Barcodes::EAN13; @@ -177,6 +179,7 @@ our $types = { annual => sub {C4::Barcodes::annual->new_object(@_); }, incremental => sub {C4::Barcodes::incremental->new_object(@_);}, hbyymmincr => sub {C4::Barcodes::hbyymmincr->new_object(@_); }, + hbincr => sub {C4::Barcodes::hbincr->new_object(@_); }, OFF => sub {C4::Barcodes::OFF->new_object(@_); }, EAN13 => sub {C4::Barcodes::EAN13->new_object(@_); }, }; diff --git a/C4/Barcodes/hbincr.pm b/C4/Barcodes/hbincr.pm new file mode 100644 index 0000000..e457750 --- /dev/null +++ b/C4/Barcodes/hbincr.pm @@ -0,0 +1,129 @@ +package C4::Barcodes::hbincr; + +# Copyright 2012 KohaAloha +# +# 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 2 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use strict; +use warnings; + +use Carp; + +use C4::Context; +use C4::Debug; +use C4::Dates; + +#use Smart::Comments '####'; + +use vars qw($VERSION @ISA); +use vars qw($debug $cgi_debug); # from C4::Debug, of course +use vars qw($branch $width); + +BEGIN { + # $VERSION = 0.01; + @ISA = qw(C4::Barcodes); +} + +INIT { + $branch = ''; + $width = 8; +} + +# Generates barcode where hb = homebranch code, incr = an 8 digit number incremental number + +sub db_max ($;$) { + my $self = shift; + my $query = +"SELECT MAX(SUBSTRING(barcode,-$width)), barcode FROM items WHERE barcode REGEXP ? GROUP BY barcode"; + $debug and print STDERR "(hbincr) db_max query: $query\n"; + my $sth = C4::Context->dbh->prepare($query); + + $sth->execute("^[a-zA-Z]{1,}"); + unless ( $sth->rows ) { + warn "No existing hbincr barcodes found. Reverting to initial value."; + return $self->initial; + } + + my ($row) = $sth->fetchrow_hashref; + my $max = $row->{barcode}; + warn "barcode max (hbincr format): $max" if $debug; + return ( $max || 0 ); +} + +sub initial () { + my $self = shift; + return $self->branch; +} + +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+)$/ ) { + carp "Barcode '$barcode' has no incrementing part!"; + return ( $barcode, undef, undef ); + } + $debug and warn "Barcode '$barcode' parses into: '$1', '$2', ''"; + 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}; +} + +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{8}$//; # else strip the old + # my $iso = C4::Dates->new->output('iso'); # like "2008-07-02" + return $head; +} + +sub new_object { + $debug and warn "hbincr: new_object called"; + 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('hbincr'); + bless $self, $type; + $self->branch( + @_ ? shift : $from_obj ? $class_or_object->branch : $branch ); + + # take the branch from argument, or existing object, or default + use Data::Dumper; + $debug and print STDERR "(hbincr) new_object: ", Dumper($self), "\n"; + return $self; +} + +1; +__END__ + diff --git a/cataloguing/value_builder/barcode.pl b/cataloguing/value_builder/barcode.pl index 0ec6b62..f4edcc9 100755 --- a/cataloguing/value_builder/barcode.pl +++ b/cataloguing/value_builder/barcode.pl @@ -21,13 +21,12 @@ use strict; use warnings; no warnings 'redefine'; # otherwise loading up multiple plugins fills the log with subroutine redefine warnings +use C4::Debug; use C4::Context; require C4::Dates; use Algorithm::CheckDigits; -my $DEBUG = 0; - =head1 plugin_parameters : other parameters added when the plugin is called by the dopop function @@ -67,7 +66,7 @@ sub plugin_javascript { my $query; my $scr; my $autoBarcodeType = C4::Context->preference("autoBarcode"); - warn "Barcode type = $autoBarcodeType" if $DEBUG; + warn "Barcode type = $autoBarcodeType" if $debug; if ((not $autoBarcodeType) or $autoBarcodeType eq 'OFF') { # don't return a value unless we have the appropriate syspref set return ($function_name, @@ -83,7 +82,7 @@ sub plugin_javascript { my $sth=$dbh->prepare($query); $sth->execute("$year%"); while (my ($count)= $sth->fetchrow_array) { - warn "Examining Record: $count" if $DEBUG; + warn "Examining Record: $count" if $debug; $nextnum = $count if $count; } $nextnum++; @@ -100,6 +99,8 @@ sub plugin_javascript { } $nextnum++; } + + elsif ($autoBarcodeType eq 'hbyymmincr') { # Generates a barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number, reset yearly -fbcit $year = substr($year, -2); $query = "SELECT MAX(CAST(SUBSTRING(barcode,-4) AS signed)) AS number FROM items WHERE barcode REGEXP ?"; @@ -108,12 +109,38 @@ sub plugin_javascript { while (my ($count)= $sth->fetchrow_array) { $nextnum = $count if $count; $nextnum = 0 if $nextnum == 9999; # this sequence only allows for cataloging 10000 books per month - warn "Existing incremental number = $nextnum" if $DEBUG; + warn "Existing incremental number = $nextnum" if $debug; } $nextnum++; $nextnum = sprintf("%0*d", "4",$nextnum); $nextnum = $year . $mon . $nextnum; - warn "New hbyymmincr Barcode = $nextnum" if $DEBUG; + warn "New hbyymmincr Barcode = $nextnum" if $debug; + $scr = " + for (i=0 ; iprepare($query); + $sth->execute("^[-a-zA-Z]{1,}"); + while (my ($count)= $sth->fetchrow_array) { + $nextnum = $count if $count; + + warn "Existing incremental number = $nextnum" if $debug; + } + $nextnum++; + $nextnum = sprintf("%0*d", "8",$nextnum); + $nextnum = $nextnum; + warn "New hbincr Barcode = $nextnum" if $debug; $scr = " for (i=0 ; i