Bug 26638: DBRev 20.06.00.057
[koha.git] / members / pay.pl
blobeb16f045748fb1c9e317829c9352738628fed0d4
1 #!/usr/bin/perl
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>.
22 =head1 pay.pl
24 written 11/1/2000 by chris@katipo.oc.nz
25 part of the koha library system, script to facilitate paying off fines
27 =cut
29 use Modern::Perl;
31 use URI::Escape;
32 use C4::Context;
33 use C4::Auth;
34 use C4::Output;
35 use CGI qw ( -utf8 );
36 use C4::Members;
37 use C4::Accounts;
38 use C4::Stats;
39 use C4::Koha;
40 use C4::Overdues;
41 use Koha::Patrons;
42 use Koha::Items;
44 use Koha::Patron::Categories;
45 use URI::Escape;
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',
52 query => $input,
53 type => 'intranet',
54 flagsrequired => { borrowers => 'edit_borrowers', updatecharges => $updatecharges_permissions },
55 debug => 1,
59 my @names = $input->param;
61 our $borrowernumber = $input->param('borrowernumber');
62 if ( !$borrowernumber ) {
63 $borrowernumber = $input->param('borrowernumber0');
66 my $payment_id = $input->param('payment_id');
67 our $change_given = $input->param('change_given');
68 our @renew_results = $input->multi_param('renew_result');
70 # get borrower details
71 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
72 our $patron = Koha::Patrons->find($borrowernumber);
73 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
75 our $user = $input->remote_user;
76 $user ||= q{};
78 our $branch = C4::Context->userenv->{'branch'};
80 if ( $input->param('paycollect') ) {
81 print $input->redirect(
82 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber&change_given=$change_given");
84 elsif ( $input->param('payselected') ) {
85 payselected({ params => \@names });
87 elsif ( $input->param('writeoff_selected') ) {
88 payselected({ params => \@names, type => 'WRITEOFF' });
90 elsif ( $input->param('woall') ) {
91 writeoff_all(@names);
93 elsif ( $input->param('apply_credits') ) {
94 apply_credits({ patron => $patron, cgi => $input });
96 elsif ( $input->param('confirm_writeoff') ) {
97 my $accountlines_id = $input->param('accountlines_id');
98 my $amount = $input->param('amountwrittenoff');
99 my $payment_note = $input->param("payment_note");
101 my $accountline = Koha::Account::Lines->find( $accountlines_id );
103 $amount = $accountline->amountoutstanding if (abs($amount - $accountline->amountoutstanding) < 0.01) && C4::Context->preference('RoundFinesAtPayment');
104 if ( $amount > $accountline->amountoutstanding ) {
105 print $input->redirect( "/cgi-bin/koha/members/paycollect.pl?"
106 . "borrowernumber=$borrowernumber"
107 . "&amount=" . $accountline->amount
108 . "&amountoutstanding=" . $accountline->amountoutstanding
109 . "&debit_type_code=" . $accountline->debit_type_code
110 . "&accountlines_id=" . $accountlines_id
111 . "&change_given=" . $change_given
112 . "&writeoff_individual=1"
113 . "&error_over=1" );
115 } else {
116 $payment_id = Koha::Account->new( { patron_id => $borrowernumber } )->pay(
118 amount => $amount,
119 lines => [ Koha::Account::Lines->find($accountlines_id) ],
120 type => 'WRITEOFF',
121 note => $payment_note,
122 interface => C4::Context->interface,
123 library_id => $branch,
129 for (@names) {
130 if (/^pay_indiv_(\d+)$/) {
131 my $line_no = $1;
132 redirect_to_paycollect( 'pay_individual', $line_no );
133 } elsif (/^wo_indiv_(\d+)$/) {
134 my $line_no = $1;
135 redirect_to_paycollect( 'writeoff_individual', $line_no );
139 # Populate an arrayref with everything we need to display any
140 # renew results that occurred based on what we were passed
141 my $renew_results_display = [];
142 foreach my $renew_result(@renew_results) {
143 my ($itemnumber, $success, $info) = split(/,/, $renew_result);
144 my $item = Koha::Items->find($itemnumber);
145 if ($success) {
146 $info = uri_unescape($info);
148 push @{$renew_results_display}, {
149 item => $item,
150 success => $success,
151 info => $info
155 $template->param(
156 finesview => 1,
157 payment_id => $payment_id,
158 change_given => $change_given,
159 renew_results => $renew_results_display
162 add_accounts_to_template();
164 output_html_with_http_headers $input, $cookie, $template->output;
166 sub add_accounts_to_template {
168 my $patron = Koha::Patrons->find( $borrowernumber );
169 my $account = $patron->account;
170 my $outstanding_credits = $account->outstanding_credits;
171 my $account_lines = $account->outstanding_debits;
172 my $total = $account_lines->total_outstanding;
173 my @accounts;
174 while ( my $account_line = $account_lines->next ) {
175 push @accounts, $account_line;
178 $template->param(
179 patron => $patron,
180 accounts => \@accounts,
181 total => $total,
182 outstanding_credits => $outstanding_credits
185 return;
189 sub get_for_redirect {
190 my ( $name, $name_in, $money ) = @_;
191 my $s = q{&} . $name . q{=};
192 my $value;
193 if (defined $input->param($name_in)) {
194 $value = uri_escape_utf8( scalar $input->param($name_in) );
196 if ( !defined $value ) {
197 $value = ( $money == 1 ) ? 0 : q{};
199 if ($money) {
200 $s .= sprintf '%.2f', $value;
201 } else {
202 $s .= $value;
204 return $s;
207 sub redirect_to_paycollect {
208 my ( $action, $line_no ) = @_;
209 my $redirect =
210 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
211 $redirect .= q{&};
212 $redirect .= "$action=1";
213 $redirect .= get_for_redirect( 'debit_type_code', "debit_type_code$line_no", 0 );
214 $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
215 $redirect .=
216 get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
217 $redirect .= get_for_redirect( 'description', "description$line_no", 0 );
218 $redirect .= get_for_redirect( 'title', "title$line_no", 0 );
219 $redirect .= get_for_redirect( 'itemnumber', "itemnumber$line_no", 0 );
220 $redirect .= get_for_redirect( 'accountlines_id', "accountlines_id$line_no", 0 );
221 $redirect .= q{&} . 'payment_note' . q{=} . uri_escape_utf8( scalar $input->param("payment_note_$line_no") );
222 $redirect .= '&remote_user=';
223 $redirect .= "change_given=$change_given";
224 $redirect .= $user;
225 return print $input->redirect($redirect);
228 sub writeoff_all {
229 my @params = @_;
230 my @wo_lines = grep { /^accountlines_id\d+$/ } @params;
232 my $borrowernumber = $input->param('borrowernumber');
234 for (@wo_lines) {
235 if (/(\d+)/) {
236 my $value = $1;
237 my $amount = $input->param("amountoutstanding$value");
238 my $accountlines_id = $input->param("accountlines_id$value");
239 my $payment_note = $input->param("payment_note_$value");
240 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
242 amount => $amount,
243 lines => [ Koha::Account::Lines->find($accountlines_id) ],
244 type => 'WRITEOFF',
245 note => $payment_note,
246 interface => C4::Context->interface,
247 library_id => $branch,
253 print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
254 return;
257 sub payselected {
258 my $parameters = shift;
260 my @params = @{ $parameters->{params} };
261 my $type = $parameters->{type} || 'PAYMENT';
263 my $amt = 0;
264 my @lines_to_pay;
265 foreach (@params) {
266 if (/^incl_par_(\d+)$/) {
267 my $index = $1;
268 push @lines_to_pay, scalar $input->param("accountlines_id$index");
269 $amt += $input->param("amountoutstanding$index");
272 $amt = '&amt=' . $amt;
273 my $sel = '&selected=' . join ',', @lines_to_pay;
274 my $notes = '&notes=' . join("%0A", map { scalar $input->param("payment_note_$_") } @lines_to_pay );
275 my $redirect =
276 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
277 . "&type=$type"
278 . $amt
279 . $sel
280 . $notes;
282 print $input->redirect($redirect);
283 return;
286 sub apply_credits {
287 my ($args) = @_;
289 my $patron = $args->{patron};
290 my $cgi = $args->{cgi};
292 $patron->account->reconcile_balance();
294 print $cgi->redirect("/cgi-bin/koha/members/pay.pl?borrowernumber=" . $patron->borrowernumber );
295 return;