Bug 13813: Remove deprecated module C4::Dates from system
[koha.git] / C4 / Barcodes / annual.pm
1 package C4::Barcodes::annual;
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( output_pref dt_from_string );
29
30 use vars qw($VERSION @ISA);
31 use vars qw($debug $cgi_debug); # from C4::Debug, of course
32 use vars qw($width);
33
34 BEGIN {
35     $VERSION = 3.07.00.049;
36     @ISA = qw(C4::Barcodes);
37         $width = 4;
38 }
39
40 sub db_max ($;$) {
41         my $self = shift;
42         my $query = "SELECT max(substring_index(barcode,'-',-1)) AS chunk,barcode FROM items WHERE barcode LIKE ? GROUP BY barcode";
43                 # FIXME: unreasonably expensive query on large datasets
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,0,4);    # YYYY
57         $sth->execute("$year-%");
58         my $row = $sth->fetchrow_hashref;
59         warn "barcode db_max (annual format, year $year): $row->{barcode}" if $debug;
60         return $row->{barcode};
61 }
62
63 sub initial () {
64         my $self = shift;
65     return substr(output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), 0, 4 ) .'-'. sprintf('%'."$width.$width".'d', 1);
66 }
67
68 sub parse ($;$) {
69         my $self = shift;
70     my $barcode = (@_) ? shift : $self->value;
71         unless ($barcode =~ /(\d{4}-)(\d+)$/) {    # non-greedy match in first part
72                 carp "Barcode '$barcode' has no incrementing part!";
73                 return ($barcode,undef,undef);
74         }
75         $debug and warn "Barcode '$barcode' parses into: '$1', '$2', ''";
76         return ($1,$2,'');  # the third part is in anticipation of barcodes that include checkdigits
77 }
78 sub width ($;$) {
79         my $self = shift;
80         (@_) and $width = shift;        # hitting the class variable.
81         return $width;
82 }
83 sub process_head($$;$$) {       # (self,head,whole,specific)
84         my ($self,$head,$whole,$specific) = @_;
85         $specific and return $head;     # if this is built off an existing barcode, just return the head unchanged.
86     return substr(output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), 0, 4 ) . '-';      # else get new YYYY-
87 }
88
89 sub new_object {
90         my $class = shift;
91         my $type = ref($class) || $class;
92         my $self = $type->default_self('annual');
93         return bless $self, $type;
94 }
95
96 1;
97 __END__
98