Bug 21395: Make perlcritic happy
[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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 package C4::Barcodes::ValueBuilder::incremental;
22
23 use Modern::Perl;
24 use C4::Context;
25 my $DEBUG = 0;
26
27 sub get_barcode {
28     my ($args) = @_;
29     my $nextnum;
30     # not the best, two catalogers could add the same barcode easily this way :/
31     my $query = "select max(abs(barcode)) from items";
32     my $sth = C4::Context->dbh->prepare($query);
33     $sth->execute();
34     while (my ($count)= $sth->fetchrow_array) {
35         $nextnum = $count;
36     }
37     $nextnum++;
38     return $nextnum;
39 }
40
41 1;
42
43 package C4::Barcodes::ValueBuilder::hbyymmincr;
44 use C4::Context;
45 my $DEBUG = 0;
46
47 sub get_barcode {
48     my ($args) = @_;
49     my $nextnum = 0;
50     my $year = substr($args->{year}, -2);
51     my $month = $args->{mon};
52     my $query = "SELECT MAX(CAST(SUBSTRING(barcode,-4) AS signed)) AS number FROM items WHERE barcode REGEXP ?";
53     my $sth = C4::Context->dbh->prepare($query);
54     $sth->execute("^[-a-zA-Z]{1,}$year$month");
55     while (my ($count)= $sth->fetchrow_array) {
56         $nextnum = $count if $count;
57         $nextnum = 0 if $nextnum == 9999; # this sequence only allows for cataloging 9999 items per month
58             warn "Existing incremental number = $nextnum" if $DEBUG;
59     }
60     $nextnum++;
61     $nextnum = sprintf("%0*d", "4",$nextnum);
62     $nextnum = $year . $month . $nextnum;
63     warn "New hbyymmincr Barcode = $nextnum" if $DEBUG;
64     my $scr = "
65         var form = document.getElementById('f');
66         if ( !form ) {
67             form = document.getElementById('serials_edit');
68         }
69         if ( !form ) {
70             form = document.getElementById('Aform');
71         }
72         for (i=0 ; i<form.field_value.length ; i++) {
73             if (form.tag[i].value == '$args->{loctag}' && form.subfield[i].value == '$args->{locsubfield}') {
74                 fnum = i;
75             }
76         }
77     if (\$('#' + id).val() == '') {
78         \$('#' + id).val(form.field_value[fnum].value + '$nextnum');
79     }
80     ";
81     return $nextnum, $scr;
82 }
83
84
85 package C4::Barcodes::ValueBuilder::annual;
86 use C4::Context;
87 my $DEBUG = 0;
88
89 sub get_barcode {
90     my ($args) = @_;
91     my $nextnum;
92     my $query = "select max(cast( substring_index(barcode, '-',-1) as signed)) from items where barcode like ?";
93     my $sth=C4::Context->dbh->prepare($query);
94     $sth->execute($args->{year} . '-%');
95     while (my ($count)= $sth->fetchrow_array) {
96         warn "Examining Record: $count" if $DEBUG;
97         $nextnum = $count if $count;
98     }
99     $nextnum++;
100     $nextnum = sprintf("%0*d", "4",$nextnum);
101     $nextnum = "$args->{year}-$nextnum";
102     return $nextnum;
103 }
104
105 1;
106
107
108 =head1 Barcodes::ValueBuilder
109
110 This module is intended as a shim to ease the eventual transition from
111 having all barcode-related code in the value builder plugin .pl file
112 to using C4::Barcodes. Since the shift will require a rather significant
113 amount of refactoring, this module will return value builder-formatted
114 results, at first by merely running the code that was formerly in the
115 barcodes.pl value builder, but later by using C4::Barcodes.
116
117 =cut
118
119 1;