2 # Copyright 2000-2002 Katipo Communications
3 # Parts copyright 2008-2010 Foundations Bible College
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 no warnings 'redefine'; # otherwise loading up multiple plugins fills the log with subroutine redefine warnings
25 require C4::Barcodes::ValueBuilder;
28 use Algorithm::CheckDigits;
34 plugin_parameters : other parameters added when the plugin is called by the dopop function
38 sub plugin_parameters {
39 # my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
45 plugin_javascript : the javascript function called when the user enters the subfield.
46 contain 3 javascript functions :
47 * one called when the field is entered (OnFocus). Named FocusXXX
48 * one called when the field is leaved (onBlur). Named BlurXXX
49 * one called when the ... link is clicked (<a href="javascript:function">) named ClicXXX
53 * a variable containing the 3 scripts.
54 the 3 scripts are inserted after the <input> in the html code
58 sub plugin_javascript {
59 my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
60 my $function_name= "barcode".(int(rand(100000))+1);
64 ($args{year}, $args{mon}, $args{day}) = split('-', C4::Dates->today('iso'));
65 ($args{tag},$args{subfield}) = GetMarcFromKohaField("items.barcode", '');
66 ($args{loctag},$args{locsubfield}) = GetMarcFromKohaField("items.homebranch", '');
70 my $autoBarcodeType = C4::Context->preference("autoBarcode");
71 warn "Barcode type = $autoBarcodeType" if $DEBUG;
72 if ((not $autoBarcodeType) or $autoBarcodeType eq 'OFF') {
73 # don't return a value unless we have the appropriate syspref set
74 return ($function_name,
75 "<script type=\"text/javascript\">
76 // autoBarcodeType OFF (or not defined)
77 function Focus$function_name() { return 0;}
78 function Clic$function_name() { return 0;}
79 function Blur$function_name() { return 0;}
82 if ($autoBarcodeType eq 'annual') {
83 ($nextnum, $scr) = C4::Barcodes::ValueBuilder::annual::get_barcode(\%args);
85 elsif ($autoBarcodeType eq 'incremental') {
86 ($nextnum, $scr) = C4::Barcodes::ValueBuilder::incremental::get_barcode(\%args);
88 elsif ($autoBarcodeType eq 'hbyymmincr') { # Generates a barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number, reset yearly -fbcit
89 ($nextnum, $scr) = C4::Barcodes::ValueBuilder::hbyymmincr::get_barcode(\%args);
91 elsif ($autoBarcodeType eq 'EAN13') {
92 # not the best, two catalogers could add the same barcode easily this way :/
93 my $query = "select max(abs(barcode)) from items";
94 my $sth = $dbh->prepare($query);
96 while (my ($last)= $sth->fetchrow_array) {
99 my $ean = CheckDigits('ean');
100 if ( $ean->is_valid($nextnum) ) {
101 my $next = $ean->basenumber( $nextnum ) + 1;
102 $nextnum = $ean->complete( $next );
103 $nextnum = '0' x ( 13 - length($nextnum) ) . $nextnum; # pad zeros
105 warn "ERROR: invalid EAN-13 $nextnum, using increment";
110 warn "ERROR: unknown autoBarcode: $autoBarcodeType";
113 # default js body (if not filled by hbyymmincr)
114 $scr or $scr = <<END_OF_JS;
115 if (\$('#' + id).val() == '' || force) {
116 \$('#' + id).val('$nextnum');
120 my $js = <<END_OF_JS;
121 <script type="text/javascript">
124 function Blur$function_name(index) {
125 //barcode validation might go here
128 function Focus$function_name(subfield_managed, id, force) {
133 function Clic$function_name(id) {
134 return Focus$function_name('not_relavent', id, 1);
139 return ($function_name, $js);