Bug 23049: Update maninvoice to reference debit types
[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 C4::Context;
23 use C4::Auth;
24 use C4::Output;
25
26 use Koha::Account::DebitType;
27 use Koha::Account::DebitTypes;
28
29 my $input = new CGI;
30 my $code  = $input->param('code');
31 my $op    = $input->param('op') || 'list';
32 my @messages;
33
34 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
35     {
36         template_name   => "admin/debit_types.tt",
37         query           => $input,
38         type            => "intranet",
39         authnotrequired => 0,
40         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
41         debug           => 1,
42     }
43 );
44
45 my $debit_type;
46 if ($code) {
47     $debit_type = Koha::Account::DebitTypes->find($code);
48 }
49
50 if ( $op eq 'add_form' ) {
51
52     my $selected_branches =
53       $debit_type ? $debit_type->get_library_limits : undef;
54     my $branches =
55       Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
56     my @branches_loop;
57     foreach my $branch (@$branches) {
58         my $selected =
59           ( $selected_branches
60               && grep { $_->branchcode eq $branch->{branchcode} }
61               @{ $selected_branches->as_list } ) ? 1 : 0;
62         push @branches_loop,
63           {
64             branchcode => $branch->{branchcode},
65             branchname => $branch->{branchname},
66             selected   => $selected,
67           };
68     }
69
70     $template->param(
71         debit_type    => $debit_type,
72         branches_loop => \@branches_loop
73     );
74 }
75 elsif ( $op eq 'add_validate' ) {
76     my $description           = $input->param('description');
77     my $can_be_added_manually = $input->param('can_be_added_manually') || 0;
78     my $default_amount        = $input->param('default_amount') || undef;
79     my @branches = grep { $_ ne q{} } $input->multi_param('branches');
80
81     if ( not defined $debit_type ) {
82         $debit_type = Koha::Account::DebitType->new( { code => $code } );
83     }
84     $debit_type->description($description);
85     $debit_type->can_be_added_manually($can_be_added_manually);
86     $debit_type->default_amount($default_amount);
87
88     eval {
89         $debit_type->store;
90         $debit_type->replace_library_limits( \@branches );
91     };
92     if ($@) {
93         push @messages, { type => 'error', code => 'error_on_saving' };
94     }
95     else {
96         push @messages, { type => 'message', code => 'success_on_saving' };
97     }
98     $op = 'list';
99 }
100 elsif ( $op eq 'delete_confirm' ) {
101     $template->param( debit_type => $debit_type );
102 }
103 elsif ( $op eq 'delete_confirmed' ) {
104     my $deleted = eval { $debit_type->delete; };
105
106     if ( $@ or not $deleted ) {
107         push @messages, { type => 'error', code => 'error_on_delete' };
108     }
109     else {
110         push @messages, { type => 'message', code => 'success_on_delete' };
111     }
112     $op = 'list';
113 }
114
115 if ( $op eq 'list' ) {
116     my $debit_types = Koha::Account::DebitTypes->search();
117     $template->param(
118         debit_types  => $debit_types,
119     );
120 }
121
122 $template->param(
123     code     => $code,
124     messages => \@messages,
125     op       => $op,
126 );
127
128 output_html_with_http_headers $input, $cookie, $template->output;