Bug 23731: Give staff detail/OPAC detail option to display LOC classification
[koha.git] / members / pay.pl
bloba490e58e42b4b82de569510e7418aef55237e5b1
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 Modern::Perl;
31 use URI::Escape;
32 use C4::Context;
33 use C4::Auth;
34 use C4::Output;
35 use CGI qw ( -utf8 );
36 use C4::Members;
37 use C4::Accounts;
38 use C4::Stats;
39 use C4::Koha;
40 use C4::Overdues;
41 use Koha::Patrons;
43 use Koha::Patron::Categories;
44 use URI::Escape;
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',
51 query => $input,
52 type => 'intranet',
53 authnotrequired => 0,
54 flagsrequired => { borrowers => 'edit_borrowers', updatecharges => $updatecharges_permissions },
55 debug => 1,
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;
75 $user ||= q{};
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') ) {
90 writeoff_all(@names);
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 if ( $amount > $accountline->amountoutstanding ) {
103 print $input->redirect( "/cgi-bin/koha/members/paycollect.pl?"
104 . "borrowernumber=$borrowernumber"
105 . "&amount=" . $accountline->amount
106 . "&amountoutstanding=" . $accountline->amountoutstanding
107 . "&accounttype=" . $accountline->accounttype
108 . "&accountlines_id=" . $accountlines_id
109 . "&change_given=" . $change_given
110 . "&writeoff_individual=1"
111 . "&error_over=1" );
113 } else {
114 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
116 amount => $amount,
117 lines => [ scalar Koha::Account::Lines->find($accountlines_id) ],
118 type => 'writeoff',
119 note => $payment_note,
120 interface => C4::Context->interface,
121 library_id => $branch,
127 for (@names) {
128 if (/^pay_indiv_(\d+)$/) {
129 my $line_no = $1;
130 redirect_to_paycollect( 'pay_individual', $line_no );
131 } elsif (/^wo_indiv_(\d+)$/) {
132 my $line_no = $1;
133 redirect_to_paycollect( 'writeoff_individual', $line_no );
137 $template->param(
138 finesview => 1,
139 payment_id => $payment_id,
140 change_given => $change_given,
143 add_accounts_to_template();
145 output_html_with_http_headers $input, $cookie, $template->output;
147 sub add_accounts_to_template {
149 my $patron = Koha::Patrons->find( $borrowernumber );
150 my $account = $patron->account;
151 my $outstanding_credits = $account->outstanding_credits;
152 my $account_lines = $account->outstanding_debits;
153 my $total = $account_lines->total_outstanding;
154 my @accounts;
155 while ( my $account_line = $account_lines->next ) {
156 push @accounts, $account_line;
159 $template->param(
160 patron => $patron,
161 accounts => \@accounts,
162 total => $total,
163 outstanding_credits => $outstanding_credits
166 return;
170 sub get_for_redirect {
171 my ( $name, $name_in, $money ) = @_;
172 my $s = q{&} . $name . q{=};
173 my $value;
174 if (defined $input->param($name_in)) {
175 $value = uri_escape_utf8( scalar $input->param($name_in) );
177 if ( !defined $value ) {
178 $value = ( $money == 1 ) ? 0 : q{};
180 if ($money) {
181 $s .= sprintf '%.2f', $value;
182 } else {
183 $s .= $value;
185 return $s;
188 sub redirect_to_paycollect {
189 my ( $action, $line_no ) = @_;
190 my $redirect =
191 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
192 $redirect .= q{&};
193 $redirect .= "$action=1";
194 $redirect .= get_for_redirect( 'accounttype', "accounttype$line_no", 0 );
195 $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
196 $redirect .=
197 get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
198 $redirect .= get_for_redirect( 'description', "description$line_no", 0 );
199 $redirect .= get_for_redirect( 'title', "title$line_no", 0 );
200 $redirect .= get_for_redirect( 'itemnumber', "itemnumber$line_no", 0 );
201 $redirect .= get_for_redirect( 'accountlines_id', "accountlines_id$line_no", 0 );
202 $redirect .= q{&} . 'payment_note' . q{=} . uri_escape_utf8( scalar $input->param("payment_note_$line_no") );
203 $redirect .= '&remote_user=';
204 $redirect .= "change_given=$change_given";
205 $redirect .= $user;
206 return print $input->redirect($redirect);
209 sub writeoff_all {
210 my @params = @_;
211 my @wo_lines = grep { /^accountlines_id\d+$/ } @params;
213 my $borrowernumber = $input->param('borrowernumber');
215 for (@wo_lines) {
216 if (/(\d+)/) {
217 my $value = $1;
218 my $amount = $input->param("amountoutstanding$value");
219 my $accountlines_id = $input->param("accountlines_id$value");
220 my $payment_note = $input->param("payment_note_$value");
221 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
223 amount => $amount,
224 lines => [ scalar Koha::Account::Lines->find($accountlines_id) ],
225 type => 'writeoff',
226 note => $payment_note,
227 interface => C4::Context->interface,
228 library_id => $branch,
234 print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
235 return;
238 sub payselected {
239 my $parameters = shift;
241 my @params = @{ $parameters->{params} };
242 my $type = $parameters->{type} || 'payment';
244 my $amt = 0;
245 my @lines_to_pay;
246 foreach (@params) {
247 if (/^incl_par_(\d+)$/) {
248 my $index = $1;
249 push @lines_to_pay, scalar $input->param("accountlines_id$index");
250 $amt += $input->param("amountoutstanding$index");
253 $amt = '&amt=' . $amt;
254 my $sel = '&selected=' . join ',', @lines_to_pay;
255 my $notes = '&notes=' . join("%0A", map { scalar $input->param("payment_note_$_") } @lines_to_pay );
256 my $redirect =
257 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
258 . "&type=$type"
259 . $amt
260 . $sel
261 . $notes;
263 print $input->redirect($redirect);
264 return;
267 sub apply_credits {
268 my ($args) = @_;
270 my $patron = $args->{patron};
271 my $cgi = $args->{cgi};
273 $patron->account->reconcile_balance();
275 print $cgi->redirect("/cgi-bin/koha/members/pay.pl?borrowernumber=" . $patron->borrowernumber );
276 return;