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

(-)a/Koha/Patrons.pm (+23 lines)
Lines 463-468 sub filter_by_attribute_value { Link Here
463
}
463
}
464
464
465
465
466
=head3
467
468
    my $patrons = Koha::Patrons->filter_by_owings({ ... });
469
470
    Returns a Koha::Patrons set of all unique patrons with accountlines
471
    matching filtering params.
472
473
=cut
474
475
sub filter_by_owings {
476
    my ( $self, $params ) = @_;
477
    my $rs = Koha::Account::Lines->search(
478
        {
479
            %{$params}
480
        },
481
        {
482
            distinct => 1,
483
            select => 'borrowernumber'
484
        }
485
    )->_resultset()->search_related('borrowernumber');
486
    return Koha::Patrons->_new_from_dbic($rs);
487
}
488
466
=head3 _type
489
=head3 _type
467
490
468
=cut
491
=cut
(-)a/t/db_dependent/Koha/Patrons.t (-2 / +43 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 41;
22
use Test::More tests => 42;
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 2121-2123 subtest 'extended_attributes' => sub { Link Here
2121
2121
2122
    $schema->storage->txn_rollback;
2122
    $schema->storage->txn_rollback;
2123
};
2123
};
2124
- 
2124
2125
subtest 'filter_by_debts' => sub {
2126
2127
    plan tests => 7;
2128
    my $schema = Koha::Database->new->schema;
2129
    $schema->storage->txn_begin;
2130
2131
    my $library = $builder->build({source => 'Branch' });
2132
    my $max_owing = 10;
2133
2134
    my $patrons = Koha::Patrons->filter_by_owings({ branchcode => $library->{branchcode}, amountoutstanding => { '>=' => $max_owing } });
2135
    is(ref($patrons), 'Koha::Patrons', 'filter_by_owings returns set of Koha::Patrons');
2136
    is($patrons->count, 0, 'Empty set of Koha::Patrons returned');
2137
2138
    my $patron1 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->{branchcode} }});
2139
    my $account1 = $patron1->account;
2140
    $account1->add_debit({ amount => 10, interface => 'commandline', type => 'OVERDUE', library_id => $library->{branchcode} });
2141
2142
    my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->{branchcode} }});
2143
    my $account2 = $patron2->account;
2144
    $account2->add_debit({ amount => 10, interface => 'commandline', type => 'OVERDUE', library_id => $library->{branchcode} });
2145
2146
    t::lib::Mocks::mock_userenv({ patron => $patron1 });
2147
2148
    $patrons = Koha::Patrons->filter_by_owings({ branchcode => $library->{branchcode}, amountoutstanding => { '>=' => $max_owing } });
2149
    is($patrons->count, 2, 'filter_by_debts() should return set of 2 patrons when filtered with amountoutstanding');
2150
2151
    my $found_patron1 = $patrons->find($patron1->borrowernumber);
2152
    my $found_patron2 = $patrons->find($patron2->borrowernumber);
2153
2154
    is($found_patron1->borrowernumber, $patron1->borrowernumber, 'patron 1 returned');
2155
    is($found_patron2->borrowernumber, $patron2->borrowernumber, 'patron 2 returned');
2156
2157
    $account1->pay({ amount => 10, interface => 'commandline', library_id => $library->{branchcode}, manager_id => $patron1->borrowernumber });
2158
    $patrons = Koha::Patrons->filter_by_owings({ branchcode => $library->{branchcode}, amountoutstanding => { '>=' => $max_owing } });
2159
2160
    is( $patrons->count, 1, 'filter_by_debts() should return 1 patron when filtered with amountoutstanding');
2161
    my $found_patron = $patrons->next;
2162
    is( $found_patron->borrowernumber, $patron2->borrowernumber, 'Only patron 2 returned');
2163
2164
    $schema->storage->txn_rollback;
2165
};

Return to bug 15156