wr72054 fixing the z3950 search for acquisitions
[koha.git] / cataloguing / value_builder / barcode.pl
blobe21105b3ff89059003d2aa6dcbc049bdc040de88
1 #!/usr/bin/perl
3 # $Id: barcode.pl,v 1.1.2.2 2006/09/20 02:24:42 kados Exp $
5 # Copyright 2000-2002 Katipo Communications
7 # This file is part of Koha.
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.
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.
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.
22 #use strict;
23 #use warnings; FIXME - Bug 2505
24 use C4::Context;
25 require C4::Dates;
26 my $DEBUG = 0;
28 =head1
30 plugin_parameters : other parameters added when the plugin is called by the dopop function
32 =cut
33 sub plugin_parameters {
34 # my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
35 return "";
38 =head1
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
46 returns :
47 * XXX
48 * a variable containing the 3 scripts.
49 the 3 scripts are inserted after the <input> in the html code
51 =cut
52 sub plugin_javascript {
53 my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
54 my $function_name= "barcode".(int(rand(100000))+1);
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", '');
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>");
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;
84 $nextnum++;
85 $nextnum = sprintf("%0*d", "4",$nextnum);
86 $nextnum = "$year-$nextnum";
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;
96 $nextnum++;
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;
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;
117 if (\$('#' + id).val() == '' || force) {
118 \$('#' + id).val(document.f.field_value[fnum].value + '$nextnum');
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');
128 END_OF_JS
130 my $js = <<END_OF_JS;
131 <script type="text/javascript">
132 //<![CDATA[
134 function Blur$function_name(index) {
135 //barcode validation might go here
138 function Focus$function_name(subfield_managed, id, force) {
139 $scr
140 return 0;
143 function Clic$function_name(id) {
144 return Focus$function_name('not_relavent', id, 1);
146 //]]>
147 </script>
148 END_OF_JS
149 return ($function_name, $js);
152 =head1
154 plugin: useless here
156 =cut
158 sub plugin {
159 # my ($input) = @_;
160 return "";