Bug 7276 : Follow up, adding a sub to clear the cache
[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 my $DEBUG = 0;
28
29 =head1
30
31 plugin_parameters : other parameters added when the plugin is called by the dopop function
32
33 =cut
34
35 sub plugin_parameters {
36 #   my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
37     return "";
38 }
39
40 =head1
41
42 plugin_javascript : the javascript function called when the user enters the subfield.
43 contain 3 javascript functions :
44 * one called when the field is entered (OnFocus). Named FocusXXX
45 * one called when the field is leaved (onBlur). Named BlurXXX
46 * one called when the ... link is clicked (<a href="javascript:function">) named ClicXXX
47
48 returns :
49 * XXX
50 * a variable containing the 3 scripts.
51 the 3 scripts are inserted after the <input> in the html code
52
53 =cut
54
55 sub plugin_javascript {
56         my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
57         my $function_name= "barcode".(int(rand(100000))+1);
58
59         # find today's date
60         my ($year, $mon, $day) = split('-', C4::Dates->today('iso'));
61         my ($tag,$subfield)       =  GetMarcFromKohaField("items.barcode", '');
62         my ($loctag,$locsubfield) =  GetMarcFromKohaField("items.homebranch", '');
63
64         my $nextnum;
65         my $query;
66     my $scr;
67         my $autoBarcodeType = C4::Context->preference("autoBarcode");
68     warn "Barcode type = $autoBarcodeType" if $DEBUG;
69         if ((not $autoBarcodeType) or $autoBarcodeType eq 'OFF') {
70         # don't return a value unless we have the appropriate syspref set
71                 return ($function_name, 
72         "<script type=\"text/javascript\">
73         // autoBarcodeType OFF (or not defined)
74         function Focus$function_name() { return 0;}
75         function  Clic$function_name() { return 0;}
76         function  Blur$function_name() { return 0;}
77         </script>");
78     }
79         if ($autoBarcodeType eq 'annual') {
80                 $query = "select max(cast( substring_index(barcode, '-',-1) as signed)) from items where barcode like ?";
81                 my $sth=$dbh->prepare($query);
82                 $sth->execute("$year%");
83                 while (my ($count)= $sth->fetchrow_array) {
84             warn "Examining Record: $count" if $DEBUG;
85                 $nextnum = $count if $count;
86                 }
87                 $nextnum++;
88                 $nextnum = sprintf("%0*d", "4",$nextnum);
89                 $nextnum = "$year-$nextnum";
90         }
91         elsif ($autoBarcodeType eq 'incremental') {
92                 # not the best, two catalogers could add the same barcode easily this way :/
93                 $query = "select max(abs(barcode)) from items";
94         my $sth = $dbh->prepare($query);
95                 $sth->execute();
96                 while (my ($count)= $sth->fetchrow_array) {
97                         $nextnum = $count;
98                 }
99                 $nextnum++;
100     }
101     elsif ($autoBarcodeType eq 'hbyymmincr') {      # Generates a barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number, reset yearly -fbcit
102         $year = substr($year, -2);
103         $query = "SELECT MAX(CAST(SUBSTRING(barcode,-4) AS signed)) AS number FROM items WHERE barcode REGEXP ?";
104         my $sth = $dbh->prepare($query);
105         $sth->execute("^[-a-zA-Z]{1,}$year");
106         while (my ($count)= $sth->fetchrow_array) {
107             $nextnum = $count if $count;
108             $nextnum = 0 if $nextnum == 9999; # this sequence only allows for cataloging 10000 books per month
109             warn "Existing incremental number = $nextnum" if $DEBUG;
110         }
111         $nextnum++;
112         $nextnum = sprintf("%0*d", "4",$nextnum);
113         $nextnum = $year . $mon . $nextnum;
114         warn "New hbyymmincr Barcode = $nextnum" if $DEBUG;
115         $scr = " 
116         for (i=0 ; i<document.f.field_value.length ; i++) {
117             if (document.f.tag[i].value == '$loctag' && document.f.subfield[i].value == '$locsubfield') {
118                 fnum = i;
119             }
120         }
121         if (\$('#' + id).val() == '' || force) {
122             \$('#' + id).val(document.f.field_value[fnum].value + '$nextnum');
123         }
124         ";
125     }
126
127     # default js body (if not filled by hbyymmincr)
128     $scr or $scr = <<END_OF_JS;
129 if (\$('#' + id).val() == '' || force) {
130     \$('#' + id).val('$nextnum');
131 }
132 END_OF_JS
133
134     my $js  = <<END_OF_JS;
135 <script type="text/javascript">
136 //<![CDATA[
137
138 function Blur$function_name(index) {
139     //barcode validation might go here
140 }
141
142 function Focus$function_name(subfield_managed, id, force) {
143 $scr
144     return 0;
145 }
146
147 function Clic$function_name(id) {
148     return Focus$function_name('not_relavent', id, 1);
149 }
150 //]]>
151 </script>
152 END_OF_JS
153     return ($function_name, $js);
154 }
155
156 =head1
157
158 plugin: useless here
159
160 =cut
161
162 sub plugin {
163     # my ($input) = @_;
164     return "";
165 }
166
167 1;