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