Bug 24412: (follow-up) prevent request.pl from failing
[koha.git] / members / paycollect.pl
blob7fb55737e7f8598c00f1be84ecd3c615f3fc25f8
1 #!/usr/bin/perl
2 # Copyright 2009,2010 PTFS Inc.
3 # Copyright 2011 PTFS-Europe Ltd
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
21 use URI::Escape;
22 use CGI qw ( -utf8 );
24 use C4::Context;
25 use C4::Auth;
26 use C4::Output;
27 use C4::Members;
28 use C4::Accounts;
29 use C4::Koha;
31 use Koha::Cash::Registers;
32 use Koha::Patrons;
33 use Koha::Patron::Categories;
34 use Koha::AuthorisedValues;
35 use Koha::Account;
36 use Koha::Token;
37 use Koha::DateUtils;
39 my $input = CGI->new();
41 my $payment_id = $input->param('payment_id');
42 my $writeoff_individual = $input->param('writeoff_individual');
43 my $change_given = $input->param('change_given');
44 my $type = scalar $input->param('type') || 'PAYMENT';
46 my $updatecharges_permissions = ($writeoff_individual || $type eq 'WRITEOFF') ? 'writeoff' : 'remaining_permissions';
47 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
48 { template_name => 'members/paycollect.tt',
49 query => $input,
50 type => 'intranet',
51 flagsrequired => { borrowers => 'edit_borrowers', updatecharges => $updatecharges_permissions },
52 debug => 1,
56 # get borrower details
57 my $borrowernumber = $input->param('borrowernumber');
58 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
59 my $patron = Koha::Patrons->find( $borrowernumber );
60 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
62 my $borrower = $patron->unblessed;
63 my $account = $patron->account;
64 my $category = $patron->category;
65 my $user = $input->remote_user;
67 my $library_id = C4::Context->userenv->{'branch'};
68 my $total_due = $account->outstanding_debits->total_outstanding;
70 my $total_paid = $input->param('paid');
71 my $total_collected = $input->param('collected');
73 my $selected_lines = $input->param('selected'); # comes from pay.pl
74 my $pay_individual = $input->param('pay_individual');
75 my $selected_accts = $input->param('selected_accts'); # comes from paycollect.pl
76 my $payment_note = uri_unescape scalar $input->param('payment_note');
77 my $payment_type = scalar $input->param('payment_type');
78 my $accountlines_id;
80 my $cash_register_id = $input->param('cash_register');
81 if ( $pay_individual || $writeoff_individual ) {
82 if ($pay_individual) {
83 $template->param( pay_individual => 1 );
84 } elsif ($writeoff_individual) {
85 $template->param( writeoff_individual => 1 );
87 my $debit_type_code = $input->param('debit_type_code');
88 $accountlines_id = $input->param('accountlines_id');
89 my $amount = $input->param('amount');
90 my $amountoutstanding = $input->param('amountoutstanding');
91 my $itemnumber = $input->param('itemnumber');
92 my $description = $input->param('description');
93 my $title = $input->param('title');
94 $total_due = $amountoutstanding;
95 $template->param(
96 debit_type_code => $debit_type_code,
97 accountlines_id => $accountlines_id,
98 amount => $amount,
99 amountoutstanding => $amountoutstanding,
100 title => $title,
101 itemnumber => $itemnumber,
102 individual_description => $description,
103 payment_note => $payment_note,
105 } elsif ($selected_lines) {
106 $total_due = $input->param('amt');
107 $template->param(
108 selected_accts => $selected_lines,
109 amt => $total_due,
110 selected_accts_notes => scalar $input->param('notes'),
114 my @selected_accountlines;
115 if ( $selected_accts ) {
116 if ( $selected_accts =~ /^([\d,]*).*/ ) {
117 $selected_accts = $1; # ensure passing no junk
119 my @acc = split /,/, $selected_accts;
121 my $search_params = {
122 borrowernumber => $borrowernumber,
123 amountoutstanding => { '<>' => 0 },
124 accountlines_id => { 'in' => \@acc },
127 @selected_accountlines = Koha::Account::Lines->search(
128 $search_params,
129 { order_by => 'date' }
132 my $sum = Koha::Account::Lines->search(
133 $search_params,
135 select => [ { sum => 'amountoutstanding' } ],
136 as => [ 'total_amountoutstanding'],
139 $total_due = $sum->_resultset->first->get_column('total_amountoutstanding');
142 if ( $total_paid and $total_paid ne '0.00' ) {
143 $total_paid = $total_due if (abs($total_paid - $total_due) < 0.01) && C4::Context->preference('RoundFinesAtPayment');
144 if ( $total_paid < 0 or $total_paid > $total_due ) {
145 $template->param(
146 error_over => 1,
147 total_due => $total_due
149 } elsif ( $total_collected < $total_paid && !( $writeoff_individual || $type eq 'WRITEOFF' ) ) {
150 $template->param(
151 error_under => 1,
152 total_paid => $total_paid
154 } else {
155 output_and_exit( $input, $cookie, $template, 'wrong_csrf_token' )
156 unless Koha::Token->new->check_csrf( {
157 session_id => $input->cookie('CGISESSID'),
158 token => scalar $input->param('csrf_token'),
161 my $url;
162 my $pay_result;
163 if ($pay_individual) {
164 my $line = Koha::Account::Lines->find($accountlines_id);
165 $pay_result = $account->pay(
167 lines => [$line],
168 amount => $total_paid,
169 library_id => $library_id,
170 note => $payment_note,
171 interface => C4::Context->interface,
172 payment_type => $payment_type,
173 cash_register => $cash_register_id
176 $payment_id = $pay_result->{payment_id};
178 $url = "/cgi-bin/koha/members/pay.pl";
179 } else {
180 if ($selected_accts) {
181 if ( $total_paid > $total_due ) {
182 $template->param(
183 error_over => 1,
184 total_due => $total_due
186 } else {
187 my $note = $input->param('selected_accts_notes');
189 $pay_result = $account->pay(
191 type => $type,
192 amount => $total_paid,
193 library_id => $library_id,
194 lines => \@selected_accountlines,
195 note => $note,
196 interface => C4::Context->interface,
197 payment_type => $payment_type,
198 cash_register => $cash_register_id
202 $payment_id = $pay_result->{payment_id};
204 else {
205 my $note = $input->param('selected_accts_notes');
206 $pay_result = $account->pay(
208 amount => $total_paid,
209 library_id => $library_id,
210 note => $note,
211 payment_type => $payment_type,
212 interface => C4::Context->interface,
213 payment_type => $payment_type,
214 cash_register => $cash_register_id
217 $payment_id = $pay_result->{payment_id};
219 $payment_id = $pay_result->{payment_id};
221 $url = "/cgi-bin/koha/members/boraccount.pl";
223 # It's possible renewals took place, parse any renew results
224 # and pass on
225 my @renew_result = ();
226 foreach my $ren( @{$pay_result->{renew_result}} ) {
227 my $str = "renew_result=$ren->{itemnumber},$ren->{success},";
228 my $app = $ren->{success} ?
229 uri_escape(
230 output_pref({ dt => $ren->{due_date}, as_due_date => 1 })
231 ) : $ren->{error};
232 push @renew_result, "${str}${app}";
234 my $append = scalar @renew_result ? '&' . join('&', @renew_result) : '';
236 $url .= "?borrowernumber=$borrowernumber&payment_id=$payment_id&change_given=${change_given}${append}";
238 print $input->redirect($url);
240 } else {
241 $total_paid = '0.00'; #TODO not right with pay_individual
244 $template->param(%$borrower);
246 if ( $input->param('error_over') ) {
247 $template->param( error_over => 1, total_due => scalar $input->param('amountoutstanding') );
250 $template->param(
251 payment_id => $payment_id,
253 type => $type,
254 borrowernumber => $borrowernumber, # some templates require global
255 patron => $patron,
256 total => $total_due,
258 csrf_token => Koha::Token->new->generate_csrf( { session_id => scalar $input->cookie('CGISESSID') } ),
261 output_html_with_http_headers $input, $cookie, $template->output;