From ee04c4d42cfce22bf12de5880691ae96ad848ee1 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 27 Nov 2018 19:34:29 -0300 Subject: [PATCH] [18.05.x] Bug 21896: Add Koha::Account::normalize_balance This patch adds the normalize_balance() method to Koha::Account. Its purpose is to apply outstanding credits (i.e. manual ones ore remaining amounts like in the case of refunds) to outstanding debts. To test: - Apply this patchset - Run: $ kshell k$ prove t/db_dependent/Koha/Account.t => SUCCESS: Tests pass! - Sign off :-D Signed-off-by: Christopher Brannon Signed-off-by: Mark Tompsett Signed-off-by: Martin Renvoize --- Koha/Account.pm | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Koha/Account.pm b/Koha/Account.pm index 33353b1be2..ddc53d3103 100644 --- a/Koha/Account.pm +++ b/Koha/Account.pm @@ -492,6 +492,33 @@ sub non_issues_charges { : 0; } +=head3 normalize_balance + +$account->normalize_balance(); + +Find outstanding credits and use them to pay outstanding debits + +=cut + +sub normalize_balance { + my ($self) = @_; + + my $outstanding_debits = $self->outstanding_debits; + my $outstanding_credits = $self->outstanding_credits; + + while ( $outstanding_debits->total_outstanding > 0 + and my $credit = $outstanding_credits->next ) + { + # there's both outstanding debits and credits + $credit->apply( { debits => $outstanding_debits } ); # applying credit, no special offset + + $outstanding_debits = $self->outstanding_debits; + + } + + return $self; +} + 1; =head2 Name mappings -- 2.19.2