Bug 26133: Remove GetMarcHosts
[koha.git] / admin / debit_types.pl
blobfe09d0b83937803b3db1905735aa9f8be82d68a8
1 #! /usr/bin/perl
3 # Copyright 2019 Koha Development Team
5 # This file is part of Koha.
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.
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.
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>.
20 use Modern::Perl;
21 use CGI qw ( -utf8 );
22 use Try::Tiny;
24 use C4::Context;
25 use C4::Auth;
26 use C4::Output;
28 use Koha::Account::DebitType;
29 use Koha::Account::DebitTypes;
31 my $input = new CGI;
32 my $code = $input->param('code');
33 my $op = $input->param('op') || 'list';
34 my @messages;
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
38 template_name => "admin/debit_types.tt",
39 query => $input,
40 type => "intranet",
41 authnotrequired => 0,
42 flagsrequired => { parameters => 'parameters_remaining_permissions' },
43 debug => 1,
47 my $debit_type;
48 if ($code) {
49 $debit_type = Koha::Account::DebitTypes->find($code);
52 if ( $op eq 'add_form' ) {
54 my $selected_branches =
55 $debit_type ? $debit_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,
66 branchcode => $branch->{branchcode},
67 branchname => $branch->{branchname},
68 selected => $selected,
72 $template->param(
73 debit_type => $debit_type,
74 branches_loop => \@branches_loop
77 elsif ( $op eq 'add_validate' ) {
78 my $description = $input->param('description');
79 my $can_be_invoiced = $input->param('can_be_invoiced') || 0;
80 my $can_be_sold = $input->param('can_be_sold') || 0;
81 my $default_amount = $input->param('default_amount') || undef;
82 my @branches = grep { $_ ne q{} } $input->multi_param('branches');
84 if ( not defined $debit_type ) {
85 $debit_type = Koha::Account::DebitType->new( { code => $code } );
87 $debit_type->description($description);
88 $debit_type->can_be_invoiced($can_be_invoiced);
89 $debit_type->can_be_sold($can_be_sold);
90 $debit_type->default_amount($default_amount);
92 try {
93 $debit_type->store;
94 $debit_type->replace_library_limits( \@branches );
95 push @messages, { type => 'message', code => 'success_on_saving' };
97 catch {
98 push @messages, { type => 'error', code => 'error_on_saving' };
100 $op = 'list';
102 elsif ( $op eq 'archive' ) {
103 try {
104 $debit_type->archived(1)->store();
105 push @messages, { code => 'success_on_archive', type => 'message' };
107 catch {
108 push @messages, { code => 'error_on_archive', type => 'alert' };
111 $op = 'list';
113 elsif ( $op eq 'unarchive' ) {
114 try {
115 $debit_type->archived(0)->store();
116 push @messages, { code => 'success_on_restore', type => 'message' };
118 catch {
119 push @messages, { code => 'error_on_restore', type => 'alert' };
121 $op = 'list';
124 if ( $op eq 'list' ) {
125 my $debit_types = Koha::Account::DebitTypes->search();
126 $template->param( debit_types => $debit_types, );
129 $template->param(
130 code => $code,
131 messages => \@messages,
132 op => $op,
135 output_html_with_http_headers $input, $cookie, $template->output;