Bug 20990: Add Koha::Account->outstanding_credits
authorTomas Cohen Arazi <tomascohen@theke.io>
Mon, 25 Jun 2018 14:15:14 +0000 (11:15 -0300)
committerNick Clemens <nick@bywatersolutions.com>
Fri, 13 Jul 2018 10:34:30 +0000 (10:34 +0000)
This patch adds a method that retrieves (for a patron's account) the
outstanding credits (i.e. those that haven't been applied to any debit.

To test:
- Apply this patches
- Run:
  $ kshell
 k$ prove t/db_dependent/Koha/Account.t
=> SUCCESS: Tests pass!
- Sign off :-D

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

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

Koha/Account.pm

index 8e3ed40..1912e68 100644 (file)
@@ -422,6 +422,38 @@ sub outstanding_debits {
     return $lines;
 }
 
+=head3 outstanding_credits
+
+my ( $total, $lines ) = Koha::Account->new({ patron_id => $patron_id })->outstanding_credits;
+
+=cut
+
+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},
+            amountoutstanding => { '<' => 0 }
+        }
+    );
+
+    return ( $total, $lines );
+}
+
 =head3 non_issues_charges
 
 my $non_issues_charges = $self->non_issues_charges