Bug 11715: require authentication for various staff 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 under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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;
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.tmpl',
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 my $branches = GetBranches();
71 our $branch = GetBranch( $input, $branches );
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     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     $template->param(
136         accounts => $accounts,
137         borrower => $borrower,
138         total    => $total,
139     );
140     return;
141
142 }
143
144 sub get_for_redirect {
145     my ( $name, $name_in, $money ) = @_;
146     my $s     = q{&} . $name . q{=};
147     my $value = $input->param($name_in);
148     if ( !defined $value ) {
149         $value = ( $money == 1 ) ? 0 : q{};
150     }
151     if ($money) {
152         $s .= sprintf '%.2f', $value;
153     } else {
154         $s .= $value;
155     }
156     return $s;
157 }
158
159 sub redirect_to_paycollect {
160     my ( $action, $line_no ) = @_;
161     my $redirect =
162       "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
163     $redirect .= q{&};
164     $redirect .= "$action=1";
165     $redirect .= get_for_redirect( 'accounttype', "accounttype$line_no", 0 );
166     $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
167     $redirect .=
168       get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
169     $redirect .= get_for_redirect( 'accountno',    "accountno$line_no",    0 );
170     $redirect .= get_for_redirect( 'title',        "title$line_no",        0 );
171     $redirect .= get_for_redirect( 'itemnumber',   "itemnumber$line_no",   0 );
172     $redirect .= get_for_redirect( 'notify_id',    "notify_id$line_no",    0 );
173     $redirect .= get_for_redirect( 'notify_level', "notify_level$line_no", 0 );
174     $redirect .= get_for_redirect( 'accountlines_id', "accountlines_id$line_no", 0 );
175     $redirect .= q{&} . 'payment_note' . q{=} . uri_escape( $input->param("payment_note_$line_no") );
176     $redirect .= '&remote_user=';
177     $redirect .= $user;
178     return print $input->redirect($redirect);
179 }
180
181 sub writeoff_all {
182     my @params = @_;
183     my @wo_lines = grep { /^accountno\d+$/ } @params;
184     for (@wo_lines) {
185         if (/(\d+)/) {
186             my $value       = $1;
187             my $accounttype = $input->param("accounttype$value");
188
189             #    my $borrowernum    = $input->param("borrowernumber$value");
190             my $itemno    = $input->param("itemnumber$value");
191             my $amount    = $input->param("amountoutstanding$value");
192             my $accountno = $input->param("accountno$value");
193             my $accountlines_id = $input->param("accountlines_id$value");
194             my $payment_note = $input->param("payment_note_$value");
195             WriteOffFee( $borrowernumber, $accountlines_id, $itemno, $accounttype, $amount, $branch, $payment_note );
196         }
197     }
198
199     $borrowernumber = $input->param('borrowernumber');
200     print $input->redirect(
201         "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
202     return;
203 }
204
205 sub borrower_add_additional_fields {
206     my $b_ref = shift;
207
208 # some borrower info is not returned in the standard call despite being assumed
209 # in a number of templates. It should not be the business of this script but in lieu of
210 # a revised api here it is ...
211     if ( $b_ref->{category_type} eq 'C' ) {
212         my ( $catcodes, $labels ) =
213           GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
214         if ( @{$catcodes} ) {
215             if ( @{$catcodes} > 1 ) {
216                 $b_ref->{CATCODE_MULTI} = 1;
217             } elsif ( @{$catcodes} == 1 ) {
218                 $b_ref->{catcode} = $catcodes->[0];
219             }
220         }
221     } elsif ( $b_ref->{category_type} eq 'A' ) {
222         $b_ref->{adultborrower} = 1;
223     }
224     my ( $picture, $dberror ) = GetPatronImage( $b_ref->{borrowernumber} );
225     if ($picture) {
226         $b_ref->{has_picture} = 1;
227     }
228
229     if (C4::Context->preference('ExtendedPatronAttributes')) {
230         $b_ref->{extendedattributes} = GetBorrowerAttributes($borrowernumber);
231         $template->param(
232             ExtendedPatronAttributes => 1,
233         );
234     }
235
236     $b_ref->{branchname} = GetBranchName( $b_ref->{branchcode} );
237     return;
238 }
239
240 sub payselected {
241     my @params = @_;
242     my $amt    = 0;
243     my @lines_to_pay;
244     foreach (@params) {
245         if (/^incl_par_(\d+)$/) {
246             my $index = $1;
247             push @lines_to_pay, $input->param("accountno$index");
248             $amt += $input->param("amountoutstanding$index");
249         }
250     }
251     $amt = '&amt=' . $amt;
252     my $sel = '&selected=' . join ',', @lines_to_pay;
253     my $notes = '&notes=' . join("%0A", map { $input->param("payment_note_$_") } @lines_to_pay );
254     my $redirect =
255         "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
256       . $amt
257       . $sel
258       . $notes;
259
260     print $input->redirect($redirect);
261     return;
262 }