Bug 21395: Remove 'variable $DEBUG masks earlier declaration in same scope' warning
[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 {
22     package C4::Barcodes::ValueBuilder::incremental;
23
24     use Modern::Perl;
25     use C4::Context;
26     my $DEBUG = 0;
27
28     sub get_barcode {
29         my ($args) = @_;
30         my $nextnum;
31         # not the best, two catalogers could add the same barcode easily this way :/
32         my $query = "select max(abs(barcode)) from items";
33         my $sth = C4::Context->dbh->prepare($query);
34         $sth->execute();
35         while (my ($count)= $sth->fetchrow_array) {
36             $nextnum = $count;
37         }
38         $nextnum++;
39         return $nextnum;
40     }
41 }
42
43 {
44     package C4::Barcodes::ValueBuilder::hbyymmincr;
45     use C4::Context;
46     my $DEBUG = 0;
47
48     sub get_barcode {
49         my ($args) = @_;
50         my $nextnum = 0;
51         my $year = substr($args->{year}, -2);
52         my $month = $args->{mon};
53         my $query = "SELECT MAX(CAST(SUBSTRING(barcode,-4) AS signed)) AS number FROM items WHERE barcode REGEXP ?";
54         my $sth = C4::Context->dbh->prepare($query);
55         $sth->execute("^[-a-zA-Z]{1,}$year$month");
56         while (my ($count)= $sth->fetchrow_array) {
57             $nextnum = $count if $count;
58             $nextnum = 0 if $nextnum == 9999; # this sequence only allows for cataloging 9999 items per month
59                 warn "Existing incremental number = $nextnum" if $DEBUG;
60         }
61         $nextnum++;
62         $nextnum = sprintf("%0*d", "4",$nextnum);
63         $nextnum = $year . $month . $nextnum;
64         warn "New hbyymmincr Barcode = $nextnum" if $DEBUG;
65         my $scr = "
66             var form = document.getElementById('f');
67             if ( !form ) {
68                 form = document.getElementById('serials_edit');
69             }
70             if ( !form ) {
71                 form = document.getElementById('Aform');
72             }
73             for (i=0 ; i<form.field_value.length ; i++) {
74                 if (form.tag[i].value == '$args->{loctag}' && form.subfield[i].value == '$args->{locsubfield}') {
75                     fnum = i;
76                 }
77             }
78         if (\$('#' + id).val() == '') {
79             \$('#' + id).val(form.field_value[fnum].value + '$nextnum');
80         }
81         ";
82         return $nextnum, $scr;
83     }
84 }
85
86 {
87     package C4::Barcodes::ValueBuilder::annual;
88     use C4::Context;
89     my $DEBUG = 0;
90
91     sub get_barcode {
92         my ($args) = @_;
93         my $nextnum;
94         my $query = "select max(cast( substring_index(barcode, '-',-1) as signed)) from items where barcode like ?";
95         my $sth=C4::Context->dbh->prepare($query);
96         $sth->execute($args->{year} . '-%');
97         while (my ($count)= $sth->fetchrow_array) {
98             warn "Examining Record: $count" if $DEBUG;
99             $nextnum = $count if $count;
100         }
101         $nextnum++;
102         $nextnum = sprintf("%0*d", "4",$nextnum);
103         $nextnum = "$args->{year}-$nextnum";
104         return $nextnum;
105     }
106 }
107
108
109 =head1 Barcodes::ValueBuilder
110
111 This module is intended as a shim to ease the eventual transition from
112 having all barcode-related code in the value builder plugin .pl file
113 to using C4::Barcodes. Since the shift will require a rather significant
114 amount of refactoring, this module will return value builder-formatted
115 results, at first by merely running the code that was formerly in the
116 barcodes.pl value builder, but later by using C4::Barcodes.
117
118 =cut
119
120 1;