Bug 22951: Markup error in OPAC holds template
[koha.git] / members / pay.pl
blobdd7a1efb5f0909cf56b013480a4d2f5c60fc9e5b
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;
43 use Koha::Patron::Categories;
44 use URI::Escape;
46 our $input = CGI->new;
48 my $updatecharges_permissions = $input->param('woall') ? 'writeoff' : 'remaining_permissions';
49 our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
50 { template_name => 'members/pay.tt',
51 query => $input,
52 type => 'intranet',
53 authnotrequired => 0,
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 # get borrower details
67 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
68 our $patron = Koha::Patrons->find($borrowernumber);
69 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
71 our $user = $input->remote_user;
72 $user ||= q{};
74 our $branch = C4::Context->userenv->{'branch'};
76 if ( $input->param('paycollect') ) {
77 print $input->redirect(
78 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber");
80 elsif ( $input->param('payselected') ) {
81 payselected({ params => \@names });
83 elsif ( $input->param('writeoff_selected') ) {
84 payselected({ params => \@names, type => 'writeoff' });
86 elsif ( $input->param('woall') ) {
87 writeoff_all(@names);
89 elsif ( $input->param('apply_credits') ) {
90 apply_credits({ patron => $patron, cgi => $input });
92 elsif ( $input->param('confirm_writeoff') ) {
93 my $accountlines_id = $input->param('accountlines_id');
94 my $amount = $input->param('amountwrittenoff');
95 my $payment_note = $input->param("payment_note");
97 my $accountline = Koha::Account::Lines->find( $accountlines_id );
99 if ( $amount > $accountline->amountoutstanding ) {
100 print $input->redirect( "/cgi-bin/koha/members/paycollect.pl?"
101 . "borrowernumber=$borrowernumber"
102 . "&amount=" . $accountline->amount
103 . "&amountoutstanding=" . $accountline->amountoutstanding
104 . "&accounttype=" . $accountline->accounttype
105 . "&accountlines_id=" . $accountlines_id
106 . "&writeoff_individual=1"
107 . "&error_over=1" );
109 } else {
110 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
112 amount => $amount,
113 lines => [ scalar Koha::Account::Lines->find($accountlines_id) ],
114 type => 'writeoff',
115 note => $payment_note,
116 interface => C4::Context->interface,
117 library_id => $branch,
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 );
133 $template->param(
134 finesview => 1,
137 add_accounts_to_template();
139 output_html_with_http_headers $input, $cookie, $template->output;
141 sub add_accounts_to_template {
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 push @accounts, $account_line;
153 $template->param(
154 patron => $patron,
155 accounts => \@accounts,
156 total => $total,
157 outstanding_credits => $outstanding_credits
160 return;
164 sub get_for_redirect {
165 my ( $name, $name_in, $money ) = @_;
166 my $s = q{&} . $name . q{=};
167 my $value;
168 if (defined $input->param($name_in)) {
169 $value = uri_escape_utf8( scalar $input->param($name_in) );
171 if ( !defined $value ) {
172 $value = ( $money == 1 ) ? 0 : q{};
174 if ($money) {
175 $s .= sprintf '%.2f', $value;
176 } else {
177 $s .= $value;
179 return $s;
182 sub redirect_to_paycollect {
183 my ( $action, $line_no ) = @_;
184 my $redirect =
185 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
186 $redirect .= q{&};
187 $redirect .= "$action=1";
188 $redirect .= get_for_redirect( 'accounttype', "accounttype$line_no", 0 );
189 $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
190 $redirect .=
191 get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
192 $redirect .= get_for_redirect( 'description', "description$line_no", 0 );
193 $redirect .= get_for_redirect( 'title', "title$line_no", 0 );
194 $redirect .= get_for_redirect( 'itemnumber', "itemnumber$line_no", 0 );
195 $redirect .= get_for_redirect( 'accountlines_id', "accountlines_id$line_no", 0 );
196 $redirect .= q{&} . 'payment_note' . q{=} . uri_escape_utf8( scalar $input->param("payment_note_$line_no") );
197 $redirect .= '&remote_user=';
198 $redirect .= $user;
199 return print $input->redirect($redirect);
202 sub writeoff_all {
203 my @params = @_;
204 my @wo_lines = grep { /^accountlines_id\d+$/ } @params;
206 my $borrowernumber = $input->param('borrowernumber');
208 for (@wo_lines) {
209 if (/(\d+)/) {
210 my $value = $1;
211 my $amount = $input->param("amountoutstanding$value");
212 my $accountlines_id = $input->param("accountlines_id$value");
213 my $payment_note = $input->param("payment_note_$value");
214 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
216 amount => $amount,
217 lines => [ scalar Koha::Account::Lines->find($accountlines_id) ],
218 type => 'writeoff',
219 note => $payment_note,
220 interface => C4::Context->interface,
221 library_id => $branch,
227 print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
228 return;
231 sub payselected {
232 my $parameters = shift;
234 my @params = @{ $parameters->{params} };
235 my $type = $parameters->{type} || 'payment';
237 my $amt = 0;
238 my @lines_to_pay;
239 foreach (@params) {
240 if (/^incl_par_(\d+)$/) {
241 my $index = $1;
242 push @lines_to_pay, scalar $input->param("accountlines_id$index");
243 $amt += $input->param("amountoutstanding$index");
246 $amt = '&amt=' . $amt;
247 my $sel = '&selected=' . join ',', @lines_to_pay;
248 my $notes = '&notes=' . join("%0A", map { scalar $input->param("payment_note_$_") } @lines_to_pay );
249 my $redirect =
250 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
251 . "&type=$type"
252 . $amt
253 . $sel
254 . $notes;
256 print $input->redirect($redirect);
257 return;
260 sub apply_credits {
261 my ($args) = @_;
263 my $patron = $args->{patron};
264 my $cgi = $args->{cgi};
266 $patron->account->reconcile_balance();
268 print $cgi->redirect("/cgi-bin/koha/members/pay.pl?borrowernumber=" . $patron->borrowernumber );
269 return;