Bug 18289 - Fix t/Prices.t having a Test::DBIx::Class cache issue
[koha.git] / members / pay.pl
blob3400ee06676bf5eca1be2a421b404f9762c1e8df
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 => [ scalar 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 RoutingSerials => C4::Context->preference('RoutingSerials'),
120 add_accounts_to_template();
122 output_html_with_http_headers $input, $cookie, $template->output;
124 sub add_accounts_to_template {
126 my ( $total, undef, undef ) = GetMemberAccountRecords($borrowernumber);
127 my $accounts = [];
128 my @notify = NumberNotifyId($borrowernumber);
130 my $notify_groups = [];
131 for my $notify_id (@notify) {
132 my ( $acct_total, $accountlines, undef ) =
133 GetBorNotifyAcctRecord( $borrowernumber, $notify_id );
134 if ( @{$accountlines} ) {
135 my $totalnotify = AmountNotify( $notify_id, $borrowernumber );
136 push @{$accounts},
137 { accountlines => $accountlines,
138 notify => $notify_id,
139 total => $totalnotify,
143 borrower_add_additional_fields($borrower);
145 $template->param(%$borrower);
147 my $patron_image = Koha::Patron::Images->find($borrower->{borrowernumber});
148 $template->param( picture => 1 ) if $patron_image;
149 $template->param(
150 accounts => $accounts,
151 borrower => $borrower,
152 categoryname => $borrower->{'description'},
153 total => $total,
155 return;
159 sub get_for_redirect {
160 my ( $name, $name_in, $money ) = @_;
161 my $s = q{&} . $name . q{=};
162 my $value = uri_escape_utf8( $input->param($name_in) );
163 if ( !defined $value ) {
164 $value = ( $money == 1 ) ? 0 : q{};
166 if ($money) {
167 $s .= sprintf '%.2f', $value;
168 } else {
169 $s .= $value;
171 return $s;
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( '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 .= q{&} . 'payment_note' . q{=} . uri_escape_utf8( $input->param("payment_note_$line_no") );
191 $redirect .= '&remote_user=';
192 $redirect .= $user;
193 return print $input->redirect($redirect);
196 sub writeoff_all {
197 my @params = @_;
198 my @wo_lines = grep { /^accountlines_id\d+$/ } @params;
200 my $borrowernumber = $input->param('borrowernumber');
202 for (@wo_lines) {
203 if (/(\d+)/) {
204 my $value = $1;
205 my $amount = $input->param("amountoutstanding$value");
206 my $accountlines_id = $input->param("accountlines_id$value");
207 my $payment_note = $input->param("payment_note_$value");
208 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
210 amount => $amount,
211 lines => [ scalar Koha::Account::Lines->find($accountlines_id) ],
212 type => 'writeoff',
213 note => $payment_note,
214 library_id => $branch,
220 print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
221 return;
224 sub borrower_add_additional_fields {
225 my $b_ref = shift;
227 # some borrower info is not returned in the standard call despite being assumed
228 # in a number of templates. It should not be the business of this script but in lieu of
229 # a revised api here it is ...
230 if ( $b_ref->{category_type} eq 'C' ) {
231 my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
232 $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
233 $template->param( 'catcode' => $patron_categories->next ) if $patron_categories->count == 1;
234 } elsif ( $b_ref->{category_type} eq 'A' || $b_ref->{category_type} eq 'I' ) {
235 $b_ref->{adultborrower} = 1;
238 if (C4::Context->preference('ExtendedPatronAttributes')) {
239 $b_ref->{extendedattributes} = GetBorrowerAttributes($borrowernumber);
240 $template->param(
241 ExtendedPatronAttributes => 1,
245 return;
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("accountlines_id$index");
256 $amt += $input->param("amountoutstanding$index");
259 $amt = '&amt=' . $amt;
260 my $sel = '&selected=' . join ',', @lines_to_pay;
261 my $notes = '&notes=' . join("%0A", map { $input->param("payment_note_$_") } @lines_to_pay );
262 my $redirect =
263 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
264 . $amt
265 . $sel
266 . $notes;
268 print $input->redirect($redirect);
269 return;