|
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 |
borrowernumber => $borrowernumber, |
| 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 = Koha::Database->new()->schema->resultset('AccountDebit')->create( |
| 123 |
{ |
| 124 |
borrowernumber => $borrower->borrowernumber(), |
| 125 |
itemnumber => $itemnumber, |
| 126 |
issue_id => $issue_id, |
| 127 |
type => $type, |
| 128 |
accruing => $accruing, |
| 129 |
amount_original => $amount, |
| 130 |
amount_outstanding => $amount, |
| 131 |
amount_last_increment => $amount, |
| 132 |
description => $description, |
| 133 |
notes => $notes, |
| 134 |
manager_id => $manager_id, |
| 135 |
created_on => get_timestamp(), |
| 136 |
} |
| 137 |
); |
| 138 |
|
| 139 |
if ($debit) { |
| 140 |
$borrower->account_balance( $borrower->account_balance() + $amount ); |
| 141 |
$borrower->update(); |
| 142 |
|
| 143 |
NormalizeBalances( { borrower => $borrower } ); |
| 144 |
|
| 145 |
if ( C4::Context->preference("FinesLog") ) { |
| 146 |
logaction( "FINES", "CREATE_FEE", $debit->id, |
| 147 |
Dumper( { $debit->get_columns(), accruing => $accruing } ) ); |
| 148 |
} |
| 149 |
} |
| 150 |
else { |
| 151 |
carp("Something went wrong! Debit not created!"); |
| 152 |
} |
| 153 |
|
| 154 |
return $debit; |
| 155 |
} |
| 156 |
|
| 157 |
=head2 DebitLostItem |
| 158 |
|
| 159 |
my $debit = DebitLostItem({ |
| 160 |
borrower => $borrower, |
| 161 |
issue => $issue, |
| 162 |
[ description => $description, ] |
| 163 |
[ notes => $notes, ] |
| 164 |
[ branchcode => $branchcode, ] |
| 165 |
[ manager_id => $manager_id, ] |
| 166 |
}); |
| 167 |
|
| 168 |
DebitLostItem adds a replacement fee charge for the item |
| 169 |
of the given issue. |
| 170 |
|
| 171 |
=cut |
| 172 |
|
| 173 |
sub DebitLostItem { |
| 174 |
my ($params) = @_; |
| 175 |
|
| 176 |
my $borrower = $params->{borrower}; |
| 177 |
my $issue = $params->{issue}; |
| 178 |
|
| 179 |
croak("Required param 'borrower' not passed in!") unless ($borrower); |
| 180 |
croak("Required param 'issue' not passed in!") unless ($issue); |
| 181 |
|
| 182 |
# Don't add lost debit if borrower has already been charged for this lost item before, |
| 183 |
# for this issue. It seems reasonable that a borrower could lose an item, find and return it, |
| 184 |
# check it out again, and lose it again, so we should do this based on issue_id, not itemnumber. |
| 185 |
unless ( |
| 186 |
Koha::Database->new()->schema->resultset('AccountDebit')->search( |
| 187 |
{ |
| 188 |
borrowernumber => $borrower->borrowernumber(), |
| 189 |
issue_id => $issue->issue_id(), |
| 190 |
type => Koha::Accounts::DebitTypes::Lost |
| 191 |
} |
| 192 |
)->count() |
| 193 |
) |
| 194 |
{ |
| 195 |
my $item = $issue->itemnumber(); |
| 196 |
|
| 197 |
$params->{accruing} = 0; |
| 198 |
$params->{type} = Koha::Accounts::DebitTypes::Lost; |
| 199 |
$params->{amount} = $item->replacementprice(); |
| 200 |
$params->{itemnumber} = $item->itemnumber(); |
| 201 |
|
| 202 |
#TODO: Shouldn't we have a default replacement price? Either as a syspref or as part of the issuing rules? |
| 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 |
borrower => $borrower, |
| 216 |
issue => $issue, |
| 217 |
[ description => $description, ] |
| 218 |
[ notes => $notes, ] |
| 219 |
[ branchcode => $branchcode, ] |
| 220 |
[ manager_id => $manager_id, ] |
| 221 |
}); |
| 222 |
|
| 223 |
CreditLostItem creates a payment in the ammount equal |
| 224 |
to the replacement price charge created by DebitLostItem. |
| 225 |
|
| 226 |
=cut |
| 227 |
|
| 228 |
sub CreditLostItem { |
| 229 |
my ($params) = @_; |
| 230 |
|
| 231 |
my $borrower = $params->{borrower}; |
| 232 |
my $account_debit = $params->{account_debit}; |
| 233 |
|
| 234 |
croak("Required param 'borrower' not passed in!") unless ($borrower); |
| 235 |
croak("Required param 'account_debit' not passed in!") |
| 236 |
unless ($account_debit); |
| 237 |
|
| 238 |
my $item = |
| 239 |
Koha::Database->new()->schema->resultset('Item') |
| 240 |
->find( $account_debit->itemnumber() ); |
| 241 |
carp("No item found!") unless $item; |
| 242 |
|
| 243 |
$params->{type} = Koha::Accounts::CreditTypes::Found; |
| 244 |
$params->{amount} = $account_debit->amount_original(); |
| 245 |
$params->{debit_id} = $account_debit->debit_id(); |
| 246 |
$params->{notes} = "Lost item found: " . $item->barcode(); |
| 247 |
# TODO: It would be nice if we could directly connect the item and issue |
| 248 |
# directly to this credit in a simple way. Perhaps the action log is good enough |
| 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 $debit_id = $params->{debit_id}; |
| 285 |
my $notes = $params->{notes}; |
| 286 |
my $branchcode = $params->{branchcode}; |
| 287 |
my $manager_id = $params->{manager_id}; |
| 288 |
|
| 289 |
my $userenv = C4::Context->userenv; |
| 290 |
|
| 291 |
unless ( $manager_id || $userenv ) { |
| 292 |
$manager_id = $userenv->{number}; |
| 293 |
} |
| 294 |
|
| 295 |
unless ( $branchcode || $userenv ) { |
| 296 |
$branchcode = $userenv->{branch}; |
| 297 |
} |
| 298 |
|
| 299 |
unless ($borrower) { |
| 300 |
croak("Required parameter 'borrower' not passed in"); |
| 301 |
} |
| 302 |
unless ($amount) { |
| 303 |
croak("Required parameter amount not passed in"); |
| 304 |
} |
| 305 |
|
| 306 |
unless ( Koha::Accounts::CreditTypes::IsValid($type) ) { |
| 307 |
carp("Invalid credit type! Returning without creating credit."); |
| 308 |
return; |
| 309 |
} |
| 310 |
|
| 311 |
unless ($type) { |
| 312 |
carp("No type passed in, assuming Payment"); |
| 313 |
$type = Koha::Accounts::CreditTypes::Payment; |
| 314 |
} |
| 315 |
|
| 316 |
my $debit = 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 = Koha::Database->new()->schema->resultset('AccountCredit')->create( |
| 320 |
{ |
| 321 |
borrowernumber => $borrower->borrowernumber(), |
| 322 |
type => $type, |
| 323 |
amount_paid => $amount, |
| 324 |
amount_remaining => $amount, |
| 325 |
notes => $notes, |
| 326 |
manager_id => $manager_id, |
| 327 |
created_on => get_timestamp(), |
| 328 |
} |
| 329 |
); |
| 330 |
|
| 331 |
$borrower->account_balance( $borrower->account_balance() - $amount ); |
| 332 |
$borrower->update(); |
| 333 |
|
| 334 |
# If we are given specific debits, pay those ones first. |
| 335 |
if ( $debit_id ) { |
| 336 |
my @debit_ids = ref( $debit_id ) eq "ARRAY" ? @$debit_id : $debit_id; |
| 337 |
foreach my $debit_id (@debit_ids) { |
| 338 |
my $debit = |
| 339 |
Koha::Database->new()->schema->resultset('AccountDebit')->find($debit_id); |
| 340 |
|
| 341 |
if ($debit) { |
| 342 |
CreditDebit( { credit => $credit, debit => $debit } ); |
| 343 |
} |
| 344 |
else { |
| 345 |
carp("Invalid debit_id passed in!"); |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
# We still have leftover money, or we weren't given a specific debit to pay |
| 351 |
if ( $credit->amount_remaining() > 0 ) { |
| 352 |
my @debits = Koha::Database->new()->schema->resultset('AccountDebit')->search( |
| 353 |
{ |
| 354 |
borrowernumber => $borrower->borrowernumber(), |
| 355 |
amount_outstanding => { '>' => '0' } |
| 356 |
} |
| 357 |
); |
| 358 |
|
| 359 |
foreach my $debit (@debits) { |
| 360 |
if ( $credit->amount_remaining() > 0 ) { |
| 361 |
CreditDebit( |
| 362 |
{ |
| 363 |
credit => $credit, |
| 364 |
debit => $debit, |
| 365 |
borrower => $borrower |
| 366 |
} |
| 367 |
); |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
return $credit; |
| 373 |
} |
| 374 |
|
| 375 |
=head2 CreditDebit |
| 376 |
|
| 377 |
$account_offset = CreditDebit({ |
| 378 |
credit => $credit, |
| 379 |
debit => $debit, |
| 380 |
}); |
| 381 |
|
| 382 |
Given a credit and a debit, this subroutine |
| 383 |
will pay the appropriate amount of the debit, |
| 384 |
update the debit's amount outstanding, the credit's |
| 385 |
amout remaining, and create the appropriate account |
| 386 |
offset. |
| 387 |
|
| 388 |
=cut |
| 389 |
|
| 390 |
sub CreditDebit { |
| 391 |
my ($params) = @_; |
| 392 |
|
| 393 |
my $credit = $params->{credit}; |
| 394 |
my $debit = $params->{debit}; |
| 395 |
|
| 396 |
croak("Required parameter 'credit' not passed in!") |
| 397 |
unless $credit; |
| 398 |
croak("Required parameter 'debit' not passed in!") unless $debit; |
| 399 |
|
| 400 |
my $amount_to_pay = |
| 401 |
( $debit->amount_outstanding() > $credit->amount_remaining() ) |
| 402 |
? $credit->amount_remaining() |
| 403 |
: $debit->amount_outstanding(); |
| 404 |
|
| 405 |
if ( $amount_to_pay > 0 ) { |
| 406 |
$debit->amount_outstanding( |
| 407 |
$debit->amount_outstanding() - $amount_to_pay ); |
| 408 |
$debit->update(); |
| 409 |
|
| 410 |
$credit->amount_remaining( |
| 411 |
$credit->amount_remaining() - $amount_to_pay ); |
| 412 |
$credit->update(); |
| 413 |
|
| 414 |
my $offset = Koha::Database->new()->schema->resultset('AccountOffset')->create( |
| 415 |
{ |
| 416 |
amount => $amount_to_pay, |
| 417 |
debit_id => $debit->id(), |
| 418 |
credit_id => $credit->id(), |
| 419 |
created_on => get_timestamp(), |
| 420 |
} |
| 421 |
); |
| 422 |
|
| 423 |
return $offset; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
=head2 RecalculateAccountBalance |
| 428 |
|
| 429 |
$account_balance = RecalculateAccountBalance({ |
| 430 |
borrower => $borrower |
| 431 |
}); |
| 432 |
|
| 433 |
Recalculates a borrower's balance based on the |
| 434 |
sum of the amount outstanding for the borrower's |
| 435 |
debits minus the sum of the amount remaining for |
| 436 |
the borrowers credits. |
| 437 |
|
| 438 |
TODO: Would it be better to use af.amount_original - ap.amount_paid for any reason? |
| 439 |
Or, perhaps calculate both and compare the two, for error checking purposes. |
| 440 |
=cut |
| 441 |
|
| 442 |
sub RecalculateAccountBalance { |
| 443 |
my ($params) = @_; |
| 444 |
|
| 445 |
my $borrower = $params->{borrower}; |
| 446 |
croak("Requred paramter 'borrower' not passed in!") |
| 447 |
unless ($borrower); |
| 448 |
|
| 449 |
my $debits = |
| 450 |
Koha::Database->new()->schema->resultset('AccountDebit') |
| 451 |
->search( { borrowernumber => $borrower->borrowernumber() } ); |
| 452 |
my $amount_outstanding = $debits->get_column('amount_outstanding')->sum(); |
| 453 |
|
| 454 |
my $credits = |
| 455 |
Koha::Database->new()->schema->resultset('AccountCredit') |
| 456 |
->search( { borrowernumber => $borrower->borrowernumber() } ); |
| 457 |
my $amount_remaining = $credits->get_column('amount_remaining')->sum(); |
| 458 |
|
| 459 |
my $account_balance = $amount_outstanding - $amount_remaining; |
| 460 |
$borrower->account_balance($account_balance); |
| 461 |
$borrower->update(); |
| 462 |
|
| 463 |
return $account_balance; |
| 464 |
} |
| 465 |
|
| 466 |
=head2 CreditDebits |
| 467 |
|
| 468 |
$account_balance = CreditDebits({ borrower => $borrower }); |
| 469 |
|
| 470 |
For a given borrower, this subroutine will find all debits |
| 471 |
with an outstanding balance and all credits with an unused |
| 472 |
amount remaining and will pay those debits with those credits. |
| 473 |
|
| 474 |
=cut |
| 475 |
|
| 476 |
sub NormalizeBalances { |
| 477 |
my ($params) = @_; |
| 478 |
|
| 479 |
my $borrower = $params->{borrower}; |
| 480 |
|
| 481 |
croak("Required param 'borrower' not passed in!") unless $borrower; |
| 482 |
|
| 483 |
my @credits = Koha::Database->new()->schema->resultset('AccountCredit')->search( |
| 484 |
{ |
| 485 |
borrowernumber => $borrower->borrowernumber(), |
| 486 |
amount_remaining => { '>' => '0' } |
| 487 |
} |
| 488 |
); |
| 489 |
|
| 490 |
return unless @credits; # short circuit for speed |
| 491 |
|
| 492 |
my @debits = Koha::Database->new()->schema->resultset('AccountDebit')->search( |
| 493 |
{ |
| 494 |
borrowernumber => $borrower->borrowernumber(), |
| 495 |
amount_outstanding => { '>' => '0' } |
| 496 |
} |
| 497 |
); |
| 498 |
|
| 499 |
return unless @debits; # short circuit for speed |
| 500 |
|
| 501 |
foreach my $credit (@credits) { |
| 502 |
foreach my $debit (@debits) { |
| 503 |
if ( $credit->amount_remaining() |
| 504 |
&& $debit->amount_outstanding() ) |
| 505 |
{ |
| 506 |
CreditDebit( { credit => $credit, debit => $debit } ); |
| 507 |
} |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
return RecalculateAccountBalance( { borrower => $borrower } ); |
| 512 |
} |
| 513 |
|
| 514 |
1; |
| 515 |
__END__ |
| 516 |
|
| 517 |
=head1 AUTHOR |
| 518 |
|
| 519 |
Kyle M Hall <kyle@bywatersolutions.com> |
| 520 |
|
| 521 |
=cut |