Bug 35438: Transact each record import separately
[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     output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
74     my $payment_id = scalar $input->param('accountlines_id');
75     my $payment    = Koha::Account::Lines->find( $payment_id );
76     $payment->void(
77         {
78             branch    => $library_id,
79             staff_id  => $logged_in_user->id,
80             interface => 'intranet',
81         }
82     );
83 }
84
85 if ( $action eq 'payout' ) {
86     output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
87     my $payment_id  = scalar $input->param('accountlines_id');
88     my $payment     = Koha::Account::Lines->find($payment_id);
89     my $amount      = scalar $input->param('amount');
90     my $payout_type = scalar $input->param('payout_type');
91     if ( $payment_id eq "" ) {
92         $schema->txn_do(
93             sub {
94                 $patron->account->payout_amount(
95                      {
96                         payout_type   => $payout_type,
97                         branch        => $library_id,
98                         staff_id      => $logged_in_user->id,
99                         cash_register => $registerid,
100                         interface     => 'intranet',
101                         amount        => $amount
102                     }
103                 );
104             }
105         );
106     } else {
107         my $payment = Koha::Account::Lines->find($payment_id);
108         $schema->txn_do(
109             sub {
110                 my $payout = $payment->payout(
111                     {
112                         payout_type   => $payout_type,
113                         branch        => $library_id,
114                         staff_id      => $logged_in_user->id,
115                         cash_register => $registerid,
116                         interface     => 'intranet',
117                         amount        => $amount
118                     }
119                 );
120             }
121         );
122     }
123 }
124
125 if ( $action eq 'refund' ) {
126     output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
127     my $charge_id   = scalar $input->param('accountlines_id');
128     my $charge      = Koha::Account::Lines->find($charge_id);
129     my $amount      = scalar $input->param('amount');
130     my $refund_type = scalar $input->param('refund_type');
131     $schema->txn_do(
132         sub {
133
134             my $refund = $charge->reduce(
135                 {
136                     reduction_type => 'REFUND',
137                     branch         => $library_id,
138                     staff_id       => $logged_in_user->id,
139                     interface      => 'intranet',
140                     amount         => $amount
141                 }
142             );
143             unless ( $refund_type eq 'AC' ) {
144                 my $payout = $refund->payout(
145                     {
146                         payout_type   => $refund_type,
147                         branch        => $library_id,
148                         staff_id      => $logged_in_user->id,
149                         cash_register => $registerid,
150                         interface     => 'intranet',
151                         amount        => $amount
152                     }
153                 );
154             }
155         }
156     );
157 }
158
159 if ( $action eq 'discount' ) {
160     output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
161     my $charge_id        = scalar $input->param('accountlines_id');
162     my $charge           = Koha::Account::Lines->find($charge_id);
163     my $amount           = scalar $input->param('amount');
164     $schema->txn_do(
165         sub {
166
167             my $discount = $charge->reduce(
168                 {
169                     reduction_type => 'DISCOUNT',
170                     branch         => $library_id,
171                     staff_id       => $logged_in_user->id,
172                     interface      => 'intranet',
173                     amount         => $amount
174                 }
175             );
176         }
177     );
178 }
179
180 my $receipt_sent = 0;
181 if ( $action eq 'send_receipt' ) {
182     my $credit_id = scalar $input->param('accountlines_id');
183     my $credit    = Koha::Account::Lines->find($credit_id);
184     my @credit_offsets =
185       $credit->credit_offsets( { type => 'APPLY' } )->as_list;
186     if (
187         my $letter = C4::Letters::GetPreparedLetter(
188             module      => 'circulation',
189             letter_code => uc( "ACCOUNT_" . $credit->credit_type_code ),
190             message_transport_type => 'email',
191             lang                   => $patron->lang,
192             tables                 => {
193                 borrowers => $patron->borrowernumber,
194                 branches  => C4::Context::mybranch,
195             },
196             substitute => {
197                 credit  => $credit,
198                 offsets => \@credit_offsets,
199             },
200         )
201       )
202     {
203         my $message_id = C4::Letters::EnqueueLetter(
204             {
205                 letter                 => $letter,
206                 borrowernumber         => $patron->borrowernumber,
207                 message_transport_type => 'email',
208             }
209         );
210         C4::Letters::SendQueuedMessages( { message_id => $message_id } ) if $message_id;
211         $receipt_sent = $message_id ? 1 : -1;
212     }
213     else {
214         $receipt_sent = -1;
215     }
216 }
217
218 #get account details
219 my $total = $patron->account->balance;
220
221 my $accountlines = Koha::Account::Lines->search(
222     { borrowernumber => $patron->borrowernumber },
223     { order_by       => { -desc => 'accountlines_id' } }
224 );
225
226 my $totalcredit;
227 if($total <= 0){
228         $totalcredit = 1;
229 }
230
231 # Populate an arrayref with everything we need to display any
232 # renew errors that occurred based on what we were passed
233 my $renew_results_display = [];
234 foreach my $renew_result(@renew_results) {
235     my ($itemnumber, $success, $info) = split(/,/, $renew_result);
236     my $item = Koha::Items->find($itemnumber);
237     if ($success) {
238         $info = uri_unescape($info);
239     }
240     push @{$renew_results_display}, {
241         item    => $item,
242         success => $success,
243         info    => $info
244     };
245 }
246
247 my $csrf_token = Koha::Token->new->generate_csrf({
248     session_id => scalar $input->cookie('CGISESSID'),
249 });
250
251 $template->param(
252     patron              => $patron,
253     finesview           => 1,
254     total               => sprintf("%.2f",$total),
255     totalcredit         => $totalcredit,
256     accounts            => $accountlines,
257     payment_id          => $payment_id,
258     change_given        => $change_given,
259     renew_results       => $renew_results_display,
260     receipt_sent        => $receipt_sent,
261     csrf_token          => $csrf_token,
262 );
263
264 output_html_with_http_headers $input, $cookie, $template->output;