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