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