Bug 19921: Fix update child when only one adult patron category exist
[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('amountwrittenoff');
92     my $payment_note = $input->param("payment_note");
93
94     Koha::Account->new( { patron_id => $borrowernumber } )->pay(
95         {
96             amount     => $amount,
97             lines      => [ scalar 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 );
118
119 add_accounts_to_template();
120
121 output_html_with_http_headers $input, $cookie, $template->output;
122
123 sub add_accounts_to_template {
124
125     my ( $total, undef, undef ) = GetMemberAccountRecords($borrowernumber);
126     my $accounts = [];
127     my @notify   = NumberNotifyId($borrowernumber);
128
129     my $notify_groups = [];
130     for my $notify_id (@notify) {
131         my ( $acct_total, $accountlines, undef ) =
132           GetBorNotifyAcctRecord( $borrowernumber, $notify_id );
133         if ( @{$accountlines} ) {
134             my $totalnotify = AmountNotify( $notify_id, $borrowernumber );
135             push @{$accounts},
136               { accountlines => $accountlines,
137                 notify       => $notify_id,
138                 total        => $totalnotify,
139               };
140         }
141     }
142     borrower_add_additional_fields($borrower);
143
144     $template->param(%$borrower);
145
146     my $patron_image = Koha::Patron::Images->find($borrower->{borrowernumber});
147     $template->param( picture => 1 ) if $patron_image;
148     $template->param(
149         accounts => $accounts,
150         borrower => $borrower,
151         categoryname => $borrower->{'description'},
152         total    => $total,
153     );
154     return;
155
156 }
157
158 sub get_for_redirect {
159     my ( $name, $name_in, $money ) = @_;
160     my $s     = q{&} . $name . q{=};
161     my $value;
162     if (defined $input->param($name_in)) {
163         $value = uri_escape_utf8( scalar $input->param($name_in) );
164     }
165     if ( !defined $value ) {
166         $value = ( $money == 1 ) ? 0 : q{};
167     }
168     if ($money) {
169         $s .= sprintf '%.2f', $value;
170     } else {
171         $s .= $value;
172     }
173     return $s;
174 }
175
176 sub redirect_to_paycollect {
177     my ( $action, $line_no ) = @_;
178     my $redirect =
179       "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
180     $redirect .= q{&};
181     $redirect .= "$action=1";
182     $redirect .= get_for_redirect( 'accounttype', "accounttype$line_no", 0 );
183     $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
184     $redirect .=
185       get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
186     $redirect .= get_for_redirect( 'description', "description$line_no", 0 );
187     $redirect .= get_for_redirect( 'title', "title$line_no", 0 );
188     $redirect .= get_for_redirect( 'itemnumber',   "itemnumber$line_no",   0 );
189     $redirect .= get_for_redirect( 'notify_id',    "notify_id$line_no",    0 );
190     $redirect .= get_for_redirect( 'notify_level', "notify_level$line_no", 0 );
191     $redirect .= get_for_redirect( 'accountlines_id', "accountlines_id$line_no", 0 );
192     $redirect .= q{&} . 'payment_note' . q{=} . uri_escape_utf8( scalar $input->param("payment_note_$line_no") );
193     $redirect .= '&remote_user=';
194     $redirect .= $user;
195     return print $input->redirect($redirect);
196 }
197
198 sub writeoff_all {
199     my @params = @_;
200     my @wo_lines = grep { /^accountlines_id\d+$/ } @params;
201
202     my $borrowernumber = $input->param('borrowernumber');
203
204     for (@wo_lines) {
205         if (/(\d+)/) {
206             my $value           = $1;
207             my $amount          = $input->param("amountoutstanding$value");
208             my $accountlines_id = $input->param("accountlines_id$value");
209             my $payment_note    = $input->param("payment_note_$value");
210             Koha::Account->new( { patron_id => $borrowernumber } )->pay(
211                 {
212                     amount => $amount,
213                     lines  => [ scalar Koha::Account::Lines->find($accountlines_id) ],
214                     type   => 'writeoff',
215                     note   => $payment_note,
216                     library_id => $branch,
217                 }
218             );
219         }
220     }
221
222     print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
223     return;
224 }
225
226 sub borrower_add_additional_fields {
227     my $b_ref = shift;
228
229 # some borrower info is not returned in the standard call despite being assumed
230 # in a number of templates. It should not be the business of this script but in lieu of
231 # a revised api here it is ...
232     if ( $b_ref->{category_type} eq 'C' ) {
233         my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
234         $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
235         $template->param( 'catcode' => $patron_categories->next->categorycode )  if $patron_categories->count == 1;
236     } elsif ( $b_ref->{category_type} eq 'A' || $b_ref->{category_type} eq 'I' ) {
237         $b_ref->{adultborrower} = 1;
238     }
239
240     if (C4::Context->preference('ExtendedPatronAttributes')) {
241         $b_ref->{extendedattributes} = GetBorrowerAttributes($borrowernumber);
242         $template->param(
243             ExtendedPatronAttributes => 1,
244         );
245     }
246
247     return;
248 }
249
250 sub payselected {
251     my @params = @_;
252     my $amt    = 0;
253     my @lines_to_pay;
254     foreach (@params) {
255         if (/^incl_par_(\d+)$/) {
256             my $index = $1;
257             push @lines_to_pay, scalar $input->param("accountlines_id$index");
258             $amt += $input->param("amountoutstanding$index");
259         }
260     }
261     $amt = '&amt=' . $amt;
262     my $sel = '&selected=' . join ',', @lines_to_pay;
263     my $notes = '&notes=' . join("%0A", map { scalar $input->param("payment_note_$_") } @lines_to_pay );
264     my $redirect =
265         "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
266       . $amt
267       . $sel
268       . $notes;
269
270     print $input->redirect($redirect);
271     return;
272 }