From 19bbb29f75e524f63ae739b067864e23fc4df161 Mon Sep 17 00:00:00 2001 From: Emmi Takkinen Date: Mon, 21 Sep 2020 08:46:25 +0300 Subject: [PATCH] Bug 15156: Get all patrons with owings This patch adds method to return set of unique patrons with accountlines matching filtering params. To test: prove t/db_dependent/Koha/Patrons.t --- Koha/Patrons.pm | 23 +++++++++++++++++++ t/db_dependent/Koha/Patrons.t | 43 +++++++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/Koha/Patrons.pm b/Koha/Patrons.pm index 616fefacba..5104be2f9e 100644 --- a/Koha/Patrons.pm +++ b/Koha/Patrons.pm @@ -483,6 +483,29 @@ sub filter_by_attribute_value { } +=head3 + + my $patrons = Koha::Patrons->filter_by_owings({ ... }); + + Returns a Koha::Patrons set of all unique patrons with accountlines + matching filtering params. + +=cut + +sub filter_by_owings { + my ( $self, $params ) = @_; + my $rs = Koha::Account::Lines->search( + { + %{$params} + }, + { + distinct => 1, + select => 'borrowernumber' + } + )->_resultset()->search_related('borrowernumber'); + return Koha::Patrons->_new_from_dbic($rs); +} + =head3 _type =cut diff --git a/t/db_dependent/Koha/Patrons.t b/t/db_dependent/Koha/Patrons.t index 120e981324..afb6ec4a6d 100755 --- 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 => 42; +use Test::More tests => 43; use Test::Warn; use Test::Exception; use Test::MockModule; @@ -2184,4 +2184,43 @@ subtest 'queue_notice' => sub { is( Koha::Notice::Messages->search({borrowernumber => $patron->borrowernumber })->count, $counter,"Count of queued notices not increased in test mode"); }; -$schema->storage->txn_rollback; +subtest 'filter_by_owings' => sub { + + plan tests => 7; + + my $library = $builder->build({source => 'Branch' }); + my $max_owing = 10; + + my $patrons = Koha::Patrons->filter_by_owings({ branchcode => $library->{branchcode}, amountoutstanding => { '>=' => $max_owing } }); + is(ref($patrons), 'Koha::Patrons', 'filter_by_owings returns set of Koha::Patrons'); + is($patrons->count, 0, 'Empty set of Koha::Patrons returned'); + + 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', library_id => $library->{branchcode} }); + + 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', library_id => $library->{branchcode} }); + + t::lib::Mocks::mock_userenv({ patron => $patron1 }); + + $patrons = Koha::Patrons->filter_by_owings({ branchcode => $library->{branchcode}, amountoutstanding => { '>=' => $max_owing } }); + is($patrons->count, 2, 'filter_by_debts() should return set of 2 patrons when filtered with amountoutstanding'); + + my $found_patron1 = $patrons->find($patron1->borrowernumber); + my $found_patron2 = $patrons->find($patron2->borrowernumber); + + is($found_patron1->borrowernumber, $patron1->borrowernumber, 'patron 1 returned'); + is($found_patron2->borrowernumber, $patron2->borrowernumber, 'patron 2 returned'); + + $account1->pay({ amount => 10, interface => 'commandline', library_id => $library->{branchcode}, manager_id => $patron1->borrowernumber }); + $patrons = Koha::Patrons->filter_by_owings({ branchcode => $library->{branchcode}, amountoutstanding => { '>=' => $max_owing } }); + + is( $patrons->count, 1, 'filter_by_debts() should return 1 patron when filtered with amountoutstanding'); + my $found_patron = $patrons->next; + is( $found_patron->borrowernumber, $patron2->borrowernumber, 'Only patron 2 returned'); + +}; + +$schema->storage->txn_rollback; \ No newline at end of file -- 2.25.1