Bug 20629: Pass accountline objects to template instead of hashref
[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('confirm_writeoff') ) {
91     my $accountlines_id = $input->param('accountlines_id');
92     my $amount          = $input->param('amountwrittenoff');
93     my $payment_note    = $input->param("payment_note");
94
95     my $accountline = Koha::Account::Lines->find( $accountlines_id );
96
97     if ( $amount > $accountline->amountoutstanding ) {
98         print $input->redirect( "/cgi-bin/koha/members/paycollect.pl?"
99               . "borrowernumber=$borrowernumber"
100               . "&amount=" . $accountline->amount
101               . "&amountoutstanding=" . $accountline->amountoutstanding
102               . "&accounttype=" . $accountline->accounttype
103               . "&accountlines_id=" . $accountlines_id
104               . "&writeoff_individual=1"
105               . "&error_over=1" );
106
107     } else {
108         Koha::Account->new( { patron_id => $borrowernumber } )->pay(
109             {
110                 amount     => $amount,
111                 lines      => [ scalar Koha::Account::Lines->find($accountlines_id) ],
112                 type       => 'writeoff',
113                 note       => $payment_note,
114                 library_id => $branch,
115             }
116         );
117     }
118 }
119
120 for (@names) {
121     if (/^pay_indiv_(\d+)$/) {
122         my $line_no = $1;
123         redirect_to_paycollect( 'pay_individual', $line_no );
124     } elsif (/^wo_indiv_(\d+)$/) {
125         my $line_no = $1;
126         redirect_to_paycollect( 'writeoff_individual', $line_no );
127     }
128 }
129
130 $template->param(
131     finesview => 1,
132 );
133
134 add_accounts_to_template();
135
136 output_html_with_http_headers $input, $cookie, $template->output;
137
138 sub add_accounts_to_template {
139
140     my $patron = Koha::Patrons->find( $borrowernumber );
141     my $account_lines = $patron->account->outstanding_debits;
142     my $total = $account_lines->total_outstanding;
143     my @accounts;
144     while ( my $account_line = $account_lines->next ) {
145         $account_line = $account_line->unblessed;
146         if ( $account_line->{itemnumber} ) {
147             my $item = Koha::Items->find( $account_line->{itemnumber} );
148             my $biblio = $item->biblio;
149             $account_line->{biblionumber} = $biblio->biblionumber;
150             $account_line->{title}        = $biblio->title;
151         }
152         push @accounts, $account_line;
153     }
154     borrower_add_additional_fields($patron);
155
156     $template->param(
157         patron   => $patron,
158         accounts => \@accounts,
159         total    => $total,
160     );
161     return;
162
163 }
164
165 sub get_for_redirect {
166     my ( $name, $name_in, $money ) = @_;
167     my $s     = q{&} . $name . q{=};
168     my $value;
169     if (defined $input->param($name_in)) {
170         $value = uri_escape_utf8( scalar $input->param($name_in) );
171     }
172     if ( !defined $value ) {
173         $value = ( $money == 1 ) ? 0 : q{};
174     }
175     if ($money) {
176         $s .= sprintf '%.2f', $value;
177     } else {
178         $s .= $value;
179     }
180     return $s;
181 }
182
183 sub redirect_to_paycollect {
184     my ( $action, $line_no ) = @_;
185     my $redirect =
186       "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
187     $redirect .= q{&};
188     $redirect .= "$action=1";
189     $redirect .= get_for_redirect( 'accounttype', "accounttype$line_no", 0 );
190     $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
191     $redirect .=
192       get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
193     $redirect .= get_for_redirect( 'description', "description$line_no", 0 );
194     $redirect .= get_for_redirect( 'title', "title$line_no", 0 );
195     $redirect .= get_for_redirect( 'itemnumber',   "itemnumber$line_no",   0 );
196     $redirect .= get_for_redirect( 'accountlines_id', "accountlines_id$line_no", 0 );
197     $redirect .= q{&} . 'payment_note' . q{=} . uri_escape_utf8( scalar $input->param("payment_note_$line_no") );
198     $redirect .= '&remote_user=';
199     $redirect .= $user;
200     return print $input->redirect($redirect);
201 }
202
203 sub writeoff_all {
204     my @params = @_;
205     my @wo_lines = grep { /^accountlines_id\d+$/ } @params;
206
207     my $borrowernumber = $input->param('borrowernumber');
208
209     for (@wo_lines) {
210         if (/(\d+)/) {
211             my $value           = $1;
212             my $amount          = $input->param("amountoutstanding$value");
213             my $accountlines_id = $input->param("accountlines_id$value");
214             my $payment_note    = $input->param("payment_note_$value");
215             Koha::Account->new( { patron_id => $borrowernumber } )->pay(
216                 {
217                     amount => $amount,
218                     lines  => [ scalar Koha::Account::Lines->find($accountlines_id) ],
219                     type   => 'writeoff',
220                     note   => $payment_note,
221                     library_id => $branch,
222                 }
223             );
224         }
225     }
226
227     print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
228     return;
229 }
230
231 sub borrower_add_additional_fields {
232     my $patron = shift;
233
234 # some borrower info is not returned in the standard call despite being assumed
235 # in a number of templates. It should not be the business of this script but in lieu of
236 # a revised api here it is ...
237     if (C4::Context->preference('ExtendedPatronAttributes')) {
238         my $extendedattributes = GetBorrowerAttributes($patron->borrowernumber);
239         $template->param(
240             extendedattributes       => $extendedattributes,
241             ExtendedPatronAttributes => 1,
242         );
243     }
244
245     return;
246 }
247
248 sub payselected {
249     my $parameters = shift;
250
251     my @params = @{ $parameters->{params} };
252     my $type = $parameters->{type} || 'payment';
253
254     my $amt    = 0;
255     my @lines_to_pay;
256     foreach (@params) {
257         if (/^incl_par_(\d+)$/) {
258             my $index = $1;
259             push @lines_to_pay, scalar $input->param("accountlines_id$index");
260             $amt += $input->param("amountoutstanding$index");
261         }
262     }
263     $amt = '&amt=' . $amt;
264     my $sel = '&selected=' . join ',', @lines_to_pay;
265     my $notes = '&notes=' . join("%0A", map { scalar $input->param("payment_note_$_") } @lines_to_pay );
266     my $redirect =
267         "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
268       . "&type=$type"
269       . $amt
270       . $sel
271       . $notes;
272
273     print $input->redirect($redirect);
274     return;
275 }