Bug 20996: (follow-up) Fix merge problems
[koha.git] / Koha / Account.pm
blob6f41b3bbd2d02a0cfb409dbaa1f31516f497c7de
1 package Koha::Account;
3 # Copyright 2016 ByWater Solutions
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;
22 use Carp;
23 use Data::Dumper;
24 use List::MoreUtils qw( uniq );
26 use C4::Circulation qw( ReturnLostItem );
27 use C4::Letters;
28 use C4::Log qw( logaction );
29 use C4::Stats qw( UpdateStats );
31 use Koha::Patrons;
32 use Koha::Account::Lines;
33 use Koha::Account::Offsets;
34 use Koha::DateUtils qw( dt_from_string );
36 =head1 NAME
38 Koha::Accounts - Module for managing payments and fees for patrons
40 =cut
42 sub new {
43 my ( $class, $params ) = @_;
45 Carp::croak("No patron id passed in!") unless $params->{patron_id};
47 return bless( $params, $class );
50 =head2 pay
52 This method allows payments to be made against fees/fines
54 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
56 amount => $amount,
57 sip => $sipmode,
58 note => $note,
59 description => $description,
60 library_id => $branchcode,
61 lines => $lines, # Arrayref of Koha::Account::Line objects to pay
62 account_type => $type, # accounttype code
63 offset_type => $offset_type, # offset type code
67 =cut
69 sub pay {
70 my ( $self, $params ) = @_;
72 my $amount = $params->{amount};
73 my $sip = $params->{sip};
74 my $description = $params->{description};
75 my $note = $params->{note} || q{};
76 my $library_id = $params->{library_id};
77 my $lines = $params->{lines};
78 my $type = $params->{type} || 'payment';
79 my $payment_type = $params->{payment_type} || undef;
80 my $account_type = $params->{account_type};
81 my $offset_type = $params->{offset_type} || $type eq 'writeoff' ? 'Writeoff' : 'Payment';
83 my $userenv = C4::Context->userenv;
85 my $patron = Koha::Patrons->find( $self->{patron_id} );
87 # We should remove accountno, it is no longer needed
88 my $last = Koha::Account::Lines->search(
90 borrowernumber => $self->{patron_id}
93 order_by => 'accountno'
95 )->next();
96 my $accountno = $last ? $last->accountno + 1 : 1;
98 my $manager_id = $userenv ? $userenv->{number} : 0;
100 my @fines_paid; # List of account lines paid on with this payment
102 my $balance_remaining = $amount; # Set it now so we can adjust the amount if necessary
103 $balance_remaining ||= 0;
105 my @account_offsets;
107 # We were passed a specific line to pay
108 foreach my $fine ( @$lines ) {
109 my $amount_to_pay =
110 $fine->amountoutstanding > $balance_remaining
111 ? $balance_remaining
112 : $fine->amountoutstanding;
114 my $old_amountoutstanding = $fine->amountoutstanding;
115 my $new_amountoutstanding = $old_amountoutstanding - $amount_to_pay;
116 $fine->amountoutstanding($new_amountoutstanding)->store();
117 $balance_remaining = $balance_remaining - $amount_to_pay;
119 if ( $fine->itemnumber && $fine->accounttype && ( $fine->accounttype eq 'Rep' || $fine->accounttype eq 'L' ) )
121 C4::Circulation::ReturnLostItem( $self->{patron_id}, $fine->itemnumber );
124 my $account_offset = Koha::Account::Offset->new(
126 debit_id => $fine->id,
127 type => $offset_type,
128 amount => $amount_to_pay * -1,
131 push( @account_offsets, $account_offset );
133 if ( C4::Context->preference("FinesLog") ) {
134 logaction(
135 "FINES", 'MODIFY',
136 $self->{patron_id},
137 Dumper(
139 action => 'fee_payment',
140 borrowernumber => $fine->borrowernumber,
141 old_amountoutstanding => $old_amountoutstanding,
142 new_amountoutstanding => 0,
143 amount_paid => $old_amountoutstanding,
144 accountlines_id => $fine->id,
145 accountno => $fine->accountno,
146 manager_id => $manager_id,
147 note => $note,
151 push( @fines_paid, $fine->id );
155 # Were not passed a specific line to pay, or the payment was for more
156 # than the what was owed on the given line. In that case pay down other
157 # lines with remaining balance.
158 my @outstanding_fines;
159 @outstanding_fines = Koha::Account::Lines->search(
161 borrowernumber => $self->{patron_id},
162 amountoutstanding => { '>' => 0 },
164 ) if $balance_remaining > 0;
166 foreach my $fine (@outstanding_fines) {
167 my $amount_to_pay =
168 $fine->amountoutstanding > $balance_remaining
169 ? $balance_remaining
170 : $fine->amountoutstanding;
172 my $old_amountoutstanding = $fine->amountoutstanding;
173 $fine->amountoutstanding( $old_amountoutstanding - $amount_to_pay );
174 $fine->store();
176 my $account_offset = Koha::Account::Offset->new(
178 debit_id => $fine->id,
179 type => $offset_type,
180 amount => $amount_to_pay * -1,
183 push( @account_offsets, $account_offset );
185 if ( C4::Context->preference("FinesLog") ) {
186 logaction(
187 "FINES", 'MODIFY',
188 $self->{patron_id},
189 Dumper(
191 action => "fee_$type",
192 borrowernumber => $fine->borrowernumber,
193 old_amountoutstanding => $old_amountoutstanding,
194 new_amountoutstanding => $fine->amountoutstanding,
195 amount_paid => $amount_to_pay,
196 accountlines_id => $fine->id,
197 accountno => $fine->accountno,
198 manager_id => $manager_id,
199 note => $note,
203 push( @fines_paid, $fine->id );
206 $balance_remaining = $balance_remaining - $amount_to_pay;
207 last unless $balance_remaining > 0;
210 $account_type ||=
211 $type eq 'writeoff' ? 'W'
212 : defined($sip) ? "Pay$sip"
213 : 'Pay';
215 $description ||= $type eq 'writeoff' ? 'Writeoff' : q{};
217 my $payment = Koha::Account::Line->new(
219 borrowernumber => $self->{patron_id},
220 accountno => $accountno,
221 date => dt_from_string(),
222 amount => 0 - $amount,
223 description => $description,
224 accounttype => $account_type,
225 payment_type => $payment_type,
226 amountoutstanding => 0 - $balance_remaining,
227 manager_id => $manager_id,
228 note => $note,
230 )->store();
232 foreach my $o ( @account_offsets ) {
233 $o->credit_id( $payment->id() );
234 $o->store();
237 $library_id ||= $userenv ? $userenv->{'branch'} : undef;
239 UpdateStats(
241 branch => $library_id,
242 type => $type,
243 amount => $amount,
244 borrowernumber => $self->{patron_id},
245 accountno => $accountno,
249 if ( C4::Context->preference("FinesLog") ) {
250 logaction(
251 "FINES", 'CREATE',
252 $self->{patron_id},
253 Dumper(
255 action => "create_$type",
256 borrowernumber => $self->{patron_id},
257 accountno => $accountno,
258 amount => 0 - $amount,
259 amountoutstanding => 0 - $balance_remaining,
260 accounttype => $account_type,
261 accountlines_paid => \@fines_paid,
262 manager_id => $manager_id,
268 if ( C4::Context->preference('UseEmailReceipts') ) {
269 if (
270 my $letter = C4::Letters::GetPreparedLetter(
271 module => 'circulation',
272 letter_code => uc("ACCOUNT_$type"),
273 message_transport_type => 'email',
274 lang => $patron->lang,
275 tables => {
276 borrowers => $self->{patron_id},
277 branches => $self->{library_id},
279 substitute => {
280 credit => $payment,
281 offsets => \@account_offsets,
286 C4::Letters::EnqueueLetter(
288 letter => $letter,
289 borrowernumber => $self->{patron_id},
290 message_transport_type => 'email',
292 ) or warn "can't enqueue letter $letter";
296 return $payment->id;
299 =head3 add_credit
301 This method allows adding credits to a patron's account
303 my $credit_line = Koha::Account->new({ patron_id => $patron_id })->add_credit(
305 amount => $amount,
306 description => $description,
307 note => $note,
308 user_id => $user_id,
309 library_id => $library_id,
310 sip => $sip,
311 payment_type => $payment_type,
312 type => $credit_type,
313 item_id => $item_id
317 $credit_type can be any of:
318 - 'credit'
319 - 'payment'
320 - 'forgiven'
321 - 'lost_item_return'
322 - 'writeoff'
324 =cut
326 sub add_credit {
328 my ( $self, $params ) = @_;
330 # amount is passed as a positive value, but we store credit as negative values
331 my $amount = $params->{amount} * -1;
332 my $description = $params->{description} // q{};
333 my $note = $params->{note} // q{};
334 my $user_id = $params->{user_id};
335 my $library_id = $params->{library_id};
336 my $sip = $params->{sip};
337 my $payment_type = $params->{payment_type};
338 my $type = $params->{type} || 'payment';
339 my $item_id = $params->{item_id};
341 my $schema = Koha::Database->new->schema;
343 my $account_type = $Koha::Account::account_type->{$type};
344 $account_type .= $sip
345 if defined $sip &&
346 $type eq 'payment';
348 my $line;
350 $schema->txn_do(
351 sub {
352 # We should remove accountno, it is no longer needed
353 my $last = Koha::Account::Lines->search( { borrowernumber => $self->{patron_id} },
354 { order_by => 'accountno' } )->next();
355 my $accountno = $last ? $last->accountno + 1 : 1;
357 # Insert the account line
358 $line = Koha::Account::Line->new(
359 { borrowernumber => $self->{patron_id},
360 date => \'NOW()',
361 amount => $amount,
362 description => $description,
363 accounttype => $account_type,
364 amountoutstanding => $amount,
365 payment_type => $payment_type,
366 note => $note,
367 manager_id => $user_id,
368 itemnumber => $item_id
370 )->store();
372 # Record the account offset
373 my $account_offset = Koha::Account::Offset->new(
374 { credit_id => $line->id,
375 type => $Koha::Account::offset_type->{$type},
376 amount => $amount
378 )->store();
380 UpdateStats(
381 { branch => $library_id,
382 type => $type,
383 amount => $amount,
384 borrowernumber => $self->{patron_id},
385 accountno => $accountno,
387 ) if grep { $type eq $_ } ('payment', 'writeoff') ;
389 if ( C4::Context->preference("FinesLog") ) {
390 logaction(
391 "FINES", 'CREATE',
392 $self->{patron_id},
393 Dumper(
394 { action => "create_$type",
395 borrowernumber => $self->{patron_id},
396 accountno => $accountno,
397 amount => $amount,
398 description => $description,
399 amountoutstanding => $amount,
400 accounttype => $account_type,
401 note => $note,
402 itemnumber => $item_id,
403 manager_id => $user_id,
411 return $line;
414 =head3 balance
416 my $balance = $self->balance
418 Return the balance (sum of amountoutstanding columns)
420 =cut
422 sub balance {
423 my ($self) = @_;
424 my $fines = Koha::Account::Lines->search(
426 borrowernumber => $self->{patron_id},
429 select => [ { sum => 'amountoutstanding' } ],
430 as => ['total_amountoutstanding'],
434 return ( $fines->count )
435 ? $fines->next->get_column('total_amountoutstanding') + 0
436 : 0;
439 =head3 outstanding_debits
441 my $lines = Koha::Account->new({ patron_id => $patron_id })->outstanding_debits;
443 =cut
445 sub outstanding_debits {
446 my ($self) = @_;
448 my $lines = Koha::Account::Lines->search(
450 borrowernumber => $self->{patron_id},
451 amountoutstanding => { '>' => 0 }
455 return $lines;
458 =head3 outstanding_credits
460 my $lines = Koha::Account->new({ patron_id => $patron_id })->outstanding_credits;
462 =cut
464 sub outstanding_credits {
465 my ($self) = @_;
467 my $lines = Koha::Account::Lines->search(
469 borrowernumber => $self->{patron_id},
470 amountoutstanding => { '<' => 0 }
474 return $lines;
477 =head3 non_issues_charges
479 my $non_issues_charges = $self->non_issues_charges
481 Calculates amount immediately owing by the patron - non-issue charges.
483 Charges exempt from non-issue are:
484 * Res (holds) if HoldsInNoissuesCharge syspref is set to false
485 * Rent (rental) if RentalsInNoissuesCharge syspref is set to false
486 * Manual invoices if ManInvInNoissuesCharge syspref is set to false
488 =cut
490 sub non_issues_charges {
491 my ($self) = @_;
493 # FIXME REMOVE And add a warning in the about page + update DB if length(MANUAL_INV) > 5
494 my $ACCOUNT_TYPE_LENGTH = 5; # this is plain ridiculous...
496 my @not_fines;
497 push @not_fines, 'Res'
498 unless C4::Context->preference('HoldsInNoissuesCharge');
499 push @not_fines, 'Rent'
500 unless C4::Context->preference('RentalsInNoissuesCharge');
501 unless ( C4::Context->preference('ManInvInNoissuesCharge') ) {
502 my $dbh = C4::Context->dbh;
503 push @not_fines,
505 $dbh->selectcol_arrayref(q|
506 SELECT authorised_value FROM authorised_values WHERE category = 'MANUAL_INV'
510 @not_fines = map { substr( $_, 0, $ACCOUNT_TYPE_LENGTH ) } uniq(@not_fines);
512 my $non_issues_charges = Koha::Account::Lines->search(
514 borrowernumber => $self->{patron_id},
515 accounttype => { -not_in => \@not_fines }
518 select => [ { sum => 'amountoutstanding' } ],
519 as => ['non_issues_charges'],
522 return $non_issues_charges->count
523 ? $non_issues_charges->next->get_column('non_issues_charges') + 0
524 : 0;
527 =head3 lines
529 my $lines = $self->lines;
531 Return all credits and debits for the user, outstanding or otherwise
533 =cut
535 sub lines {
536 my ($self) = @_;
538 return Koha::Account::Lines->search(
540 borrowernumber => $self->{patron_id},
547 =head2 Name mappings
549 =head3 $offset_type
551 =cut
553 our $offset_type = {
554 'credit' => 'Manual Credit',
555 'forgiven' => 'Writeoff',
556 'lost_item_return' => 'Lost Item',
557 'payment' => 'Payment',
558 'writeoff' => 'Writeoff'
561 =head3 $account_type
563 =cut
565 our $account_type = {
566 'credit' => 'C',
567 'forgiven' => 'FOR',
568 'lost_item_return' => 'CR',
569 'payment' => 'Pay',
570 'writeoff' => 'W'
573 =head1 AUTHOR
575 Kyle M Hall <kyle.m.hall@gmail.com>
577 =cut