Merge remote-tracking branch 'origin/new/bug_6488'
[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 my $input = CGI->new;
45
46 my ( $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 $writeoff_sth;
57 my $add_writeoff_sth;
58
59 my @names = $input->param;
60
61 my $borrowernumber = $input->param('borrowernumber');
62 if ( !$borrowernumber ) {
63     $borrowernumber = $input->param('borrowernumber0');
64 }
65
66 # get borrower details
67 my $borrower = GetMember( borrowernumber => $borrowernumber );
68 my $user = $input->remote_user;
69 $user ||= q{};
70
71 my $branches = GetBranches();
72 my $branch = GetBranch( $input, $branches );
73
74 my $writeoff_item = $input->param('confirm_writeoff');
75 my $paycollect    = $input->param('paycollect');
76 if ($paycollect) {
77     print $input->redirect(
78         "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber");
79 }
80 my $payselected = $input->param('payselected');
81 if ($payselected) {
82     payselected(@names);
83 }
84
85 my $writeoff_all = $input->param('woall');    # writeoff all fines
86 if ($writeoff_all) {
87     writeoff_all(@names);
88 } elsif ($writeoff_item) {
89     my $accountno    = $input->param('accountno');
90     my $itemno       = $input->param('itemnumber');
91     my $account_type = $input->param('accounttype');
92     my $amount       = $input->param('amount');
93     writeoff( $accountno, $itemno, $account_type, $amount );
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( activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne '') );
107
108 add_accounts_to_template();
109
110 output_html_with_http_headers $input, $cookie, $template->output;
111
112 sub writeoff {
113     my ( $accountnum, $itemnum, $accounttype, $amount ) = @_;
114     my $manager_id = 0;
115     $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
116
117     # if no item is attached to fine, make sure to store it as a NULL
118     $itemnum ||= undef;
119     get_writeoff_sth();
120     $writeoff_sth->execute( $accountnum, $borrowernumber );
121
122     my $acct = getnextacctno($borrowernumber);
123     $add_writeoff_sth->execute( $borrowernumber, $acct, $itemnum, $amount, $manager_id );
124
125     UpdateStats( $branch, 'writeoff', $amount, q{}, q{}, q{}, $borrowernumber );
126
127     return;
128 }
129
130 sub add_accounts_to_template {
131
132     my ( $total, undef, undef ) = GetMemberAccountRecords($borrowernumber);
133     my $accounts = [];
134     my @notify   = NumberNotifyId($borrowernumber);
135
136     my $notify_groups = [];
137     for my $notify_id (@notify) {
138         my ( $acct_total, $accountlines, undef ) =
139           GetBorNotifyAcctRecord( $borrowernumber, $notify_id );
140         if ( @{$accountlines} ) {
141             my $totalnotify = AmountNotify( $notify_id, $borrowernumber );
142             push @{$accounts},
143               { accountlines => $accountlines,
144                 notify       => $notify_id,
145                 total        => $totalnotify,
146               };
147         }
148     }
149     borrower_add_additional_fields($borrower);
150     $template->param(
151         accounts => $accounts,
152         borrower => $borrower,
153         total    => $total,
154     );
155     return;
156
157 }
158
159 sub get_for_redirect {
160     my ( $name, $name_in, $money ) = @_;
161     my $s     = q{&} . $name . q{=};
162     my $value = $input->param($name_in);
163     if ( !defined $value ) {
164         $value = ( $money == 1 ) ? 0 : q{};
165     }
166     if ($money) {
167         $s .= sprintf '%.2f', $value;
168     } else {
169         $s .= $value;
170     }
171     return $s;
172 }
173
174 sub redirect_to_paycollect {
175     my ( $action, $line_no ) = @_;
176     my $redirect =
177       "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
178     $redirect .= q{&};
179     $redirect .= "$action=1";
180     $redirect .= get_for_redirect( 'accounttype', "accounttype$line_no", 0 );
181     $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
182     $redirect .=
183       get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
184     $redirect .= get_for_redirect( 'accountno',    "accountno$line_no",    0 );
185     $redirect .= get_for_redirect( 'description',  "description$line_no",  0 );
186     $redirect .= get_for_redirect( 'title',        "title$line_no",        0 );
187     $redirect .= get_for_redirect( 'itemnumber',   "itemnumber$line_no",   0 );
188     $redirect .= get_for_redirect( 'notify_id',    "notify_id$line_no",    0 );
189     $redirect .= get_for_redirect( 'notify_level', "notify_level$line_no", 0 );
190     $redirect .= '&remote_user=';
191     $redirect .= $user;
192     return print $input->redirect($redirect);
193 }
194
195 sub writeoff_all {
196     my @params = @_;
197     my @wo_lines = grep { /^accountno\d+$/ } @params;
198     for (@wo_lines) {
199         if (/(\d+)/) {
200             my $value       = $1;
201             my $accounttype = $input->param("accounttype$value");
202
203             #    my $borrowernum    = $input->param("borrowernumber$value");
204             my $itemno    = $input->param("itemnumber$value");
205             my $amount    = $input->param("amount$value");
206             my $accountno = $input->param("accountno$value");
207             writeoff( $accountno, $itemno, $accounttype, $amount );
208         }
209     }
210
211     $borrowernumber = $input->param('borrowernumber');
212     print $input->redirect(
213         "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
214     return;
215 }
216
217 sub borrower_add_additional_fields {
218     my $b_ref = shift;
219
220 # some borrower info is not returned in the standard call despite being assumed
221 # in a number of templates. It should not be the business of this script but in lieu of
222 # a revised api here it is ...
223     if ( $b_ref->{category_type} eq 'C' ) {
224         my ( $catcodes, $labels ) =
225           GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
226         if ( @{$catcodes} ) {
227             if ( @{$catcodes} > 1 ) {
228                 $b_ref->{CATCODE_MULTI} = 1;
229             } elsif ( @{$catcodes} == 1 ) {
230                 $b_ref->{catcode} = $catcodes->[0];
231             }
232         }
233     } elsif ( $b_ref->{category_type} eq 'A' ) {
234         $b_ref->{adultborrower} = 1;
235     }
236     my ( $picture, $dberror ) = GetPatronImage( $b_ref->{cardnumber} );
237     if ($picture) {
238         $b_ref->{has_picture} = 1;
239     }
240
241     if (C4::Context->preference('ExtendedPatronAttributes')) {
242         $b_ref->{extendedattributes} = GetBorrowerAttributes($borrowernumber);
243         $template->param(
244             ExtendedPatronAttributes => 1,
245         );
246     }
247
248     $b_ref->{branchname} = GetBranchName( $b_ref->{branchcode} );
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, $input->param("accountno$index");
260             $amt += $input->param("amountoutstanding$index");
261         }
262     }
263     $amt = '&amt=' . $amt;
264     my $sel = '&selected=' . join ',', @lines_to_pay;
265     my $redirect =
266         "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
267       . $amt
268       . $sel;
269
270     print $input->redirect($redirect);
271     return;
272 }
273
274 sub get_writeoff_sth {
275
276     # lets prepare these statement handles only once
277     if ($writeoff_sth) {
278         return;
279     } else {
280         my $dbh = C4::Context->dbh;
281
282         # Do we need to validate accounttype
283         my $sql = 'Update accountlines set amountoutstanding=0 '
284           . 'WHERE accountno=? and borrowernumber=?';
285         $writeoff_sth = $dbh->prepare($sql);
286         my $insert =
287 q{insert into accountlines (borrowernumber,accountno,itemnumber,date,amount,description,accounttype,manager_id)}
288           . q{values (?,?,?,now(),?,'Writeoff','W',?)};
289         $add_writeoff_sth = $dbh->prepare($insert);
290     }
291     return;
292 }