From 0a6f67324b0dde1da6756884ddc2f75f8cd02be1 Mon Sep 17 00:00:00 2001 From: Emmi Takkinen Date: Tue, 25 Feb 2020 09:24:18 +0200 Subject: [PATCH] Bug 15156: Get all Borrowers with pending/unpaid fines/accountlines This patch adds method to fetch all borrowers with unpaid fines. To test: prove t/db_dependent/Koha/Patrons.t Sponsored-by: Koha-Suomi Oy --- Koha/Account/Lines.pm | 1 + Koha/Patrons.pm | 23 ++++++++++++++++++++++ t/db_dependent/Koha/Patrons.t | 36 ++++++++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/Koha/Account/Lines.pm b/Koha/Account/Lines.pm index 41c621671c..e6ea62a821 100644 --- a/Koha/Account/Lines.pm +++ b/Koha/Account/Lines.pm @@ -23,6 +23,7 @@ use Koha::Database; use Koha::Account::Line; use base qw(Koha::Objects); +use List::MoreUtils qw( uniq ); =head1 NAME diff --git a/Koha/Patrons.pm b/Koha/Patrons.pm index f4ca8456a6..e6d4ee6104 100644 --- a/Koha/Patrons.pm +++ b/Koha/Patrons.pm @@ -436,6 +436,29 @@ sub update_category_to { return $counter; } +=head3 + + my $patrons_with_fines = Koha::Patrons->search({ ... })->search_patrons_with_unpaid_fines(); + +Returns unique borrowernumbers of all patrons with unpaid fines. + +=cut + +sub search_patrons_with_unpaid_fines { + my ( $self ) = @_; + my @patrons_with_fines; + while ( my $patron = $self->next ) { + my $account_balance = $patron->account()->balance; + if($account_balance > 0){ + push @patrons_with_fines, { + borrowernumber => $patron->borrowernumber, + accountbalance => $account_balance + } + } + } + return \@patrons_with_fines; +} + =head3 _type =cut diff --git a/t/db_dependent/Koha/Patrons.t b/t/db_dependent/Koha/Patrons.t index 53ee33e15b..5527c5ee63 100644 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 40; +use Test::More tests => 41; use Test::Warn; use Test::Exception; use Test::MockModule; @@ -1977,4 +1977,38 @@ subtest 'anonymize' => sub { $patron2->discard_changes; # refresh is( $patron2->firstname, undef, 'First name patron2 cleared' ); }; + $schema->storage->txn_rollback; + +subtest 'search_patrons_with_unpaid_fines() tests' => sub { + + plan tests => 7; + + $schema->storage->txn_begin; + + my $patrons_with_fines = Koha::Patrons->search({ branchcode => $library->{branchcode} })->search_patrons_with_unpaid_fines(); + is( scalar( @$patrons_with_fines ), 0, 'search_patrons_with_unpaid_fines() should return empty array' ); + + my $library = $builder->build({source => 'Branch' }); + + my $patron1 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->{branchcode} }}); + my $account1 = $patron1->account; + $account1->add_debit({ amount => 10, interface => 'commandline', type => 'OVERDUE' }); + + my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->{branchcode} }}); + my $account2 = $patron2->account; + $account2->add_debit({ amount => 10, interface => 'commandline', type => 'OVERDUE' }); + + $patrons_with_fines = Koha::Patrons->search({ branchcode => $library->{branchcode} })->search_patrons_with_unpaid_fines(); + is( scalar( @$patrons_with_fines ), 2, 'search_patrons_with_unpaid_fines() should return array with 2 values' ); + is( $patrons_with_fines->[0]->{ borrowernumber }, $patron1->borrowernumber, 'patron1 should be in array'); + is( $patrons_with_fines->[1]->{ borrowernumber }, $patron2->borrowernumber, 'patron2 should be in array'); + is( $patrons_with_fines->[0]->{ accountbalance }, $account1->balance, 'patron1 fines are correct'); + is( $patrons_with_fines->[1]->{ accountbalance }, $account2->balance, 'patron2 fines are correct'); + + $account2->add_credit({ amount => 10, interface => 'commandline' }); + $patrons_with_fines = Koha::Patrons->search({ branchcode => $library->{branchcode} })->search_patrons_with_unpaid_fines(); + is( scalar( @$patrons_with_fines ), 1, 'Patron without fines is not in array'); + + $schema->storage->txn_rollback; +}; -- 2.17.1