Bug 15878 [QA Followup]
[koha.git] / C4 / Barcodes / hbyymmincr.pm
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
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.
11 #
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.
16 #
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>.
19
20 use strict;
21 use warnings;
22
23 use Carp;
24
25 use C4::Context;
26 use C4::Debug;
27
28 use Koha::DateUtils qw( dt_from_string output_pref );
29
30 use vars qw(@ISA);
31 use vars qw($debug $cgi_debug); # from C4::Debug, of course
32 use vars qw($branch $width);
33
34 BEGIN {
35     @ISA = qw(C4::Barcodes);
36 }
37
38 INIT {
39         $branch = '';
40         $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         my $self = shift;
47     my $query = "SELECT SUBSTRING(barcode,-$width) AS chunk, barcode FROM items WHERE barcode REGEXP ?  ORDER BY chunk DESC LIMIT 1";
48         $debug and print STDERR "(hbyymmincr) db_max query: $query\n";
49         my $sth = C4::Context->dbh->prepare($query);
50         my ($iso);
51         if (@_) {
52                 my $input = shift;
53                 $iso = output_pref({ dt => dt_from_string( $input, 'iso' ), dateformat => 'iso', dateonly => 1 }); # try to set the date w/ 2nd arg
54                 unless ($iso) {
55                         warn "Failed to create 'iso' Dates object with input '$input'.  Reverting to today's date.";
56                         $iso = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });      # failover back to today
57                 }
58         } else {
59                 $iso = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
60         }
61         my $year = substr($iso,2,2);    # i.e. "08" for 2008
62         my $andtwo = $width+2;
63         $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         unless ($sth->rows) {
65                 warn "No existing hbyymmincr barcodes found.  Reverting to initial value.";
66                 return $self->initial;
67         }
68         my ($row) = $sth->fetchrow_hashref;
69         my $max = $row->{barcode};
70         warn "barcode max (hbyymmincr format): $max" if $debug;
71         return ($max || 0);
72 }
73
74 sub initial {
75         my $self = shift;
76         # FIXME: populated branch?
77     my $iso = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }); # like "2008-07-02"
78     warn "HBYYMM Barcode was not passed a branch, default is blank" if ( $self->branch eq '' );
79         return $self->branch . substr($iso,2,2) . substr($iso,5,2) . sprintf('%' . "$width.$width" . 'd',1);
80 }
81
82 sub parse {   # return 3 parts of barcode: non-incrementing, incrementing, non-incrementing
83         my $self = shift;
84         my $barcode = (@_) ? shift : $self->value;
85         my $branch = $self->branch;
86         unless ($barcode =~ /($branch\d{4})(\d+)$/) {
87                 carp "Barcode '$barcode' has no incrementing part!";
88                 return ($barcode,undef,undef);
89         }
90         $debug and warn "Barcode '$barcode' parses into: '$1', '$2', ''";
91         return ($1,$2,'');  # the third part is in anticipation of barcodes that include checkdigits
92 }
93
94 sub branch {
95         my $self = shift;
96         (@_) and $self->{branch} = shift;
97         return $self->{branch};
98 }
99 sub width {
100         my $self = shift;
101         (@_) and $width = shift;        # hitting the class variable.
102         return $width;
103 }
104 sub process_head {      # (self,head,whole,specific)
105         my ($self,$head,$whole,$specific) = @_;
106         $specific and return $head;     # if this is built off an existing barcode, just return the head unchanged.
107         $head =~ s/\d{4}$//;            # else strip the old yymm
108     my $iso = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }); # like "2008-07-02"
109         return $head . substr($iso,2,2) . substr($iso,5,2);
110 }
111
112 sub new_object {
113     $debug and warn "hbyymmincr: new_object called";
114     my $class_or_object = shift;
115
116     my $type = ref($class_or_object) || $class_or_object;
117
118     my $from_obj =
119       ref($class_or_object)
120       ? 1
121       : 0;    # are we building off another Barcodes object?
122
123       my $self = $class_or_object->default_self('hbyymmincr');
124     bless $self, $type;
125
126     $self->branch( @_ ? shift : $from_obj ? $class_or_object->branch : $branch );
127     warn "HBYYMM Barcode created with no branchcode, default is blank" if ( $self->branch() eq '' );
128
129     # take the branch from argument, or existing object, or default
130     use Data::Dumper;
131     $debug and print STDERR "(hbyymmincr) new_object: ", Dumper($self), "\n";
132
133     return $self;
134 }
135
136 1;
137 __END__
138
139 =head1 NOTICE 
140
141 This format is deprecated and SHOULD NOT BE USED.
142
143 It is fairly clear the originator of the format did not intend to accommodate
144 multiple branch libraries, given that the format caps the available namespace to
145 10,000 barcodes per year TOTAL.  
146
147 Also, the question of what to do with an item that changes branch is unsettled.  
148 Nothing prevents the barcode from working fine, but it will look out of place
149 with the old branchcode in it.  Rebarcoding a single item is trivial, but if you
150 consider the scenario of branches being consolidated, it is an unnecessary 
151 burden to force the rebarcoding of thousands of items, especially when the format
152 will limit you to under 10,000 on the year!
153
154 The main purpose of the format seems to be to get the branch code into the barcode.
155 This is wholly unnecessary, since the barcodes can be printed with the branchcode
156 directly on it, without it being part of the barcode itself.  
157
158 The API for this module should exist almost exclusively through C4::Barcodes.  
159 One novel aspect of this format is the fact that the barcode is tied to a branch.  
160
161 =cut