Bug 20946: Reduce number of db calls
authorKyle M Hall <kyle@bywatersolutions.com>
Tue, 26 Jun 2018 13:08:36 +0000 (13:08 +0000)
committerNick Clemens <nick@bywatersolutions.com>
Fri, 6 Jul 2018 10:33:13 +0000 (10:33 +0000)
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Koha/Account.pm

index 02832d3..bffc98b 100644 (file)
@@ -22,6 +22,7 @@ use Modern::Perl;
 use Carp;
 use Data::Dumper;
 use List::MoreUtils qw( uniq );
+use List::Util qw( sum );
 
 use C4::Log qw( logaction );
 use C4::Stats qw( UpdateStats );
@@ -297,19 +298,6 @@ my ( $total, $lines ) = Koha::Account->new({ patron_id => $patron_id })->outstan
 sub outstanding_debits {
     my ($self) = @_;
 
-    my $outstanding_debits = Koha::Account::Lines->search(
-        {   borrowernumber    => $self->{patron_id},
-            amountoutstanding => { '>' => 0 }
-        },
-        {   select => [ { sum => 'amountoutstanding' } ],
-            as     => ['outstanding_debits_total'],
-        }
-    );
-    my $total
-        = ( $outstanding_debits->count )
-        ? $outstanding_debits->next->get_column('outstanding_debits_total') + 0
-        : 0;
-
     my $lines = Koha::Account::Lines->search(
         {
             borrowernumber    => $self->{patron_id},
@@ -317,6 +305,9 @@ sub outstanding_debits {
         }
     );
 
+    # sum returns undef it list is empty
+    my $total = sum( $lines->get_column('amountoutstanding') ) + 0;
+
     return ( $total, $lines );
 }