Bug 9467 - Use DataTables on catalog by item type report page
[koha.git] / C4 / Barcodes / ValueBuilder.pm
1 #!/usr/bin/perl
2 #
3 # Copyright 2008-2010 Foundations Bible College
4 # Parts copyright 2012 C & P Bibliography Services
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 package C4::Barcodes::ValueBuilder::incremental;
22 use C4::Context;
23 my $DEBUG = 0;
24
25 sub get_barcode {
26     my ($args) = @_;
27     my $nextnum;
28     # not the best, two catalogers could add the same barcode easily this way :/
29     my $query = "select max(abs(barcode)) from items";
30     my $sth = C4::Context->dbh->prepare($query);
31     $sth->execute();
32     while (my ($count)= $sth->fetchrow_array) {
33         $nextnum = $count;
34     }
35     $nextnum++;
36     return $nextnum;
37 }
38
39 1;
40
41 package C4::Barcodes::ValueBuilder::hbyymmincr;
42 use C4::Context;
43 my $DEBUG = 0;
44
45 sub get_barcode {
46     my ($args) = @_;
47     my $nextnum;
48     my $year = substr($args->{year}, -2);
49     my $query = "SELECT MAX(CAST(SUBSTRING(barcode,-4) AS signed)) AS number FROM items WHERE barcode REGEXP ?";
50     my $sth = C4::Context->dbh->prepare($query);
51     $sth->execute("^[-a-zA-Z]{1,}$year");
52     while (my ($count)= $sth->fetchrow_array) {
53         $nextnum = $count if $count;
54         $nextnum = 0 if $nextnum == 9999; # this sequence only allows for cataloging 10000 books per month
55             warn "Existing incremental number = $nextnum" if $DEBUG;
56     }
57     $nextnum++;
58     $nextnum = sprintf("%0*d", "4",$nextnum);
59     $nextnum = $year . $args->{mon} . $nextnum;
60     warn "New hbyymmincr Barcode = $nextnum" if $DEBUG;
61     my $scr = "
62         for (i=0 ; i<document.f.field_value.length ; i++) {
63             if (document.f.tag[i].value == '$args->{loctag}' && document.f.subfield[i].value == '$args->{locsubfield}') {
64                 fnum = i;
65             }
66         }
67     if (\$('#' + id).val() == '') {
68         \$('#' + id).val(document.f.field_value[fnum].value + '$nextnum');
69     }
70     ";
71     return $nextnum, $scr;
72 }
73
74
75 package C4::Barcodes::ValueBuilder::annual;
76 use C4::Context;
77 my $DEBUG = 0;
78
79 sub get_barcode {
80     my ($args) = @_;
81     my $nextnum;
82     my $query = "select max(cast( substring_index(barcode, '-',-1) as signed)) from items where barcode like ?";
83     my $sth=C4::Context->dbh->prepare($query);
84     $sth->execute("$args->{year}%");
85     while (my ($count)= $sth->fetchrow_array) {
86         warn "Examining Record: $count" if $DEBUG;
87         $nextnum = $count if $count;
88     }
89     $nextnum++;
90     $nextnum = sprintf("%0*d", "4",$nextnum);
91     $nextnum = "$args->{year}-$nextnum";
92     return $nextnum;
93 }
94
95 1;
96
97
98 =head1 Barcodes::ValueBuilder
99
100 This module is intended as a shim to ease the eventual transition from
101 having all barcode-related code in the value builder plugin .pl file
102 to using C4::Barcodes. Since the shift will require a rather significant
103 amount of refactoring, this module will return value builder-formatted
104 results, at first by merely running the code that was formerly in the
105 barcodes.pl value builder, but later by using C4::Barcodes.
106
107 =cut
108
109 1;