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 1947-1952 subtest 'anonymize' => sub { Link Here
1947
    $patron2->discard_changes; # refresh
1947
    $patron2->discard_changes; # refresh
1948
    is( $patron2->firstname, undef, 'First name patron2 cleared' );
1948
    is( $patron2->firstname, undef, 'First name patron2 cleared' );
1949
};
1949
};
1950
1950
$schema->storage->txn_rollback;
1951
$schema->storage->txn_rollback;
1951
1952
1952
subtest 'extended_attributes' => sub {
1953
subtest 'extended_attributes' => sub {
Lines 2121-2123 subtest 'extended_attributes' => sub { Link Here
2121
2122
2122
    $schema->storage->txn_rollback;
2123
    $schema->storage->txn_rollback;
2123
};
2124
};
2124
- 
2125
2126
subtest 'search_patrons_with_unpaid_fines() tests' => sub {
2127
2128
    plan tests => 7;
2129
2130
    $schema->storage->txn_begin;
2131
2132
    my $patrons_with_fines = Koha::Patrons->search({ branchcode => $library->{branchcode} })->search_patrons_with_unpaid_fines();
2133
    is( scalar( @$patrons_with_fines ), 0, 'search_patrons_with_unpaid_fines() should return empty array' );
2134
2135
    my $library = $builder->build({source => 'Branch' });
2136
2137
    my $patron1 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->{branchcode} }});
2138
    my $account1 = $patron1->account;
2139
    $account1->add_debit({ amount => 10, interface => 'commandline', type => 'OVERDUE' });
2140
2141
    my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->{branchcode} }});
2142
    my $account2 = $patron2->account;
2143
    $account2->add_debit({ amount => 10, interface => 'commandline', type => 'OVERDUE' });
2144
2145
    $patrons_with_fines = Koha::Patrons->search({ branchcode => $library->{branchcode} })->search_patrons_with_unpaid_fines();
2146
    is( scalar( @$patrons_with_fines ), 2, 'search_patrons_with_unpaid_fines() should return array with 2 values' );
2147
    is( $patrons_with_fines->[0]->{ borrowernumber }, $patron1->borrowernumber, 'patron1 should be in array');
2148
    is( $patrons_with_fines->[1]->{ borrowernumber }, $patron2->borrowernumber, 'patron2 should be in array');
2149
    is( $patrons_with_fines->[0]->{ accountbalance }, $account1->balance, 'patron1 fines are correct');
2150
    is( $patrons_with_fines->[1]->{ accountbalance }, $account2->balance, 'patron2 fines are correct');
2151
2152
    $account2->add_credit({ amount => 10, interface => 'commandline' });
2153
    $patrons_with_fines = Koha::Patrons->search({ branchcode => $library->{branchcode} })->search_patrons_with_unpaid_fines();
2154
    is( scalar( @$patrons_with_fines ), 1, 'Patron without fines is not in array');
2155
2156
    $schema->storage->txn_rollback;
2157
}

Return to bug 15156