From 7de5c19f0c1da0ef77b583935edca1bc0c743404 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 | 44 ++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/Koha/Patrons.pm b/Koha/Patrons.pm index 3b3b4b1cb6..d4336c0406 100644 --- a/Koha/Patrons.pm +++ b/Koha/Patrons.pm @@ -463,6 +463,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 0f00008588..207198f464 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 => 41; +use Test::More tests => 42; use Test::Warn; use Test::Exception; use Test::MockModule; @@ -2121,3 +2121,45 @@ subtest 'extended_attributes' => sub { $schema->storage->txn_rollback; }; + +subtest 'filter_by_debts' => sub { + + plan tests => 7; + my $schema = Koha::Database->new->schema; + $schema->storage->txn_begin; + + 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; +}; -- 2.17.1