Bug 17260 - DBRev 16.12.00.011
[koha.git] / members / pay.pl
blob7e008d4f1a79390f13618a0db8f27ec4e28f8d09
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 BibLibre
5 # Copyright 2010,2011 PTFS-Europe Ltd
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 =head1 pay.pl
24 written 11/1/2000 by chris@katipo.oc.nz
25 part of the koha library system, script to facilitate paying off fines
27 =cut
29 use strict;
30 use warnings;
32 use URI::Escape;
33 use C4::Context;
34 use C4::Auth;
35 use C4::Output;
36 use CGI qw ( -utf8 );
37 use C4::Members;
38 use C4::Accounts;
39 use C4::Stats;
40 use C4::Koha;
41 use C4::Overdues;
42 use C4::Members::Attributes qw(GetBorrowerAttributes);
43 use Koha::Patron::Images;
45 use Koha::Patron::Categories;
46 use URI::Escape;
48 our $input = CGI->new;
50 my $updatecharges_permissions = $input->param('woall') ? 'writeoff' : 'remaining_permissions';
51 our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
52 { template_name => 'members/pay.tt',
53 query => $input,
54 type => 'intranet',
55 authnotrequired => 0,
56 flagsrequired => { borrowers => 1, updatecharges => $updatecharges_permissions },
57 debug => 1,
61 my @names = $input->param;
63 our $borrowernumber = $input->param('borrowernumber');
64 if ( !$borrowernumber ) {
65 $borrowernumber = $input->param('borrowernumber0');
68 # get borrower details
69 our $borrower = GetMember( borrowernumber => $borrowernumber );
70 our $user = $input->remote_user;
71 $user ||= q{};
73 our $branch = C4::Context->userenv->{'branch'};
75 my $writeoff_item = $input->param('confirm_writeoff');
76 my $paycollect = $input->param('paycollect');
77 if ($paycollect) {
78 print $input->redirect(
79 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber");
81 my $payselected = $input->param('payselected');
82 if ($payselected) {
83 payselected(@names);
86 my $writeoff_all = $input->param('woall'); # writeoff all fines
87 if ($writeoff_all) {
88 writeoff_all(@names);
89 } elsif ($writeoff_item) {
90 my $accountlines_id = $input->param('accountlines_id');
91 my $amount = $input->param('amountoutstanding');
92 my $payment_note = $input->param("payment_note");
94 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
96 amount => $amount,
97 lines => [Koha::Account::Lines->find($accountlines_id)],
98 type => 'writeoff',
99 note => $payment_note,
100 library_id => $branch,
105 for (@names) {
106 if (/^pay_indiv_(\d+)$/) {
107 my $line_no = $1;
108 redirect_to_paycollect( 'pay_individual', $line_no );
109 } elsif (/^wo_indiv_(\d+)$/) {
110 my $line_no = $1;
111 redirect_to_paycollect( 'writeoff_individual', $line_no );
115 $template->param(
116 finesview => 1,
117 activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
118 RoutingSerials => C4::Context->preference('RoutingSerials'),
121 add_accounts_to_template();
123 output_html_with_http_headers $input, $cookie, $template->output;
125 sub add_accounts_to_template {
127 my ( $total, undef, undef ) = GetMemberAccountRecords($borrowernumber);
128 my $accounts = [];
129 my @notify = NumberNotifyId($borrowernumber);
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,
144 borrower_add_additional_fields($borrower);
146 $template->param(%$borrower);
148 my $patron_image = Koha::Patron::Images->find($borrower->{borrowernumber});
149 $template->param( picture => 1 ) if $patron_image;
150 $template->param(
151 accounts => $accounts,
152 borrower => $borrower,
153 categoryname => $borrower->{'description'},
154 total => $total,
156 return;
160 sub get_for_redirect {
161 my ( $name, $name_in, $money ) = @_;
162 my $s = q{&} . $name . q{=};
163 my $value = uri_escape_utf8( $input->param($name_in) );
164 if ( !defined $value ) {
165 $value = ( $money == 1 ) ? 0 : q{};
167 if ($money) {
168 $s .= sprintf '%.2f', $value;
169 } else {
170 $s .= $value;
172 return $s;
175 sub redirect_to_paycollect {
176 my ( $action, $line_no ) = @_;
177 my $redirect =
178 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
179 $redirect .= q{&};
180 $redirect .= "$action=1";
181 $redirect .= get_for_redirect( 'accounttype', "accounttype$line_no", 0 );
182 $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
183 $redirect .=
184 get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
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 .= get_for_redirect( 'accountlines_id', "accountlines_id$line_no", 0 );
191 $redirect .= q{&} . 'payment_note' . q{=} . uri_escape_utf8( $input->param("payment_note_$line_no") );
192 $redirect .= '&remote_user=';
193 $redirect .= $user;
194 return print $input->redirect($redirect);
197 sub writeoff_all {
198 my @params = @_;
199 my @wo_lines = grep { /^accountlines_id\d+$/ } @params;
201 my $borrowernumber = $input->param('borrowernumber');
203 for (@wo_lines) {
204 if (/(\d+)/) {
205 my $value = $1;
206 my $amount = $input->param("amountoutstanding$value");
207 my $accountlines_id = $input->param("accountlines_id$value");
208 my $payment_note = $input->param("payment_note_$value");
209 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
211 amount => $amount,
212 lines => [ Koha::Account::Lines->find($accountlines_id) ],
213 type => 'writeoff',
214 note => $payment_note,
215 library_id => $branch,
221 print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
222 return;
225 sub borrower_add_additional_fields {
226 my $b_ref = shift;
228 # some borrower info is not returned in the standard call despite being assumed
229 # in a number of templates. It should not be the business of this script but in lieu of
230 # a revised api here it is ...
231 if ( $b_ref->{category_type} eq 'C' ) {
232 my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
233 $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
234 $template->param( 'catcode' => $patron_categories->next ) if $patron_categories->count == 1;
235 } elsif ( $b_ref->{category_type} eq 'A' ) {
236 $b_ref->{adultborrower} = 1;
239 if (C4::Context->preference('ExtendedPatronAttributes')) {
240 $b_ref->{extendedattributes} = GetBorrowerAttributes($borrowernumber);
241 $template->param(
242 ExtendedPatronAttributes => 1,
246 return;
249 sub payselected {
250 my @params = @_;
251 my $amt = 0;
252 my @lines_to_pay;
253 foreach (@params) {
254 if (/^incl_par_(\d+)$/) {
255 my $index = $1;
256 push @lines_to_pay, $input->param("accountlines_id$index");
257 $amt += $input->param("amountoutstanding$index");
260 $amt = '&amt=' . $amt;
261 my $sel = '&selected=' . join ',', @lines_to_pay;
262 my $notes = '&notes=' . join("%0A", map { $input->param("payment_note_$_") } @lines_to_pay );
263 my $redirect =
264 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
265 . $amt
266 . $sel
267 . $notes;
269 print $input->redirect($redirect);
270 return;