From 3e0aa0f3191a570dceb834e4036932d9af3c820e Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Tue, 26 Jun 2018 14:31:40 +0000 Subject: [PATCH] Bug 20990: Make same changes that were made to outstanding_debits Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi Signed-off-by: Nick Clemens --- Koha/Account.pm | 16 +++------------- t/db_dependent/Koha/Account.t | 6 +++++- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/Koha/Account.pm b/Koha/Account.pm index 1912e68ceb..82535159b6 100644 --- a/Koha/Account.pm +++ b/Koha/Account.pm @@ -431,19 +431,6 @@ my ( $total, $lines ) = Koha::Account->new({ patron_id => $patron_id })->outstan sub outstanding_credits { my ($self) = @_; - my $outstanding_credits = Koha::Account::Lines->search( - { borrowernumber => $self->{patron_id}, - amountoutstanding => { '<' => 0 } - }, - { select => [ { sum => 'amountoutstanding' } ], - as => ['outstanding_credits_total'], - } - ); - my $total - = ( $outstanding_credits->count ) - ? $outstanding_credits->next->get_column('outstanding_credits_total') + 0 - : 0; - my $lines = Koha::Account::Lines->search( { borrowernumber => $self->{patron_id}, @@ -451,6 +438,9 @@ sub outstanding_credits { } ); + # sum returns undef if list is empty + my $total = sum0( $lines->get_column('amountoutstanding') ); + return ( $total, $lines ); } diff --git a/t/db_dependent/Koha/Account.t b/t/db_dependent/Koha/Account.t index 6350b165bf..28d296a094 100755 --- a/t/db_dependent/Koha/Account.t +++ b/t/db_dependent/Koha/Account.t @@ -85,7 +85,7 @@ subtest 'outstanding_debits() tests' => sub { subtest 'outstanding_credits() tests' => sub { - plan tests => 5; + plan tests => 7; $schema->storage->txn_begin; @@ -109,6 +109,10 @@ subtest 'outstanding_credits() tests' => sub { $i++; } + ( $total, $lines ) = Koha::Account->new({ patron_id => 'InvalidBorrowernumber' })->outstanding_credits(); + is( $total, 0, "Total if no outstanding credits is 0" ); + is( $lines->count, 0, "With no outstanding credits, we get back a Lines object with 0 lines" ); + $schema->storage->txn_rollback; }; -- 2.39.5