Bug 17314: OpenAPI spec
[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 C4::Biblio qw( GetMarcFromKohaField );
28 use Koha::DateUtils qw( dt_from_string output_pref );
29
30 use Algorithm::CheckDigits qw( CheckDigits );
31
32 my $builder = sub {
33     my ( $params ) = @_;
34     my $function_name = $params->{id};
35     my %args;
36
37         # find today's date
38     ($args{year}, $args{mon}, $args{day}) = split('-', output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }));
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         if ((not $autoBarcodeType) or $autoBarcodeType eq 'OFF') {
46         # don't return a value unless we have the appropriate syspref set
47         return q|<script></script>|;
48     }
49         if ($autoBarcodeType eq 'annual') {
50         ($nextnum, $scr) = C4::Barcodes::ValueBuilder::annual::get_barcode(\%args);
51         }
52         elsif ($autoBarcodeType eq 'incremental') {
53         ($nextnum, $scr) = C4::Barcodes::ValueBuilder::incremental::get_barcode(\%args);
54     }
55     elsif ($autoBarcodeType eq 'hbyymmincr') {      # Generates a barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number, reset yearly -fbcit
56         ($nextnum, $scr) = C4::Barcodes::ValueBuilder::hbyymmincr::get_barcode(\%args);
57     }
58     elsif ($autoBarcodeType eq 'EAN13') {
59         # not the best, two catalogers could add the same barcode easily this way :/
60         my $query = "select max(abs(barcode)) from items";
61     my $dbh = $params->{dbh};
62         my $sth = $dbh->prepare($query);
63         $sth->execute();
64         while (my ($last)= $sth->fetchrow_array) {
65             $nextnum = $last;
66         }
67         my $ean = CheckDigits('ean');
68         if ( $ean->is_valid($nextnum) ) {
69             my $next = $ean->basenumber( $nextnum ) + 1;
70             $nextnum = $ean->complete( $next );
71             $nextnum = '0' x ( 13 - length($nextnum) ) . $nextnum; # pad zeros
72         } else {
73             warn "ERROR: invalid EAN-13 $nextnum, using increment";
74             $nextnum++;
75         }
76     }
77     else {
78         warn "ERROR: unknown autoBarcode: $autoBarcodeType";
79     }
80
81     # default js body (if not filled by hbyymmincr)
82     $scr or $scr = <<END_OF_JS;
83 if (\$('#' + id).val() == '' || force) {
84     \$('#' + id).val('$nextnum');
85 };
86 END_OF_JS
87
88     my $js  = <<END_OF_JS;
89 <script>
90
91 function Focus$function_name(id, force) {
92 $scr
93 }
94
95 function Click$function_name(id) {
96     Focus$function_name(id, 1);
97     return false;
98 }
99 </script>
100 END_OF_JS
101     return $js;
102 };
103
104 return { builder => $builder };