Bug 10480: Remove useless routines and irrelevant pod lines
[koha.git] / cataloguing / value_builder / barcode.pl
1 #!/usr/bin/perl
2 # Copyright 2000-2002 Katipo Communications
3 # Parts copyright 2008-2010 Foundations Bible College
4 #
5 # This file is part of Koha.
6 #
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
10 # version.
11 #
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.
15 #
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.
19
20 use strict;
21 use warnings;
22 no warnings 'redefine'; # otherwise loading up multiple plugins fills the log with subroutine redefine warnings
23
24 use C4::Context;
25 require C4::Barcodes::ValueBuilder;
26 require C4::Dates;
27
28 use Algorithm::CheckDigits;
29
30 my $DEBUG = 0;
31
32 sub plugin_javascript {
33         my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
34         my $function_name= "barcode".(int(rand(100000))+1);
35     my %args;
36
37         # find today's date
38     ($args{year}, $args{mon}, $args{day}) = split('-', C4::Dates->today('iso'));
39     ($args{tag},$args{subfield})       =  GetMarcFromKohaField("items.barcode", '');
40     ($args{loctag},$args{locsubfield}) =  GetMarcFromKohaField("items.homebranch", '');
41
42         my $nextnum;
43     my $scr;
44         my $autoBarcodeType = C4::Context->preference("autoBarcode");
45     warn "Barcode type = $autoBarcodeType" if $DEBUG;
46         if ((not $autoBarcodeType) or $autoBarcodeType eq 'OFF') {
47         # don't return a value unless we have the appropriate syspref set
48                 return ($function_name, 
49         "<script type=\"text/javascript\">
50         // autoBarcodeType OFF (or not defined)
51         function Focus$function_name() { return 0;}
52         function  Clic$function_name() { return 0;}
53         function  Blur$function_name() { return 0;}
54         </script>");
55     }
56         if ($autoBarcodeType eq 'annual') {
57         ($nextnum, $scr) = C4::Barcodes::ValueBuilder::annual::get_barcode(\%args);
58         }
59         elsif ($autoBarcodeType eq 'incremental') {
60         ($nextnum, $scr) = C4::Barcodes::ValueBuilder::incremental::get_barcode(\%args);
61     }
62     elsif ($autoBarcodeType eq 'hbyymmincr') {      # Generates a barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number, reset yearly -fbcit
63         ($nextnum, $scr) = C4::Barcodes::ValueBuilder::hbyymmincr::get_barcode(\%args);
64     }
65     elsif ($autoBarcodeType eq 'EAN13') {
66         # not the best, two catalogers could add the same barcode easily this way :/
67         my $query = "select max(abs(barcode)) from items";
68         my $sth = $dbh->prepare($query);
69         $sth->execute();
70         while (my ($last)= $sth->fetchrow_array) {
71             $nextnum = $last;
72         }
73         my $ean = CheckDigits('ean');
74         if ( $ean->is_valid($nextnum) ) {
75             my $next = $ean->basenumber( $nextnum ) + 1;
76             $nextnum = $ean->complete( $next );
77             $nextnum = '0' x ( 13 - length($nextnum) ) . $nextnum; # pad zeros
78         } else {
79             warn "ERROR: invalid EAN-13 $nextnum, using increment";
80             $nextnum++;
81         }
82     }
83     else {
84         warn "ERROR: unknown autoBarcode: $autoBarcodeType";
85     }
86
87     # default js body (if not filled by hbyymmincr)
88     $scr or $scr = <<END_OF_JS;
89 if (\$('#' + id).val() == '' || force) {
90     \$('#' + id).val('$nextnum');
91 }
92 END_OF_JS
93
94     my $js  = <<END_OF_JS;
95 <script type="text/javascript">
96 //<![CDATA[
97
98 function Focus$function_name(subfield_managed, id, force) {
99 $scr
100     return 0;
101 }
102
103 function Clic$function_name(id) {
104     return Focus$function_name('not_relavent', id, 1);
105 }
106 //]]>
107 </script>
108 END_OF_JS
109     return ($function_name, $js);
110 }