Line 0
Link Here
|
|
|
1 |
package Koha::Accounts; |
2 |
|
3 |
# Copyright 2013 ByWater Solutions |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it under the |
8 |
# terms of the GNU General Public License as published by the Free Software |
9 |
# Foundation; either version 3 of the License, or (at your option) any later |
10 |
# version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License along |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use Carp; |
23 |
use Data::Dumper qw(Dumper); |
24 |
|
25 |
use C4::Context; |
26 |
use C4::Log qw(logaction); |
27 |
use Koha::DateUtils qw(get_timestamp); |
28 |
use Koha::Accounts::CreditTypes; |
29 |
use Koha::Accounts::DebitTypes; |
30 |
use Koha::Accounts::OffsetTypes; |
31 |
use Koha::Database; |
32 |
|
33 |
use vars qw($VERSION @ISA @EXPORT); |
34 |
|
35 |
BEGIN { |
36 |
require Exporter; |
37 |
@ISA = qw(Exporter); |
38 |
@EXPORT = qw( |
39 |
AddDebit |
40 |
AddCredit |
41 |
|
42 |
VoidCredit |
43 |
|
44 |
NormalizeBalances |
45 |
|
46 |
RecalculateAccountBalance |
47 |
|
48 |
DebitLostItem |
49 |
CreditLostItem |
50 |
); |
51 |
} |
52 |
|
53 |
=head1 NAME |
54 |
|
55 |
Koha::Accounts - Functions for dealing with Koha accounts |
56 |
|
57 |
=head1 SYNOPSIS |
58 |
|
59 |
use Koha::Accounts; |
60 |
|
61 |
=head1 DESCRIPTION |
62 |
|
63 |
The functions in this module deal with the monetary aspect of Koha, |
64 |
including looking up and modifying the amount of money owed by a |
65 |
patron. |
66 |
|
67 |
=head1 FUNCTIONS |
68 |
|
69 |
=head2 AddDebit |
70 |
|
71 |
my $debit = AddDebit({ |
72 |
borrower => $borrower, |
73 |
amount => $amount, |
74 |
[ type => $type, ] |
75 |
[ itemnumber => $itemnumber, ] |
76 |
[ issue_id => $issue_id, ] |
77 |
[ description => $description, ] |
78 |
[ notes => $notes, ] |
79 |
[ branchcode => $branchcode, ] |
80 |
[ manager_id => $manager_id, ] |
81 |
[ accruing => $accruing, ] # Default 0 if not accruing, 1 if accruing |
82 |
}); |
83 |
|
84 |
Create a new debit for a given borrower. To standardize nomenclature, any charge |
85 |
against a borrower ( e.g. a fine, a new card charge, the cost of losing an item ) |
86 |
will be referred to as a 'debit'. |
87 |
|
88 |
=cut |
89 |
|
90 |
sub AddDebit { |
91 |
my ($params) = @_; |
92 |
|
93 |
my $borrower = $params->{borrower}; |
94 |
my $amount = $params->{amount}; |
95 |
my $type = $params->{type}; |
96 |
my $itemnumber = $params->{itemnumber}; |
97 |
my $issue_id = $params->{issue_id}; |
98 |
my $description = $params->{description}; |
99 |
my $notes = $params->{notes}; |
100 |
my $branchcode = $params->{branchcode}; |
101 |
my $manager_id = $params->{manager_id}; |
102 |
|
103 |
my $userenv = C4::Context->userenv; |
104 |
|
105 |
$branchcode ||= |
106 |
$userenv |
107 |
? $userenv->{branch} |
108 |
: undef; |
109 |
|
110 |
$manager_id ||= |
111 |
$userenv |
112 |
? $userenv->{number} |
113 |
: undef; |
114 |
|
115 |
my $accruing = $params->{accruing} || 0; |
116 |
|
117 |
croak("Required parameter 'borrower' not passed in.") |
118 |
unless ($borrower); |
119 |
croak("Required parameter 'amount' not passed in.") |
120 |
unless ($amount); |
121 |
croak("Invalid debit type: '$type'!") |
122 |
unless ( Koha::Accounts::DebitTypes::IsValid($type) ); |
123 |
croak("No issue id passed in for accruing debit!") |
124 |
if ( $accruing && !$issue_id ); |
125 |
|
126 |
my $debit = |
127 |
Koha::Database->new()->schema->resultset('AccountDebit')->create( |
128 |
{ |
129 |
borrowernumber => $borrower->borrowernumber(), |
130 |
itemnumber => $itemnumber, |
131 |
issue_id => $issue_id, |
132 |
type => $type, |
133 |
accruing => $accruing, |
134 |
amount_original => $amount, |
135 |
amount_outstanding => $amount, |
136 |
amount_last_increment => $amount, |
137 |
description => $description, |
138 |
notes => $notes, |
139 |
branchcode => $branchcode, |
140 |
manager_id => $manager_id, |
141 |
created_on => get_timestamp(), |
142 |
} |
143 |
); |
144 |
|
145 |
if ($debit) { |
146 |
NormalizeBalances( { borrower => $borrower } ); |
147 |
|
148 |
if ( C4::Context->preference("FinesLog") ) { |
149 |
logaction( "FINES", "CREATE_FEE", $debit->id, |
150 |
Dumper( $debit->get_columns() ) ); |
151 |
} |
152 |
} |
153 |
else { |
154 |
carp("Something went wrong! Debit not created!"); |
155 |
} |
156 |
|
157 |
return $debit; |
158 |
} |
159 |
|
160 |
=head2 DebitLostItem |
161 |
|
162 |
my $debit = DebitLostItem({ |
163 |
borrower => $borrower, |
164 |
issue => $issue, |
165 |
}); |
166 |
|
167 |
DebitLostItem adds a replacement fee charge for the item |
168 |
of the given issue. |
169 |
|
170 |
=cut |
171 |
|
172 |
sub DebitLostItem { |
173 |
my ($params) = @_; |
174 |
|
175 |
my $borrower = $params->{borrower}; |
176 |
my $issue = $params->{issue}; |
177 |
|
178 |
croak("Required param 'borrower' not passed in!") unless ($borrower); |
179 |
croak("Required param 'issue' not passed in!") unless ($issue); |
180 |
|
181 |
# Don't add lost debit if borrower has already been charged for this lost item before, |
182 |
# for this issue. It seems reasonable that a borrower could lose an item, find and return it, |
183 |
# check it out again, and lose it again, so we should do this based on issue_id, not itemnumber. |
184 |
unless ( |
185 |
Koha::Database->new()->schema->resultset('AccountDebit')->search( |
186 |
{ |
187 |
borrowernumber => $borrower->borrowernumber(), |
188 |
issue_id => $issue->issue_id(), |
189 |
type => Koha::Accounts::DebitTypes::Lost |
190 |
} |
191 |
)->count() |
192 |
) |
193 |
{ |
194 |
my $item = $issue->item(); |
195 |
|
196 |
$params->{accruing} = 0; |
197 |
$params->{type} = Koha::Accounts::DebitTypes::Lost; |
198 |
$params->{amount} = $item->replacementprice(); |
199 |
$params->{itemnumber} = $item->itemnumber(); |
200 |
$params->{issue_id} = $issue->issue_id(); |
201 |
$params->{description} = |
202 |
"Lost Item " |
203 |
. $issue->item()->biblio()->title() . " " |
204 |
. $issue->item()->barcode(); |
205 |
|
206 |
#TODO: Shouldn't we have a default replacement price as a syspref? |
207 |
if ( $params->{amount} ) { |
208 |
return AddDebit($params); |
209 |
} |
210 |
else { |
211 |
carp("Cannot add lost debit! Item has no replacement price!"); |
212 |
} |
213 |
} |
214 |
} |
215 |
|
216 |
=head2 CreditLostItem |
217 |
|
218 |
my $debit = CreditLostItem( |
219 |
{ |
220 |
borrower => $borrower, |
221 |
debit => $debit, |
222 |
} |
223 |
); |
224 |
|
225 |
CreditLostItem creates a payment in the amount equal |
226 |
to the replacement price charge created by DebitLostItem. |
227 |
|
228 |
=cut |
229 |
|
230 |
sub CreditLostItem { |
231 |
my ($params) = @_; |
232 |
|
233 |
my $borrower = $params->{borrower}; |
234 |
my $debit = $params->{debit}; |
235 |
|
236 |
croak("Required param 'borrower' not passed in!") unless ($borrower); |
237 |
croak("Required param 'debit' not passed in!") |
238 |
unless ($debit); |
239 |
|
240 |
my $item = |
241 |
Koha::Database->new()->schema->resultset('Item') |
242 |
->find( $debit->itemnumber() ); |
243 |
carp("No item found!") unless $item; |
244 |
|
245 |
$params->{type} = Koha::Accounts::CreditTypes::Found; |
246 |
$params->{amount} = $debit->amount_original(); |
247 |
$params->{debit_id} = $debit->debit_id(); |
248 |
$params->{notes} = "Lost item found: " . $item->barcode(); |
249 |
|
250 |
return AddCredit($params); |
251 |
} |
252 |
|
253 |
=head2 AddCredit |
254 |
|
255 |
AddCredit({ |
256 |
borrower => $borrower, |
257 |
amount => $amount, |
258 |
[ branchcode => $branchcode, ] |
259 |
[ manager_id => $manager_id, ] |
260 |
[ debit_id => $debit_id, ] # The primary debit to be paid |
261 |
[ notes => $notes, ] |
262 |
}); |
263 |
|
264 |
Record credit by a patron. C<$borrowernumber> is the patron's |
265 |
borrower number. C<$credit> is a floating-point number, giving the |
266 |
amount that was paid. |
267 |
|
268 |
Amounts owed are paid off oldest first. That is, if the patron has a |
269 |
$1 fine from Feb. 1, another $1 fine from Mar. 1, and makes a credit |
270 |
of $1.50, then the oldest fine will be paid off in full, and $0.50 |
271 |
will be credited to the next one. |
272 |
|
273 |
debit_id can be passed as a scalar or an array ref to make the passed |
274 |
in debit or debits the first to be credited. |
275 |
|
276 |
=cut |
277 |
|
278 |
sub AddCredit { |
279 |
my ($params) = @_; |
280 |
|
281 |
my $type = $params->{type}; |
282 |
my $borrower = $params->{borrower}; |
283 |
my $amount = $params->{amount}; |
284 |
my $amount_received = $params->{amount_received}; |
285 |
my $debit_id = $params->{debit_id}; |
286 |
my $notes = $params->{notes}; |
287 |
my $branchcode = $params->{branchcode}; |
288 |
my $manager_id = $params->{manager_id}; |
289 |
|
290 |
my $userenv = C4::Context->userenv; |
291 |
|
292 |
$branchcode ||= |
293 |
$userenv |
294 |
? $userenv->{branch} |
295 |
: undef; |
296 |
|
297 |
$manager_id ||= |
298 |
$userenv |
299 |
? $userenv->{number} |
300 |
: undef; |
301 |
|
302 |
unless ($borrower) { |
303 |
croak("Required parameter 'borrower' not passed in"); |
304 |
} |
305 |
unless ($amount) { |
306 |
croak("Required parameter amount not passed in"); |
307 |
} |
308 |
|
309 |
unless ($type && Koha::Accounts::CreditTypes::IsValid($type) ) { |
310 |
carp("Invalid type $type passed in, assuming Payment"); |
311 |
$type = Koha::Accounts::CreditTypes::Payment; |
312 |
} |
313 |
|
314 |
# First, we make the credit. We'll worry about what we paid later on |
315 |
my $credit = |
316 |
Koha::Database->new()->schema->resultset('AccountCredit')->create( |
317 |
{ |
318 |
borrowernumber => $borrower->borrowernumber(), |
319 |
type => $type, |
320 |
amount_received => $amount_received, |
321 |
amount_paid => $amount, |
322 |
amount_remaining => $amount, |
323 |
notes => $notes, |
324 |
branchcode => $branchcode, |
325 |
manager_id => $manager_id, |
326 |
created_on => get_timestamp(), |
327 |
} |
328 |
); |
329 |
|
330 |
if ( C4::Context->preference("FinesLog") ) { |
331 |
logaction( "FINES", "CREATE_PAYMENT", $credit->id, |
332 |
Dumper( $credit->get_columns() ) ); |
333 |
} |
334 |
|
335 |
# If we are given specific debits, pay those ones first. |
336 |
if ($debit_id) { |
337 |
my @debit_ids = ref($debit_id) eq "ARRAY" ? @$debit_id : $debit_id; |
338 |
foreach my $debit_id (@debit_ids) { |
339 |
my $debit = |
340 |
Koha::Database->new()->schema->resultset('AccountDebit') |
341 |
->find($debit_id); |
342 |
|
343 |
if ($debit) { |
344 |
CreditDebit( { credit => $credit, debit => $debit } ); |
345 |
} |
346 |
else { |
347 |
carp("Invalid debit_id passed in!"); |
348 |
} |
349 |
} |
350 |
} |
351 |
|
352 |
NormalizeBalances( { borrower => $borrower } ); |
353 |
|
354 |
return $credit; |
355 |
} |
356 |
|
357 |
=head2 CreditDebit |
358 |
|
359 |
$account_offset = CreditDebit({ |
360 |
credit => $credit, |
361 |
debit => $debit, |
362 |
}); |
363 |
|
364 |
Given a credit and a debit, this subroutine |
365 |
will pay the appropriate amount of the debit, |
366 |
update the debit's amount outstanding, the credit's |
367 |
amout remaining, and create the appropriate account |
368 |
offset. |
369 |
|
370 |
=cut |
371 |
|
372 |
sub CreditDebit { |
373 |
my ($params) = @_; |
374 |
|
375 |
my $credit = $params->{credit}; |
376 |
my $debit = $params->{debit}; |
377 |
|
378 |
croak("Required parameter 'credit' not passed in!") |
379 |
unless $credit; |
380 |
croak("Required parameter 'debit' not passed in!") |
381 |
unless $debit; |
382 |
|
383 |
my $amount_to_pay = |
384 |
( $debit->amount_outstanding() > $credit->amount_remaining() ) |
385 |
? $credit->amount_remaining() |
386 |
: $debit->amount_outstanding(); |
387 |
|
388 |
if ( $amount_to_pay > 0 ) { |
389 |
$debit->amount_outstanding( |
390 |
$debit->amount_outstanding() - $amount_to_pay ); |
391 |
$debit->updated_on( get_timestamp() ); |
392 |
$debit->update(); |
393 |
|
394 |
$credit->amount_remaining( |
395 |
$credit->amount_remaining() - $amount_to_pay ); |
396 |
$credit->updated_on( get_timestamp() ); |
397 |
$credit->update(); |
398 |
|
399 |
my $offset = |
400 |
Koha::Database->new()->schema->resultset('AccountOffset')->create( |
401 |
{ |
402 |
amount => $amount_to_pay * -1, |
403 |
debit_id => $debit->id(), |
404 |
credit_id => $credit->id(), |
405 |
created_on => get_timestamp(), |
406 |
} |
407 |
); |
408 |
|
409 |
if ( C4::Context->preference("FinesLog") ) { |
410 |
logaction( "FINES", "MODIFY", $offset->id, |
411 |
Dumper( $offset->get_columns() ) ); |
412 |
} |
413 |
|
414 |
return $offset; |
415 |
} |
416 |
} |
417 |
|
418 |
=head2 RecalculateAccountBalance |
419 |
|
420 |
$account_balance = RecalculateAccountBalance({ |
421 |
borrower => $borrower |
422 |
}); |
423 |
|
424 |
Recalculates a borrower's balance based on the |
425 |
sum of the amount outstanding for the borrower's |
426 |
debits minus the sum of the amount remaining for |
427 |
the borrowers credits. |
428 |
|
429 |
TODO: Would it be better to use af.amount_original - ap.amount_paid for any reason? |
430 |
Or, perhaps calculate both and compare the two, for error checking purposes. |
431 |
=cut |
432 |
|
433 |
sub RecalculateAccountBalance { |
434 |
my ($params) = @_; |
435 |
|
436 |
my $borrower = $params->{borrower}; |
437 |
croak("Requred parameter 'borrower' not passed in!") |
438 |
unless ($borrower); |
439 |
|
440 |
my $debits = |
441 |
Koha::Database->new()->schema->resultset('AccountDebit') |
442 |
->search( { borrowernumber => $borrower->borrowernumber() } ); |
443 |
my $amount_outstanding = $debits->get_column('amount_outstanding')->sum() |
444 |
|| 0; |
445 |
|
446 |
my $credits = |
447 |
Koha::Database->new()->schema->resultset('AccountCredit') |
448 |
->search( { borrowernumber => $borrower->borrowernumber() } ); |
449 |
my $amount_remaining = $credits->get_column('amount_remaining')->sum() || 0; |
450 |
|
451 |
my $account_balance = $amount_outstanding - $amount_remaining; |
452 |
$borrower->account_balance($account_balance); |
453 |
$borrower->update(); |
454 |
|
455 |
return $account_balance; |
456 |
} |
457 |
|
458 |
=head2 VoidCredit |
459 |
$account_balance = VoidCredit({ id => $credit_id }); |
460 |
|
461 |
Reverts a payment. All debits paid by this payment will be |
462 |
updated such that the amount offset is reinstated for the debit. |
463 |
=cut |
464 |
|
465 |
sub VoidCredit { |
466 |
my ($params) = @_; |
467 |
|
468 |
my $id = $params->{id}; |
469 |
|
470 |
croak("No id passed in!") unless $id; |
471 |
|
472 |
my $schema = Koha::Database->new()->schema(); |
473 |
|
474 |
my $credit = $schema->resultset('AccountCredit')->find($id); |
475 |
|
476 |
foreach my $offset ( $credit->account_offsets() ) { |
477 |
my $debit = $offset->debit(); |
478 |
|
479 |
$debit->amount_outstanding( |
480 |
$debit->amount_outstanding() - $offset->amount() ); |
481 |
$debit->updated_on( get_timestamp() ); |
482 |
$debit->update(); |
483 |
|
484 |
Koha::Database->new()->schema->resultset('AccountOffset')->create( |
485 |
{ |
486 |
amount => $offset->amount() * -1, |
487 |
debit_id => $debit->id(), |
488 |
credit_id => $credit->id(), |
489 |
created_on => get_timestamp(), |
490 |
type => Koha::Accounts::OffsetTypes::Void(), |
491 |
} |
492 |
); |
493 |
} |
494 |
|
495 |
$credit->amount_voided( $credit->amount_paid ); |
496 |
$credit->amount_paid(0); |
497 |
$credit->amount_remaining(0); |
498 |
$credit->updated_on( get_timestamp() ); |
499 |
$credit->update(); |
500 |
|
501 |
return RecalculateAccountBalance( { borrower => $credit->borrower() } ); |
502 |
} |
503 |
|
504 |
=head2 NormalizeBalances |
505 |
|
506 |
$account_balance = NormalizeBalances({ borrower => $borrower }); |
507 |
|
508 |
For a given borrower, this subroutine will find all debits |
509 |
with an outstanding balance and all credits with an unused |
510 |
amount remaining and will pay those debits with those credits. |
511 |
|
512 |
=cut |
513 |
|
514 |
sub NormalizeBalances { |
515 |
my ($params) = @_; |
516 |
|
517 |
my $borrower = $params->{borrower}; |
518 |
|
519 |
croak("Required param 'borrower' not passed in!") unless $borrower; |
520 |
|
521 |
my $schema = Koha::Database->new()->schema(); |
522 |
|
523 |
my @credits = $schema->resultset('AccountCredit')->search( |
524 |
{ |
525 |
borrowernumber => $borrower->borrowernumber(), |
526 |
amount_remaining => { '>' => '0' } |
527 |
} |
528 |
); |
529 |
|
530 |
my @debits = $schema->resultset('AccountDebit')->search( |
531 |
{ |
532 |
borrowernumber => $borrower->borrowernumber(), |
533 |
amount_outstanding => { '>' => '0' } |
534 |
} |
535 |
); |
536 |
|
537 |
foreach my $credit (@credits) { |
538 |
foreach my $debit (@debits) { |
539 |
if ( $credit->amount_remaining() |
540 |
&& $debit->amount_outstanding() ) |
541 |
{ |
542 |
CreditDebit( { credit => $credit, debit => $debit } ); |
543 |
} |
544 |
} |
545 |
} |
546 |
|
547 |
return RecalculateAccountBalance( { borrower => $borrower } ); |
548 |
} |
549 |
|
550 |
1; |
551 |
__END__ |
552 |
|
553 |
=head1 AUTHOR |
554 |
|
555 |
Kyle M Hall <kyle@bywatersolutions.com> |
556 |
|
557 |
=cut |