From 31e43ca24f987d1be611440e4a9d321dfc41b9d2 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 13 Sep 2019 14:05:45 +0100 Subject: [PATCH] Bug 23355: Add summation methods to Koha::Account::Lines This patch adds a number of summation methods to Koha::Account::Lines giving quick access to overall total, total credits and total debits. --- Koha/Account/Lines.pm | 82 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/Koha/Account/Lines.pm b/Koha/Account/Lines.pm index 75225fcfeb..07c6f0b24b 100644 --- a/Koha/Account/Lines.pm +++ b/Koha/Account/Lines.pm @@ -43,13 +43,13 @@ empty it returns 0. =cut sub total_outstanding { - my ( $self ) = @_; + my ($self) = @_; my $lines = $self->search( {}, { select => [ { sum => 'amountoutstanding' } ], - as => ['total_amountoutstanding'], + as => ['total_amountoutstanding'], } ); @@ -58,6 +58,84 @@ sub total_outstanding { : 0; } +=head3 total + + my $lines = Koha::Account::Lines->search({ ... }); + my $total = $lines->total; + +Returns the sum of the amounts of the resultset. If the resultset is +empty it returns 0. + +=cut + +sub total { + my ( $self, $conditions ) = @_; + + $conditions //= {}; + my $lines = $self->search( + $conditions, + { + select => [ { sum => 'me.amount' } ], + as => ['total'] + } + ); + return $lines->count ? $lines->next->get_column('total') + 0 : 0; +} + +=head3 credits_total + + my $lines = Koha::Account::Lines->search({ ... }); + my $credits_total = $lines->credits_total; + +Returns the sum of the amounts of the resultset. If the resultset is +empty it returns 0. + +=cut + +sub credits_total { + my ( $self, $conditions ) = @_; + + my $local_conditions = { 'me.amount' => { '<' => 0 } }; + $conditions //= {}; + my $merged_conditions = { %{$conditions}, %{$local_conditions} }; + + my $lines = $self->search( + $merged_conditions, + { + select => [ { sum => 'me.amount' } ], + as => ['total'] + } + ); + return $lines->count ? $lines->next->get_column('total') + 0 : 0; +} + +=head3 debits_total + + my $lines = Koha::Account::Lines->search({ ... }); + my $debits_total = $lines->debits_total; + +Returns the sum of the amounts of the resultset. If the resultset is +empty it returns 0. + +=cut + +sub debits_total { + my ( $self, $conditions ) = @_; + + my $local_conditions = { 'me.amount' => { '>' => 0 } }; + $conditions //= {}; + my $merged_conditions = { %{$conditions}, %{$local_conditions} }; + + my $lines = $self->search( + $merged_conditions, + { + select => [ { sum => 'me.amount' } ], + as => ['total'] + } + ); + return $lines->count ? $lines->next->get_column('total') + 0 : 0; +} + =head2 Internal methods =head3 _type -- 2.20.1