View | Details | Raw Unified | Return to bug 15156
Collapse All | Expand All

(-)a/Koha/Patrons.pm (+23 lines)
Lines 483-488 sub filter_by_attribute_value { Link Here
483
}
483
}
484
484
485
485
486
=head3
487
488
    my $patrons = Koha::Patrons->filter_by_owings({ ... });
489
490
    Returns a Koha::Patrons set of all unique patrons with accountlines
491
    matching filtering params.
492
493
=cut
494
495
sub filter_by_owings {
496
    my ( $self, $params ) = @_;
497
    my $rs = Koha::Account::Lines->search(
498
        {
499
            %{$params}
500
        },
501
        {
502
            distinct => 1,
503
            select => 'borrowernumber'
504
        }
505
    )->_resultset()->search_related('borrowernumber');
506
    return Koha::Patrons->_new_from_dbic($rs);
507
}
508
486
=head3 _type
509
=head3 _type
487
510
488
=cut
511
=cut
(-)a/t/db_dependent/Koha/Patrons.t (-3 / +41 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 42;
22
use Test::More tests => 43;
23
use Test::Warn;
23
use Test::Warn;
24
use Test::Exception;
24
use Test::Exception;
25
use Test::MockModule;
25
use Test::MockModule;
Lines 2184-2187 subtest 'queue_notice' => sub { Link Here
2184
    is( Koha::Notice::Messages->search({borrowernumber => $patron->borrowernumber })->count, $counter,"Count of queued notices not increased in test mode");
2184
    is( Koha::Notice::Messages->search({borrowernumber => $patron->borrowernumber })->count, $counter,"Count of queued notices not increased in test mode");
2185
};
2185
};
2186
2186
2187
$schema->storage->txn_rollback;
2187
subtest 'filter_by_owings' => sub {
2188
2189
    plan tests => 7;
2190
2191
    my $library = $builder->build({source => 'Branch' });
2192
    my $max_owing = 10;
2193
2194
    my $patrons = Koha::Patrons->filter_by_owings({ branchcode => $library->{branchcode}, amountoutstanding => { '>=' => $max_owing } });
2195
    is(ref($patrons), 'Koha::Patrons', 'filter_by_owings returns set of Koha::Patrons');
2196
    is($patrons->count, 0, 'Empty set of Koha::Patrons returned');
2197
2198
    my $patron1 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->{branchcode} }});
2199
    my $account1 = $patron1->account;
2200
    $account1->add_debit({ amount => 10, interface => 'commandline', type => 'OVERDUE', library_id => $library->{branchcode} });
2201
2202
    my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->{branchcode} }});
2203
    my $account2 = $patron2->account;
2204
    $account2->add_debit({ amount => 10, interface => 'commandline', type => 'OVERDUE', library_id => $library->{branchcode} });
2205
2206
    t::lib::Mocks::mock_userenv({ patron => $patron1 });
2207
2208
    $patrons = Koha::Patrons->filter_by_owings({ branchcode => $library->{branchcode}, amountoutstanding => { '>=' => $max_owing } });
2209
    is($patrons->count, 2, 'filter_by_debts() should return set of 2 patrons when filtered with amountoutstanding');
2210
2211
    my $found_patron1 = $patrons->find($patron1->borrowernumber);
2212
    my $found_patron2 = $patrons->find($patron2->borrowernumber);
2213
2214
    is($found_patron1->borrowernumber, $patron1->borrowernumber, 'patron 1 returned');
2215
    is($found_patron2->borrowernumber, $patron2->borrowernumber, 'patron 2 returned');
2216
2217
    $account1->pay({ amount => 10, interface => 'commandline', library_id => $library->{branchcode}, manager_id => $patron1->borrowernumber });
2218
    $patrons = Koha::Patrons->filter_by_owings({ branchcode => $library->{branchcode}, amountoutstanding => { '>=' => $max_owing } });
2219
2220
    is( $patrons->count, 1, 'filter_by_debts() should return 1 patron when filtered with amountoutstanding');
2221
    my $found_patron = $patrons->next;
2222
    is( $found_patron->borrowernumber, $patron2->borrowernumber, 'Only patron 2 returned');
2223
2224
};
2225
2226
$schema->storage->txn_rollback;
2188
- 

Return to bug 15156