Bug 16272 - DBRev 16.06.00.020
[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
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 => 1, 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 our $borrower = GetMember( borrowernumber => $borrowernumber );
69 our $user = $input->remote_user;
70 $user ||= q{};
71
72 our $branch = C4::Context->userenv->{'branch'};
73
74 my $writeoff_item = $input->param('confirm_writeoff');
75 my $paycollect    = $input->param('paycollect');
76 if ($paycollect) {
77     print $input->redirect(
78         "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber");
79 }
80 my $payselected = $input->param('payselected');
81 if ($payselected) {
82     payselected(@names);
83 }
84
85 my $writeoff_all = $input->param('woall');    # writeoff all fines
86 if ($writeoff_all) {
87     writeoff_all(@names);
88 } elsif ($writeoff_item) {
89     my $accountlines_id = $input->param('accountlines_id');
90     my $itemno       = $input->param('itemnumber');
91     my $account_type = $input->param('accounttype');
92     my $amount       = $input->param('amountoutstanding');
93     my $payment_note = $input->param("payment_note");
94     WriteOffFee( $borrowernumber, $accountlines_id, $itemno, $account_type, $amount, $branch, $payment_note );
95 }
96
97 for (@names) {
98     if (/^pay_indiv_(\d+)$/) {
99         my $line_no = $1;
100         redirect_to_paycollect( 'pay_individual', $line_no );
101     } elsif (/^wo_indiv_(\d+)$/) {
102         my $line_no = $1;
103         redirect_to_paycollect( 'writeoff_individual', $line_no );
104     }
105 }
106
107 $template->param(
108     finesview => 1,
109     activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
110     RoutingSerials => C4::Context->preference('RoutingSerials'),
111 );
112
113 add_accounts_to_template();
114
115 output_html_with_http_headers $input, $cookie, $template->output;
116
117 sub add_accounts_to_template {
118
119     my ( $total, undef, undef ) = GetMemberAccountRecords($borrowernumber);
120     my $accounts = [];
121     my @notify   = NumberNotifyId($borrowernumber);
122
123     my $notify_groups = [];
124     for my $notify_id (@notify) {
125         my ( $acct_total, $accountlines, undef ) =
126           GetBorNotifyAcctRecord( $borrowernumber, $notify_id );
127         if ( @{$accountlines} ) {
128             my $totalnotify = AmountNotify( $notify_id, $borrowernumber );
129             push @{$accounts},
130               { accountlines => $accountlines,
131                 notify       => $notify_id,
132                 total        => $totalnotify,
133               };
134         }
135     }
136     borrower_add_additional_fields($borrower);
137
138     $template->param(%$borrower);
139
140     my $patron_image = Koha::Patron::Images->find($borrower->{borrowernumber});
141     $template->param( picture => 1 ) if $patron_image;
142     $template->param(
143         accounts => $accounts,
144         borrower => $borrower,
145         categoryname => $borrower->{'description'},
146         total    => $total,
147     );
148     return;
149
150 }
151
152 sub get_for_redirect {
153     my ( $name, $name_in, $money ) = @_;
154     my $s     = q{&} . $name . q{=};
155     my $value = $input->param($name_in);
156     if ( !defined $value ) {
157         $value = ( $money == 1 ) ? 0 : q{};
158     }
159     if ($money) {
160         $s .= sprintf '%.2f', $value;
161     } else {
162         $s .= $value;
163     }
164     return $s;
165 }
166
167 sub redirect_to_paycollect {
168     my ( $action, $line_no ) = @_;
169     my $redirect =
170       "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
171     $redirect .= q{&};
172     $redirect .= "$action=1";
173     $redirect .= get_for_redirect( 'accounttype', "accounttype$line_no", 0 );
174     $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
175     $redirect .=
176       get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
177     $redirect .= get_for_redirect( 'description',    "description$line_no",    0 );
178     $redirect .= get_for_redirect( 'title',        "title$line_no",        0 );
179     $redirect .= get_for_redirect( 'itemnumber',   "itemnumber$line_no",   0 );
180     $redirect .= get_for_redirect( 'notify_id',    "notify_id$line_no",    0 );
181     $redirect .= get_for_redirect( 'notify_level', "notify_level$line_no", 0 );
182     $redirect .= get_for_redirect( 'accountlines_id', "accountlines_id$line_no", 0 );
183     $redirect .= q{&} . 'payment_note' . q{=} . uri_escape_utf8( $input->param("payment_note_$line_no") );
184     $redirect .= '&remote_user=';
185     $redirect .= $user;
186     return print $input->redirect($redirect);
187 }
188
189 sub writeoff_all {
190     my @params = @_;
191     my @wo_lines = grep { /^accountlines_id\d+$/ } @params;
192     for (@wo_lines) {
193         if (/(\d+)/) {
194             my $value       = $1;
195             my $accounttype = $input->param("accounttype$value");
196
197             #    my $borrowernum    = $input->param("borrowernumber$value");
198             my $itemno    = $input->param("itemnumber$value");
199             my $amount    = $input->param("amountoutstanding$value");
200             my $accountlines_id = $input->param("accountlines_id$value");
201             my $payment_note = $input->param("payment_note_$value");
202             WriteOffFee( $borrowernumber, $accountlines_id, $itemno, $accounttype, $amount, $branch, $payment_note );
203         }
204     }
205
206     $borrowernumber = $input->param('borrowernumber');
207     print $input->redirect(
208         "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
209     return;
210 }
211
212 sub borrower_add_additional_fields {
213     my $b_ref = shift;
214
215 # some borrower info is not returned in the standard call despite being assumed
216 # in a number of templates. It should not be the business of this script but in lieu of
217 # a revised api here it is ...
218     if ( $b_ref->{category_type} eq 'C' ) {
219         my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
220         $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
221         $template->param( 'catcode' => $patron_categories->next )  if $patron_categories->count == 1;
222     } elsif ( $b_ref->{category_type} eq 'A' ) {
223         $b_ref->{adultborrower} = 1;
224     }
225
226     if (C4::Context->preference('ExtendedPatronAttributes')) {
227         $b_ref->{extendedattributes} = GetBorrowerAttributes($borrowernumber);
228         $template->param(
229             ExtendedPatronAttributes => 1,
230         );
231     }
232
233     return;
234 }
235
236 sub payselected {
237     my @params = @_;
238     my $amt    = 0;
239     my @lines_to_pay;
240     foreach (@params) {
241         if (/^incl_par_(\d+)$/) {
242             my $index = $1;
243             push @lines_to_pay, $input->param("accountlines_id$index");
244             $amt += $input->param("amountoutstanding$index");
245         }
246     }
247     $amt = '&amt=' . $amt;
248     my $sel = '&selected=' . join ',', @lines_to_pay;
249     my $notes = '&notes=' . join("%0A", map { $input->param("payment_note_$_") } @lines_to_pay );
250     my $redirect =
251         "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
252       . $amt
253       . $sel
254       . $notes;
255
256     print $input->redirect($redirect);
257     return;
258 }