Barcodes - OO replacements, extensible module, tests.
[koha.git] / C4 / Barcodes / hbyymmincr.pm
1 #!/usr/bin/perl
2
3 package C4::Barcodes::hbyymmincr;
4
5 # Copyright 2008 LibLime
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 use warnings;
24
25 use Carp;
26
27 use C4::Context;
28 use C4::Debug;
29 use C4::Dates;
30
31 use vars qw($VERSION @ISA);
32 use vars qw($debug $cgi_debug); # from C4::Debug, of course
33 use vars qw($branch $width);
34
35 BEGIN {
36     $VERSION = 0.01;
37     @ISA = qw(C4::Barcodes);
38 }
39
40 INIT {
41         $branch = '';
42         $width = 4;             # FIXME: 4 is too small for sizeable or multi-branch libraries.
43 }
44 # Generates barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number,
45 #       increment resets yearly -fbcit
46
47 sub db_max ($;$) {
48         my $self = shift;
49         my $query = "SELECT MAX(SUBSTRING(barcode,-$width)), barcode FROM items WHERE barcode REGEXP ? GROUP BY barcode";
50         $debug and print STDERR "(hbyymmincr) db_max query: $query\n";
51         my $sth = C4::Context->dbh->prepare($query);
52         my ($iso);
53         if (@_) {
54                 my $input = shift;
55                 $iso = C4::Dates->new($input,'iso')->output('iso'); # try to set the date w/ 2nd arg
56                 unless ($iso) {
57                         warn "Failed to create 'iso' Dates object with input '$input'.  Reverting to today's date.";
58                         $iso = C4::Dates->new->output('iso');   # failover back to today
59                 }
60         } else {
61                 $iso = C4::Dates->new->output('iso');
62         }
63         my $year = substr($iso,2,2);    # i.e. "08" for 2008
64         my $andtwo = $width+2;
65         $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.
66         unless ($sth->rows) {
67                 warn "No existing hbyymmincr barcodes found.  Reverting to initial value.";
68                 return $self->initial;
69         }
70         my ($row) = $sth->fetchrow_hashref;
71         my $max = $row->{barcode};
72         warn "barcode max (hbyymmincr format): $max" if $debug;
73         return ($max || 0);
74 }
75
76 sub initial () {
77         my $self = shift;
78         # FIXME: populated branch?
79         my $iso = C4::Dates->new->output('iso');        # like "2008-07-02"
80         return $self->branch . substr($iso,2,2) . substr($iso,5,2) . sprintf('%' . "$width.$width" . 'd',1);
81 }
82
83 sub parse ($;$) {   # return 3 parts of barcode: non-incrementing, incrementing, non-incrementing
84         my $self = shift;
85         my $barcode = (@_) ? shift : $self->value;
86         my $branch = $self->branch;
87         unless ($barcode =~ /($branch\d{4})(\d+)$/) {
88                 carp "Barcode '$barcode' has no incrementing part!";
89                 return ($barcode,undef,undef);
90         }
91         $debug and warn "Barcode '$barcode' parses into: '$1', '$2', ''";
92         return ($1,$2,'');  # the third part is in anticipation of barcodes that include checkdigits
93 }
94
95 sub branch ($;$) {
96         my $self = shift;
97         (@_) and $self->{branch} = shift;
98         return $self->{branch};
99 }
100 sub width ($;$) {
101         my $self = shift;
102         (@_) and $width = shift;        # hitting the class variable.
103         return $width;
104 }
105 sub process_head($$;$$) {       # (self,head,whole,specific)
106         my ($self,$head,$whole,$specific) = @_;
107         $specific and return $head;     # if this is built off an existing barcode, just return the head unchanged.
108         $head =~ s/\d{4}$//;            # else strip the old yymm
109         my $iso = C4::Dates->new->output('iso');        # like "2008-07-02"
110         return $head . substr($iso,2,2) . substr($iso,5,2);
111 }
112
113 sub new_object {
114         $debug and warn "hbyymmincr: new_object called";
115         my $class_or_object = shift;
116         my $type = ref($class_or_object) || $class_or_object;
117         my $from_obj = ref($class_or_object) ? 1 : 0;   # are we building off another Barcodes object?
118         my $self = $class_or_object->default_self('hbyymmincr');
119         bless $self, $type;
120         $self->branch(@_ ? shift : $from_obj ? $class_or_object->branch : $branch);
121                 # take the branch from argument, or existing object, or default
122         use Data::Dumper;
123         $debug and print STDERR "(hbyymmincr) new_object: ", Dumper($self), "\n";
124         return $self;
125 }
126
127 1;
128 __END__
129
130 =doc 
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 accomodate 
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