From 3dae385f5db96be1f9357e37f9ad437c3ab26732 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 | 40 +++++----------------------------------- t/db_dependent/Accounts.t | 2 +- 2 files changed, 6 insertions(+), 36 deletions(-) diff --git a/Koha/Accounts.pm b/Koha/Accounts.pm index 0a3952a..cffa141 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") ) { @@ -336,9 +333,6 @@ sub AddCredit { Dumper( $credit->get_columns() ) ); } - $borrower->account_balance( $borrower->account_balance() - $amount ); - $borrower->update(); - # If we are given specific debits, pay those ones first. if ($debit_id) { my @debit_ids = ref($debit_id) eq "ARRAY" ? @$debit_id : $debit_id; @@ -356,29 +350,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 +469,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