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>.
21 use URI::Escape qw( uri_escape uri_unescape );
25 use C4::Auth qw( get_template_and_user );
26 use C4::Output qw( output_and_exit_if_error output_and_exit output_html_with_http_headers );
30 use Koha::Cash::Registers;
32 use Koha::Patron::Categories;
33 use Koha::AuthorisedValues;
35 use Koha::Account::Lines;
36 use Koha::AdditionalFields;
37 use Koha::DateUtils qw( output_pref );
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';
47 my $op = $input->param('op') // qw{};
49 my $updatecharges_permissions = ($writeoff_individual || $type eq 'WRITEOFF') ? 'writeoff' : 'remaining_permissions';
50 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
51 { template_name => 'members/paycollect.tt',
54 flagsrequired => { borrowers => 'edit_borrowers', updatecharges => $updatecharges_permissions },
58 # get borrower details
59 my $borrowernumber = $input->param('borrowernumber');
60 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
61 my $patron = Koha::Patrons->find( $borrowernumber );
62 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
64 my $account = $patron->account;
65 my $category = $patron->category;
66 my $user = $input->remote_user;
68 my $library_id = C4::Context->userenv->{'branch'};
69 my $total_due = $account->outstanding_debits->total_outstanding;
71 my $total_paid = $input->param('paid');
72 my $total_collected = $input->param('collected');
74 my $selected_lines = $input->param('selected'); # comes from pay.pl
75 my $pay_individual = $input->param('pay_individual');
76 my $selected_accts = $input->param('selected_accts'); # comes from paycollect.pl
77 my $payment_note = uri_unescape scalar $input->param('payment_note');
78 my $payment_type = scalar $input->param('payment_type');
81 my $cash_register_id = $input->param('cash_register');
82 if ( $pay_individual || $writeoff_individual ) {
83 if ($pay_individual) {
84 $template->param( pay_individual => 1 );
85 } elsif ($writeoff_individual) {
86 $template->param( writeoff_individual => 1 );
88 my $debit_type_code = $input->param('debit_type_code');
89 $accountlines_id = $input->param('accountlines_id');
90 my $amount = $input->param('amount');
91 my $amountoutstanding = $input->param('amountoutstanding');
92 my $itemnumber = $input->param('itemnumber');
93 my $description = $input->param('description');
94 $total_due = $amountoutstanding;
96 debit_type_code => $debit_type_code,
97 accountlines_id => $accountlines_id,
99 amountoutstanding => $amountoutstanding,
100 itemnumber => $itemnumber,
101 individual_description => $description,
102 payment_note => $payment_note,
104 } elsif ($selected_lines) {
105 $total_due = $input->param('amt');
107 selected_accts => $selected_lines,
109 selected_accts_notes => scalar $input->param('notes'),
113 my @selected_accountlines;
114 if ( $selected_accts ) {
115 if ( $selected_accts =~ /^([\d,]*).*/ ) {
116 $selected_accts = $1; # ensure passing no junk
118 my @acc = split /,/, $selected_accts;
120 my $search_params = {
121 borrowernumber => $borrowernumber,
122 amountoutstanding => { '<>' => 0 },
123 accountlines_id => { 'in' => \@acc },
126 @selected_accountlines = Koha::Account::Lines->search(
128 { order_by => 'date' }
131 my $sum = Koha::Account::Lines->search(
134 select => [ { sum => 'amountoutstanding' } ],
135 as => [ 'total_amountoutstanding'],
138 $total_due = $sum->_resultset->first->get_column('total_amountoutstanding');
141 if ( $total_paid and $total_paid ne '0.00' ) {
142 $accountlines_id = $input->param('accountlines_id');
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 ) {
147 total_due => $total_due
149 } elsif ( $total_collected < $total_paid && !( $op eq 'cud-writeoff_individual' || $type eq 'WRITEOFF' ) ) {
152 total_paid => $total_paid
157 if ($op eq 'cud-pay_individual') {
158 my $line = Koha::Account::Lines->find($accountlines_id);
159 $pay_result = $account->pay(
163 amount => $total_paid,
164 library_id => $library_id,
165 note => $payment_note,
166 interface => C4::Context->interface,
167 payment_type => $payment_type,
168 cash_register => $cash_register_id
171 $payment_id = $pay_result->{payment_id};
173 my @additional_fields;
174 my $accountline_fields = Koha::AdditionalFields->search({ tablename => 'accountlines:credit' });
175 while ( my $field = $accountline_fields->next ) {
176 my $value = $input->param('additional_field_' . $field->id);
177 if (defined $value) {
178 push @additional_fields, {
184 if (@additional_fields) {
185 my $payment = Koha::Account::Lines->find($payment_id);
186 $payment->set_additional_fields(\@additional_fields);
189 $url = "/cgi-bin/koha/members/pay.pl";
190 } elsif ($op eq 'cud-writeoff_individual') {
191 my $item_id = $input->param('itemnumber');
192 my $payment_note = $input->param("payment_note");
194 my $accountline = Koha::Account::Lines->find( $accountlines_id );
195 $pay_result = $account->pay(
198 amount => $total_paid,
199 lines => [ $accountline ],
200 note => $payment_note,
201 interface => C4::Context->interface,
203 library_id => $library_id,
206 $payment_id = $pay_result->{payment_id};
208 $url = "/cgi-bin/koha/members/pay.pl";
209 } elsif ( $op eq 'cud-pay' || $op eq 'cud-writeoff' ) {
210 if ($selected_accts) {
211 if ( $total_paid > $total_due ) {
214 total_due => $total_due
217 my $note = $input->param('selected_accts_notes');
219 $pay_result = $account->pay(
222 amount => $total_paid,
223 library_id => $library_id,
224 lines => \@selected_accountlines,
226 interface => C4::Context->interface,
227 payment_type => $payment_type,
228 cash_register => $cash_register_id
232 $payment_id = $pay_result->{payment_id};
235 my $note = $input->param('selected_accts_notes');
236 $pay_result = $account->pay(
238 amount => $total_paid,
239 library_id => $library_id,
241 payment_type => $payment_type,
242 interface => C4::Context->interface,
243 payment_type => $payment_type,
244 cash_register => $cash_register_id
247 $payment_id = $pay_result->{payment_id};
249 $payment_id = $pay_result->{payment_id};
251 my @additional_fields;
252 my $accountline_fields = Koha::AdditionalFields->search({ tablename => 'accountlines:credit' });
253 while ( my $field = $accountline_fields->next ) {
254 my $value = $input->param('additional_field_' . $field->id);
255 if (defined $value) {
256 push @additional_fields, {
262 if (@additional_fields) {
263 my $payment = Koha::Account::Lines->find($payment_id);
264 $payment->set_additional_fields(\@additional_fields);
267 $url = "/cgi-bin/koha/members/boraccount.pl";
269 # It's possible renewals took place, parse any renew results
271 my @renew_result = ();
272 foreach my $ren( @{$pay_result->{renew_result}} ) {
273 my $str = "renew_result=$ren->{itemnumber},$ren->{success},";
274 my $app = $ren->{success} ?
276 output_pref({ dt => $ren->{due_date}, as_due_date => 1 })
278 push @renew_result, "${str}${app}";
280 my $append = scalar @renew_result ? '&' . join('&', @renew_result) : '';
282 $url .= "?borrowernumber=$borrowernumber&payment_id=$payment_id&change_given=${change_given}${append}";
284 print $input->redirect($url);
287 $total_paid = '0.00'; #TODO not right with pay_individual
290 if ( $input->param('error_over') ) {
291 $template->param( error_over => 1, total_due => scalar $input->param('amountoutstanding') );
295 payment_id => $payment_id,
297 borrowernumber => $borrowernumber, # some templates require global
300 available_additional_fields =>
301 [ Koha::AdditionalFields->search( { tablename => 'accountlines:credit' } )->as_list ],
304 output_html_with_http_headers $input, $cookie, $template->output;