Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.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;
23
24 use C4::Context;
25 use C4::Auth;
26 use C4::Output;
27
28 use Koha::Account::CreditType;
29 use Koha::Account::CreditTypes;
30
31 my $input = new CGI;
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         authnotrequired => 0,
42         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
43         debug           => 1,
44     }
45 );
46
47 my $credit_type;
48 if ($code) {
49     $credit_type = Koha::Account::CreditTypes->find($code);
50 }
51
52 if ( $op eq 'add_form' ) {
53
54     my $selected_branches =
55       $credit_type ? $credit_type->get_library_limits : undef;
56     my $branches =
57       Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
58     my @branches_loop;
59     foreach my $branch (@$branches) {
60         my $selected =
61           ( $selected_branches
62               && grep { $_->branchcode eq $branch->{branchcode} }
63               @{ $selected_branches->as_list } ) ? 1 : 0;
64         push @branches_loop,
65           {
66             branchcode => $branch->{branchcode},
67             branchname => $branch->{branchname},
68             selected   => $selected,
69           };
70     }
71
72     $template->param(
73         credit_type    => $credit_type,
74         branches_loop => \@branches_loop
75     );
76 }
77 elsif ( $op eq 'add_validate' ) {
78     my $description           = $input->param('description');
79     my $can_be_added_manually = $input->param('can_be_added_manually') || 0;
80     my $credit_number_enabled = $input->param('credit_number_enabled') || 0;
81     my @branches = grep { $_ ne q{} } $input->multi_param('branches');
82
83     if ( not defined $credit_type ) {
84         $credit_type = Koha::Account::CreditType->new( { code => $code } );
85     }
86     unless ($credit_type->is_system) {
87         $credit_type->description($description);
88         $credit_type->can_be_added_manually($can_be_added_manually);
89     }
90     $credit_type->credit_number_enabled($credit_number_enabled);
91
92     try {
93         $credit_type->store;
94         unless ($credit_type->is_system) {
95             $credit_type->replace_library_limits( \@branches );
96         }
97         push @messages, { type => 'message', code => 'success_on_saving' };
98     }
99     catch {
100         push @messages, { type => 'error', code => 'error_on_saving' };
101     };
102     $op = 'list';
103 }
104 elsif ( $op eq 'archive' ) {
105     try {
106         $credit_type->archived(1)->store();
107         push @messages, { code => 'success_on_archive', type => 'message' };
108     }
109     catch {
110         push @messages, { code => 'error_on_archive', type => 'alert' };
111
112     };
113     $op = 'list';
114 }
115 elsif ( $op eq 'unarchive' ) {
116     try {
117         $credit_type->archived(0)->store();
118         push @messages, { code => 'success_on_restore', type => 'message' };
119     }
120     catch {
121         push @messages, { code => 'error_on_restore', type => 'alert' };
122     };
123     $op = 'list';
124 }
125
126 if ( $op eq 'list' ) {
127     my $credit_types = Koha::Account::CreditTypes->search();
128     $template->param( credit_types => $credit_types, );
129 }
130
131 $template->param(
132     code     => $code,
133     messages => \@messages,
134     op       => $op,
135 );
136
137 output_html_with_http_headers $input, $cookie, $template->output;