Bug 28787: (QA follow-up) Remove unused variable
[koha.git] / cataloguing / value_builder / stocknumberAV.pl
1 #!/usr/bin/perl
2
3 # Converted to new plugin style (Bug 13437)
4
5 # Copyright 2012 BibLibre SARL
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 use Modern::Perl;
23 use CGI qw ( -utf8 );
24
25 use C4::Auth qw( get_template_and_user );
26 use C4::Output qw( output_html_with_http_headers );
27 use Koha::AuthorisedValues;
28
29 =head1 DESCRIPTION
30
31 This plugin is based on authorised values from INVENTORY.
32 It is used for stocknumber computation.
33
34 If no prefix is submitted, or the prefix does contain only
35 numbers, it returns the inserted code (= keep the field unchanged).
36
37 If a prefix is submitted, we look for the highest stocknumber
38 with this prefix and return it incremented.
39
40 In this case, a stocknumber has this form (e.g. "PREFIX 0009678570"):
41 PREFIX containing letters, a space separator and 10 digits with leading
42 0s if needed.
43
44 =cut
45
46 my $builder = sub {
47     my ( $params ) = @_;
48     my $res = qq{
49     <script>
50         function Click$params->{id}() {
51                 var code = document.getElementById('$params->{id}');
52                 \$.ajax({
53                     url: '/cgi-bin/koha/cataloguing/plugin_launcher.pl',
54                     type: 'POST',
55                     data: {
56                         'plugin_name': 'stocknumberAV.pl',
57                         'code'    : code.value,
58                     },
59                     success: function(data){
60                         var field = document.getElementById('$params->{id}');
61                         field.value = data;
62                         return 1;
63                     }
64                 });
65         }
66     </script>
67     };
68
69     return $res;
70 };
71
72 my $launcher = sub {
73     my ( $params ) = @_;
74     my $input = $params->{cgi};
75     my $code = $input->param('code');
76
77     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
78         {   template_name   => "cataloguing/value_builder/ajax.tt",
79             query           => $input,
80             type            => "intranet",
81             flagsrequired   => { editcatalogue => '*' },
82         }
83     );
84
85     # If a prefix is submited, we look for the highest stocknumber with this prefix, and return it incremented
86     $code =~ s/ *$//g;
87     if ( $code =~ m/^[a-zA-Z]+$/ ) {
88         my $av = Koha::AuthorisedValues->find({
89             'category' => 'INVENTORY',
90             'authorised_value' => $code
91         });
92         if ( $av ) {
93             $av->lib($av->lib + 1);
94             $av->store;
95             $template->param( return => $code . ' ' . sprintf( '%010s', ( $av->lib ) ), );
96         } else {
97             $template->param( return => "There is no defined value for $code");
98         }
99         # The user entered a custom value, we don't touch it, this could be handled in js
100     } else {
101         $template->param( return => $code, );
102     }
103
104     output_html_with_http_headers $input, $cookie, $template->output;
105 };
106
107 return { builder => $builder, launcher => $launcher };