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>.
24 written 11/1/2000 by chris@katipo.oc.nz
25 part of the koha library system, script to facilitate paying off fines
41 use C4
::Members
::Attributes
qw(GetBorrowerAttributes);
44 use Koha
::Patron
::Categories
;
47 our $input = CGI
->new;
49 my $updatecharges_permissions = $input->param('woall') ?
'writeoff' : 'remaining_permissions';
50 our ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
51 { template_name
=> 'members/pay.tt',
55 flagsrequired
=> { borrowers
=> 'edit_borrowers', updatecharges
=> $updatecharges_permissions },
60 my @names = $input->param;
62 our $borrowernumber = $input->param('borrowernumber');
63 if ( !$borrowernumber ) {
64 $borrowernumber = $input->param('borrowernumber0');
67 # get borrower details
68 my $logged_in_user = Koha
::Patrons
->find( $loggedinuser ) or die "Not logged in";
69 our $patron = Koha
::Patrons
->find($borrowernumber);
70 output_and_exit_if_error
( $input, $cookie, $template, { module
=> 'members', logged_in_user
=> $logged_in_user, current_patron
=> $patron } );
72 our $user = $input->remote_user;
75 our $branch = C4
::Context
->userenv->{'branch'};
77 my $writeoff_item = $input->param('confirm_writeoff');
78 my $paycollect = $input->param('paycollect');
80 print $input->redirect(
81 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber");
83 my $payselected = $input->param('payselected');
88 my $writeoff_all = $input->param('woall'); # writeoff all fines
91 } elsif ($writeoff_item) {
92 my $accountlines_id = $input->param('accountlines_id');
93 my $amount = $input->param('amountwrittenoff');
94 my $payment_note = $input->param("payment_note");
96 my $accountline = Koha
::Account
::Lines
->find( $accountlines_id );
98 if ( $amount > $accountline->amountoutstanding ) {
99 print $input->redirect( "/cgi-bin/koha/members/paycollect.pl?"
100 . "borrowernumber=$borrowernumber"
101 . "&amount=" . $accountline->amount
102 . "&amountoutstanding=" . $accountline->amountoutstanding
103 . "&accounttype=" . $accountline->accounttype
104 . "&accountlines_id=" . $accountlines_id
105 . "&writeoff_individual=1"
109 Koha
::Account
->new( { patron_id
=> $borrowernumber } )->pay(
112 lines
=> [ scalar Koha
::Account
::Lines
->find($accountlines_id) ],
114 note
=> $payment_note,
115 library_id
=> $branch,
122 if (/^pay_indiv_(\d+)$/) {
124 redirect_to_paycollect
( 'pay_individual', $line_no );
125 } elsif (/^wo_indiv_(\d+)$/) {
127 redirect_to_paycollect
( 'writeoff_individual', $line_no );
135 add_accounts_to_template
();
137 output_html_with_http_headers
$input, $cookie, $template->output;
139 sub add_accounts_to_template
{
141 my $patron = Koha
::Patrons
->find( $borrowernumber );
142 my $total = $patron->account->balance;
143 my $account_lines = Koha
::Account
::Lines
->search({ borrowernumber
=> $borrowernumber, amountoutstanding
=> { '!=' => 0 } }, { order_by
=> ['accounttype'] });
145 while ( my $account_line = $account_lines->next ) {
146 $account_line = $account_line->unblessed;
147 if ( $account_line->{itemnumber
} ) {
148 my $item = Koha
::Items
->find( $account_line->{itemnumber
} );
149 my $biblio = $item->biblio;
150 $account_line->{biblionumber
} = $biblio->biblionumber;
151 $account_line->{title
} = $biblio->title;
153 push @accounts, $account_line;
155 borrower_add_additional_fields
($patron->unblessed);
159 accounts
=> \
@accounts,
166 sub get_for_redirect
{
167 my ( $name, $name_in, $money ) = @_;
168 my $s = q{&} . $name . q{=};
170 if (defined $input->param($name_in)) {
171 $value = uri_escape_utf8
( scalar $input->param($name_in) );
173 if ( !defined $value ) {
174 $value = ( $money == 1 ) ?
0 : q{};
177 $s .= sprintf '%.2f', $value;
184 sub redirect_to_paycollect
{
185 my ( $action, $line_no ) = @_;
187 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
189 $redirect .= "$action=1";
190 $redirect .= get_for_redirect
( 'accounttype', "accounttype$line_no", 0 );
191 $redirect .= get_for_redirect
( 'amount', "amount$line_no", 1 );
193 get_for_redirect
( 'amountoutstanding', "amountoutstanding$line_no", 1 );
194 $redirect .= get_for_redirect
( 'description', "description$line_no", 0 );
195 $redirect .= get_for_redirect
( 'title', "title$line_no", 0 );
196 $redirect .= get_for_redirect
( 'itemnumber', "itemnumber$line_no", 0 );
197 $redirect .= get_for_redirect
( 'accountlines_id', "accountlines_id$line_no", 0 );
198 $redirect .= q{&} . 'payment_note' . q{=} . uri_escape_utf8
( scalar $input->param("payment_note_$line_no") );
199 $redirect .= '&remote_user=';
201 return print $input->redirect($redirect);
206 my @wo_lines = grep { /^accountlines_id\d+$/ } @params;
208 my $borrowernumber = $input->param('borrowernumber');
213 my $amount = $input->param("amountoutstanding$value");
214 my $accountlines_id = $input->param("accountlines_id$value");
215 my $payment_note = $input->param("payment_note_$value");
216 Koha
::Account
->new( { patron_id
=> $borrowernumber } )->pay(
219 lines
=> [ scalar Koha
::Account
::Lines
->find($accountlines_id) ],
221 note
=> $payment_note,
222 library_id
=> $branch,
228 print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
232 sub borrower_add_additional_fields
{
235 # some borrower info is not returned in the standard call despite being assumed
236 # in a number of templates. It should not be the business of this script but in lieu of
237 # a revised api here it is ...
238 if ( $b_ref->{category_type
} eq 'C' ) {
239 my $patron_categories = Koha
::Patron
::Categories
->search_limited({ category_type
=> 'A' }, {order_by
=> ['categorycode']});
240 $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
241 $template->param( 'catcode' => $patron_categories->next->categorycode ) if $patron_categories->count == 1;
244 if (C4
::Context
->preference('ExtendedPatronAttributes')) {
245 $b_ref->{extendedattributes
} = GetBorrowerAttributes
($borrowernumber);
247 ExtendedPatronAttributes
=> 1,
259 if (/^incl_par_(\d+)$/) {
261 push @lines_to_pay, scalar $input->param("accountlines_id$index");
262 $amt += $input->param("amountoutstanding$index");
265 $amt = '&amt=' . $amt;
266 my $sel = '&selected=' . join ',', @lines_to_pay;
267 my $notes = '¬es=' . join("%0A", map { scalar $input->param("payment_note_$_") } @lines_to_pay );
269 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
274 print $input->redirect($redirect);