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 with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
28 plugin_parameters : other parameters added when the plugin is called by the dopop function
31 sub plugin_parameters {
32 # my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
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
46 * a variable containing the 3 scripts.
47 the 3 scripts are inserted after the <input> in the html code
50 sub plugin_javascript {
51 my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
52 my $function_name= "barcode".(int(rand(100000))+1);
55 my ($year, $mon, $day) = split('-', C4::Dates->today('iso'));
56 my ($tag,$subfield) = GetMarcFromKohaField("items.barcode", '');
57 my ($loctag,$locsubfield) = GetMarcFromKohaField("items.homebranch", '');
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;}
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;
83 $nextnum = sprintf("%0*d", "4",$nextnum);
84 $nextnum = "$year-$nextnum";
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);
91 while (my ($count)= $sth->fetchrow_array) {
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;
106 $nextnum = sprintf("%0*d", "4",$nextnum);
107 $nextnum = $year . $mon . $nextnum;
108 warn "New hbyymmincr Barcode = $nextnum" if $DEBUG;
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') {
115 if (\$('#' + id).val() == '' || force) {
116 \$('#' + id).val(document.f.field_value[fnum].value + '$nextnum');
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');
128 my $js = <<END_OF_JS;
129 <script type="text/javascript">
132 function Blur$function_name(index) {
133 //barcode validation might go here
136 function Focus$function_name(subfield_managed, id, force) {
141 function Clic$function_name(id) {
142 return Focus$function_name('not_relavent', id, 1);
147 return ($function_name, $js);