Bug 14252: (followup) Adds OpacLangSelectorMode syspref
[koha.git] / C4 / Barcodes / annual.pm
blobcb62b584dcc5b6e06eaecbd7ff9b9b00136dda29
1 package C4::Barcodes::annual;
3 # Copyright 2008 LibLime
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use strict;
21 use warnings;
23 use Carp;
25 use C4::Context;
26 use C4::Debug;
27 use C4::Dates;
29 use vars qw($VERSION @ISA);
30 use vars qw($debug $cgi_debug); # from C4::Debug, of course
31 use vars qw($width);
33 BEGIN {
34 $VERSION = 3.07.00.049;
35 @ISA = qw(C4::Barcodes);
36 $width = 4;
39 sub db_max ($;$) {
40 my $self = shift;
41 my $query = "SELECT max(substring_index(barcode,'-',-1)) AS chunk,barcode FROM items WHERE barcode LIKE ? GROUP BY barcode";
42 # FIXME: unreasonably expensive query on large datasets
43 my $sth = C4::Context->dbh->prepare($query);
44 my ($iso);
45 if (@_) {
46 my $input = shift;
47 $iso = C4::Dates->new($input,'iso')->output('iso'); # try to set the date w/ 2nd arg
48 unless ($iso) {
49 warn "Failed to create 'iso' Dates object with input '$input'. Reverting to today's date.";
50 $iso = C4::Dates->new->output('iso'); # failover back to today
52 } else {
53 $iso = C4::Dates->new->output('iso');
55 my $year = substr($iso,0,4); # YYYY
56 $sth->execute("$year-%");
57 my $row = $sth->fetchrow_hashref;
58 warn "barcode db_max (annual format, year $year): $row->{barcode}" if $debug;
59 return $row->{barcode};
62 sub initial () {
63 my $self = shift;
64 return substr(C4::Dates->new->output('iso'),0,4) .'-'. sprintf('%'."$width.$width".'d', 1);
67 sub parse ($;$) {
68 my $self = shift;
69 my $barcode = (@_) ? shift : $self->value;
70 unless ($barcode =~ /(\d{4}-)(\d+)$/) { # non-greedy match in first part
71 carp "Barcode '$barcode' has no incrementing part!";
72 return ($barcode,undef,undef);
74 $debug and warn "Barcode '$barcode' parses into: '$1', '$2', ''";
75 return ($1,$2,''); # the third part is in anticipation of barcodes that include checkdigits
77 sub width ($;$) {
78 my $self = shift;
79 (@_) and $width = shift; # hitting the class variable.
80 return $width;
82 sub process_head($$;$$) { # (self,head,whole,specific)
83 my ($self,$head,$whole,$specific) = @_;
84 $specific and return $head; # if this is built off an existing barcode, just return the head unchanged.
85 return substr(C4::Dates->new->output('iso'),0,4) . '-'; # else get new YYYY-
88 sub new_object {
89 my $class = shift;
90 my $type = ref($class) || $class;
91 my $self = $type->default_self('annual');
92 return bless $self, $type;
96 __END__