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