Barcodes - OO replacements, extensible module, tests.
[koha.git] / C4 / Barcodes / annual.pm
1 #!/usr/bin/perl
2
3 package C4::Barcodes::annual;
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($width);
34
35 BEGIN {
36     $VERSION = 0.01;
37     @ISA = qw(C4::Barcodes);
38         $width = 4;
39 }
40
41 sub db_max ($;$) {
42         my $self = shift;
43         my $query = "SELECT max(substring_index(barcode,'-',-1)) AS chunk,barcode FROM items WHERE barcode LIKE ? GROUP BY barcode";
44                 # FIXME: unreasonably expensive query on large datasets
45         my $sth = C4::Context->dbh->prepare($query);
46         my ($iso);
47         if (@_) {
48                 my $input = shift;
49                 $iso = C4::Dates->new($input,'iso')->output('iso'); # try to set the date w/ 2nd arg
50                 unless ($iso) {
51                         warn "Failed to create 'iso' Dates object with input '$input'.  Reverting to today's date.";
52                         $iso = C4::Dates->new->output('iso');   # failover back to today
53                 }
54         } else {
55                 $iso = C4::Dates->new->output('iso');
56         }
57         my $year = substr($iso,0,4);    # YYYY
58         $sth->execute("$year-%");
59         my $row = $sth->fetchrow_hashref;
60         warn "barcode db_max (annual format, year $year): $row->{barcode}" if $debug;
61         return $row->{barcode};
62 }
63
64 sub initial () {
65         my $self = shift;
66         return substr(C4::Dates->new->output('iso'),0,4) .'-'. sprintf('%'."$width.$width".'d', 1);
67 }
68
69 sub parse ($;$) {
70         my $self = shift;
71     my $barcode = (@_) ? shift : $self->value;
72         unless ($barcode =~ /(\d{4}-)(\d+)$/) {    # non-greedy match in first part
73                 carp "Barcode '$barcode' has no incrementing part!";
74                 return ($barcode,undef,undef);
75         }
76         $debug and warn "Barcode '$barcode' parses into: '$1', '$2', ''";
77         return ($1,$2,'');  # the third part is in anticipation of barcodes that include checkdigits
78 }
79 sub width ($;$) {
80         my $self = shift;
81         (@_) and $width = shift;        # hitting the class variable.
82         return $width;
83 }
84 sub process_head($$;$$) {       # (self,head,whole,specific)
85         my ($self,$head,$whole,$specific) = @_;
86         $specific and return $head;     # if this is built off an existing barcode, just return the head unchanged.
87         return substr(C4::Dates->new->output('iso'),0,4) . '-'; # else get new YYYY-
88 }
89
90 sub new_object {
91         my $class = shift;
92         my $type = ref($class) || $class;
93         my $self = $type->default_self('annual');
94         return bless $self, $type;
95 }
96
97 1;
98 __END__
99