Bug 20946: Add Koha::Account::outstanding_debits

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>
This commit is contained in:
Tomás Cohen Arazi 2018-06-23 05:41:00 -03:00 committed by Nick Clemens
parent 3022247983
commit 0e45263492
2 changed files with 91 additions and 0 deletions

View 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

59
t/db_dependent/Koha/Account.t Executable file
View file

@ -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;
};