Bug 21694: Add the Koha::Account->lines method
authorMartin Renvoize <martin.renvoize@ptfs-europe.com>
Fri, 26 Oct 2018 17:34:35 +0000 (18:34 +0100)
committerNick Clemens <nick@bywatersolutions.com>
Wed, 31 Oct 2018 13:51:06 +0000 (13:51 +0000)
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

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

Koha/Account.pm
t/db_dependent/Koha/Account.t

index e686397..ca7a807 100644 (file)
@@ -524,6 +524,26 @@ sub non_issues_charges {
       : 0;
 }
 
+=head3 lines
+
+my $lines = $self->lines;
+
+Return all credits and debits for the user, outstanding or otherwise
+
+=cut
+
+sub lines {
+    my ($self) = @_;
+
+    my $lines = Koha::Account::Lines->search(
+        {
+            borrowernumber    => $self->{patron_id},
+        }
+    );
+
+    return $lines;
+}
+
 1;
 
 =head2 Name mappings
index 21f0e62..ff400c7 100755 (executable)
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 3;
+use Test::More tests => 4;
 
 use Koha::Account;
 use Koha::Account::Lines;
@@ -192,3 +192,36 @@ subtest 'add_credit() tests' => sub {
 
     $schema->storage->txn_rollback;
 };
+
+subtest 'lines() tests' => sub {
+
+    plan tests => 1;
+
+    $schema->storage->txn_begin;
+
+    my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
+    my $account = $patron->account;
+
+    my @generated_lines;
+
+    # Add Credits
+    push @generated_lines, $account->add_credit({ amount => 1 });
+    push @generated_lines, $account->add_credit({ amount => 2 });
+    push @generated_lines, $account->add_credit({ amount => 3 });
+    push @generated_lines, $account->add_credit({ amount => 4 });
+
+    # Add Debits
+    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;
+
+    # Paid Off
+    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 0 })->store;
+    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 0 })->store;
+
+    my $lines = $account->lines;
+    is( $lines->_resultset->count, 10, "All accountlines (debits, credits and paid off) were fetched");
+
+    $schema->storage->txn_rollback;
+};