Bug 16203: Convert item plugins to new style (see bug 10480)
[koha.git] / cataloguing / value_builder / barcode.pl
1 #!/usr/bin/perl
2
3 # Converted to new plugin style (Bug 13437)
4
5 # Copyright 2000-2002 Katipo Communications
6 # Parts copyright 2008-2010 Foundations Bible College
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22
23 use Modern::Perl;
24
25 use C4::Context;
26 use C4::Barcodes::ValueBuilder;
27 use Koha::DateUtils;
28
29 use Algorithm::CheckDigits;
30
31 my $DEBUG = 0;
32
33 my $builder = sub {
34     my ( $params ) = @_;
35     my $function_name = $params->{id};
36     my %args;
37
38         # find today's date
39     ($args{year}, $args{mon}, $args{day}) = split('-', output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }));
40     ($args{tag},$args{subfield})       =  GetMarcFromKohaField("items.barcode", '');
41     ($args{loctag},$args{locsubfield}) =  GetMarcFromKohaField("items.homebranch", '');
42
43         my $nextnum;
44     my $scr;
45         my $autoBarcodeType = C4::Context->preference("autoBarcode");
46     warn "Barcode type = $autoBarcodeType" if $DEBUG;
47         if ((not $autoBarcodeType) or $autoBarcodeType eq 'OFF') {
48         # don't return a value unless we have the appropriate syspref set
49         return q|<script type=\"text/javascript\"></script>|;
50     }
51         if ($autoBarcodeType eq 'annual') {
52         ($nextnum, $scr) = C4::Barcodes::ValueBuilder::annual::get_barcode(\%args);
53         }
54         elsif ($autoBarcodeType eq 'incremental') {
55         ($nextnum, $scr) = C4::Barcodes::ValueBuilder::incremental::get_barcode(\%args);
56     }
57     elsif ($autoBarcodeType eq 'hbyymmincr') {      # Generates a barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number, reset yearly -fbcit
58         ($nextnum, $scr) = C4::Barcodes::ValueBuilder::hbyymmincr::get_barcode(\%args);
59     }
60     elsif ($autoBarcodeType eq 'EAN13') {
61         # not the best, two catalogers could add the same barcode easily this way :/
62         my $query = "select max(abs(barcode)) from items";
63     my $dbh = $params->{dbh};
64         my $sth = $dbh->prepare($query);
65         $sth->execute();
66         while (my ($last)= $sth->fetchrow_array) {
67             $nextnum = $last;
68         }
69         my $ean = CheckDigits('ean');
70         if ( $ean->is_valid($nextnum) ) {
71             my $next = $ean->basenumber( $nextnum ) + 1;
72             $nextnum = $ean->complete( $next );
73             $nextnum = '0' x ( 13 - length($nextnum) ) . $nextnum; # pad zeros
74         } else {
75             warn "ERROR: invalid EAN-13 $nextnum, using increment";
76             $nextnum++;
77         }
78     }
79     else {
80         warn "ERROR: unknown autoBarcode: $autoBarcodeType";
81     }
82
83     # default js body (if not filled by hbyymmincr)
84     $scr or $scr = <<END_OF_JS;
85 if (\$('#' + id).val() == '' || force) {
86     \$('#' + id).val('$nextnum');
87 };
88 END_OF_JS
89
90     my $js  = <<END_OF_JS;
91 <script type="text/javascript">
92 //<![CDATA[
93
94 function Focus$function_name(id, force) {
95 $scr
96 }
97
98 function Click$function_name(id) {
99     Focus$function_name(id, 1);
100     return false;
101 }
102 //]]>
103 </script>
104 END_OF_JS
105     return $js;
106 };
107
108 return { builder => $builder };