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