4 #written 11/1/2000 by chris@katipo.oc.nz
5 #script to display borrowers account details
8 # Copyright 2000-2002 Katipo Communications
10 # This file is part of Koha.
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.
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.
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>.
26 use URI::Escape qw( uri_unescape );
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 );
34 use Koha::Cash::Registers;
36 use Koha::Patron::Categories;
43 my ($template, $loggedinuser, $cookie) = get_template_and_user(
45 template_name => "members/boraccount.tt",
48 flagsrequired => { borrowers => 'edit_borrowers',
49 updatecharges => 'remaining_permissions'},
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');
60 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
61 my $library_id = C4::Context->userenv->{'branch'};
62 my $patron = Koha::Patrons->find( $borrowernumber );
64 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
68 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
70 my $registerid = $input->param('registerid');
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 );
78 branch => $library_id,
79 staff_id => $logged_in_user->id,
80 interface => 'intranet',
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 "" ) {
94 $patron->account->payout_amount(
96 payout_type => $payout_type,
97 branch => $library_id,
98 staff_id => $logged_in_user->id,
99 cash_register => $registerid,
100 interface => 'intranet',
107 my $payment = Koha::Account::Lines->find($payment_id);
110 my $payout = $payment->payout(
112 payout_type => $payout_type,
113 branch => $library_id,
114 staff_id => $logged_in_user->id,
115 cash_register => $registerid,
116 interface => 'intranet',
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');
134 my $refund = $charge->reduce(
136 reduction_type => 'REFUND',
137 branch => $library_id,
138 staff_id => $logged_in_user->id,
139 interface => 'intranet',
143 unless ( $refund_type eq 'AC' ) {
144 my $payout = $refund->payout(
146 payout_type => $refund_type,
147 branch => $library_id,
148 staff_id => $logged_in_user->id,
149 cash_register => $registerid,
150 interface => 'intranet',
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');
167 my $discount = $charge->reduce(
169 reduction_type => 'DISCOUNT',
170 branch => $library_id,
171 staff_id => $logged_in_user->id,
172 interface => 'intranet',
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);
185 $credit->credit_offsets( { type => 'APPLY' } )->as_list;
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,
193 borrowers => $patron->borrowernumber,
194 branches => C4::Context::mybranch,
198 offsets => \@credit_offsets,
203 my $message_id = C4::Letters::EnqueueLetter(
206 borrowernumber => $patron->borrowernumber,
207 message_transport_type => 'email',
210 C4::Letters::SendQueuedMessages( { message_id => $message_id } ) if $message_id;
211 $receipt_sent = $message_id ? 1 : -1;
219 my $total = $patron->account->balance;
221 my $accountlines = Koha::Account::Lines->search(
222 { borrowernumber => $patron->borrowernumber },
223 { order_by => { -desc => 'accountlines_id' } }
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);
238 $info = uri_unescape($info);
240 push @{$renew_results_display}, {
247 my $csrf_token = Koha::Token->new->generate_csrf({
248 session_id => scalar $input->cookie('CGISESSID'),
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,
264 output_html_with_http_headers $input, $cookie, $template->output;