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