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);
43 use Koha::Patron::Images;
45 use Koha::Patron::Categories;
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',
56 flagsrequired => { borrowers => 'edit_borrowers', updatecharges => $updatecharges_permissions },
61 my @names = $input->param;
63 our $borrowernumber = $input->param('borrowernumber');
64 if ( !$borrowernumber ) {
65 $borrowernumber = $input->param('borrowernumber0');
68 # get borrower details
69 my $patron = Koha::Patrons->find( $borrowernumber );
71 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
74 my $category = $patron->category;
75 our $borrower = $patron->unblessed;
76 $borrower->{description} = $category->description;
77 $borrower->{category_type} = $category->category_type;
78 our $user = $input->remote_user;
81 our $branch = C4::Context->userenv->{'branch'};
83 my $writeoff_item = $input->param('confirm_writeoff');
84 my $paycollect = $input->param('paycollect');
86 print $input->redirect(
87 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber");
89 my $payselected = $input->param('payselected');
94 my $writeoff_all = $input->param('woall'); # writeoff all fines
97 } elsif ($writeoff_item) {
98 my $accountlines_id = $input->param('accountlines_id');
99 my $amount = $input->param('amountwrittenoff');
100 my $payment_note = $input->param("payment_note");
102 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
105 lines => [ scalar Koha::Account::Lines->find($accountlines_id) ],
107 note => $payment_note,
108 library_id => $branch,
114 if (/^pay_indiv_(\d+)$/) {
116 redirect_to_paycollect( 'pay_individual', $line_no );
117 } elsif (/^wo_indiv_(\d+)$/) {
119 redirect_to_paycollect( 'writeoff_individual', $line_no );
127 add_accounts_to_template();
129 output_html_with_http_headers $input, $cookie, $template->output;
131 sub add_accounts_to_template {
133 my ( $total, undef, undef ) = GetMemberAccountRecords($borrowernumber);
134 my $account_lines = Koha::Account::Lines->search({ borrowernumber => $borrowernumber, amountoutstanding => { '!=' => 0 } }, { order_by => ['accounttype'] });
136 while ( my $account_line = $account_lines->next ) {
137 $account_line = $account_line->unblessed;
138 if ( $account_line->{itemnumber} ) {
139 my $item = Koha::Items->find( $account_line->{itemnumber} );
140 my $biblio = $item->biblio;
141 $account_line->{biblionumber} = $biblio->biblionumber;
142 $account_line->{title} = $biblio->title;
144 push @accounts, $account_line;
146 borrower_add_additional_fields($borrower);
148 $template->param(%$borrower);
150 my $patron_image = Koha::Patron::Images->find($borrower->{borrowernumber});
151 $template->param( picture => 1 ) if $patron_image;
153 accounts => \@accounts,
154 borrower => $borrower,
155 categoryname => $borrower->{'description'},
162 sub get_for_redirect {
163 my ( $name, $name_in, $money ) = @_;
164 my $s = q{&} . $name . q{=};
166 if (defined $input->param($name_in)) {
167 $value = uri_escape_utf8( scalar $input->param($name_in) );
169 if ( !defined $value ) {
170 $value = ( $money == 1 ) ? 0 : q{};
173 $s .= sprintf '%.2f', $value;
180 sub redirect_to_paycollect {
181 my ( $action, $line_no ) = @_;
183 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
185 $redirect .= "$action=1";
186 $redirect .= get_for_redirect( 'accounttype', "accounttype$line_no", 0 );
187 $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
189 get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
190 $redirect .= get_for_redirect( 'description', "description$line_no", 0 );
191 $redirect .= get_for_redirect( 'title', "title$line_no", 0 );
192 $redirect .= get_for_redirect( 'itemnumber', "itemnumber$line_no", 0 );
193 $redirect .= get_for_redirect( 'accountlines_id', "accountlines_id$line_no", 0 );
194 $redirect .= q{&} . 'payment_note' . q{=} . uri_escape_utf8( scalar $input->param("payment_note_$line_no") );
195 $redirect .= '&remote_user=';
197 return print $input->redirect($redirect);
202 my @wo_lines = grep { /^accountlines_id\d+$/ } @params;
204 my $borrowernumber = $input->param('borrowernumber');
209 my $amount = $input->param("amountoutstanding$value");
210 my $accountlines_id = $input->param("accountlines_id$value");
211 my $payment_note = $input->param("payment_note_$value");
212 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
215 lines => [ scalar Koha::Account::Lines->find($accountlines_id) ],
217 note => $payment_note,
218 library_id => $branch,
224 print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
228 sub borrower_add_additional_fields {
231 # some borrower info is not returned in the standard call despite being assumed
232 # in a number of templates. It should not be the business of this script but in lieu of
233 # a revised api here it is ...
234 if ( $b_ref->{category_type} eq 'C' ) {
235 my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
236 $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
237 $template->param( 'catcode' => $patron_categories->next->categorycode ) if $patron_categories->count == 1;
238 } elsif ( $b_ref->{category_type} eq 'A' || $b_ref->{category_type} eq 'I' ) {
239 $b_ref->{adultborrower} = 1;
242 if (C4::Context->preference('ExtendedPatronAttributes')) {
243 $b_ref->{extendedattributes} = GetBorrowerAttributes($borrowernumber);
245 ExtendedPatronAttributes => 1,
257 if (/^incl_par_(\d+)$/) {
259 push @lines_to_pay, scalar $input->param("accountlines_id$index");
260 $amt += $input->param("amountoutstanding$index");
263 $amt = '&amt=' . $amt;
264 my $sel = '&selected=' . join ',', @lines_to_pay;
265 my $notes = '¬es=' . join("%0A", map { scalar $input->param("payment_note_$_") } @lines_to_pay );
267 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
272 print $input->redirect($redirect);