Bug 18805: Add ability to use up account credits
[koha.git] / members / pay.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 BibLibre
5 # Copyright 2010,2011 PTFS-Europe Ltd
6 #
7 # This file is part of Koha.
8 #
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.
13 #
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.
18 #
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>.
21
22 =head1 pay.pl
23
24  written 11/1/2000 by chris@katipo.oc.nz
25  part of the koha library system, script to facilitate paying off fines
26
27 =cut
28
29 use Modern::Perl;
30
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 C4::Members::Attributes qw(GetBorrowerAttributes);
42 use Koha::Patrons;
43
44 use Koha::Patron::Categories;
45 use URI::Escape;
46
47 our $input = CGI->new;
48
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,
57     }
58 );
59
60 my @names = $input->param;
61
62 our $borrowernumber = $input->param('borrowernumber');
63 if ( !$borrowernumber ) {
64     $borrowernumber = $input->param('borrowernumber0');
65 }
66
67 # get borrower details
68 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
69 our $patron         = Koha::Patrons->find($borrowernumber);
70 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
71
72 our $user = $input->remote_user;
73 $user ||= q{};
74
75 our $branch = C4::Context->userenv->{'branch'};
76
77 if ( $input->param('paycollect') ) {
78     print $input->redirect(
79         "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber");
80 }
81 elsif ( $input->param('payselected') ) {
82     payselected({ params => \@names });
83 }
84 elsif ( $input->param('writeoff_selected') ) {
85     payselected({ params => \@names, type => 'writeoff' });
86 }
87 elsif ( $input->param('woall') ) {
88     writeoff_all(@names);
89 }
90 elsif ( $input->param('apply_credits') ) {
91     apply_credits({ patron => $patron, cgi => $input });
92 }
93 elsif ( $input->param('confirm_writeoff') ) {
94     my $accountlines_id = $input->param('accountlines_id');
95     my $amount          = $input->param('amountwrittenoff');
96     my $payment_note    = $input->param("payment_note");
97
98     my $accountline = Koha::Account::Lines->find( $accountlines_id );
99
100     if ( $amount > $accountline->amountoutstanding ) {
101         print $input->redirect( "/cgi-bin/koha/members/paycollect.pl?"
102               . "borrowernumber=$borrowernumber"
103               . "&amount=" . $accountline->amount
104               . "&amountoutstanding=" . $accountline->amountoutstanding
105               . "&accounttype=" . $accountline->accounttype
106               . "&accountlines_id=" . $accountlines_id
107               . "&writeoff_individual=1"
108               . "&error_over=1" );
109
110     } else {
111         Koha::Account->new( { patron_id => $borrowernumber } )->pay(
112             {
113                 amount     => $amount,
114                 lines      => [ scalar Koha::Account::Lines->find($accountlines_id) ],
115                 type       => 'writeoff',
116                 note       => $payment_note,
117                 library_id => $branch,
118             }
119         );
120     }
121 }
122
123 for (@names) {
124     if (/^pay_indiv_(\d+)$/) {
125         my $line_no = $1;
126         redirect_to_paycollect( 'pay_individual', $line_no );
127     } elsif (/^wo_indiv_(\d+)$/) {
128         my $line_no = $1;
129         redirect_to_paycollect( 'writeoff_individual', $line_no );
130     }
131 }
132
133 $template->param(
134     finesview => 1,
135 );
136
137 add_accounts_to_template();
138
139 output_html_with_http_headers $input, $cookie, $template->output;
140
141 sub add_accounts_to_template {
142
143     my $patron = Koha::Patrons->find( $borrowernumber );
144     my $account = $patron->account;
145     my $outstanding_credits = $account->outstanding_credits;
146     my $account_lines = $account->outstanding_debits;
147     my $total = $account_lines->total_outstanding;
148     my @accounts;
149     while ( my $account_line = $account_lines->next ) {
150         $account_line = $account_line->unblessed;
151         if ( $account_line->{itemnumber} ) {
152             my $item = Koha::Items->find( $account_line->{itemnumber} );
153             my $biblio = $item->biblio;
154             $account_line->{biblionumber} = $biblio->biblionumber;
155             $account_line->{title}        = $biblio->title;
156         }
157         push @accounts, $account_line;
158     }
159     borrower_add_additional_fields($patron);
160
161     $template->param(
162         patron   => $patron,
163         accounts => \@accounts,
164         total    => $total,
165         outstanding_credits => $outstanding_credits
166     );
167
168     return;
169
170 }
171
172 sub get_for_redirect {
173     my ( $name, $name_in, $money ) = @_;
174     my $s     = q{&} . $name . q{=};
175     my $value;
176     if (defined $input->param($name_in)) {
177         $value = uri_escape_utf8( scalar $input->param($name_in) );
178     }
179     if ( !defined $value ) {
180         $value = ( $money == 1 ) ? 0 : q{};
181     }
182     if ($money) {
183         $s .= sprintf '%.2f', $value;
184     } else {
185         $s .= $value;
186     }
187     return $s;
188 }
189
190 sub redirect_to_paycollect {
191     my ( $action, $line_no ) = @_;
192     my $redirect =
193       "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
194     $redirect .= q{&};
195     $redirect .= "$action=1";
196     $redirect .= get_for_redirect( 'accounttype', "accounttype$line_no", 0 );
197     $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
198     $redirect .=
199       get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
200     $redirect .= get_for_redirect( 'description', "description$line_no", 0 );
201     $redirect .= get_for_redirect( 'title', "title$line_no", 0 );
202     $redirect .= get_for_redirect( 'itemnumber',   "itemnumber$line_no",   0 );
203     $redirect .= get_for_redirect( 'accountlines_id', "accountlines_id$line_no", 0 );
204     $redirect .= q{&} . 'payment_note' . q{=} . uri_escape_utf8( scalar $input->param("payment_note_$line_no") );
205     $redirect .= '&remote_user=';
206     $redirect .= $user;
207     return print $input->redirect($redirect);
208 }
209
210 sub writeoff_all {
211     my @params = @_;
212     my @wo_lines = grep { /^accountlines_id\d+$/ } @params;
213
214     my $borrowernumber = $input->param('borrowernumber');
215
216     for (@wo_lines) {
217         if (/(\d+)/) {
218             my $value           = $1;
219             my $amount          = $input->param("amountoutstanding$value");
220             my $accountlines_id = $input->param("accountlines_id$value");
221             my $payment_note    = $input->param("payment_note_$value");
222             Koha::Account->new( { patron_id => $borrowernumber } )->pay(
223                 {
224                     amount => $amount,
225                     lines  => [ scalar Koha::Account::Lines->find($accountlines_id) ],
226                     type   => 'writeoff',
227                     note   => $payment_note,
228                     library_id => $branch,
229                 }
230             );
231         }
232     }
233
234     print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
235     return;
236 }
237
238 sub borrower_add_additional_fields {
239     my $patron = shift;
240
241 # some borrower info is not returned in the standard call despite being assumed
242 # in a number of templates. It should not be the business of this script but in lieu of
243 # a revised api here it is ...
244     if (C4::Context->preference('ExtendedPatronAttributes')) {
245         my $extendedattributes = GetBorrowerAttributes($patron->borrowernumber);
246         $template->param(
247             extendedattributes       => $extendedattributes,
248             ExtendedPatronAttributes => 1,
249         );
250     }
251
252     return;
253 }
254
255 sub payselected {
256     my $parameters = shift;
257
258     my @params = @{ $parameters->{params} };
259     my $type = $parameters->{type} || 'payment';
260
261     my $amt    = 0;
262     my @lines_to_pay;
263     foreach (@params) {
264         if (/^incl_par_(\d+)$/) {
265             my $index = $1;
266             push @lines_to_pay, scalar $input->param("accountlines_id$index");
267             $amt += $input->param("amountoutstanding$index");
268         }
269     }
270     $amt = '&amt=' . $amt;
271     my $sel = '&selected=' . join ',', @lines_to_pay;
272     my $notes = '&notes=' . join("%0A", map { scalar $input->param("payment_note_$_") } @lines_to_pay );
273     my $redirect =
274         "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
275       . "&type=$type"
276       . $amt
277       . $sel
278       . $notes;
279
280     print $input->redirect($redirect);
281     return;
282 }
283
284 sub apply_credits {
285     my ($args) = @_;
286
287     my $patron = $args->{patron};
288     my $cgi    = $args->{cgi};
289
290     $patron->account->reconcile_balance();
291
292     print $cgi->redirect("/cgi-bin/koha/members/pay.pl?borrowernumber=" . $patron->borrowernumber );
293     return;
294 }