File: | C4/Barcodes/annual.pm |
Coverage: | 40.0% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
1 | package C4::Barcodes::annual; | |||||
2 | ||||||
3 | # Copyright 2008 LibLime | |||||
4 | # | |||||
5 | # This file is part of Koha. | |||||
6 | # | |||||
7 | # Koha is free software; you can redistribute it and/or modify it under the | |||||
8 | # terms of the GNU General Public License as published by the Free Software | |||||
9 | # Foundation; either version 2 of the License, or (at your option) any later | |||||
10 | # version. | |||||
11 | # | |||||
12 | # Koha is distributed in the hope that it will be useful, but WITHOUT ANY | |||||
13 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | |||||
14 | # A PARTICULAR PURPOSE. See the GNU General Public License for more details. | |||||
15 | # | |||||
16 | # You should have received a copy of the GNU General Public License along | |||||
17 | # with Koha; if not, write to the Free Software Foundation, Inc., | |||||
18 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |||||
19 | ||||||
20 | 2 2 2 | 278 2 890 | use strict; | |||
21 | 2 2 2 | 9 4 86 | use warnings; | |||
22 | ||||||
23 | 2 2 2 | 8 2 184 | use Carp; | |||
24 | ||||||
25 | 2 2 2 | 182 36 39 | use C4::Context; | |||
26 | 2 2 2 | 11 2 181 | use C4::Debug; | |||
27 | 2 2 2 | 148 3 81 | use C4::Dates; | |||
28 | ||||||
29 | 2 2 2 | 9 2 119 | use vars qw($VERSION @ISA); | |||
30 | 2 2 2 | 9 7 74 | use vars qw($debug $cgi_debug); # from C4::Debug, of course | |||
31 | 2 2 2 | 8 26 140 | use vars qw($width); | |||
32 | ||||||
33 | BEGIN { | |||||
34 | 2 | 4 | $VERSION = 0.01; | |||
35 | 2 | 24 | @ISA = qw(C4::Barcodes); | |||
36 | 2 | 1159 | $width = 4; | |||
37 | } | |||||
38 | ||||||
39 | sub db_max ($;$) { | |||||
40 | 0 | my $self = shift; | ||||
41 | 0 | 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 | 0 | my $sth = C4::Context->dbh->prepare($query); | ||||
44 | 0 | my ($iso); | ||||
45 | 0 | if (@_) { | ||||
46 | 0 | my $input = shift; | ||||
47 | 0 | $iso = C4::Dates->new($input,'iso')->output('iso'); # try to set the date w/ 2nd arg | ||||
48 | 0 | unless ($iso) { | ||||
49 | 0 | warn "Failed to create 'iso' Dates object with input '$input'. Reverting to today's date."; | ||||
50 | 0 | $iso = C4::Dates->new->output('iso'); # failover back to today | ||||
51 | } | |||||
52 | } else { | |||||
53 | 0 | $iso = C4::Dates->new->output('iso'); | ||||
54 | } | |||||
55 | 0 | my $year = substr($iso,0,4); # YYYY | ||||
56 | 0 | $sth->execute("$year-%"); | ||||
57 | 0 | my $row = $sth->fetchrow_hashref; | ||||
58 | 0 | warn "barcode db_max (annual format, year $year): $row->{barcode}" if $debug; | ||||
59 | 0 | return $row->{barcode}; | ||||
60 | } | |||||
61 | ||||||
62 | sub initial () { | |||||
63 | 0 | my $self = shift; | ||||
64 | 0 | return substr(C4::Dates->new->output('iso'),0,4) .'-'. sprintf('%'."$width.$width".'d', 1); | ||||
65 | } | |||||
66 | ||||||
67 | sub parse ($;$) { | |||||
68 | 0 | my $self = shift; | ||||
69 | 0 | my $barcode = (@_) ? shift : $self->value; | ||||
70 | 0 | unless ($barcode =~ /(\d{4}-)(\d+)$/) { # non-greedy match in first part | ||||
71 | 0 | carp "Barcode '$barcode' has no incrementing part!"; | ||||
72 | 0 | return ($barcode,undef,undef); | ||||
73 | } | |||||
74 | 0 | $debug and warn "Barcode '$barcode' parses into: '$1', '$2', ''"; | ||||
75 | 0 | return ($1,$2,''); # the third part is in anticipation of barcodes that include checkdigits | ||||
76 | } | |||||
77 | sub width ($;$) { | |||||
78 | 0 | my $self = shift; | ||||
79 | 0 | (@_) and $width = shift; # hitting the class variable. | ||||
80 | 0 | return $width; | ||||
81 | } | |||||
82 | sub process_head($$;$$) { # (self,head,whole,specific) | |||||
83 | 0 | my ($self,$head,$whole,$specific) = @_; | ||||
84 | 0 | $specific and return $head; # if this is built off an existing barcode, just return the head unchanged. | ||||
85 | 0 | return substr(C4::Dates->new->output('iso'),0,4) . '-'; # else get new YYYY- | ||||
86 | } | |||||
87 | ||||||
88 | sub new_object { | |||||
89 | 0 | my $class = shift; | ||||
90 | 0 | my $type = ref($class) || $class; | ||||
91 | 0 | my $self = $type->default_self('annual'); | ||||
92 | 0 | return bless $self, $type; | ||||
93 | } | |||||
94 | ||||||
95 | 1; |