Bug 30650: Be more flexible with opening slots
[koha.git] / admin / credit_types.pl
1 #! /usr/bin/perl
2
3 # Copyright 2020 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 qw( catch try );
23
24 use C4::Context;
25 use C4::Auth qw( get_template_and_user );
26 use C4::Output qw( output_html_with_http_headers );
27
28 use Koha::Account::CreditType;
29 use Koha::Account::CreditTypes;
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/credit_types.tt",
39         query           => $input,
40         type            => "intranet",
41         flagsrequired   => { parameters => 'manage_accounts' },
42     }
43 );
44
45 my $credit_type;
46 if ($code) {
47     $credit_type = Koha::Account::CreditTypes->find($code);
48 }
49
50 if ( $op eq 'add_form' ) {
51
52     my $selected_branches =
53       $credit_type ? $credit_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         credit_type    => $credit_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 $credit_number_enabled = $input->param('credit_number_enabled') || 0;
79     my @branches = grep { $_ ne q{} } $input->multi_param('branches');
80
81     if ( not defined $credit_type ) {
82         $credit_type = Koha::Account::CreditType->new( { code => $code } );
83     }
84     unless ($credit_type->is_system) {
85         $credit_type->description($description);
86         $credit_type->can_be_added_manually($can_be_added_manually);
87     }
88     $credit_type->credit_number_enabled($credit_number_enabled);
89
90     try {
91         $credit_type->store;
92         unless ($credit_type->is_system) {
93             $credit_type->replace_library_limits( \@branches );
94         }
95         push @messages, { type => 'message', code => 'success_on_saving' };
96     }
97     catch {
98         push @messages, { type => 'error', code => 'error_on_saving' };
99     };
100     $op = 'list';
101 }
102 elsif ( $op eq 'archive' ) {
103     try {
104         $credit_type->archived(1)->store();
105         push @messages, { code => 'success_on_archive', type => 'message' };
106     }
107     catch {
108         push @messages, { code => 'error_on_archive', type => 'alert' };
109
110     };
111     $op = 'list';
112 }
113 elsif ( $op eq 'unarchive' ) {
114     try {
115         $credit_type->archived(0)->store();
116         push @messages, { code => 'success_on_restore', type => 'message' };
117     }
118     catch {
119         push @messages, { code => 'error_on_restore', type => 'alert' };
120     };
121     $op = 'list';
122 }
123
124 if ( $op eq 'list' ) {
125     my $credit_types = Koha::Account::CreditTypes->search();
126     $template->param( credit_types => $credit_types, );
127 }
128
129 $template->param(
130     code     => $code,
131     messages => \@messages,
132     op       => $op,
133 );
134
135 output_html_with_http_headers $input, $cookie, $template->output;