Bug 17894 - Update pay() and use it internally for WriteOffFee
[koha.git] / members / pay.pl
blobbfca78b80eb72cf9fedeebdc8f2a5f23c3433444
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 $itemno = $input->param('itemnumber');
92 my $account_type = $input->param('accounttype');
93 my $amount = $input->param('amountoutstanding');
94 my $payment_note = $input->param("payment_note");
95 WriteOffFee( $borrowernumber, $accountlines_id, $itemno, $account_type, $amount, $branch, $payment_note );
98 for (@names) {
99 if (/^pay_indiv_(\d+)$/) {
100 my $line_no = $1;
101 redirect_to_paycollect( 'pay_individual', $line_no );
102 } elsif (/^wo_indiv_(\d+)$/) {
103 my $line_no = $1;
104 redirect_to_paycollect( 'writeoff_individual', $line_no );
108 $template->param(
109 finesview => 1,
110 activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
111 RoutingSerials => C4::Context->preference('RoutingSerials'),
114 add_accounts_to_template();
116 output_html_with_http_headers $input, $cookie, $template->output;
118 sub add_accounts_to_template {
120 my ( $total, undef, undef ) = GetMemberAccountRecords($borrowernumber);
121 my $accounts = [];
122 my @notify = NumberNotifyId($borrowernumber);
124 my $notify_groups = [];
125 for my $notify_id (@notify) {
126 my ( $acct_total, $accountlines, undef ) =
127 GetBorNotifyAcctRecord( $borrowernumber, $notify_id );
128 if ( @{$accountlines} ) {
129 my $totalnotify = AmountNotify( $notify_id, $borrowernumber );
130 push @{$accounts},
131 { accountlines => $accountlines,
132 notify => $notify_id,
133 total => $totalnotify,
137 borrower_add_additional_fields($borrower);
139 $template->param(%$borrower);
141 my $patron_image = Koha::Patron::Images->find($borrower->{borrowernumber});
142 $template->param( picture => 1 ) if $patron_image;
143 $template->param(
144 accounts => $accounts,
145 borrower => $borrower,
146 categoryname => $borrower->{'description'},
147 total => $total,
149 return;
153 sub get_for_redirect {
154 my ( $name, $name_in, $money ) = @_;
155 my $s = q{&} . $name . q{=};
156 my $value = $input->param($name_in);
157 if ( !defined $value ) {
158 $value = ( $money == 1 ) ? 0 : q{};
160 if ($money) {
161 $s .= sprintf '%.2f', $value;
162 } else {
163 $s .= $value;
165 return $s;
168 sub redirect_to_paycollect {
169 my ( $action, $line_no ) = @_;
170 my $redirect =
171 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
172 $redirect .= q{&};
173 $redirect .= "$action=1";
174 $redirect .= get_for_redirect( 'accounttype', "accounttype$line_no", 0 );
175 $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
176 $redirect .=
177 get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
178 $redirect .= uri_escape_utf8( get_for_redirect( 'description', "description$line_no", 0 ) );
179 $redirect .= uri_escape_utf8( get_for_redirect( 'title', "title$line_no", 0 ) );
180 $redirect .= get_for_redirect( 'itemnumber', "itemnumber$line_no", 0 );
181 $redirect .= get_for_redirect( 'notify_id', "notify_id$line_no", 0 );
182 $redirect .= get_for_redirect( 'notify_level', "notify_level$line_no", 0 );
183 $redirect .= get_for_redirect( 'accountlines_id', "accountlines_id$line_no", 0 );
184 $redirect .= q{&} . 'payment_note' . q{=} . uri_escape_utf8( $input->param("payment_note_$line_no") );
185 $redirect .= '&remote_user=';
186 $redirect .= $user;
187 return print $input->redirect($redirect);
190 sub writeoff_all {
191 my @params = @_;
192 my @wo_lines = grep { /^accountlines_id\d+$/ } @params;
193 for (@wo_lines) {
194 if (/(\d+)/) {
195 my $value = $1;
196 my $accounttype = $input->param("accounttype$value");
198 # my $borrowernum = $input->param("borrowernumber$value");
199 my $itemno = $input->param("itemnumber$value");
200 my $amount = $input->param("amountoutstanding$value");
201 my $accountlines_id = $input->param("accountlines_id$value");
202 my $payment_note = $input->param("payment_note_$value");
203 WriteOffFee( $borrowernumber, $accountlines_id, $itemno, $accounttype, $amount, $branch, $payment_note );
207 $borrowernumber = $input->param('borrowernumber');
208 print $input->redirect(
209 "/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
210 return;
213 sub borrower_add_additional_fields {
214 my $b_ref = shift;
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 $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
221 $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
222 $template->param( 'catcode' => $patron_categories->next ) if $patron_categories->count == 1;
223 } elsif ( $b_ref->{category_type} eq 'A' ) {
224 $b_ref->{adultborrower} = 1;
227 if (C4::Context->preference('ExtendedPatronAttributes')) {
228 $b_ref->{extendedattributes} = GetBorrowerAttributes($borrowernumber);
229 $template->param(
230 ExtendedPatronAttributes => 1,
234 return;
237 sub payselected {
238 my @params = @_;
239 my $amt = 0;
240 my @lines_to_pay;
241 foreach (@params) {
242 if (/^incl_par_(\d+)$/) {
243 my $index = $1;
244 push @lines_to_pay, $input->param("accountlines_id$index");
245 $amt += $input->param("amountoutstanding$index");
248 $amt = '&amt=' . $amt;
249 my $sel = '&selected=' . join ',', @lines_to_pay;
250 my $notes = '&notes=' . join("%0A", map { $input->param("payment_note_$_") } @lines_to_pay );
251 my $redirect =
252 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
253 . $amt
254 . $sel
255 . $notes;
257 print $input->redirect($redirect);
258 return;