Bug 18309: Fix INSERT sql query in unimarc_framework_DEFAULT.sql
[koha.git] / members / pay.pl
blob00e6f7b38656f4130465fa1235d4153cbe87327f
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');
68 # get borrower details
69 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
70 our $patron = Koha::Patrons->find($borrowernumber);
71 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
73 our $user = $input->remote_user;
74 $user ||= q{};
76 our $branch = C4::Context->userenv->{'branch'};
78 if ( $input->param('paycollect') ) {
79 print $input->redirect(
80 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber");
82 elsif ( $input->param('payselected') ) {
83 payselected({ params => \@names });
85 elsif ( $input->param('writeoff_selected') ) {
86 payselected({ params => \@names, type => 'writeoff' });
88 elsif ( $input->param('woall') ) {
89 writeoff_all(@names);
91 elsif ( $input->param('apply_credits') ) {
92 apply_credits({ patron => $patron, cgi => $input });
94 elsif ( $input->param('confirm_writeoff') ) {
95 my $accountlines_id = $input->param('accountlines_id');
96 my $amount = $input->param('amountwrittenoff');
97 my $payment_note = $input->param("payment_note");
99 my $accountline = Koha::Account::Lines->find( $accountlines_id );
101 if ( $amount > $accountline->amountoutstanding ) {
102 print $input->redirect( "/cgi-bin/koha/members/paycollect.pl?"
103 . "borrowernumber=$borrowernumber"
104 . "&amount=" . $accountline->amount
105 . "&amountoutstanding=" . $accountline->amountoutstanding
106 . "&accounttype=" . $accountline->accounttype
107 . "&accountlines_id=" . $accountlines_id
108 . "&writeoff_individual=1"
109 . "&error_over=1" );
111 } else {
112 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
114 amount => $amount,
115 lines => [ scalar Koha::Account::Lines->find($accountlines_id) ],
116 type => 'writeoff',
117 note => $payment_note,
118 interface => C4::Context->interface,
119 library_id => $branch,
125 for (@names) {
126 if (/^pay_indiv_(\d+)$/) {
127 my $line_no = $1;
128 redirect_to_paycollect( 'pay_individual', $line_no );
129 } elsif (/^wo_indiv_(\d+)$/) {
130 my $line_no = $1;
131 redirect_to_paycollect( 'writeoff_individual', $line_no );
135 $template->param(
136 finesview => 1,
137 payment_id => $payment_id,
140 add_accounts_to_template();
142 output_html_with_http_headers $input, $cookie, $template->output;
144 sub add_accounts_to_template {
146 my $patron = Koha::Patrons->find( $borrowernumber );
147 my $account = $patron->account;
148 my $outstanding_credits = $account->outstanding_credits;
149 my $account_lines = $account->outstanding_debits;
150 my $total = $account_lines->total_outstanding;
151 my @accounts;
152 while ( my $account_line = $account_lines->next ) {
153 push @accounts, $account_line;
156 $template->param(
157 patron => $patron,
158 accounts => \@accounts,
159 total => $total,
160 outstanding_credits => $outstanding_credits
163 return;
167 sub get_for_redirect {
168 my ( $name, $name_in, $money ) = @_;
169 my $s = q{&} . $name . q{=};
170 my $value;
171 if (defined $input->param($name_in)) {
172 $value = uri_escape_utf8( scalar $input->param($name_in) );
174 if ( !defined $value ) {
175 $value = ( $money == 1 ) ? 0 : q{};
177 if ($money) {
178 $s .= sprintf '%.2f', $value;
179 } else {
180 $s .= $value;
182 return $s;
185 sub redirect_to_paycollect {
186 my ( $action, $line_no ) = @_;
187 my $redirect =
188 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
189 $redirect .= q{&};
190 $redirect .= "$action=1";
191 $redirect .= get_for_redirect( 'accounttype', "accounttype$line_no", 0 );
192 $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
193 $redirect .=
194 get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
195 $redirect .= get_for_redirect( 'description', "description$line_no", 0 );
196 $redirect .= get_for_redirect( 'title', "title$line_no", 0 );
197 $redirect .= get_for_redirect( 'itemnumber', "itemnumber$line_no", 0 );
198 $redirect .= get_for_redirect( 'accountlines_id', "accountlines_id$line_no", 0 );
199 $redirect .= q{&} . 'payment_note' . q{=} . uri_escape_utf8( scalar $input->param("payment_note_$line_no") );
200 $redirect .= '&remote_user=';
201 $redirect .= $user;
202 return print $input->redirect($redirect);
205 sub writeoff_all {
206 my @params = @_;
207 my @wo_lines = grep { /^accountlines_id\d+$/ } @params;
209 my $borrowernumber = $input->param('borrowernumber');
211 for (@wo_lines) {
212 if (/(\d+)/) {
213 my $value = $1;
214 my $amount = $input->param("amountoutstanding$value");
215 my $accountlines_id = $input->param("accountlines_id$value");
216 my $payment_note = $input->param("payment_note_$value");
217 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
219 amount => $amount,
220 lines => [ scalar Koha::Account::Lines->find($accountlines_id) ],
221 type => 'writeoff',
222 note => $payment_note,
223 interface => C4::Context->interface,
224 library_id => $branch,
230 print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
231 return;
234 sub payselected {
235 my $parameters = shift;
237 my @params = @{ $parameters->{params} };
238 my $type = $parameters->{type} || 'payment';
240 my $amt = 0;
241 my @lines_to_pay;
242 foreach (@params) {
243 if (/^incl_par_(\d+)$/) {
244 my $index = $1;
245 push @lines_to_pay, scalar $input->param("accountlines_id$index");
246 $amt += $input->param("amountoutstanding$index");
249 $amt = '&amt=' . $amt;
250 my $sel = '&selected=' . join ',', @lines_to_pay;
251 my $notes = '&notes=' . join("%0A", map { scalar $input->param("payment_note_$_") } @lines_to_pay );
252 my $redirect =
253 "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
254 . "&type=$type"
255 . $amt
256 . $sel
257 . $notes;
259 print $input->redirect($redirect);
260 return;
263 sub apply_credits {
264 my ($args) = @_;
266 my $patron = $args->{patron};
267 my $cgi = $args->{cgi};
269 $patron->account->reconcile_balance();
271 print $cgi->redirect("/cgi-bin/koha/members/pay.pl?borrowernumber=" . $patron->borrowernumber );
272 return;