Bug 20946: Add Koha::Account::outstanding_debits
authorTomas Cohen Arazi <tomascohen@theke.io>
Sat, 23 Jun 2018 08:41:00 +0000 (05:41 -0300)
committerNick Clemens <nick@bywatersolutions.com>
Fri, 6 Jul 2018 10:33:13 +0000 (10:33 +0000)
This patch adds a handy method that returns the total for outstanding
debits (i.e. those that haven't been canceled with credits), and the
corresponding Koha::Account::Line objects.

Unit tests are added.

To test:
- Apply this patch
- 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
t/db_dependent/Koha/Account.t [new file with mode: 0755]

index 493f4ac..02832d3 100644 (file)
@@ -288,6 +288,38 @@ sub balance {
       : 0;
 }
 
+=head3 outstanding_debits
+
+my ( $total, $lines ) = Koha::Account->new({ patron_id => $patron_id })->outstanding_debits;
+
+=cut
+
+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},
+            amountoutstanding => { '>' => 0 }
+        }
+    );
+
+    return ( $total, $lines );
+}
+
 =head3 non_issues_charges
 
 my $non_issues_charges = $self->non_issues_charges
diff --git a/t/db_dependent/Koha/Account.t b/t/db_dependent/Koha/Account.t
new file mode 100755 (executable)
index 0000000..bf0e0b6
--- /dev/null
@@ -0,0 +1,59 @@
+#!/usr/bin/perl
+
+# Copyright 2018 Koha Development team
+#
+# This file is part of Koha
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>
+
+use Modern::Perl;
+
+use Test::More tests => 1;
+
+use Koha::Account;
+use Koha::Account::Lines;
+
+use t::lib::TestBuilder;
+
+my $schema  = Koha::Database->new->schema;
+my $builder = t::lib::TestBuilder->new;
+
+subtest 'outstanding_debits() tests' => sub {
+
+    plan tests => 5;
+
+    $schema->storage->txn_begin;
+
+    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+
+    my @generated_lines;
+    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 1 })->store;
+    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 2 })->store;
+    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 3 })->store;
+    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 4 })->store;
+
+    my $account = Koha::Account->new({ patron_id => $patron->id });
+    my ( $total, $lines ) = $account->outstanding_debits();
+
+    is( $total, 10, 'Outstandig debits total is correctly calculated' );
+
+    my $i = 0;
+    foreach my $line ( @{ $lines->as_list } ) {
+        my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
+        is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
+        $i++;
+    }
+
+    $schema->storage->txn_rollback;
+};