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