Bug 25898: Prohibit indirect object notation
[koha.git] / admin / debit_types.pl
1 #! /usr/bin/perl
2
3 # Copyright 2019 Koha Development Team
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use CGI qw ( -utf8 );
22 use Try::Tiny;
23
24 use C4::Context;
25 use C4::Auth;
26 use C4::Output;
27
28 use Koha::Account::DebitType;
29 use Koha::Account::DebitTypes;
30
31 my $input = CGI->new;
32 my $code  = $input->param('code');
33 my $op    = $input->param('op') || 'list';
34 my @messages;
35
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
37     {
38         template_name   => "admin/debit_types.tt",
39         query           => $input,
40         type            => "intranet",
41         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
42         debug           => 1,
43     }
44 );
45
46 my $debit_type;
47 if ($code) {
48     $debit_type = Koha::Account::DebitTypes->find($code);
49 }
50
51 if ( $op eq 'add_form' ) {
52
53     my $selected_branches =
54       $debit_type ? $debit_type->get_library_limits : undef;
55     my $branches =
56       Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
57     my @branches_loop;
58     foreach my $branch (@$branches) {
59         my $selected =
60           ( $selected_branches
61               && grep { $_->branchcode eq $branch->{branchcode} }
62               @{ $selected_branches->as_list } ) ? 1 : 0;
63         push @branches_loop,
64           {
65             branchcode => $branch->{branchcode},
66             branchname => $branch->{branchname},
67             selected   => $selected,
68           };
69     }
70
71     $template->param(
72         debit_type    => $debit_type,
73         branches_loop => \@branches_loop
74     );
75 }
76 elsif ( $op eq 'add_validate' ) {
77     my $description           = $input->param('description');
78     my $can_be_invoiced = $input->param('can_be_invoiced') || 0;
79     my $can_be_sold = $input->param('can_be_sold') || 0;
80     my $default_amount        = $input->param('default_amount') || undef;
81     my @branches = grep { $_ ne q{} } $input->multi_param('branches');
82
83     if ( not defined $debit_type ) {
84         $debit_type = Koha::Account::DebitType->new( { code => $code } );
85     }
86     $debit_type->description($description);
87     $debit_type->can_be_invoiced($can_be_invoiced);
88     $debit_type->can_be_sold($can_be_sold);
89     $debit_type->default_amount($default_amount);
90
91     try {
92         $debit_type->store;
93         $debit_type->replace_library_limits( \@branches );
94         push @messages, { type => 'message', code => 'success_on_saving' };
95     }
96     catch {
97         push @messages, { type => 'error', code => 'error_on_saving' };
98     };
99     $op = 'list';
100 }
101 elsif ( $op eq 'archive' ) {
102     try {
103         $debit_type->archived(1)->store();
104         push @messages, { code => 'success_on_archive', type => 'message' };
105     }
106     catch {
107         push @messages, { code => 'error_on_archive', type => 'alert' };
108
109     };
110     $op = 'list';
111 }
112 elsif ( $op eq 'unarchive' ) {
113     try {
114         $debit_type->archived(0)->store();
115         push @messages, { code => 'success_on_restore', type => 'message' };
116     }
117     catch {
118         push @messages, { code => 'error_on_restore', type => 'alert' };
119     };
120     $op = 'list';
121 }
122
123 if ( $op eq 'list' ) {
124     my $debit_types = Koha::Account::DebitTypes->search();
125     $template->param( debit_types => $debit_types, );
126 }
127
128 $template->param(
129     code     => $code,
130     messages => \@messages,
131     op       => $op,
132 );
133
134 output_html_with_http_headers $input, $cookie, $template->output;