Bug 32030: I18N - Load de-DE, es-ES, fr-FR strings
[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 qw( carp );
24
25 use C4::Context;
26
27 # FIXME We should certainly remove output_pref from here
28 use Koha::DateUtils qw( dt_from_string output_pref );
29
30 use vars qw(@ISA);
31 use vars qw($width);
32
33 BEGIN {
34     @ISA = qw(C4::Barcodes);
35         $width = 4;
36 }
37
38 sub db_max {
39         my $self = shift;
40         my $query = "SELECT substring_index(barcode,'-',-1) AS chunk,barcode FROM items WHERE barcode LIKE ? ORDER BY chunk DESC LIMIT 1";
41                 # FIXME: unreasonably expensive query on large datasets (I think removal of group by does this?)
42         my $sth = C4::Context->dbh->prepare($query);
43         my ($iso);
44         if (@_) {
45                 my $input = shift;
46         $iso = output_pref({ dt => dt_from_string( $input, 'iso' ), dateformat => 'iso', dateonly => 1 }); # try to set the date w/ 2nd arg
47                 unless ($iso) {
48                         warn "Failed to create 'iso' Dates object with input '$input'.  Reverting to today's date.";
49             $iso = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }); # failover back to today
50                 }
51         } else {
52         $iso = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
53         }
54         my $year = substr($iso,0,4);    # YYYY
55         $sth->execute("$year-%");
56         my $row = $sth->fetchrow_hashref;
57         return $row->{barcode};
58 }
59
60 sub initial () {
61         my $self = shift;
62     return substr(output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), 0, 4 ) .'-'. sprintf('%'."$width.$width".'d', 1);
63 }
64
65 sub parse {
66         my $self = shift;
67     my $barcode = (@_) ? shift : $self->value;
68         unless ($barcode =~ /(\d{4}-)(\d+)$/) {    # non-greedy match in first part
69                 carp "Barcode '$barcode' has no incrementing part!";
70                 return ($barcode,undef,undef);
71         }
72         return ($1,$2,'');  # the third part is in anticipation of barcodes that include checkdigits
73 }
74 sub width {
75         my $self = shift;
76         (@_) and $width = shift;        # hitting the class variable.
77         return $width;
78 }
79 sub process_head {
80         my ($self,$head,$whole,$specific) = @_;
81         $specific and return $head;     # if this is built off an existing barcode, just return the head unchanged.
82     return substr(output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), 0, 4 ) . '-'; # else get new YYYY-
83 }
84
85 sub new_object {
86         my $class = shift;
87         my $type = ref($class) || $class;
88         my $self = $type->default_self('annual');
89         return bless $self, $type;
90 }
91
92 1;
93 __END__
94