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