Bug 8385: Dirty patch to make tt_valid.t happy
[koha.git] / cataloguing / value_builder / barcode.pl
1 #!/usr/bin/perl
2 # Copyright 2000-2002 Katipo Communications
3 # Parts copyright 2008-2010 Foundations Bible College
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22 no warnings 'redefine'; # otherwise loading up multiple plugins fills the log with subroutine redefine warnings
23
24 use C4::Context;
25 require C4::Dates;
26
27 use Algorithm::CheckDigits;
28
29 my $DEBUG = 0;
30
31 =head1
32
33 plugin_parameters : other parameters added when the plugin is called by the dopop function
34
35 =cut
36
37 sub plugin_parameters {
38 #   my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
39     return "";
40 }
41
42 =head1
43
44 plugin_javascript : the javascript function called when the user enters the subfield.
45 contain 3 javascript functions :
46 * one called when the field is entered (OnFocus). Named FocusXXX
47 * one called when the field is leaved (onBlur). Named BlurXXX
48 * one called when the ... link is clicked (<a href="javascript:function">) named ClicXXX
49
50 returns :
51 * XXX
52 * a variable containing the 3 scripts.
53 the 3 scripts are inserted after the <input> in the html code
54
55 =cut
56
57 sub plugin_javascript {
58         my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
59         my $function_name= "barcode".(int(rand(100000))+1);
60
61         # find today's date
62         my ($year, $mon, $day) = split('-', C4::Dates->today('iso'));
63         my ($tag,$subfield)       =  GetMarcFromKohaField("items.barcode", '');
64         my ($loctag,$locsubfield) =  GetMarcFromKohaField("items.homebranch", '');
65
66         my $nextnum;
67         my $query;
68     my $scr;
69         my $autoBarcodeType = C4::Context->preference("autoBarcode");
70     warn "Barcode type = $autoBarcodeType" if $DEBUG;
71         if ((not $autoBarcodeType) or $autoBarcodeType eq 'OFF') {
72         # don't return a value unless we have the appropriate syspref set
73                 return ($function_name, 
74         "<script type=\"text/javascript\">
75         // autoBarcodeType OFF (or not defined)
76         function Focus$function_name() { return 0;}
77         function  Clic$function_name() { return 0;}
78         function  Blur$function_name() { return 0;}
79         </script>");
80     }
81         if ($autoBarcodeType eq 'annual') {
82                 $query = "select max(cast( substring_index(barcode, '-',-1) as signed)) from items where barcode like ?";
83                 my $sth=$dbh->prepare($query);
84                 $sth->execute("$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 = "$year-$nextnum";
92         }
93         elsif ($autoBarcodeType eq 'incremental') {
94                 # not the best, two catalogers could add the same barcode easily this way :/
95                 $query = "select max(abs(barcode)) from items";
96         my $sth = $dbh->prepare($query);
97                 $sth->execute();
98                 while (my ($count)= $sth->fetchrow_array) {
99                         $nextnum = $count;
100                 }
101                 $nextnum++;
102     }
103     elsif ($autoBarcodeType eq 'hbyymmincr') {      # Generates a barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number, reset yearly -fbcit
104         $year = substr($year, -2);
105         $query = "SELECT MAX(CAST(SUBSTRING(barcode,-4) AS signed)) AS number FROM items WHERE barcode REGEXP ?";
106         my $sth = $dbh->prepare($query);
107         $sth->execute("^[-a-zA-Z]{1,}$year");
108         while (my ($count)= $sth->fetchrow_array) {
109             $nextnum = $count if $count;
110             $nextnum = 0 if $nextnum == 9999; # this sequence only allows for cataloging 10000 books per month
111             warn "Existing incremental number = $nextnum" if $DEBUG;
112         }
113         $nextnum++;
114         $nextnum = sprintf("%0*d", "4",$nextnum);
115         $nextnum = $year . $mon . $nextnum;
116         warn "New hbyymmincr Barcode = $nextnum" if $DEBUG;
117         $scr = " 
118         for (i=0 ; i<document.f.field_value.length ; i++) {
119             if (document.f.tag[i].value == '$loctag' && document.f.subfield[i].value == '$locsubfield') {
120                 fnum = i;
121             }
122         }
123         if (\$('#' + id).val() == '' || force) {
124             \$('#' + id).val(document.f.field_value[fnum].value + '$nextnum');
125         }
126         ";
127     }
128     elsif ($autoBarcodeType eq 'EAN13') {
129         # not the best, two catalogers could add the same barcode easily this way :/
130         $query = "select max(abs(barcode)) from items";
131         my $sth = $dbh->prepare($query);
132         $sth->execute();
133         while (my ($last)= $sth->fetchrow_array) {
134             $nextnum = $last;
135         }
136         my $ean = CheckDigits('ean');
137         if ( $ean->is_valid($nextnum) ) {
138             my $next = $ean->basenumber( $nextnum ) + 1;
139             $nextnum = $ean->complete( $next );
140             $nextnum = '0' x ( 13 - length($nextnum) ) . $nextnum; # pad zeros
141         } else {
142             warn "ERROR: invalid EAN-13 $nextnum, using increment";
143             $nextnum++;
144         }
145     }
146     else {
147         warn "ERROR: unknown autoBarcode: $autoBarcodeType";
148     }
149
150     # default js body (if not filled by hbyymmincr)
151     $scr or $scr = <<END_OF_JS;
152 if (\$('#' + id).val() == '' || force) {
153     \$('#' + id).val('$nextnum');
154 }
155 END_OF_JS
156
157     my $js  = <<END_OF_JS;
158 <script type="text/javascript">
159 //<![CDATA[
160
161 function Blur$function_name(index) {
162     //barcode validation might go here
163 }
164
165 function Focus$function_name(subfield_managed, id, force) {
166 $scr
167     return 0;
168 }
169
170 function Clic$function_name(id) {
171     return Focus$function_name('not_relavent', id, 1);
172 }
173 //]]>
174 </script>
175 END_OF_JS
176     return ($function_name, $js);
177 }
178
179 =head1
180
181 plugin: useless here
182
183 =cut
184
185 sub plugin {
186     # my ($input) = @_;
187     return "";
188 }
189
190 1;