Bug 24157: Add new method Acq::Invoice::Adjustement->fund
[koha.git] / members / pay.pl
blobd78debc7ee9c5fbb0f608f00f5fc0350794cdd20
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 authnotrequired => 0,
55 flagsrequired => { borrowers => 'edit_borrowers', updatecharges => $updatecharges_permissions },
56 debug => 1,
60 my @names = $input->param;
62 our $borrowernumber = $input->param('borrowernumber');
63 if ( !$borrowernumber ) {
64 $borrowernumber = $input->param('borrowernumber0');
67 my $payment_id = $input->param('payment_id');
68 our $change_given = $input->param('change_given');
69 our @renew_results = $input->multi_param('renew_result');
71 # get borrower details
72 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
73 our $patron = Koha::Patrons->find($borrowernumber);
74 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
76 our $user = $input->remote_user;
77 $user ||= q{};
79 our $branch = C4::Context->userenv->{'branch'};
81 if ( $input->param('paycollect') ) {
82 print $input->redirect(
83 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber&change_given=$change_given");
85 elsif ( $input->param('payselected') ) {
86 payselected({ params => \@names });
88 elsif ( $input->param('writeoff_selected') ) {
89 payselected({ params => \@names, type => 'WRITEOFF' });
91 elsif ( $input->param('woall') ) {
92 writeoff_all(@names);
94 elsif ( $input->param('apply_credits') ) {
95 apply_credits({ patron => $patron, cgi => $input });
97 elsif ( $input->param('confirm_writeoff') ) {
98 my $accountlines_id = $input->param('accountlines_id');
99 my $amount = $input->param('amountwrittenoff');
100 my $payment_note = $input->param("payment_note");
102 my $accountline = Koha::Account::Lines->find( $accountlines_id );
104 $amount = $accountline->amountoutstanding if (abs($amount - $accountline->amountoutstanding) < 0.01) && C4::Context->preference('RoundFinesAtPayment');
105 if ( $amount > $accountline->amountoutstanding ) {
106 print $input->redirect( "/cgi-bin/koha/members/paycollect.pl?"
107 . "borrowernumber=$borrowernumber"
108 . "&amount=" . $accountline->amount
109 . "&amountoutstanding=" . $accountline->amountoutstanding
110 . "&debit_type_code=" . $accountline->debit_type_code
111 . "&accountlines_id=" . $accountlines_id
112 . "&change_given=" . $change_given
113 . "&writeoff_individual=1"
114 . "&error_over=1" );
116 } else {
117 $payment_id = Koha::Account->new( { patron_id => $borrowernumber } )->pay(
119 amount => $amount,
120 lines => [ Koha::Account::Lines->find($accountlines_id) ],
121 type => 'WRITEOFF',
122 note => $payment_note,
123 interface => C4::Context->interface,
124 library_id => $branch,
130 for (@names) {
131 if (/^pay_indiv_(\d+)$/) {
132 my $line_no = $1;
133 redirect_to_paycollect( 'pay_individual', $line_no );
134 } elsif (/^wo_indiv_(\d+)$/) {
135 my $line_no = $1;
136 redirect_to_paycollect( 'writeoff_individual', $line_no );
140 # Populate an arrayref with everything we need to display any
141 # renew results that occurred based on what we were passed
142 my $renew_results_display = [];
143 foreach my $renew_result(@renew_results) {
144 my ($itemnumber, $success, $info) = split(/,/, $renew_result);
145 my $item = Koha::Items->find($itemnumber);
146 if ($success) {
147 $info = uri_unescape($info);
149 push @{$renew_results_display}, {
150 item => $item,
151 success => $success,
152 info => $info
156 $template->param(
157 finesview => 1,
158 payment_id => $payment_id,
159 change_given => $change_given,
160 renew_results => $renew_results_display
163 add_accounts_to_template();
165 output_html_with_http_headers $input, $cookie, $template->output;
167 sub add_accounts_to_template {
169 my $patron = Koha::Patrons->find( $borrowernumber );
170 my $account = $patron->account;
171 my $outstanding_credits = $account->outstanding_credits;
172 my $account_lines = $account->outstanding_debits;
173 my $total = $account_lines->total_outstanding;
174 my @accounts;
175 while ( my $account_line = $account_lines->next ) {
176 push @accounts, $account_line;
179 $template->param(
180 patron => $patron,
181 accounts => \@accounts,
182 total => $total,
183 outstanding_credits => $outstanding_credits
186 return;
190 sub get_for_redirect {
191 my ( $name, $name_in, $money ) = @_;
192 my $s = q{&} . $name . q{=};
193 my $value;
194 if (defined $input->param($name_in)) {
195 $value = uri_escape_utf8( scalar $input->param($name_in) );
197 if ( !defined $value ) {
198 $value = ( $money == 1 ) ? 0 : q{};
200 if ($money) {
201 $s .= sprintf '%.2f', $value;
202 } else {
203 $s .= $value;
205 return $s;
208 sub redirect_to_paycollect {
209 my ( $action, $line_no ) = @_;
210 my $redirect =
211 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
212 $redirect .= q{&};
213 $redirect .= "$action=1";
214 $redirect .= get_for_redirect( 'debit_type_code', "debit_type_code$line_no", 0 );
215 $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
216 $redirect .=
217 get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
218 $redirect .= get_for_redirect( 'description', "description$line_no", 0 );
219 $redirect .= get_for_redirect( 'title', "title$line_no", 0 );
220 $redirect .= get_for_redirect( 'itemnumber', "itemnumber$line_no", 0 );
221 $redirect .= get_for_redirect( 'accountlines_id', "accountlines_id$line_no", 0 );
222 $redirect .= q{&} . 'payment_note' . q{=} . uri_escape_utf8( scalar $input->param("payment_note_$line_no") );
223 $redirect .= '&remote_user=';
224 $redirect .= "change_given=$change_given";
225 $redirect .= $user;
226 return print $input->redirect($redirect);
229 sub writeoff_all {
230 my @params = @_;
231 my @wo_lines = grep { /^accountlines_id\d+$/ } @params;
233 my $borrowernumber = $input->param('borrowernumber');
235 for (@wo_lines) {
236 if (/(\d+)/) {
237 my $value = $1;
238 my $amount = $input->param("amountoutstanding$value");
239 my $accountlines_id = $input->param("accountlines_id$value");
240 my $payment_note = $input->param("payment_note_$value");
241 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
243 amount => $amount,
244 lines => [ Koha::Account::Lines->find($accountlines_id) ],
245 type => 'WRITEOFF',
246 note => $payment_note,
247 interface => C4::Context->interface,
248 library_id => $branch,
254 print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
255 return;
258 sub payselected {
259 my $parameters = shift;
261 my @params = @{ $parameters->{params} };
262 my $type = $parameters->{type} || 'PAYMENT';
264 my $amt = 0;
265 my @lines_to_pay;
266 foreach (@params) {
267 if (/^incl_par_(\d+)$/) {
268 my $index = $1;
269 push @lines_to_pay, scalar $input->param("accountlines_id$index");
270 $amt += $input->param("amountoutstanding$index");
273 $amt = '&amt=' . $amt;
274 my $sel = '&selected=' . join ',', @lines_to_pay;
275 my $notes = '&notes=' . join("%0A", map { scalar $input->param("payment_note_$_") } @lines_to_pay );
276 my $redirect =
277 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
278 . "&type=$type"
279 . $amt
280 . $sel
281 . $notes;
283 print $input->redirect($redirect);
284 return;
287 sub apply_credits {
288 my ($args) = @_;
290 my $patron = $args->{patron};
291 my $cgi = $args->{cgi};
293 $patron->account->reconcile_balance();
295 print $cgi->redirect("/cgi-bin/koha/members/pay.pl?borrowernumber=" . $patron->borrowernumber );
296 return;