Merge remote-tracking branch 'koha-translate/21.05.04-translate-20210923' into HEAD
[koha.git] / members / boraccount.pl
1 #!/usr/bin/perl
2
3
4 #written 11/1/2000 by chris@katipo.oc.nz
5 #script to display borrowers account details
6
7
8 # Copyright 2000-2002 Katipo Communications
9 #
10 # This file is part of Koha.
11 #
12 # Koha is free software; you can redistribute it and/or modify it
13 # under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 3 of the License, or
15 # (at your option) any later version.
16 #
17 # Koha is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24
25 use Modern::Perl;
26 use URI::Escape;
27
28 use C4::Auth;
29 use C4::Output;
30 use CGI qw ( -utf8 );
31 use C4::Members;
32 use C4::Accounts;
33 use Koha::Cash::Registers;
34 use Koha::Patrons;
35 use Koha::Patron::Categories;
36 use Koha::Items;
37 use Koha::Token;
38
39 my $input=CGI->new;
40
41
42 my ($template, $loggedinuser, $cookie) = get_template_and_user(
43     {
44         template_name   => "members/boraccount.tt",
45         query           => $input,
46         type            => "intranet",
47         flagsrequired   => { borrowers     => 'edit_borrowers',
48                              updatecharges => 'remaining_permissions'},
49         debug           => 1,
50     }
51 );
52
53 my $schema         = Koha::Database->new->schema;
54 my $borrowernumber = $input->param('borrowernumber');
55 my $payment_id     = $input->param('payment_id');
56 my $change_given   = $input->param('change_given');
57 my $action         = $input->param('action') || '';
58 my @renew_results  = $input->param('renew_result');
59
60 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
61 my $library_id = C4::Context->userenv->{'branch'};
62 my $patron = Koha::Patrons->find( $borrowernumber );
63 unless ( $patron ) {
64     print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
65     exit;
66 }
67
68 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
69
70 my $registerid = $input->param('registerid');
71
72 if ( $action eq 'void' ) {
73     my $payment_id = scalar $input->param('accountlines_id');
74     my $payment    = Koha::Account::Lines->find( $payment_id );
75     $payment->void(
76         {
77             branch    => $library_id,
78             staff_id  => $logged_in_user->id,
79             interface => 'intranet',
80         }
81     );
82 }
83
84 if ( $action eq 'payout' ) {
85     my $payment_id       = scalar $input->param('accountlines_id');
86     my $amount           = scalar $input->param('amount');
87     my $payout_type = scalar $input->param('payout_type');
88     if ( $payment_id eq "" ) {
89         $schema->txn_do(
90             sub {
91                 $patron->account->payout_amount(
92                      {
93                         payout_type   => $payout_type,
94                         branch        => $library_id,
95                         staff_id      => $logged_in_user->id,
96                         cash_register => $registerid,
97                         interface     => 'intranet',
98                         amount        => $amount
99                     }
100                 );
101             }
102         );
103     } else {
104         my $payment = Koha::Account::Lines->find($payment_id);
105         $schema->txn_do(
106             sub {
107                 my $payout = $payment->payout(
108                     {
109                         payout_type   => $payout_type,
110                         branch        => $library_id,
111                         staff_id      => $logged_in_user->id,
112                         cash_register => $registerid,
113                         interface     => 'intranet',
114                         amount        => $amount
115                     }
116                 );
117             }
118         );
119     }
120 }
121
122 if ( $action eq 'refund' ) {
123     my $charge_id        = scalar $input->param('accountlines_id');
124     my $charge           = Koha::Account::Lines->find($charge_id);
125     my $amount           = scalar $input->param('amount');
126     my $refund_type = scalar $input->param('refund_type');
127     $schema->txn_do(
128         sub {
129
130             my $refund = $charge->reduce(
131                 {
132                     reduction_type => 'REFUND',
133                     branch         => $library_id,
134                     staff_id       => $logged_in_user->id,
135                     interface      => 'intranet',
136                     amount         => $amount
137                 }
138             );
139             unless ( $refund_type eq 'AC' ) {
140                 my $payout = $refund->payout(
141                     {
142                         payout_type   => $refund_type,
143                         branch        => $library_id,
144                         staff_id      => $logged_in_user->id,
145                         cash_register => $registerid,
146                         interface     => 'intranet',
147                         amount        => $amount
148                     }
149                 );
150             }
151         }
152     );
153 }
154
155 if ( $action eq 'discount' ) {
156     my $charge_id        = scalar $input->param('accountlines_id');
157     my $charge           = Koha::Account::Lines->find($charge_id);
158     my $amount           = scalar $input->param('amount');
159     $schema->txn_do(
160         sub {
161
162             my $discount = $charge->reduce(
163                 {
164                     reduction_type => 'DISCOUNT',
165                     branch         => $library_id,
166                     staff_id       => $logged_in_user->id,
167                     interface      => 'intranet',
168                     amount         => $amount
169                 }
170             );
171         }
172     );
173 }
174
175 #get account details
176 my $total = $patron->account->balance;
177
178 my @accountlines = Koha::Account::Lines->search(
179     { borrowernumber => $patron->borrowernumber },
180     { order_by       => { -desc => 'accountlines_id' } }
181 );
182
183 my $totalcredit;
184 if($total <= 0){
185         $totalcredit = 1;
186 }
187
188 # Populate an arrayref with everything we need to display any
189 # renew errors that occurred based on what we were passed
190 my $renew_results_display = [];
191 foreach my $renew_result(@renew_results) {
192     my ($itemnumber, $success, $info) = split(/,/, $renew_result);
193     my $item = Koha::Items->find($itemnumber);
194     if ($success) {
195         $info = uri_unescape($info);
196     }
197     push @{$renew_results_display}, {
198         item    => $item,
199         success => $success,
200         info    => $info
201     };
202 }
203
204 my $csrf_token = Koha::Token->new->generate_csrf({
205     session_id => scalar $input->cookie('CGISESSID'),
206 });
207
208 $template->param(
209     patron              => $patron,
210     finesview           => 1,
211     total               => sprintf("%.2f",$total),
212     totalcredit         => $totalcredit,
213     accounts            => \@accountlines,
214     payment_id          => $payment_id,
215     change_given        => $change_given,
216     renew_results       => $renew_results_display,
217     csrf_token          => $csrf_token,
218 );
219
220 output_html_with_http_headers $input, $cookie, $template->output;