From 2e5f7fd025469601729f0a6f69de59f2d6355895 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Thu, 29 May 2014 10:08:59 -0400 Subject: [PATCH] Bug 6427 [Part 26] - Simplify and improve account normalization and balancing --- Koha/Accounts.pm | 37 +++++-------------------------------- t/db_dependent/Accounts.t | 2 +- 2 files changed, 6 insertions(+), 33 deletions(-) diff --git a/Koha/Accounts.pm b/Koha/Accounts.pm index 0a3952a..021cc3c 100644 --- a/Koha/Accounts.pm +++ b/Koha/Accounts.pm @@ -140,9 +140,6 @@ sub AddDebit { ); if ($debit) { - $borrower->account_balance( $borrower->account_balance() + $amount ); - $borrower->update(); - NormalizeBalances( { borrower => $borrower } ); if ( C4::Context->preference("FinesLog") ) { @@ -356,29 +353,7 @@ sub AddCredit { } } - # We still have leftover money, or we weren't given a specific debit to pay - if ( $credit->amount_remaining() > 0 ) { - my @debits = - Koha::Database->new()->schema->resultset('AccountDebit')->search( - { - borrowernumber => $borrower->borrowernumber(), - amount_outstanding => { '>' => '0' } - } - ); - - foreach my $debit (@debits) { - if ( $credit->amount_remaining() > 0 ) { - CreditDebit( - { - credit => $credit, - debit => $debit, - borrower => $borrower, - type => $type, - } - ); - } - } - } + NormalizeBalances( { borrower => $borrower } ); return $credit; } @@ -497,26 +472,24 @@ sub NormalizeBalances { croak("Required param 'borrower' not passed in!") unless $borrower; + my $schema = Koha::Database->new()->schema(); + my @credits = - Koha::Database->new()->schema->resultset('AccountCredit')->search( + $schema->resultset('AccountCredit')->search( { borrowernumber => $borrower->borrowernumber(), amount_remaining => { '>' => '0' } } ); - return unless @credits; - my @debits = - Koha::Database->new()->schema->resultset('AccountDebit')->search( + $schema->resultset('AccountDebit')->search( { borrowernumber => $borrower->borrowernumber(), amount_outstanding => { '>' => '0' } } ); - return unless @debits; - foreach my $credit (@credits) { foreach my $debit (@debits) { if ( $credit->amount_remaining() diff --git a/t/db_dependent/Accounts.t b/t/db_dependent/Accounts.t index 7957cbe..44b7ba4 100644 --- a/t/db_dependent/Accounts.t +++ b/t/db_dependent/Accounts.t @@ -84,7 +84,7 @@ ok( $debit, "AddDebit returned a valid debit id " . $debit->id() ); ok( $borrower->account_balance() == 5.00, - "Borrower's account balance updated correctly" + "Borrower's account balance updated correctly. Should be 5.00, is " . $borrower->account_balance() ); my $debit2 = AddDebit( -- 1.7.2.5