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_with_fines = Koha::Patrons->search({ ... })->search_patrons_with_unpaid_fines();
469
470
Returns unique borrowernumbers of all patrons with unpaid fines.
471
472
=cut
473
474
sub search_patrons_with_unpaid_fines {
475
    my ( $self ) = @_;
476
    my @patrons_with_fines;
477
    while ( my $patron = $self->next ) {
478
        my $account_balance = $patron->account()->balance;
479
        if($account_balance > 0){
480
            push @patrons_with_fines, {
481
                borrowernumber => $patron->borrowernumber,
482
                accountbalance => $account_balance
483
            }
484
        }
485
    }
486
    return \@patrons_with_fines;
487
}
488
466
=head3 _type
489
=head3 _type
467
490
468
=cut
491
=cut
(-)a/t/db_dependent/Koha/Patrons.t (-2 / +35 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 2030-2035 subtest 'anonymize' => sub { Link Here
2030
    $patron2->discard_changes; # refresh
2030
    $patron2->discard_changes; # refresh
2031
    is( $patron2->firstname, undef, 'First name patron2 cleared' );
2031
    is( $patron2->firstname, undef, 'First name patron2 cleared' );
2032
};
2032
};
2033
2033
$schema->storage->txn_rollback;
2034
$schema->storage->txn_rollback;
2034
2035
2035
subtest 'extended_attributes' => sub {
2036
subtest 'extended_attributes' => sub {
Lines 2204-2206 subtest 'extended_attributes' => sub { Link Here
2204
2205
2205
    $schema->storage->txn_rollback;
2206
    $schema->storage->txn_rollback;
2206
};
2207
};
2207
- 
2208
2209
subtest 'search_patrons_with_unpaid_fines() tests' => sub {
2210
2211
    plan tests => 7;
2212
2213
    $schema->storage->txn_begin;
2214
2215
    my $patrons_with_fines = Koha::Patrons->search({ branchcode => $library->{branchcode} })->search_patrons_with_unpaid_fines();
2216
    is( scalar( @$patrons_with_fines ), 0, 'search_patrons_with_unpaid_fines() should return empty array' );
2217
2218
    my $library = $builder->build({source => 'Branch' });
2219
2220
    my $patron1 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->{branchcode} }});
2221
    my $account1 = $patron1->account;
2222
    $account1->add_debit({ amount => 10, interface => 'commandline', type => 'OVERDUE' });
2223
2224
    my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->{branchcode} }});
2225
    my $account2 = $patron2->account;
2226
    $account2->add_debit({ amount => 10, interface => 'commandline', type => 'OVERDUE' });
2227
2228
    $patrons_with_fines = Koha::Patrons->search({ branchcode => $library->{branchcode} })->search_patrons_with_unpaid_fines();
2229
    is( scalar( @$patrons_with_fines ), 2, 'search_patrons_with_unpaid_fines() should return array with 2 values' );
2230
    is( $patrons_with_fines->[0]->{ borrowernumber }, $patron1->borrowernumber, 'patron1 should be in array');
2231
    is( $patrons_with_fines->[1]->{ borrowernumber }, $patron2->borrowernumber, 'patron2 should be in array');
2232
    is( $patrons_with_fines->[0]->{ accountbalance }, $account1->balance, 'patron1 fines are correct');
2233
    is( $patrons_with_fines->[1]->{ accountbalance }, $account2->balance, 'patron2 fines are correct');
2234
2235
    $account2->add_credit({ amount => 10, interface => 'commandline' });
2236
    $patrons_with_fines = Koha::Patrons->search({ branchcode => $library->{branchcode} })->search_patrons_with_unpaid_fines();
2237
    is( scalar( @$patrons_with_fines ), 1, 'Patron without fines is not in array');
2238
2239
    $schema->storage->txn_rollback;
2240
}

Return to bug 15156