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 2 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 |
|
93 |
my $type = $params->{type}; |
94 |
my $itemnumber = $params->{itemnumber}; |
95 |
my $issue_id = $params->{issue_id}; |
96 |
my $description = $params->{description}; |
97 |
my $notes = $params->{notes}; |
98 |
|
99 |
my $branchcode = $params->{branchcode}; |
100 |
$branchcode ||= |
101 |
defined( C4::Context->userenv ) |
102 |
? C4::Context->userenv->{branch} |
103 |
: undef; |
104 |
|
105 |
my $manager_id = $params->{manager_id}; |
106 |
$manager_id ||= |
107 |
defined( C4::Context->userenv ) |
108 |
? C4::Context->userenv->{manager_id} |
109 |
: undef; |
110 |
|
111 |
my $accruing = $params->{accruing} || 0; |
112 |
|
113 |
croak("Required parameter 'borrower' not passed in.") |
114 |
unless ($borrower); |
115 |
croak("Required parameter 'amount' not passed in.") |
116 |
unless ($amount); |
117 |
croak("Invalid debit type: '$type'!") |
118 |
unless ( Koha::Accounts::DebitTypes::IsValid($type) ); |
119 |
croak("No issue id passed in for accruing debit!") |
120 |
if ( $accruing && !$issue_id ); |
121 |
|
122 |
my $debit = |
123 |
Koha::Database->new()->schema->resultset('AccountDebit')->create( |
124 |
{ |
125 |
borrowernumber => $borrower->borrowernumber(), |
126 |
itemnumber => $itemnumber, |
127 |
issue_id => $issue_id, |
128 |
type => $type, |
129 |
accruing => $accruing, |
130 |
amount_original => $amount, |
131 |
amount_outstanding => $amount, |
132 |
amount_last_increment => $amount, |
133 |
description => $description, |
134 |
notes => $notes, |
135 |
manager_id => $manager_id, |
136 |
created_on => get_timestamp(), |
137 |
} |
138 |
); |
139 |
|
140 |
if ($debit) { |
141 |
$borrower->account_balance( $borrower->account_balance() + $amount ); |
142 |
$borrower->update(); |
143 |
|
144 |
NormalizeBalances( { borrower => $borrower } ); |
145 |
|
146 |
if ( C4::Context->preference("FinesLog") ) { |
147 |
logaction( "FINES", "CREATE_FEE", $debit->id, |
148 |
Dumper( $debit->get_columns() ) ); |
149 |
} |
150 |
} |
151 |
else { |
152 |
carp("Something went wrong! Debit not created!"); |
153 |
} |
154 |
|
155 |
return $debit; |
156 |
} |
157 |
|
158 |
=head2 DebitLostItem |
159 |
|
160 |
my $debit = DebitLostItem({ |
161 |
borrower => $borrower, |
162 |
issue => $issue, |
163 |
}); |
164 |
|
165 |
DebitLostItem adds a replacement fee charge for the item |
166 |
of the given issue. |
167 |
|
168 |
=cut |
169 |
|
170 |
sub DebitLostItem { |
171 |
my ($params) = @_; |
172 |
|
173 |
my $borrower = $params->{borrower}; |
174 |
my $issue = $params->{issue}; |
175 |
|
176 |
croak("Required param 'borrower' not passed in!") unless ($borrower); |
177 |
croak("Required param 'issue' not passed in!") unless ($issue); |
178 |
|
179 |
# Don't add lost debit if borrower has already been charged for this lost item before, |
180 |
# for this issue. It seems reasonable that a borrower could lose an item, find and return it, |
181 |
# check it out again, and lose it again, so we should do this based on issue_id, not itemnumber. |
182 |
unless ( |
183 |
Koha::Database->new()->schema->resultset('AccountDebit')->search( |
184 |
{ |
185 |
borrowernumber => $borrower->borrowernumber(), |
186 |
issue_id => $issue->issue_id(), |
187 |
type => Koha::Accounts::DebitTypes::Lost |
188 |
} |
189 |
)->count() |
190 |
) |
191 |
{ |
192 |
my $item = $issue->item(); |
193 |
|
194 |
$params->{accruing} = 0; |
195 |
$params->{type} = Koha::Accounts::DebitTypes::Lost; |
196 |
$params->{amount} = $item->replacementprice(); |
197 |
$params->{itemnumber} = $item->itemnumber(); |
198 |
$params->{issue_id} = $issue->issue_id(); |
199 |
|
200 |
#TODO: Shouldn't we have a default replacement price as a syspref? |
201 |
if ( $params->{amount} ) { |
202 |
return AddDebit($params); |
203 |
} |
204 |
else { |
205 |
carp("Cannot add lost debit! Item has no replacement price!"); |
206 |
} |
207 |
} |
208 |
} |
209 |
|
210 |
=head2 CreditLostItem |
211 |
|
212 |
my $debit = CreditLostItem( |
213 |
{ |
214 |
borrower => $borrower, |
215 |
debit => $debit, |
216 |
} |
217 |
); |
218 |
|
219 |
CreditLostItem creates a payment in the amount equal |
220 |
to the replacement price charge created by DebitLostItem. |
221 |
|
222 |
=cut |
223 |
|
224 |
sub CreditLostItem { |
225 |
my ($params) = @_; |
226 |
|
227 |
my $borrower = $params->{borrower}; |
228 |
my $debit = $params->{debit}; |
229 |
|
230 |
croak("Required param 'borrower' not passed in!") unless ($borrower); |
231 |
croak("Required param 'debit' not passed in!") |
232 |
unless ($debit); |
233 |
|
234 |
my $item = |
235 |
Koha::Database->new()->schema->resultset('Item') |
236 |
->find( $debit->itemnumber() ); |
237 |
carp("No item found!") unless $item; |
238 |
|
239 |
$params->{type} = Koha::Accounts::CreditTypes::Found; |
240 |
$params->{amount} = $debit->amount_original(); |
241 |
$params->{debit_id} = $debit->debit_id(); |
242 |
$params->{notes} = "Lost item found: " . $item->barcode(); |
243 |
|
244 |
return AddCredit($params); |
245 |
} |
246 |
|
247 |
=head2 AddCredit |
248 |
|
249 |
AddCredit({ |
250 |
borrower => $borrower, |
251 |
amount => $amount, |
252 |
[ branchcode => $branchcode, ] |
253 |
[ manager_id => $manager_id, ] |
254 |
[ debit_id => $debit_id, ] # The primary debit to be paid |
255 |
[ notes => $notes, ] |
256 |
}); |
257 |
|
258 |
Record credit by a patron. C<$borrowernumber> is the patron's |
259 |
borrower number. C<$credit> is a floating-point number, giving the |
260 |
amount that was paid. |
261 |
|
262 |
Amounts owed are paid off oldest first. That is, if the patron has a |
263 |
$1 fine from Feb. 1, another $1 fine from Mar. 1, and makes a credit |
264 |
of $1.50, then the oldest fine will be paid off in full, and $0.50 |
265 |
will be credited to the next one. |
266 |
|
267 |
debit_id can be passed as a scalar or an array ref to make the passed |
268 |
in debit or debits the first to be credited. |
269 |
|
270 |
=cut |
271 |
|
272 |
sub AddCredit { |
273 |
my ($params) = @_; |
274 |
|
275 |
my $type = $params->{type}; |
276 |
my $borrower = $params->{borrower}; |
277 |
my $amount = $params->{amount}; |
278 |
my $amount_received = $params->{amount_received}; |
279 |
my $debit_id = $params->{debit_id}; |
280 |
my $notes = $params->{notes}; |
281 |
my $branchcode = $params->{branchcode}; |
282 |
my $manager_id = $params->{manager_id}; |
283 |
|
284 |
my $userenv = C4::Context->userenv; |
285 |
|
286 |
unless ( $manager_id || $userenv ) { |
287 |
$manager_id = $userenv->{number}; |
288 |
} |
289 |
|
290 |
unless ( $branchcode || $userenv ) { |
291 |
$branchcode = $userenv->{branch}; |
292 |
} |
293 |
|
294 |
unless ($borrower) { |
295 |
croak("Required parameter 'borrower' not passed in"); |
296 |
} |
297 |
unless ($amount) { |
298 |
croak("Required parameter amount not passed in"); |
299 |
} |
300 |
|
301 |
unless ( Koha::Accounts::CreditTypes::IsValid($type) ) { |
302 |
carp("Invalid credit type! Returning without creating credit."); |
303 |
return; |
304 |
} |
305 |
|
306 |
unless ($type) { |
307 |
carp("No type passed in, assuming Payment"); |
308 |
$type = Koha::Accounts::CreditTypes::Payment; |
309 |
} |
310 |
|
311 |
my $debit = |
312 |
Koha::Database->new()->schema->resultset('AccountDebit')->find($debit_id); |
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 |
manager_id => $manager_id, |
325 |
created_on => get_timestamp(), |
326 |
} |
327 |
); |
328 |
|
329 |
if ( C4::Context->preference("FinesLog") ) { |
330 |
logaction( "FINES", "CREATE_PAYMENT", $credit->id, |
331 |
Dumper( $credit->get_columns() ) ); |
332 |
} |
333 |
|
334 |
$borrower->account_balance( $borrower->account_balance() - $amount ); |
335 |
$borrower->update(); |
336 |
|
337 |
# If we are given specific debits, pay those ones first. |
338 |
if ($debit_id) { |
339 |
my @debit_ids = ref($debit_id) eq "ARRAY" ? @$debit_id : $debit_id; |
340 |
foreach my $debit_id (@debit_ids) { |
341 |
my $debit = |
342 |
Koha::Database->new()->schema->resultset('AccountDebit') |
343 |
->find($debit_id); |
344 |
|
345 |
if ($debit) { |
346 |
CreditDebit( { credit => $credit, debit => $debit } ); |
347 |
} |
348 |
else { |
349 |
carp("Invalid debit_id passed in!"); |
350 |
} |
351 |
} |
352 |
} |
353 |
|
354 |
# We still have leftover money, or we weren't given a specific debit to pay |
355 |
if ( $credit->amount_remaining() > 0 ) { |
356 |
my @debits = |
357 |
Koha::Database->new()->schema->resultset('AccountDebit')->search( |
358 |
{ |
359 |
borrowernumber => $borrower->borrowernumber(), |
360 |
amount_outstanding => { '>' => '0' } |
361 |
} |
362 |
); |
363 |
|
364 |
foreach my $debit (@debits) { |
365 |
if ( $credit->amount_remaining() > 0 ) { |
366 |
CreditDebit( |
367 |
{ |
368 |
credit => $credit, |
369 |
debit => $debit, |
370 |
borrower => $borrower, |
371 |
type => $type, |
372 |
} |
373 |
); |
374 |
} |
375 |
} |
376 |
} |
377 |
|
378 |
return $credit; |
379 |
} |
380 |
|
381 |
=head2 CreditDebit |
382 |
|
383 |
$account_offset = CreditDebit({ |
384 |
credit => $credit, |
385 |
debit => $debit, |
386 |
}); |
387 |
|
388 |
Given a credit and a debit, this subroutine |
389 |
will pay the appropriate amount of the debit, |
390 |
update the debit's amount outstanding, the credit's |
391 |
amout remaining, and create the appropriate account |
392 |
offset. |
393 |
|
394 |
=cut |
395 |
|
396 |
sub CreditDebit { |
397 |
my ($params) = @_; |
398 |
|
399 |
my $credit = $params->{credit}; |
400 |
my $debit = $params->{debit}; |
401 |
|
402 |
croak("Required parameter 'credit' not passed in!") |
403 |
unless $credit; |
404 |
croak("Required parameter 'debit' not passed in!") unless $debit; |
405 |
|
406 |
my $amount_to_pay = |
407 |
( $debit->amount_outstanding() > $credit->amount_remaining() ) |
408 |
? $credit->amount_remaining() |
409 |
: $debit->amount_outstanding(); |
410 |
|
411 |
if ( $amount_to_pay > 0 ) { |
412 |
$debit->amount_outstanding( |
413 |
$debit->amount_outstanding() - $amount_to_pay ); |
414 |
$debit->update(); |
415 |
|
416 |
$credit->amount_remaining( |
417 |
$credit->amount_remaining() - $amount_to_pay ); |
418 |
$credit->update(); |
419 |
|
420 |
my $offset = |
421 |
Koha::Database->new()->schema->resultset('AccountOffset')->create( |
422 |
{ |
423 |
amount => $amount_to_pay * -1, |
424 |
debit_id => $debit->id(), |
425 |
credit_id => $credit->id(), |
426 |
created_on => get_timestamp(), |
427 |
} |
428 |
); |
429 |
|
430 |
if ( C4::Context->preference("FinesLog") ) { |
431 |
logaction( "FINES", "MODIFY", $offset->id, |
432 |
Dumper( $offset->get_columns() ) ); |
433 |
} |
434 |
|
435 |
return $offset; |
436 |
} |
437 |
} |
438 |
|
439 |
=head2 RecalculateAccountBalance |
440 |
|
441 |
$account_balance = RecalculateAccountBalance({ |
442 |
borrower => $borrower |
443 |
}); |
444 |
|
445 |
Recalculates a borrower's balance based on the |
446 |
sum of the amount outstanding for the borrower's |
447 |
debits minus the sum of the amount remaining for |
448 |
the borrowers credits. |
449 |
|
450 |
TODO: Would it be better to use af.amount_original - ap.amount_paid for any reason? |
451 |
Or, perhaps calculate both and compare the two, for error checking purposes. |
452 |
=cut |
453 |
|
454 |
sub RecalculateAccountBalance { |
455 |
my ($params) = @_; |
456 |
|
457 |
my $borrower = $params->{borrower}; |
458 |
croak("Requred paramter 'borrower' not passed in!") |
459 |
unless ($borrower); |
460 |
|
461 |
my $debits = |
462 |
Koha::Database->new()->schema->resultset('AccountDebit') |
463 |
->search( { borrowernumber => $borrower->borrowernumber() } ); |
464 |
my $amount_outstanding = $debits->get_column('amount_outstanding')->sum(); |
465 |
|
466 |
my $credits = |
467 |
Koha::Database->new()->schema->resultset('AccountCredit') |
468 |
->search( { borrowernumber => $borrower->borrowernumber() } ); |
469 |
my $amount_remaining = $credits->get_column('amount_remaining')->sum(); |
470 |
|
471 |
my $account_balance = $amount_outstanding - $amount_remaining; |
472 |
$borrower->account_balance($account_balance); |
473 |
$borrower->update(); |
474 |
|
475 |
return $account_balance; |
476 |
} |
477 |
|
478 |
=head2 NormalizeBalances |
479 |
|
480 |
$account_balance = NormalizeBalances({ borrower => $borrower }); |
481 |
|
482 |
For a given borrower, this subroutine will find all debits |
483 |
with an outstanding balance and all credits with an unused |
484 |
amount remaining and will pay those debits with those credits. |
485 |
|
486 |
=cut |
487 |
|
488 |
sub NormalizeBalances { |
489 |
my ($params) = @_; |
490 |
|
491 |
my $borrower = $params->{borrower}; |
492 |
|
493 |
croak("Required param 'borrower' not passed in!") unless $borrower; |
494 |
|
495 |
my @credits = |
496 |
Koha::Database->new()->schema->resultset('AccountCredit')->search( |
497 |
{ |
498 |
borrowernumber => $borrower->borrowernumber(), |
499 |
amount_remaining => { '>' => '0' } |
500 |
} |
501 |
); |
502 |
|
503 |
return unless @credits; |
504 |
|
505 |
my @debits = |
506 |
Koha::Database->new()->schema->resultset('AccountDebit')->search( |
507 |
{ |
508 |
borrowernumber => $borrower->borrowernumber(), |
509 |
amount_outstanding => { '>' => '0' } |
510 |
} |
511 |
); |
512 |
|
513 |
return unless @debits; |
514 |
|
515 |
foreach my $credit (@credits) { |
516 |
foreach my $debit (@debits) { |
517 |
if ( $credit->amount_remaining() |
518 |
&& $debit->amount_outstanding() ) |
519 |
{ |
520 |
CreditDebit( { credit => $credit, debit => $debit } ); |
521 |
} |
522 |
} |
523 |
} |
524 |
|
525 |
return RecalculateAccountBalance( { borrower => $borrower } ); |
526 |
} |
527 |
|
528 |
1; |
529 |
__END__ |
530 |
|
531 |
=head1 AUTHOR |
532 |
|
533 |
Kyle M Hall <kyle@bywatersolutions.com> |
534 |
|
535 |
=cut |