Bug 31254: Add additional fields for accountlines
[koha.git] / members / mancredit.pl
1 #!/usr/bin/perl
2
3 #written 11/1/2000 by chris@katipo.oc.nz
4 #script to display borrowers account details
5
6 # Copyright 2000-2002 Katipo Communications
7 # Copyright 2010 BibLibre
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23
24 use Modern::Perl;
25
26 use C4::Auth qw( get_template_and_user );
27 use C4::Output qw( output_and_exit_if_error output_and_exit output_html_with_http_headers );
28 use CGI qw ( -utf8 );
29
30 use C4::Members;
31 use C4::Accounts;
32
33 use Koha::Items;
34 use Koha::Patrons;
35 use Koha::Patron::Categories;
36 use Koha::Account::CreditTypes;
37 use Koha::AdditionalFields;
38
39 use Koha::Token;
40
41 my $input = CGI->new;
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43     {
44         template_name   => "members/mancredit.tt",
45         query           => $input,
46         type            => "intranet",
47         flagsrequired   => {
48             borrowers     => 'edit_borrowers',
49             updatecharges => 'manual_credit'
50         }
51     }
52 );
53
54 my $logged_in_user = Koha::Patrons->find($loggedinuser);
55 my $borrowernumber = $input->param('borrowernumber');
56 my $patron         = Koha::Patrons->find($borrowernumber);
57
58 output_and_exit_if_error(
59     $input, $cookie,
60     $template,
61     {
62         module         => 'members',
63         logged_in_user => $logged_in_user,
64         current_patron => $patron
65     }
66 );
67
68 my $library_id =
69   C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
70
71 my $add = $input->param('add');
72 if ($add) {
73     output_and_exit( $input, $cookie, $template, 'wrong_csrf_token' )
74       unless Koha::Token->new->check_csrf(
75         {
76             session_id => scalar $input->cookie('CGISESSID'),
77             token      => scalar $input->param('csrf_token'),
78         }
79       );
80
81 # Note: If the logged in user is not allowed to see this patron an invoice can be forced
82 # Here we are trusting librarians not to hack the system
83     my $barcode = $input->param('barcode');
84     my $item_id;
85     if ($barcode) {
86         my $item = Koha::Items->find( { barcode => $barcode } );
87         $item_id = $item->itemnumber if $item;
88     }
89     my $description = $input->param('desc');
90     my $note        = $input->param('note');
91     my $amount      = $input->param('amount') || 0;
92     my $type        = $input->param('type');
93
94     my $line = $patron->account->add_credit(
95         {
96             amount      => $amount,
97             description => $description,
98             item_id     => $item_id,
99             library_id  => $library_id,
100             note        => $note,
101             type        => $type,
102             user_id     => $logged_in_user->id,
103             interface   => C4::Context->interface
104         }
105     );
106
107     my @additional_fields;
108     my $accountline_fields = Koha::AdditionalFields->search({ tablename => 'accountlines:credit' });
109     while ( my $field = $accountline_fields->next ) {
110         my $value = $input->param('additional_field_' . $field->id);
111         if (defined $value) {
112             push @additional_fields, {
113                 id => $field->id,
114                 value => $value,
115             };
116         }
117     }
118     if (@additional_fields) {
119         $line->set_additional_fields(\@additional_fields);
120     }
121
122     if ( C4::Context->preference('AccountAutoReconcile') ) {
123         $patron->account->reconcile_balance;
124     }
125
126     print $input->redirect(
127         "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
128     exit;
129 }
130 else {
131
132     my @credit_types = Koha::Account::CreditTypes->search_with_library_limits(
133         { can_be_added_manually => 1, archived => 0 },
134         {}, $library_id )->as_list;
135
136     $template->param(
137         patron       => $patron,
138         credit_types => \@credit_types,
139         finesview    => 1,
140         csrf_token   => Koha::Token->new->generate_csrf(
141             { session_id => scalar $input->cookie('CGISESSID') }
142         ),
143         available_additional_fields => [ Koha::AdditionalFields->search({ tablename => 'accountlines:credit' })->as_list ],
144     );
145     output_html_with_http_headers $input, $cookie, $template->output;
146 }