3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 BibLibre
5 # Copyright 2010,2011 PTFS-Europe Ltd
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 written 11/1/2000 by chris@katipo.oc.nz
25 part of the koha library system, script to facilitate paying off fines
31 use URI::Escape qw( uri_escape_utf8 uri_unescape );
33 use C4::Auth qw( get_template_and_user );
34 use C4::Output qw( output_and_exit_if_error output_and_exit output_html_with_http_headers );
44 use Koha::Patron::Categories;
45 use URI::Escape qw( uri_escape_utf8 uri_unescape );
47 our $input = CGI->new;
49 my $updatecharges_permissions = $input->param('woall') ? 'writeoff' : 'remaining_permissions';
50 our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
51 { template_name => 'members/pay.tt',
54 flagsrequired => { borrowers => 'edit_borrowers', updatecharges => $updatecharges_permissions },
58 my @names = $input->param;
60 our $borrowernumber = $input->param('borrowernumber');
61 if ( !$borrowernumber ) {
62 $borrowernumber = $input->param('borrowernumber0');
65 my $payment_id = $input->param('payment_id');
66 our $change_given = $input->param('change_given');
67 our @renew_results = $input->multi_param('renew_result');
69 # get borrower details
70 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
71 our $patron = Koha::Patrons->find($borrowernumber);
72 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
74 our $user = $input->remote_user;
77 our $branch = C4::Context->userenv->{'branch'};
79 if ( $input->param('paycollect') ) {
80 output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
81 print $input->redirect(
82 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber&change_given=$change_given");
84 elsif ( $input->param('payselected') ) {
85 output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
86 payselected({ params => \@names });
88 elsif ( $input->param('writeoff_selected') ) {
89 output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
90 payselected({ params => \@names, type => 'WRITEOFF' });
92 elsif ( $input->param('woall') ) {
93 output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
96 elsif ( $input->param('apply_credits') ) {
97 output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
98 apply_credits({ patron => $patron, cgi => $input });
100 elsif ( $input->param('confirm_writeoff') ) {
101 output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
102 my $item_id = $input->param('itemnumber');
103 my $accountlines_id = $input->param('accountlines_id');
104 my $amount = $input->param('amountwrittenoff');
105 my $payment_note = $input->param("payment_note");
107 my $accountline = Koha::Account::Lines->find( $accountlines_id );
109 $amount = $accountline->amountoutstanding if (abs($amount - $accountline->amountoutstanding) < 0.01) && C4::Context->preference('RoundFinesAtPayment');
110 if ( $amount > $accountline->amountoutstanding ) {
111 print $input->redirect( "/cgi-bin/koha/members/paycollect.pl?"
112 . "borrowernumber=$borrowernumber"
113 . "&amount=" . $accountline->amount
114 . "&amountoutstanding=" . $accountline->amountoutstanding
115 . "&debit_type_code=" . $accountline->debit_type_code
116 . "&accountlines_id=" . $accountlines_id
117 . "&change_given=" . $change_given
118 . "&writeoff_individual=1"
122 $payment_id = Koha::Account->new( { patron_id => $borrowernumber } )->pay(
125 lines => [ Koha::Account::Lines->find($accountlines_id) ],
127 note => $payment_note,
128 interface => C4::Context->interface,
130 library_id => $branch,
137 if (/^pay_indiv_(\d+)$/) {
138 output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
140 redirect_to_paycollect( 'pay_individual', $line_no );
141 } elsif (/^wo_indiv_(\d+)$/) {
142 output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
144 redirect_to_paycollect( 'writeoff_individual', $line_no );
148 # Populate an arrayref with everything we need to display any
149 # renew results that occurred based on what we were passed
150 my $renew_results_display = [];
151 foreach my $renew_result(@renew_results) {
152 my ($itemnumber, $success, $info) = split(/,/, $renew_result);
153 my $item = Koha::Items->find($itemnumber);
155 $info = uri_unescape($info);
157 push @{$renew_results_display}, {
166 payment_id => $payment_id,
167 change_given => $change_given,
168 renew_results => $renew_results_display
171 add_accounts_to_template();
173 output_html_with_http_headers $input, $cookie, $template->output;
175 sub add_accounts_to_template {
177 my $patron = Koha::Patrons->find( $borrowernumber );
178 my $account = $patron->account;
179 my $outstanding_credits = $account->outstanding_credits;
180 my $account_lines = $account->outstanding_debits;
181 my $total = $account_lines->total_outstanding;
183 while ( my $account_line = $account_lines->next ) {
184 push @accounts, $account_line;
189 accounts => \@accounts,
191 outstanding_credits => $outstanding_credits
198 sub get_for_redirect {
199 my ( $name, $name_in, $money ) = @_;
200 my $s = q{&} . $name . q{=};
202 if (defined $input->param($name_in)) {
203 $value = uri_escape_utf8( scalar $input->param($name_in) );
205 if ( !defined $value ) {
206 $value = ( $money == 1 ) ? 0 : q{};
209 $s .= sprintf '%.2f', $value;
216 sub redirect_to_paycollect {
217 my ( $action, $line_no ) = @_;
219 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
221 $redirect .= "$action=1";
222 $redirect .= get_for_redirect( 'debit_type_code', "debit_type_code$line_no", 0 );
223 $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
225 get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
226 $redirect .= get_for_redirect( 'description', "description$line_no", 0 );
227 $redirect .= get_for_redirect( 'title', "title$line_no", 0 );
228 $redirect .= get_for_redirect( 'itemnumber', "itemnumber$line_no", 0 );
229 $redirect .= get_for_redirect( 'accountlines_id', "accountlines_id$line_no", 0 );
230 $redirect .= q{&} . 'payment_note' . q{=} . uri_escape_utf8( scalar $input->param("payment_note_$line_no") );
231 $redirect .= '&remote_user=';
232 $redirect .= "change_given=$change_given";
234 return print $input->redirect($redirect);
239 my @wo_lines = grep { /^accountlines_id\d+$/ } @params;
241 my $borrowernumber = $input->param('borrowernumber');
246 my $amount = $input->param("amountoutstanding$value");
247 my $accountlines_id = $input->param("accountlines_id$value");
248 my $payment_note = $input->param("payment_note_$value");
249 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
252 lines => [ Koha::Account::Lines->find($accountlines_id) ],
254 note => $payment_note,
255 interface => C4::Context->interface,
256 library_id => $branch,
262 print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
267 my $parameters = shift;
269 my @params = @{ $parameters->{params} };
270 my $type = $parameters->{type} || 'PAYMENT';
275 if (/^incl_par_(\d+)$/) {
277 push @lines_to_pay, scalar $input->param("accountlines_id$index");
278 $amt += $input->param("amountoutstanding$index");
281 $amt = '&amt=' . $amt;
282 my $sel = '&selected=' . join ',', @lines_to_pay;
284 '¬es=' . uri_escape_utf8( join( "\n", map { scalar $input->param("payment_note_$_") } @lines_to_pay ) );
286 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
292 print $input->redirect($redirect);
299 my $patron = $args->{patron};
300 my $cgi = $args->{cgi};
302 $patron->account->reconcile_balance();
304 print $cgi->redirect("/cgi-bin/koha/members/pay.pl?borrowernumber=" . $patron->borrowernumber );