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