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