Bug 8598 - No patron image or home library on Files & Statistics tabs
[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 $accountno    = $input->param('accountno');
87     my $itemno       = $input->param('itemnumber');
88     my $account_type = $input->param('accounttype');
89     my $amount       = $input->param('amountoutstanding');
90     WriteOffFee( $borrowernumber, $accountno, $itemno, $account_type, $amount, $branch );
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( activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne '') );
104
105 add_accounts_to_template();
106
107 output_html_with_http_headers $input, $cookie, $template->output;
108
109
110 sub add_accounts_to_template {
111
112     my ( $total, undef, undef ) = GetMemberAccountRecords($borrowernumber);
113     my $accounts = [];
114     my @notify   = NumberNotifyId($borrowernumber);
115
116     my $notify_groups = [];
117     for my $notify_id (@notify) {
118         my ( $acct_total, $accountlines, undef ) =
119           GetBorNotifyAcctRecord( $borrowernumber, $notify_id );
120         if ( @{$accountlines} ) {
121             my $totalnotify = AmountNotify( $notify_id, $borrowernumber );
122             push @{$accounts},
123               { accountlines => $accountlines,
124                 notify       => $notify_id,
125                 total        => $totalnotify,
126               };
127         }
128     }
129     borrower_add_additional_fields($borrower);
130     $template->param(
131         accounts => $accounts,
132         borrower => $borrower,
133         total    => $total,
134     );
135     return;
136
137 }
138
139 sub get_for_redirect {
140     my ( $name, $name_in, $money ) = @_;
141     my $s     = q{&} . $name . q{=};
142     my $value = $input->param($name_in);
143     if ( !defined $value ) {
144         $value = ( $money == 1 ) ? 0 : q{};
145     }
146     if ($money) {
147         $s .= sprintf '%.2f', $value;
148     } else {
149         $s .= $value;
150     }
151     return $s;
152 }
153
154 sub redirect_to_paycollect {
155     my ( $action, $line_no ) = @_;
156     my $redirect =
157       "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
158     $redirect .= q{&};
159     $redirect .= "$action=1";
160     $redirect .= get_for_redirect( 'accounttype', "accounttype$line_no", 0 );
161     $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
162     $redirect .=
163       get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
164     $redirect .= get_for_redirect( 'accountno',    "accountno$line_no",    0 );
165     $redirect .= get_for_redirect( 'description',  "description$line_no",  0 );
166     $redirect .= get_for_redirect( 'title',        "title$line_no",        0 );
167     $redirect .= get_for_redirect( 'itemnumber',   "itemnumber$line_no",   0 );
168     $redirect .= get_for_redirect( 'notify_id',    "notify_id$line_no",    0 );
169     $redirect .= get_for_redirect( 'notify_level', "notify_level$line_no", 0 );
170     $redirect .= '&remote_user=';
171     $redirect .= $user;
172     return print $input->redirect($redirect);
173 }
174
175 sub writeoff_all {
176     my @params = @_;
177     my @wo_lines = grep { /^accountno\d+$/ } @params;
178     for (@wo_lines) {
179         if (/(\d+)/) {
180             my $value       = $1;
181             my $accounttype = $input->param("accounttype$value");
182
183             #    my $borrowernum    = $input->param("borrowernumber$value");
184             my $itemno    = $input->param("itemnumber$value");
185             my $amount    = $input->param("amountoutstanding$value");
186             my $accountno = $input->param("accountno$value");
187             WriteOffFee( $borrowernumber, $accountno, $itemno, $accounttype, $amount, $branch );
188         }
189     }
190
191     $borrowernumber = $input->param('borrowernumber');
192     print $input->redirect(
193         "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
194     return;
195 }
196
197 sub borrower_add_additional_fields {
198     my $b_ref = shift;
199
200 # some borrower info is not returned in the standard call despite being assumed
201 # in a number of templates. It should not be the business of this script but in lieu of
202 # a revised api here it is ...
203     if ( $b_ref->{category_type} eq 'C' ) {
204         my ( $catcodes, $labels ) =
205           GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
206         if ( @{$catcodes} ) {
207             if ( @{$catcodes} > 1 ) {
208                 $b_ref->{CATCODE_MULTI} = 1;
209             } elsif ( @{$catcodes} == 1 ) {
210                 $b_ref->{catcode} = $catcodes->[0];
211             }
212         }
213     } elsif ( $b_ref->{category_type} eq 'A' ) {
214         $b_ref->{adultborrower} = 1;
215     }
216     my ( $picture, $dberror ) = GetPatronImage( $b_ref->{cardnumber} );
217     if ($picture) {
218         $b_ref->{has_picture} = 1;
219     }
220
221     if (C4::Context->preference('ExtendedPatronAttributes')) {
222         $b_ref->{extendedattributes} = GetBorrowerAttributes($borrowernumber);
223         $template->param(
224             ExtendedPatronAttributes => 1,
225         );
226     }
227
228     $b_ref->{branchname} = GetBranchName( $b_ref->{branchcode} );
229     return;
230 }
231
232 sub payselected {
233     my @params = @_;
234     my $amt    = 0;
235     my @lines_to_pay;
236     foreach (@params) {
237         if (/^incl_par_(\d+)$/) {
238             my $index = $1;
239             push @lines_to_pay, $input->param("accountno$index");
240             $amt += $input->param("amountoutstanding$index");
241         }
242     }
243     $amt = '&amt=' . $amt;
244     my $sel = '&selected=' . join ',', @lines_to_pay;
245     my $redirect =
246         "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
247       . $amt
248       . $sel;
249
250     print $input->redirect($redirect);
251     return;
252 }