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
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.
23 #use warnings; FIXME - Bug 2505
30 plugin_parameters : other parameters added when the plugin is called by the dopop function
34 sub plugin_parameters {
35 # my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
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
49 * a variable containing the 3 scripts.
50 the 3 scripts are inserted after the <input> in the html code
54 sub plugin_javascript {
55 my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
56 my $function_name= "barcode".(int(rand(100000))+1);
59 my ($year, $mon, $day) = split('-', C4::Dates->today('iso'));
60 my ($tag,$subfield) = GetMarcFromKohaField("items.barcode", '');
61 my ($loctag,$locsubfield) = GetMarcFromKohaField("items.homebranch", '');
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;}
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;
87 $nextnum = sprintf("%0*d", "4",$nextnum);
88 $nextnum = "$year-$nextnum";
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);
95 while (my ($count)= $sth->fetchrow_array) {
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;
110 $nextnum = sprintf("%0*d", "4",$nextnum);
111 $nextnum = $year . $mon . $nextnum;
112 warn "New hbyymmincr Barcode = $nextnum" if $DEBUG;
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') {
119 if (\$('#' + id).val() == '' || force) {
120 \$('#' + id).val(document.f.field_value[fnum].value + '$nextnum');
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');
132 my $js = <<END_OF_JS;
133 <script type="text/javascript">
136 function Blur$function_name(index) {
137 //barcode validation might go here
140 function Focus$function_name(subfield_managed, id, force) {
145 function Clic$function_name(id) {
146 return Focus$function_name('not_relavent', id, 1);
151 return ($function_name, $js);