File: | C4/Barcodes/hbyymmincr.pm |
Coverage: | 30.1% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
1 | package C4::Barcodes::hbyymmincr; | |||||
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 | 4 4 4 | 45308 29 117 | use strict; | |||
21 | 4 4 4 | 17 8 136 | use warnings; | |||
22 | ||||||
23 | 4 4 4 | 19 8 290 | use Carp; | |||
24 | ||||||
25 | 4 4 4 | 438 21 49 | use C4::Context; | |||
26 | 4 4 4 | 57 9 414 | use C4::Debug; | |||
27 | 4 4 4 | 395 9 381 | use C4::Dates; | |||
28 | ||||||
29 | 4 4 4 | 19 5 287 | use vars qw($VERSION @ISA); | |||
30 | 4 4 4 | 15 5 237 | use vars qw($debug $cgi_debug); # from C4::Debug, of course | |||
31 | 4 4 4 | 16 6 224 | use vars qw($branch $width); | |||
32 | ||||||
33 | BEGIN { | |||||
34 | 4 | 8 | $VERSION = 0.01; | |||
35 | 4 | 4309 | @ISA = qw(C4::Barcodes); | |||
36 | } | |||||
37 | ||||||
38 | INIT { | |||||
39 | 0 | 0 | $branch = ''; | |||
40 | 0 | 0 | $width = 4; # FIXME: 4 is too small for sizeable or multi-branch libraries. | |||
41 | } | |||||
42 | # Generates barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number, | |||||
43 | # increment resets yearly -fbcit | |||||
44 | ||||||
45 | sub db_max ($;$) { | |||||
46 | 0 | 0 | my $self = shift; | |||
47 | 0 | 0 | my $query = "SELECT MAX(SUBSTRING(barcode,-$width)), barcode FROM items WHERE barcode REGEXP ? GROUP BY barcode"; | |||
48 | 0 | 0 | $debug and print STDERR "(hbyymmincr) db_max query: $query\n"; | |||
49 | 0 | 0 | my $sth = C4::Context->dbh->prepare($query); | |||
50 | 0 | 0 | my ($iso); | |||
51 | 0 | 0 | if (@_) { | |||
52 | 0 | 0 | my $input = shift; | |||
53 | 0 | 0 | $iso = C4::Dates->new($input,'iso')->output('iso'); # try to set the date w/ 2nd arg | |||
54 | 0 | 0 | unless ($iso) { | |||
55 | 0 | 0 | warn "Failed to create 'iso' Dates object with input '$input'. Reverting to today's date."; | |||
56 | 0 | 0 | $iso = C4::Dates->new->output('iso'); # failover back to today | |||
57 | } | |||||
58 | } else { | |||||
59 | 0 | 0 | $iso = C4::Dates->new->output('iso'); | |||
60 | } | |||||
61 | 0 | 0 | my $year = substr($iso,2,2); # i.e. "08" for 2008 | |||
62 | 0 | 0 | my $andtwo = $width+2; | |||
63 | 0 | 0 | $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. | |||
64 | 0 | 0 | unless ($sth->rows) { | |||
65 | 0 | 0 | warn "No existing hbyymmincr barcodes found. Reverting to initial value."; | |||
66 | 0 | 0 | return $self->initial; | |||
67 | } | |||||
68 | 0 | 0 | my ($row) = $sth->fetchrow_hashref; | |||
69 | 0 | 0 | my $max = $row->{barcode}; | |||
70 | 0 | 0 | warn "barcode max (hbyymmincr format): $max" if $debug; | |||
71 | 0 | 0 | return ($max || 0); | |||
72 | } | |||||
73 | ||||||
74 | sub initial () { | |||||
75 | 0 | 0 | my $self = shift; | |||
76 | # FIXME: populated branch? | |||||
77 | 0 | 0 | my $iso = C4::Dates->new->output('iso'); # like "2008-07-02" | |||
78 | 0 | 0 | return $self->branch . substr($iso,2,2) . substr($iso,5,2) . sprintf('%' . "$width.$width" . 'd',1); | |||
79 | } | |||||
80 | ||||||
81 | sub parse ($;$) { # return 3 parts of barcode: non-incrementing, incrementing, non-incrementing | |||||
82 | 0 | 0 | my $self = shift; | |||
83 | 0 | 0 | my $barcode = (@_) ? shift : $self->value; | |||
84 | 0 | 0 | my $branch = $self->branch; | |||
85 | 0 | 0 | unless ($barcode =~ /($branch\d{4})(\d+)$/) { | |||
86 | 0 | 0 | carp "Barcode '$barcode' has no incrementing part!"; | |||
87 | 0 | 0 | return ($barcode,undef,undef); | |||
88 | } | |||||
89 | 0 | 0 | $debug and warn "Barcode '$barcode' parses into: '$1', '$2', ''"; | |||
90 | 0 | 0 | return ($1,$2,''); # the third part is in anticipation of barcodes that include checkdigits | |||
91 | } | |||||
92 | ||||||
93 | sub branch ($;$) { | |||||
94 | 0 | 0 | my $self = shift; | |||
95 | 0 | 0 | (@_) and $self->{branch} = shift; | |||
96 | 0 | 0 | return $self->{branch}; | |||
97 | } | |||||
98 | sub width ($;$) { | |||||
99 | 0 | 0 | my $self = shift; | |||
100 | 0 | 0 | (@_) and $width = shift; # hitting the class variable. | |||
101 | 0 | 0 | return $width; | |||
102 | } | |||||
103 | sub process_head($$;$$) { # (self,head,whole,specific) | |||||
104 | 0 | 0 | my ($self,$head,$whole,$specific) = @_; | |||
105 | 0 | 0 | $specific and return $head; # if this is built off an existing barcode, just return the head unchanged. | |||
106 | 0 | 0 | $head =~ s/\d{4}$//; # else strip the old yymm | |||
107 | 0 | 0 | my $iso = C4::Dates->new->output('iso'); # like "2008-07-02" | |||
108 | 0 | 0 | return $head . substr($iso,2,2) . substr($iso,5,2); | |||
109 | } | |||||
110 | ||||||
111 | sub new_object { | |||||
112 | 0 | 0 | $debug and warn "hbyymmincr: new_object called"; | |||
113 | 0 | 0 | my $class_or_object = shift; | |||
114 | 0 | 0 | my $type = ref($class_or_object) || $class_or_object; | |||
115 | 0 | 0 | my $from_obj = ref($class_or_object) ? 1 : 0; # are we building off another Barcodes object? | |||
116 | 0 | 0 | my $self = $class_or_object->default_self('hbyymmincr'); | |||
117 | 0 | 0 | bless $self, $type; | |||
118 | 0 | 0 | $self->branch(@_ ? shift : $from_obj ? $class_or_object->branch : $branch); | |||
119 | # take the branch from argument, or existing object, or default | |||||
120 | 4 4 4 | 24 4 518 | use Data::Dumper; | |||
121 | 0 | 0 | $debug and print STDERR "(hbyymmincr) new_object: ", Dumper($self), "\n"; | |||
122 | 0 | 0 | return $self; | |||
123 | } | |||||
124 | ||||||
125 | 1; |