Bug 36233: Set select2 width to 100%
[koha.git] / members / pay.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 BibLibre
5 # Copyright 2010,2011 PTFS-Europe Ltd
6 #
7 # This file is part of Koha.
8 #
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.
13 #
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.
18 #
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>.
21
22 =head1 pay.pl
23
24  written 11/1/2000 by chris@katipo.oc.nz
25  part of the koha library system, script to facilitate paying off fines
26
27 =cut
28
29 use Modern::Perl;
30
31 use URI::Escape qw( uri_escape_utf8 uri_unescape );
32 use C4::Context;
33 use C4::Auth qw( get_template_and_user );
34 use C4::Output qw( output_and_exit_if_error output_and_exit output_html_with_http_headers );
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;
42 use Koha::Items;
43
44 use Koha::Patron::Categories;
45 use URI::Escape qw( uri_escape_utf8 uri_unescape );
46
47 our $input = CGI->new;
48
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',
52         query           => $input,
53         type            => 'intranet',
54         flagsrequired   => { borrowers => 'edit_borrowers', updatecharges => $updatecharges_permissions },
55     }
56 );
57
58 my @names = $input->param;
59
60 our $borrowernumber = $input->param('borrowernumber');
61 if ( !$borrowernumber ) {
62     $borrowernumber = $input->param('borrowernumber0');
63 }
64
65 my $payment_id = $input->param('payment_id');
66 our $change_given = $input->param('change_given');
67 our @renew_results = $input->multi_param('renew_result');
68
69 # get borrower details
70 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
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 } );
73
74 our $user = $input->remote_user;
75 $user ||= q{};
76
77 our $branch = C4::Context->userenv->{'branch'};
78
79 if ( $input->param('paycollect') ) {
80     output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
81     print $input->redirect(
82         "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber&change_given=$change_given");
83 }
84 elsif ( $input->param('payselected') ) {
85     output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
86     payselected({ params => \@names });
87 }
88 elsif ( $input->param('writeoff_selected') ) {
89     output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
90     payselected({ params => \@names, type => 'WRITEOFF' });
91 }
92 elsif ( $input->param('woall') ) {
93     output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
94     writeoff_all(@names);
95 }
96 elsif ( $input->param('apply_credits') ) {
97     output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
98     apply_credits({ patron => $patron, cgi => $input });
99 }
100 elsif ( $input->param('confirm_writeoff') ) {
101     output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
102     my $item_id         = $input->param('itemnumber');
103     my $accountlines_id = $input->param('accountlines_id');
104     my $amount          = $input->param('amountwrittenoff');
105     my $payment_note    = $input->param("payment_note");
106
107     my $accountline = Koha::Account::Lines->find( $accountlines_id );
108
109     $amount = $accountline->amountoutstanding if (abs($amount - $accountline->amountoutstanding) < 0.01) && C4::Context->preference('RoundFinesAtPayment');
110     if ( $amount > $accountline->amountoutstanding ) {
111         print $input->redirect( "/cgi-bin/koha/members/paycollect.pl?"
112               . "borrowernumber=$borrowernumber"
113               . "&amount=" . $accountline->amount
114               . "&amountoutstanding=" . $accountline->amountoutstanding
115               . "&debit_type_code=" . $accountline->debit_type_code
116               . "&accountlines_id=" . $accountlines_id
117               . "&change_given=" . $change_given
118               . "&writeoff_individual=1"
119               . "&error_over=1" );
120
121     } else {
122         $payment_id = Koha::Account->new( { patron_id => $borrowernumber } )->pay(
123             {
124                 amount     => $amount,
125                 lines      => [ Koha::Account::Lines->find($accountlines_id) ],
126                 type       => 'WRITEOFF',
127                 note       => $payment_note,
128                 interface  => C4::Context->interface,
129                 item_id    => $item_id,
130                 library_id => $branch,
131             }
132         )->{payment_id};
133     }
134 }
135
136 for (@names) {
137     if (/^pay_indiv_(\d+)$/) {
138         output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
139         my $line_no = $1;
140         redirect_to_paycollect( 'pay_individual', $line_no );
141     } elsif (/^wo_indiv_(\d+)$/) {
142         output_and_exit_if_error($input, $cookie, $template, { check => 'csrf_token' });
143         my $line_no = $1;
144         redirect_to_paycollect( 'writeoff_individual', $line_no );
145     }
146 }
147
148 # Populate an arrayref with everything we need to display any
149 # renew results that occurred based on what we were passed
150 my $renew_results_display = [];
151 foreach my $renew_result(@renew_results) {
152     my ($itemnumber, $success, $info) = split(/,/, $renew_result);
153     my $item = Koha::Items->find($itemnumber);
154     if ($success) {
155         $info = uri_unescape($info);
156     }
157     push @{$renew_results_display}, {
158         item => $item,
159         success => $success,
160         info => $info
161     };
162 }
163
164 $template->param(
165     finesview  => 1,
166     payment_id => $payment_id,
167     change_given => $change_given,
168     renew_results => $renew_results_display
169 );
170
171 add_accounts_to_template();
172
173 output_html_with_http_headers $input, $cookie, $template->output;
174
175 sub add_accounts_to_template {
176
177     my $patron = Koha::Patrons->find( $borrowernumber );
178     my $account = $patron->account;
179     my $outstanding_credits = $account->outstanding_credits;
180     my $account_lines = $account->outstanding_debits;
181     my $total = $account_lines->total_outstanding;
182     my @accounts;
183     while ( my $account_line = $account_lines->next ) {
184         push @accounts, $account_line;
185     }
186
187     $template->param(
188         patron   => $patron,
189         accounts => \@accounts,
190         total    => $total,
191         outstanding_credits => $outstanding_credits
192     );
193
194     return;
195
196 }
197
198 sub get_for_redirect {
199     my ( $name, $name_in, $money ) = @_;
200     my $s     = q{&} . $name . q{=};
201     my $value;
202     if (defined $input->param($name_in)) {
203         $value = uri_escape_utf8( scalar $input->param($name_in) );
204     }
205     if ( !defined $value ) {
206         $value = ( $money == 1 ) ? 0 : q{};
207     }
208     if ($money) {
209         $s .= sprintf '%.2f', $value;
210     } else {
211         $s .= $value;
212     }
213     return $s;
214 }
215
216 sub redirect_to_paycollect {
217     my ( $action, $line_no ) = @_;
218     my $redirect =
219       "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber";
220     $redirect .= q{&};
221     $redirect .= "$action=1";
222     $redirect .= get_for_redirect( 'debit_type_code', "debit_type_code$line_no", 0 );
223     $redirect .= get_for_redirect( 'amount', "amount$line_no", 1 );
224     $redirect .=
225       get_for_redirect( 'amountoutstanding', "amountoutstanding$line_no", 1 );
226     $redirect .= get_for_redirect( 'description', "description$line_no", 0 );
227     $redirect .= get_for_redirect( 'title', "title$line_no", 0 );
228     $redirect .= get_for_redirect( 'itemnumber',   "itemnumber$line_no",   0 );
229     $redirect .= get_for_redirect( 'accountlines_id', "accountlines_id$line_no", 0 );
230     $redirect .= q{&} . 'payment_note' . q{=} . uri_escape_utf8( scalar $input->param("payment_note_$line_no") );
231     $redirect .= '&remote_user=';
232     $redirect .= "change_given=$change_given";
233     $redirect .= $user;
234     return print $input->redirect($redirect);
235 }
236
237 sub writeoff_all {
238     my @params = @_;
239     my @wo_lines = grep { /^accountlines_id\d+$/ } @params;
240
241     my $borrowernumber = $input->param('borrowernumber');
242
243     for (@wo_lines) {
244         if (/(\d+)/) {
245             my $value           = $1;
246             my $amount          = $input->param("amountoutstanding$value");
247             my $accountlines_id = $input->param("accountlines_id$value");
248             my $payment_note    = $input->param("payment_note_$value");
249             Koha::Account->new( { patron_id => $borrowernumber } )->pay(
250                 {
251                     amount => $amount,
252                     lines  => [ Koha::Account::Lines->find($accountlines_id) ],
253                     type   => 'WRITEOFF',
254                     note   => $payment_note,
255                     interface  => C4::Context->interface,
256                     library_id => $branch,
257                 }
258             );
259         }
260     }
261
262     print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber");
263     return;
264 }
265
266 sub payselected {
267     my $parameters = shift;
268
269     my @params = @{ $parameters->{params} };
270     my $type = $parameters->{type} || 'PAYMENT';
271
272     my $amt    = 0;
273     my @lines_to_pay;
274     foreach (@params) {
275         if (/^incl_par_(\d+)$/) {
276             my $index = $1;
277             push @lines_to_pay, scalar $input->param("accountlines_id$index");
278             $amt += $input->param("amountoutstanding$index");
279         }
280     }
281     $amt = '&amt=' . $amt;
282     my $sel = '&selected=' . join ',', @lines_to_pay;
283     my $notes =
284         '&notes=' . uri_escape_utf8( join( "\n", map { scalar $input->param("payment_note_$_") } @lines_to_pay ) );
285     my $redirect =
286         "/cgi-bin/koha/members/paycollect.pl?borrowernumber=$borrowernumber"
287       . "&type=$type"
288       . $amt
289       . $sel
290       . $notes;
291
292     print $input->redirect($redirect);
293     return;
294 }
295
296 sub apply_credits {
297     my ($args) = @_;
298
299     my $patron = $args->{patron};
300     my $cgi    = $args->{cgi};
301
302     $patron->account->reconcile_balance();
303
304     print $cgi->redirect("/cgi-bin/koha/members/pay.pl?borrowernumber=" . $patron->borrowernumber );
305     return;
306 }