Bug 31735: Optimize OPAC checkouts view
[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 qw( uri_unescape );
27
28 use C4::Auth qw( get_template_and_user );
29 use C4::Output qw( output_and_exit_if_error output_and_exit output_html_with_http_headers );
30 use CGI qw ( -utf8 );
31 use C4::Members;
32 use C4::Accounts;
33 use C4::Letters;
34 use Koha::Cash::Registers;
35 use Koha::Patrons;
36 use Koha::Patron::Categories;
37 use Koha::Items;
38 use Koha::Token;
39
40 my $input=CGI->new;
41
42
43 my ($template, $loggedinuser, $cookie) = get_template_and_user(
44     {
45         template_name   => "members/boraccount.tt",
46         query           => $input,
47         type            => "intranet",
48         flagsrequired   => { borrowers     => 'edit_borrowers',
49                              updatecharges => 'remaining_permissions'},
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->multi_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 my $receipt_sent = 0;
176 if ( $action eq 'send_receipt' ) {
177     my $credit_id = scalar $input->param('accountlines_id');
178     my $credit    = Koha::Account::Lines->find($credit_id);
179     my @credit_offsets =
180       $credit->credit_offsets( { type => 'APPLY' } )->as_list;
181     if (
182         my $letter = C4::Letters::GetPreparedLetter(
183             module      => 'circulation',
184             letter_code => uc( "ACCOUNT_" . $credit->credit_type_code ),
185             message_transport_type => 'email',
186             lang                   => $patron->lang,
187             tables                 => {
188                 borrowers => $patron->borrowernumber,
189                 branches  => C4::Context::mybranch,
190             },
191             substitute => {
192                 credit  => $credit,
193                 offsets => \@credit_offsets,
194             },
195         )
196       )
197     {
198         my $message_id = C4::Letters::EnqueueLetter(
199             {
200                 letter                 => $letter,
201                 borrowernumber         => $patron->borrowernumber,
202                 message_transport_type => 'email',
203             }
204         );
205         C4::Letters::SendQueuedMessages( { message_id => $message_id } );
206         $receipt_sent = 1;
207     }
208     else {
209         $receipt_sent = -1;
210     }
211 }
212
213 #get account details
214 my $total = $patron->account->balance;
215
216 my $accountlines = Koha::Account::Lines->search(
217     { borrowernumber => $patron->borrowernumber },
218     { order_by       => { -desc => 'accountlines_id' } }
219 );
220
221 my $totalcredit;
222 if($total <= 0){
223         $totalcredit = 1;
224 }
225
226 # Populate an arrayref with everything we need to display any
227 # renew errors that occurred based on what we were passed
228 my $renew_results_display = [];
229 foreach my $renew_result(@renew_results) {
230     my ($itemnumber, $success, $info) = split(/,/, $renew_result);
231     my $item = Koha::Items->find($itemnumber);
232     if ($success) {
233         $info = uri_unescape($info);
234     }
235     push @{$renew_results_display}, {
236         item    => $item,
237         success => $success,
238         info    => $info
239     };
240 }
241
242 my $csrf_token = Koha::Token->new->generate_csrf({
243     session_id => scalar $input->cookie('CGISESSID'),
244 });
245
246 $template->param(
247     patron              => $patron,
248     finesview           => 1,
249     total               => sprintf("%.2f",$total),
250     totalcredit         => $totalcredit,
251     accounts            => $accountlines,
252     payment_id          => $payment_id,
253     change_given        => $change_given,
254     renew_results       => $renew_results_display,
255     receipt_sent        => $receipt_sent,
256     csrf_token          => $csrf_token,
257 );
258
259 output_html_with_http_headers $input, $cookie, $template->output;