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 |
|
29 |
use Koha::Accounts::CreditTypes; |
30 |
use Koha::Accounts::DebitTypes; |
31 |
|
32 |
use vars qw($VERSION @ISA @EXPORT); |
33 |
|
34 |
BEGIN { |
35 |
require Exporter; |
36 |
@ISA = qw(Exporter); |
37 |
@EXPORT = qw( |
38 |
AddDebit |
39 |
AddCredit |
40 |
|
41 |
NormalizeBalances |
42 |
|
43 |
RecalculateAccountBalance |
44 |
|
45 |
DebitLostItem |
46 |
CreditLostItem |
47 |
); |
48 |
} |
49 |
|
50 |
=head1 NAME |
51 |
|
52 |
Koha::Accounts - Functions for dealing with Koha accounts |
53 |
|
54 |
=head1 SYNOPSIS |
55 |
|
56 |
use Koha::Accounts; |
57 |
|
58 |
=head1 DESCRIPTION |
59 |
|
60 |
The functions in this module deal with the monetary aspect of Koha, |
61 |
including looking up and modifying the amount of money owed by a |
62 |
patron. |
63 |
|
64 |
=head1 FUNCTIONS |
65 |
|
66 |
=head2 AddDebit |
67 |
|
68 |
my $debit = AddDebit({ |
69 |
borrower => $borrower, |
70 |
amount => $amount, |
71 |
[ type => $type, ] |
72 |
[ itemnumber => $itemnumber, ] |
73 |
[ issue_id => $issue_id, ] |
74 |
[ description => $description, ] |
75 |
[ notes => $notes, ] |
76 |
[ branchcode => $branchcode, ] |
77 |
[ manager_id => $manager_id, ] |
78 |
[ accruing => $accruing, ] # Default 0 if not accruing, 1 if accruing |
79 |
}); |
80 |
|
81 |
Create a new debit for a given borrower. To standardize nomenclature, any charge |
82 |
against a borrower ( e.g. a fine, a new card charge, the cost of losing an item ) |
83 |
will be referred to as a 'debit'. |
84 |
|
85 |
=cut |
86 |
|
87 |
sub AddDebit { |
88 |
my ($params) = @_; |
89 |
|
90 |
my $borrower = $params->{borrower}; |
91 |
my $amount = $params->{amount}; |
92 |
my $type = $params->{type}; |
93 |
my $itemnumber = $params->{itemnumber}; |
94 |
my $issue_id = $params->{issue_id}; |
95 |
my $description = $params->{description}; |
96 |
my $notes = $params->{notes}; |
97 |
my $branchcode = $params->{branchcode}; |
98 |
my $manager_id = $params->{manager_id}; |
99 |
|
100 |
my $userenv = C4::Context->userenv; |
101 |
|
102 |
$branchcode ||= |
103 |
$userenv |
104 |
? $userenv->{branch} |
105 |
: undef; |
106 |
|
107 |
$manager_id ||= |
108 |
$userenv |
109 |
? $userenv->{number} |
110 |
: undef; |
111 |
|
112 |
my $accruing = $params->{accruing} || 0; |
113 |
|
114 |
croak("Required parameter 'borrower' not passed in.") |
115 |
unless ($borrower); |
116 |
croak("Required parameter 'amount' not passed in.") |
117 |
unless ($amount); |
118 |
croak("Invalid debit type: '$type'!") |
119 |
unless ( Koha::Accounts::DebitTypes::IsValid($type) ); |
120 |
croak("No issue id passed in for accruing debit!") |
121 |
if ( $accruing && !$issue_id ); |
122 |
|
123 |
my $debit = |
124 |
Koha::Database->new()->schema->resultset('AccountDebit')->create( |
125 |
{ |
126 |
borrowernumber => $borrower->borrowernumber(), |
127 |
itemnumber => $itemnumber, |
128 |
issue_id => $issue_id, |
129 |
type => $type, |
130 |
accruing => $accruing, |
131 |
amount_original => $amount, |
132 |
amount_outstanding => $amount, |
133 |
amount_last_increment => $amount, |
134 |
description => $description, |
135 |
notes => $notes, |
136 |
branchcode => $branchcode, |
137 |
manager_id => $manager_id, |
138 |
created_on => get_timestamp(), |
139 |
} |
140 |
); |
141 |
|
142 |
if ($debit) { |
143 |
$borrower->account_balance( $borrower->account_balance() + $amount ); |
144 |
$borrower->update(); |
145 |
|
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 |
|
202 |
#TODO: Shouldn't we have a default replacement price as a syspref? |
203 |
if ( $params->{amount} ) { |
204 |
return AddDebit($params); |
205 |
} |
206 |
else { |
207 |
carp("Cannot add lost debit! Item has no replacement price!"); |
208 |
} |
209 |
} |
210 |
} |
211 |
|
212 |
=head2 CreditLostItem |
213 |
|
214 |
my $debit = CreditLostItem( |
215 |
{ |
216 |
borrower => $borrower, |
217 |
debit => $debit, |
218 |
} |
219 |
); |
220 |
|
221 |
CreditLostItem creates a payment in the amount equal |
222 |
to the replacement price charge created by DebitLostItem. |
223 |
|
224 |
=cut |
225 |
|
226 |
sub CreditLostItem { |
227 |
my ($params) = @_; |
228 |
|
229 |
my $borrower = $params->{borrower}; |
230 |
my $debit = $params->{debit}; |
231 |
|
232 |
croak("Required param 'borrower' not passed in!") unless ($borrower); |
233 |
croak("Required param 'debit' not passed in!") |
234 |
unless ($debit); |
235 |
|
236 |
my $item = |
237 |
Koha::Database->new()->schema->resultset('Item') |
238 |
->find( $debit->itemnumber() ); |
239 |
carp("No item found!") unless $item; |
240 |
|
241 |
$params->{type} = Koha::Accounts::CreditTypes::Found; |
242 |
$params->{amount} = $debit->amount_original(); |
243 |
$params->{debit_id} = $debit->debit_id(); |
244 |
$params->{notes} = "Lost item found: " . $item->barcode(); |
245 |
|
246 |
return AddCredit($params); |
247 |
} |
248 |
|
249 |
=head2 AddCredit |
250 |
|
251 |
AddCredit({ |
252 |
borrower => $borrower, |
253 |
amount => $amount, |
254 |
[ branchcode => $branchcode, ] |
255 |
[ manager_id => $manager_id, ] |
256 |
[ debit_id => $debit_id, ] # The primary debit to be paid |
257 |
[ notes => $notes, ] |
258 |
}); |
259 |
|
260 |
Record credit by a patron. C<$borrowernumber> is the patron's |
261 |
borrower number. C<$credit> is a floating-point number, giving the |
262 |
amount that was paid. |
263 |
|
264 |
Amounts owed are paid off oldest first. That is, if the patron has a |
265 |
$1 fine from Feb. 1, another $1 fine from Mar. 1, and makes a credit |
266 |
of $1.50, then the oldest fine will be paid off in full, and $0.50 |
267 |
will be credited to the next one. |
268 |
|
269 |
debit_id can be passed as a scalar or an array ref to make the passed |
270 |
in debit or debits the first to be credited. |
271 |
|
272 |
=cut |
273 |
|
274 |
sub AddCredit { |
275 |
my ($params) = @_; |
276 |
|
277 |
my $type = $params->{type}; |
278 |
my $borrower = $params->{borrower}; |
279 |
my $amount = $params->{amount}; |
280 |
my $amount_received = $params->{amount_received}; |
281 |
my $debit_id = $params->{debit_id}; |
282 |
my $notes = $params->{notes}; |
283 |
my $branchcode = $params->{branchcode}; |
284 |
my $manager_id = $params->{manager_id}; |
285 |
|
286 |
my $userenv = C4::Context->userenv; |
287 |
|
288 |
$branchcode ||= |
289 |
$userenv |
290 |
? $userenv->{branch} |
291 |
: undef; |
292 |
|
293 |
$manager_id ||= |
294 |
$userenv |
295 |
? $userenv->{number} |
296 |
: undef; |
297 |
|
298 |
unless ($borrower) { |
299 |
croak("Required parameter 'borrower' not passed in"); |
300 |
} |
301 |
unless ($amount) { |
302 |
croak("Required parameter amount not passed in"); |
303 |
} |
304 |
|
305 |
unless ( Koha::Accounts::CreditTypes::IsValid($type) ) { |
306 |
carp("Invalid credit type! Returning without creating credit."); |
307 |
return; |
308 |
} |
309 |
|
310 |
unless ($type) { |
311 |
carp("No type passed in, assuming Payment"); |
312 |
$type = Koha::Accounts::CreditTypes::Payment; |
313 |
} |
314 |
|
315 |
my $debit = |
316 |
Koha::Database->new()->schema->resultset('AccountDebit')->find($debit_id); |
317 |
|
318 |
# First, we make the credit. We'll worry about what we paid later on |
319 |
my $credit = |
320 |
Koha::Database->new()->schema->resultset('AccountCredit')->create( |
321 |
{ |
322 |
borrowernumber => $borrower->borrowernumber(), |
323 |
type => $type, |
324 |
amount_received => $amount_received, |
325 |
amount_paid => $amount, |
326 |
amount_remaining => $amount, |
327 |
notes => $notes, |
328 |
branchcode => $branchcode, |
329 |
manager_id => $manager_id, |
330 |
created_on => get_timestamp(), |
331 |
} |
332 |
); |
333 |
|
334 |
if ( C4::Context->preference("FinesLog") ) { |
335 |
logaction( "FINES", "CREATE_PAYMENT", $credit->id, |
336 |
Dumper( $credit->get_columns() ) ); |
337 |
} |
338 |
|
339 |
$borrower->account_balance( $borrower->account_balance() - $amount ); |
340 |
$borrower->update(); |
341 |
|
342 |
# If we are given specific debits, pay those ones first. |
343 |
if ($debit_id) { |
344 |
my @debit_ids = ref($debit_id) eq "ARRAY" ? @$debit_id : $debit_id; |
345 |
foreach my $debit_id (@debit_ids) { |
346 |
my $debit = |
347 |
Koha::Database->new()->schema->resultset('AccountDebit') |
348 |
->find($debit_id); |
349 |
|
350 |
if ($debit) { |
351 |
CreditDebit( { credit => $credit, debit => $debit } ); |
352 |
} |
353 |
else { |
354 |
carp("Invalid debit_id passed in!"); |
355 |
} |
356 |
} |
357 |
} |
358 |
|
359 |
# We still have leftover money, or we weren't given a specific debit to pay |
360 |
if ( $credit->amount_remaining() > 0 ) { |
361 |
my @debits = |
362 |
Koha::Database->new()->schema->resultset('AccountDebit')->search( |
363 |
{ |
364 |
borrowernumber => $borrower->borrowernumber(), |
365 |
amount_outstanding => { '>' => '0' } |
366 |
} |
367 |
); |
368 |
|
369 |
foreach my $debit (@debits) { |
370 |
if ( $credit->amount_remaining() > 0 ) { |
371 |
CreditDebit( |
372 |
{ |
373 |
credit => $credit, |
374 |
debit => $debit, |
375 |
borrower => $borrower, |
376 |
type => $type, |
377 |
} |
378 |
); |
379 |
} |
380 |
} |
381 |
} |
382 |
|
383 |
return $credit; |
384 |
} |
385 |
|
386 |
=head2 CreditDebit |
387 |
|
388 |
$account_offset = CreditDebit({ |
389 |
credit => $credit, |
390 |
debit => $debit, |
391 |
}); |
392 |
|
393 |
Given a credit and a debit, this subroutine |
394 |
will pay the appropriate amount of the debit, |
395 |
update the debit's amount outstanding, the credit's |
396 |
amout remaining, and create the appropriate account |
397 |
offset. |
398 |
|
399 |
=cut |
400 |
|
401 |
sub CreditDebit { |
402 |
my ($params) = @_; |
403 |
|
404 |
my $credit = $params->{credit}; |
405 |
my $debit = $params->{debit}; |
406 |
|
407 |
croak("Required parameter 'credit' not passed in!") |
408 |
unless $credit; |
409 |
croak("Required parameter 'debit' not passed in!") unless $debit; |
410 |
|
411 |
my $amount_to_pay = |
412 |
( $debit->amount_outstanding() > $credit->amount_remaining() ) |
413 |
? $credit->amount_remaining() |
414 |
: $debit->amount_outstanding(); |
415 |
|
416 |
if ( $amount_to_pay > 0 ) { |
417 |
$debit->amount_outstanding( |
418 |
$debit->amount_outstanding() - $amount_to_pay ); |
419 |
$debit->update(); |
420 |
|
421 |
$credit->amount_remaining( |
422 |
$credit->amount_remaining() - $amount_to_pay ); |
423 |
$credit->update(); |
424 |
|
425 |
my $offset = |
426 |
Koha::Database->new()->schema->resultset('AccountOffset')->create( |
427 |
{ |
428 |
amount => $amount_to_pay * -1, |
429 |
debit_id => $debit->id(), |
430 |
credit_id => $credit->id(), |
431 |
created_on => get_timestamp(), |
432 |
} |
433 |
); |
434 |
|
435 |
if ( C4::Context->preference("FinesLog") ) { |
436 |
logaction( "FINES", "MODIFY", $offset->id, |
437 |
Dumper( $offset->get_columns() ) ); |
438 |
} |
439 |
|
440 |
return $offset; |
441 |
} |
442 |
} |
443 |
|
444 |
=head2 RecalculateAccountBalance |
445 |
|
446 |
$account_balance = RecalculateAccountBalance({ |
447 |
borrower => $borrower |
448 |
}); |
449 |
|
450 |
Recalculates a borrower's balance based on the |
451 |
sum of the amount outstanding for the borrower's |
452 |
debits minus the sum of the amount remaining for |
453 |
the borrowers credits. |
454 |
|
455 |
TODO: Would it be better to use af.amount_original - ap.amount_paid for any reason? |
456 |
Or, perhaps calculate both and compare the two, for error checking purposes. |
457 |
=cut |
458 |
|
459 |
sub RecalculateAccountBalance { |
460 |
my ($params) = @_; |
461 |
|
462 |
my $borrower = $params->{borrower}; |
463 |
croak("Requred paramter 'borrower' not passed in!") |
464 |
unless ($borrower); |
465 |
|
466 |
my $debits = |
467 |
Koha::Database->new()->schema->resultset('AccountDebit') |
468 |
->search( { borrowernumber => $borrower->borrowernumber() } ); |
469 |
my $amount_outstanding = $debits->get_column('amount_outstanding')->sum() || 0; |
470 |
|
471 |
my $credits = |
472 |
Koha::Database->new()->schema->resultset('AccountCredit') |
473 |
->search( { borrowernumber => $borrower->borrowernumber() } ); |
474 |
my $amount_remaining = $credits->get_column('amount_remaining')->sum() || 0; |
475 |
|
476 |
my $account_balance = $amount_outstanding - $amount_remaining; |
477 |
$borrower->account_balance($account_balance); |
478 |
$borrower->update(); |
479 |
|
480 |
return $account_balance; |
481 |
} |
482 |
|
483 |
=head2 NormalizeBalances |
484 |
|
485 |
$account_balance = NormalizeBalances({ borrower => $borrower }); |
486 |
|
487 |
For a given borrower, this subroutine will find all debits |
488 |
with an outstanding balance and all credits with an unused |
489 |
amount remaining and will pay those debits with those credits. |
490 |
|
491 |
=cut |
492 |
|
493 |
sub NormalizeBalances { |
494 |
my ($params) = @_; |
495 |
|
496 |
my $borrower = $params->{borrower}; |
497 |
|
498 |
croak("Required param 'borrower' not passed in!") unless $borrower; |
499 |
|
500 |
my @credits = |
501 |
Koha::Database->new()->schema->resultset('AccountCredit')->search( |
502 |
{ |
503 |
borrowernumber => $borrower->borrowernumber(), |
504 |
amount_remaining => { '>' => '0' } |
505 |
} |
506 |
); |
507 |
|
508 |
return unless @credits; |
509 |
|
510 |
my @debits = |
511 |
Koha::Database->new()->schema->resultset('AccountDebit')->search( |
512 |
{ |
513 |
borrowernumber => $borrower->borrowernumber(), |
514 |
amount_outstanding => { '>' => '0' } |
515 |
} |
516 |
); |
517 |
|
518 |
return unless @debits; |
519 |
|
520 |
foreach my $credit (@credits) { |
521 |
foreach my $debit (@debits) { |
522 |
if ( $credit->amount_remaining() |
523 |
&& $debit->amount_outstanding() ) |
524 |
{ |
525 |
CreditDebit( { credit => $credit, debit => $debit } ); |
526 |
} |
527 |
} |
528 |
} |
529 |
|
530 |
return RecalculateAccountBalance( { borrower => $borrower } ); |
531 |
} |
532 |
|
533 |
1; |
534 |
__END__ |
535 |
|
536 |
=head1 AUTHOR |
537 |
|
538 |
Kyle M Hall <kyle@bywatersolutions.com> |
539 |
|
540 |
=cut |