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