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
43 use Koha::Patron::Categories;
46 our $input = CGI->new;
48 my $updatecharges_permissions = $input->param('woall') ? 'writeoff' : 'remaining_permissions';
49 our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
50 { template_name => 'members/pay.tt',
54 flagsrequired => { borrowers => 'edit_borrowers', updatecharges => $updatecharges_permissions },
59 my @names = $input->param;
61 our $borrowernumber = $input->param('borrowernumber');
62 if ( !$borrowernumber ) {
63 $borrowernumber = $input->param('borrowernumber0');
66 my $payment_id = $input->param('payment_id');
67 our $change_given = $input->param('change_given');
69 # get borrower details
70 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
71 our $patron = Koha::Patrons->find($borrowernumber);
72 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
74 our $user = $input->remote_user;
77 our $branch = C4::Context->userenv->{'branch'};
79 if ( $input->param('paycollect') ) {
80 print $input->redirect(
81 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber&change_given=$change_given");
83 elsif ( $input->param('payselected') ) {
84 payselected({ params => \@names });
86 elsif ( $input->param('writeoff_selected') ) {
87 payselected({ params => \@names, type => 'WRITEOFF' });
89 elsif ( $input->param('woall') ) {
92 elsif ( $input->param('apply_credits') ) {
93 apply_credits({ patron => $patron, cgi => $input });
95 elsif ( $input->param('confirm_writeoff') ) {
96 my $accountlines_id = $input->param('accountlines_id');
97 my $amount = $input->param('amountwrittenoff');
98 my $payment_note = $input->param("payment_note");
100 my $accountline = Koha::Account::Lines->find( $accountlines_id );
102 $amount = $accountline->amountoutstanding if (abs($amount - $accountline->amountoutstanding) < 0.01) && C4::Context->preference('RoundFinesAtPayment');
103 if ( $amount > $accountline->amountoutstanding ) {
104 print $input->redirect( "/cgi-bin/koha/members/paycollect.pl?"
105 . "borrowernumber=$borrowernumber"
106 . "&amount=" . $accountline->amount
107 . "&amountoutstanding=" . $accountline->amountoutstanding
108 . "&debit_type_code=" . $accountline->debit_type_code
109 . "&accountlines_id=" . $accountlines_id
110 . "&change_given=" . $change_given
111 . "&writeoff_individual=1"
115 $payment_id = Koha::Account->new( { patron_id => $borrowernumber } )->pay(
118 lines => [ Koha::Account::Lines->find($accountlines_id) ],
120 note => $payment_note,
121 interface => C4::Context->interface,
122 library_id => $branch,
129 if (/^pay_indiv_(\d+)$/) {
131 redirect_to_paycollect( 'pay_individual', $line_no );
132 } elsif (/^wo_indiv_(\d+)$/) {
134 redirect_to_paycollect( 'writeoff_individual', $line_no );
140 payment_id => $payment_id,
141 change_given => $change_given,
144 add_accounts_to_template();
146 output_html_with_http_headers $input, $cookie, $template->output;
148 sub add_accounts_to_template {
150 my $patron = Koha::Patrons->find( $borrowernumber );
151 my $account = $patron->account;
152 my $outstanding_credits = $account->outstanding_credits;
153 my $account_lines = $account->outstanding_debits;
154 my $total = $account_lines->total_outstanding;
156 while ( my $account_line = $account_lines->next ) {
157 push @accounts, $account_line;
162 accounts => \@accounts,
164 outstanding_credits => $outstanding_credits
171 sub get_for_redirect {
172 my ( $name, $name_in, $money ) = @_;
173 my $s = q{&} . $name . q{=};
175 if (defined $input->param($name_in)) {
176 $value = uri_escape_utf8( scalar $input->param($name_in) );
178 if ( !defined $value ) {
179 $value = ( $money == 1 ) ? 0 : q{};
182 $s .= sprintf '%.2f', $value;
189 sub redirect_to_paycollect {
190 my ( $action, $line_no ) = @_;
192 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
194 $redirect .= "$action=1";
195 $redirect .= get_for_redirect( 'debit_type_code', "debit_type_code$line_no", 0 );
196 $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
198 get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
199 $redirect .= get_for_redirect( 'description', "description$line_no", 0 );
200 $redirect .= get_for_redirect( 'title', "title$line_no", 0 );
201 $redirect .= get_for_redirect( 'itemnumber', "itemnumber$line_no", 0 );
202 $redirect .= get_for_redirect( 'accountlines_id', "accountlines_id$line_no", 0 );
203 $redirect .= q{&} . 'payment_note' . q{=} . uri_escape_utf8( scalar $input->param("payment_note_$line_no") );
204 $redirect .= '&remote_user=';
205 $redirect .= "change_given=$change_given";
207 return print $input->redirect($redirect);
212 my @wo_lines = grep { /^accountlines_id\d+$/ } @params;
214 my $borrowernumber = $input->param('borrowernumber');
219 my $amount = $input->param("amountoutstanding$value");
220 my $accountlines_id = $input->param("accountlines_id$value");
221 my $payment_note = $input->param("payment_note_$value");
222 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
225 lines => [ Koha::Account::Lines->find($accountlines_id) ],
227 note => $payment_note,
228 interface => C4::Context->interface,
229 library_id => $branch,
235 print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
240 my $parameters = shift;
242 my @params = @{ $parameters->{params} };
243 my $type = $parameters->{type} || 'PAYMENT';
248 if (/^incl_par_(\d+)$/) {
250 push @lines_to_pay, scalar $input->param("accountlines_id$index");
251 $amt += $input->param("amountoutstanding$index");
254 $amt = '&amt=' . $amt;
255 my $sel = '&selected=' . join ',', @lines_to_pay;
256 my $notes = '¬es=' . join("%0A", map { scalar $input->param("payment_note_$_") } @lines_to_pay );
258 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
264 print $input->redirect($redirect);
271 my $patron = $args->{patron};
272 my $cgi = $args->{cgi};
274 $patron->account->reconcile_balance();
276 print $cgi->redirect("/cgi-bin/koha/members/pay.pl?borrowernumber=" . $patron->borrowernumber );