From 0e45263492dbd8123bf2507aad6dc45d95c4902c Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Sat, 23 Jun 2018 05:41:00 -0300 Subject: [PATCH] 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 Signed-off-by: Kyle M Hall Signed-off-by: Nick Clemens --- Koha/Account.pm | 32 +++++++++++++++++++ t/db_dependent/Koha/Account.t | 59 +++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100755 t/db_dependent/Koha/Account.t diff --git a/Koha/Account.pm b/Koha/Account.pm index 493f4ac698..02832d36c7 100644 --- a/Koha/Account.pm +++ b/Koha/Account.pm @@ -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 index 0000000000..bf0e0b677d --- /dev/null +++ b/t/db_dependent/Koha/Account.t @@ -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 + +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; +}; -- 2.39.5