Merge remote-tracking branch 'origin/new/bug_4118'
[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 our $writeoff_sth;
46 our $add_writeoff_sth;
47
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 => 1 },
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     WriteOffFee( $borrowernumber, $accountlines_id, $itemno, $account_type, $amount, $branch );
93 }
94
95 for (@names) {
96     if (/^pay_indiv_(\d+)$/) {
97         my $line_no = $1;
98         redirect_to_paycollect( 'pay_individual', $line_no );
99     } elsif (/^wo_indiv_(\d+)$/) {
100         my $line_no = $1;
101         redirect_to_paycollect( 'writeoff_individual', $line_no );
102     }
103 }
104
105 $template->param( activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne '') );
106
107 add_accounts_to_template();
108
109 output_html_with_http_headers $input, $cookie, $template->output;
110
111 sub writeoff {
112     my ( $accountlines_id, $itemnum, $accounttype, $amount ) = @_;
113     my $manager_id = 0;
114     $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
115
116     # if no item is attached to fine, make sure to store it as a NULL
117     $itemnum ||= undef;
118     get_writeoff_sth();
119     $writeoff_sth->execute( $accountlines_id );
120
121     my $acct = getnextacctno($borrowernumber);
122     $add_writeoff_sth->execute( $borrowernumber, $acct, $itemnum, $amount, $manager_id );
123
124     UpdateStats( $branch, 'writeoff', $amount, q{}, q{}, q{}, $borrowernumber );
125
126     return;
127 }
128
129 sub add_accounts_to_template {
130
131     my ( $total, undef, undef ) = GetMemberAccountRecords($borrowernumber);
132     my $accounts = [];
133     my @notify   = NumberNotifyId($borrowernumber);
134
135     my $notify_groups = [];
136     for my $notify_id (@notify) {
137         my ( $acct_total, $accountlines, undef ) =
138           GetBorNotifyAcctRecord( $borrowernumber, $notify_id );
139         if ( @{$accountlines} ) {
140             my $totalnotify = AmountNotify( $notify_id, $borrowernumber );
141             push @{$accounts},
142               { accountlines => $accountlines,
143                 notify       => $notify_id,
144                 total        => $totalnotify,
145               };
146         }
147     }
148     borrower_add_additional_fields($borrower);
149     $template->param(
150         accounts => $accounts,
151         borrower => $borrower,
152         total    => $total,
153     );
154     return;
155
156 }
157
158 sub get_for_redirect {
159     my ( $name, $name_in, $money ) = @_;
160     my $s     = q{&} . $name . q{=};
161     my $value = $input->param($name_in);
162     if ( !defined $value ) {
163         $value = ( $money == 1 ) ? 0 : q{};
164     }
165     if ($money) {
166         $s .= sprintf '%.2f', $value;
167     } else {
168         $s .= $value;
169     }
170     return $s;
171 }
172
173 sub redirect_to_paycollect {
174     my ( $action, $line_no ) = @_;
175     my $redirect =
176       "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
177     $redirect .= q{&};
178     $redirect .= "$action=1";
179     $redirect .= get_for_redirect( 'accounttype', "accounttype$line_no", 0 );
180     $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
181     $redirect .=
182       get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
183     $redirect .= get_for_redirect( 'accountno',    "accountno$line_no",    0 );
184     $redirect .= get_for_redirect( 'description',  "description$line_no",  0 );
185     $redirect .= get_for_redirect( 'title',        "title$line_no",        0 );
186     $redirect .= get_for_redirect( 'itemnumber',   "itemnumber$line_no",   0 );
187     $redirect .= get_for_redirect( 'notify_id',    "notify_id$line_no",    0 );
188     $redirect .= get_for_redirect( 'notify_level', "notify_level$line_no", 0 );
189     $redirect .= get_for_redirect( 'accountlines_id', "accountlines_id$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("amountoutstanding$value");
206             my $accountno = $input->param("accountno$value");
207             my $accountlines_id = $input->param("accountlines_id$value");
208             WriteOffFee( $borrowernumber, $accountlines_id, $itemno, $accounttype, $amount, $branch );
209         }
210     }
211
212     $borrowernumber = $input->param('borrowernumber');
213     print $input->redirect(
214         "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
215     return;
216 }
217
218 sub borrower_add_additional_fields {
219     my $b_ref = shift;
220
221 # some borrower info is not returned in the standard call despite being assumed
222 # in a number of templates. It should not be the business of this script but in lieu of
223 # a revised api here it is ...
224     if ( $b_ref->{category_type} eq 'C' ) {
225         my ( $catcodes, $labels ) =
226           GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
227         if ( @{$catcodes} ) {
228             if ( @{$catcodes} > 1 ) {
229                 $b_ref->{CATCODE_MULTI} = 1;
230             } elsif ( @{$catcodes} == 1 ) {
231                 $b_ref->{catcode} = $catcodes->[0];
232             }
233         }
234     } elsif ( $b_ref->{category_type} eq 'A' ) {
235         $b_ref->{adultborrower} = 1;
236     }
237     my ( $picture, $dberror ) = GetPatronImage( $b_ref->{cardnumber} );
238     if ($picture) {
239         $b_ref->{has_picture} = 1;
240     }
241
242     if (C4::Context->preference('ExtendedPatronAttributes')) {
243         $b_ref->{extendedattributes} = GetBorrowerAttributes($borrowernumber);
244         $template->param(
245             ExtendedPatronAttributes => 1,
246         );
247     }
248
249     $b_ref->{branchname} = GetBranchName( $b_ref->{branchcode} );
250     return;
251 }
252
253 sub payselected {
254     my @params = @_;
255     my $amt    = 0;
256     my @lines_to_pay;
257     foreach (@params) {
258         if (/^incl_par_(\d+)$/) {
259             my $index = $1;
260             push @lines_to_pay, $input->param("accountno$index");
261             $amt += $input->param("amountoutstanding$index");
262         }
263     }
264     $amt = '&amt=' . $amt;
265     my $sel = '&selected=' . join ',', @lines_to_pay;
266     my $redirect =
267         "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
268       . $amt
269       . $sel;
270
271     print $input->redirect($redirect);
272     return;
273 }
274
275 sub get_writeoff_sth {
276
277     # lets prepare these statement handles only once
278     if ($writeoff_sth) {
279         return;
280     } else {
281         my $dbh = C4::Context->dbh;
282
283         # Do we need to validate accounttype
284         my $sql = 'Update accountlines set amountoutstanding=0 '
285           . 'WHERE accountlines_id=?';
286         $writeoff_sth = $dbh->prepare($sql);
287         my $insert =
288 q{insert into accountlines (borrowernumber,accountno,itemnumber,date,amount,description,accounttype,manager_id)}
289           . q{values (?,?,?,now(),?,'Writeoff','W',?)};
290         $add_writeoff_sth = $dbh->prepare($insert);
291     }
292     return;
293 }