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