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