From d2792c16989841b746d9111f8724a5a2a3f9a811 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 Signed-off-by: Fridolin Somers --- Koha/Patrons.pm | 23 ++++++++++++++++++++++ t/db_dependent/Koha/Patrons.t | 36 ++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/Koha/Patrons.pm b/Koha/Patrons.pm index 3b3b4b1cb6..773d7abdc8 100644 --- a/Koha/Patrons.pm +++ b/Koha/Patrons.pm @@ -463,6 +463,29 @@ sub filter_by_attribute_value { } +=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 0f00008588..6d9810c98f 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 => 41; +use Test::More tests => 42; use Test::Warn; use Test::Exception; use Test::MockModule; @@ -1947,6 +1947,7 @@ subtest 'anonymize' => sub { $patron2->discard_changes; # refresh is( $patron2->firstname, undef, 'First name patron2 cleared' ); }; + $schema->storage->txn_rollback; subtest 'extended_attributes' => sub { @@ -2121,3 +2122,36 @@ subtest 'extended_attributes' => sub { $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; +} \ No newline at end of file -- 2.27.0