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>.
24 use List
::MoreUtils
qw( uniq );
26 use C4
::Circulation
qw( ReturnLostItem );
28 use C4
::Log
qw( logaction );
29 use C4
::Stats
qw( UpdateStats );
32 use Koha
::Account
::Lines
;
33 use Koha
::Account
::Offsets
;
34 use Koha
::DateUtils
qw( dt_from_string );
38 Koha::Accounts - Module for managing payments and fees for patrons
43 my ( $class, $params ) = @_;
45 Carp
::croak
("No patron id passed in!") unless $params->{patron_id
};
47 return bless( $params, $class );
52 This method allows payments to be made against fees/fines
54 Koha::Account->new( { patron_id => $borrowernumber } )->pay(
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
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'
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;
107 # We were passed a specific line to pay
108 foreach my $fine ( @
$lines ) {
110 $fine->amountoutstanding > $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") ) {
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,
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) {
168 $fine->amountoutstanding > $balance_remaining
170 : $fine->amountoutstanding;
172 my $old_amountoutstanding = $fine->amountoutstanding;
173 $fine->amountoutstanding( $old_amountoutstanding - $amount_to_pay );
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") ) {
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,
203 push( @fines_paid, $fine->id );
206 $balance_remaining = $balance_remaining - $amount_to_pay;
207 last unless $balance_remaining > 0;
211 $type eq 'writeoff' ?
'W'
212 : defined($sip) ?
"Pay$sip"
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,
232 foreach my $o ( @account_offsets ) {
233 $o->credit_id( $payment->id() );
237 $library_id ||= $userenv ?
$userenv->{'branch'} : undef;
241 branch
=> $library_id,
244 borrowernumber
=> $self->{patron_id
},
245 accountno
=> $accountno,
249 if ( C4
::Context
->preference("FinesLog") ) {
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') ) {
270 my $letter = C4
::Letters
::GetPreparedLetter
(
271 module
=> 'circulation',
272 letter_code
=> uc("ACCOUNT_$type"),
273 message_transport_type
=> 'email',
274 lang
=> $patron->lang,
276 borrowers
=> $self->{patron_id
},
277 branches
=> $self->{library_id
},
281 offsets
=> \
@account_offsets,
286 C4
::Letters
::EnqueueLetter
(
289 borrowernumber
=> $self->{patron_id
},
290 message_transport_type
=> 'email',
292 ) or warn "can't enqueue letter $letter";
301 This method allows adding credits to a patron's account
303 my $credit_line = Koha::Account->new({ patron_id => $patron_id })->add_credit(
306 description => $description,
309 library_id => $library_id,
311 payment_type => $payment_type,
312 type => $credit_type,
317 $credit_type can be any of:
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
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
},
362 description => $description,
363 accounttype => $account_type,
364 amountoutstanding => $amount,
365 payment_type => $payment_type,
367 manager_id => $user_id,
368 itemnumber => $item_id
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},
381 { branch => $library_id,
384 borrowernumber => $self->{patron_id},
385 accountno => $accountno,
387 ) if grep { $type eq $_ } ('payment
', 'writeoff
') ;
389 if ( C4::Context->preference("FinesLog") ) {
394 { action => "create_$type",
395 borrowernumber => $self->{patron_id},
396 accountno => $accountno,
398 description => $description,
399 amountoutstanding => $amount,
400 accounttype => $account_type,
402 itemnumber => $item_id,
403 manager_id => $user_id,
416 my $balance = $self->balance
418 Return the balance (sum of amountoutstanding columns)
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
439 =head3 outstanding_debits
441 my $lines = Koha::Account->new({ patron_id => $patron_id })->outstanding_debits;
445 sub outstanding_debits {
448 my $lines = Koha::Account::Lines->search(
450 borrowernumber => $self->{patron_id},
451 amountoutstanding => { '>' => 0 }
458 =head3 outstanding_credits
460 my $lines = Koha::Account->new({ patron_id => $patron_id })->outstanding_credits;
464 sub outstanding_credits {
467 my $lines = Koha::Account::Lines->search(
469 borrowernumber => $self->{patron_id},
470 amountoutstanding => { '<' => 0 }
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
490 sub non_issues_charges {
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...
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;
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
536 'credit
' => 'Manual Credit
',
537 'forgiven
' => 'Writeoff
',
538 'lost_item_return
' => 'Lost Item Return
',
539 'payment
' => 'Payment
',
540 'writeoff
' => 'Writeoff
'
547 our $account_type = {
550 'lost_item_return
' => 'CR
',
557 Kyle M Hall <kyle.m.hall@gmail.com>