Revert "Bug 18179: Update existing calls"
[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 strict;
30 use warnings;
31
32 use URI::Escape;
33 use C4::Context;
34 use C4::Auth;
35 use C4::Output;
36 use CGI qw ( -utf8 );
37 use C4::Members;
38 use C4::Accounts;
39 use C4::Stats;
40 use C4::Koha;
41 use C4::Overdues;
42 use C4::Members::Attributes qw(GetBorrowerAttributes);
43 use Koha::Patron::Images;
44
45 use Koha::Patron::Categories;
46 use URI::Escape;
47
48 our $input = CGI->new;
49
50 my $updatecharges_permissions = $input->param('woall') ? 'writeoff' : 'remaining_permissions';
51 our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
52     {   template_name   => 'members/pay.tt',
53         query           => $input,
54         type            => 'intranet',
55         authnotrequired => 0,
56         flagsrequired   => { borrowers => 1, updatecharges => $updatecharges_permissions },
57         debug           => 1,
58     }
59 );
60
61 my @names = $input->param;
62
63 our $borrowernumber = $input->param('borrowernumber');
64 if ( !$borrowernumber ) {
65     $borrowernumber = $input->param('borrowernumber0');
66 }
67
68 # get borrower details
69 our $borrower = GetMember( borrowernumber => $borrowernumber );
70 our $user = $input->remote_user;
71 $user ||= q{};
72
73 our $branch = C4::Context->userenv->{'branch'};
74
75 my $writeoff_item = $input->param('confirm_writeoff');
76 my $paycollect    = $input->param('paycollect');
77 if ($paycollect) {
78     print $input->redirect(
79         "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber");
80 }
81 my $payselected = $input->param('payselected');
82 if ($payselected) {
83     payselected(@names);
84 }
85
86 my $writeoff_all = $input->param('woall');    # writeoff all fines
87 if ($writeoff_all) {
88     writeoff_all(@names);
89 } elsif ($writeoff_item) {
90     my $accountlines_id = $input->param('accountlines_id');
91     my $amount       = $input->param('amountoutstanding');
92     my $payment_note = $input->param("payment_note");
93
94     Koha::Account->new( { patron_id => $borrowernumber } )->pay(
95         {
96             amount     => $amount,
97             lines      => [Koha::Account::Lines->find($accountlines_id)],
98             type       => 'writeoff',
99             note       => $payment_note,
100             library_id => $branch,
101         }
102     );
103 }
104
105 for (@names) {
106     if (/^pay_indiv_(\d+)$/) {
107         my $line_no = $1;
108         redirect_to_paycollect( 'pay_individual', $line_no );
109     } elsif (/^wo_indiv_(\d+)$/) {
110         my $line_no = $1;
111         redirect_to_paycollect( 'writeoff_individual', $line_no );
112     }
113 }
114
115 $template->param(
116     finesview => 1,
117     RoutingSerials => C4::Context->preference('RoutingSerials'),
118 );
119
120 add_accounts_to_template();
121
122 output_html_with_http_headers $input, $cookie, $template->output;
123
124 sub add_accounts_to_template {
125
126     my ( $total, undef, undef ) = GetMemberAccountRecords($borrowernumber);
127     my $accounts = [];
128     my @notify   = NumberNotifyId($borrowernumber);
129
130     my $notify_groups = [];
131     for my $notify_id (@notify) {
132         my ( $acct_total, $accountlines, undef ) =
133           GetBorNotifyAcctRecord( $borrowernumber, $notify_id );
134         if ( @{$accountlines} ) {
135             my $totalnotify = AmountNotify( $notify_id, $borrowernumber );
136             push @{$accounts},
137               { accountlines => $accountlines,
138                 notify       => $notify_id,
139                 total        => $totalnotify,
140               };
141         }
142     }
143     borrower_add_additional_fields($borrower);
144
145     $template->param(%$borrower);
146
147     my $patron_image = Koha::Patron::Images->find($borrower->{borrowernumber});
148     $template->param( picture => 1 ) if $patron_image;
149     $template->param(
150         accounts => $accounts,
151         borrower => $borrower,
152         categoryname => $borrower->{'description'},
153         total    => $total,
154     );
155     return;
156
157 }
158
159 sub get_for_redirect {
160     my ( $name, $name_in, $money ) = @_;
161     my $s     = q{&} . $name . q{=};
162     my $value = uri_escape_utf8( $input->param($name_in) );
163     if ( !defined $value ) {
164         $value = ( $money == 1 ) ? 0 : q{};
165     }
166     if ($money) {
167         $s .= sprintf '%.2f', $value;
168     } else {
169         $s .= $value;
170     }
171     return $s;
172 }
173
174 sub redirect_to_paycollect {
175     my ( $action, $line_no ) = @_;
176     my $redirect =
177       "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
178     $redirect .= q{&};
179     $redirect .= "$action=1";
180     $redirect .= get_for_redirect( 'accounttype', "accounttype$line_no", 0 );
181     $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
182     $redirect .=
183       get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
184     $redirect .= get_for_redirect( 'description', "description$line_no", 0 );
185     $redirect .= get_for_redirect( 'title', "title$line_no", 0 );
186     $redirect .= get_for_redirect( 'itemnumber',   "itemnumber$line_no",   0 );
187     $redirect .= get_for_redirect( 'notify_id',    "notify_id$line_no",    0 );
188     $redirect .= get_for_redirect( 'notify_level', "notify_level$line_no", 0 );
189     $redirect .= get_for_redirect( 'accountlines_id', "accountlines_id$line_no", 0 );
190     $redirect .= q{&} . 'payment_note' . q{=} . uri_escape_utf8( $input->param("payment_note_$line_no") );
191     $redirect .= '&remote_user=';
192     $redirect .= $user;
193     return print $input->redirect($redirect);
194 }
195
196 sub writeoff_all {
197     my @params = @_;
198     my @wo_lines = grep { /^accountlines_id\d+$/ } @params;
199
200     my $borrowernumber = $input->param('borrowernumber');
201
202     for (@wo_lines) {
203         if (/(\d+)/) {
204             my $value           = $1;
205             my $amount          = $input->param("amountoutstanding$value");
206             my $accountlines_id = $input->param("accountlines_id$value");
207             my $payment_note    = $input->param("payment_note_$value");
208             Koha::Account->new( { patron_id => $borrowernumber } )->pay(
209                 {
210                     amount => $amount,
211                     lines  => [ Koha::Account::Lines->find($accountlines_id) ],
212                     type   => 'writeoff',
213                     note   => $payment_note,
214                     library_id => $branch,
215                 }
216             );
217         }
218     }
219
220     print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
221     return;
222 }
223
224 sub borrower_add_additional_fields {
225     my $b_ref = shift;
226
227 # some borrower info is not returned in the standard call despite being assumed
228 # in a number of templates. It should not be the business of this script but in lieu of
229 # a revised api here it is ...
230     if ( $b_ref->{category_type} eq 'C' ) {
231         my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
232         $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
233         $template->param( 'catcode' => $patron_categories->next )  if $patron_categories->count == 1;
234     } elsif ( $b_ref->{category_type} eq 'A' || $b_ref->{category_type} eq 'I' ) {
235         $b_ref->{adultborrower} = 1;
236     }
237
238     if (C4::Context->preference('ExtendedPatronAttributes')) {
239         $b_ref->{extendedattributes} = GetBorrowerAttributes($borrowernumber);
240         $template->param(
241             ExtendedPatronAttributes => 1,
242         );
243     }
244
245     return;
246 }
247
248 sub payselected {
249     my @params = @_;
250     my $amt    = 0;
251     my @lines_to_pay;
252     foreach (@params) {
253         if (/^incl_par_(\d+)$/) {
254             my $index = $1;
255             push @lines_to_pay, $input->param("accountlines_id$index");
256             $amt += $input->param("amountoutstanding$index");
257         }
258     }
259     $amt = '&amt=' . $amt;
260     my $sel = '&selected=' . join ',', @lines_to_pay;
261     my $notes = '&notes=' . join("%0A", map { $input->param("payment_note_$_") } @lines_to_pay );
262     my $redirect =
263         "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
264       . $amt
265       . $sel
266       . $notes;
267
268     print $input->redirect($redirect);
269     return;
270 }