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