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

(-)a/Koha/Account.pm (+32 lines)
Lines 288-293 sub balance { Link Here
288
      : 0;
288
      : 0;
289
}
289
}
290
290
291
=head3 outstanding_debits
292
293
my ( $total, $lines ) = Koha::Account->new({ patron_id => $patron_id })->outstanding_debits;
294
295
=cut
296
297
sub outstanding_debits {
298
    my ($self) = @_;
299
300
    my $outstanding_debits = Koha::Account::Lines->search(
301
        {   borrowernumber    => $self->{patron_id},
302
            amountoutstanding => { '>' => 0 }
303
        },
304
        {   select => [ { sum => 'amountoutstanding' } ],
305
            as     => ['outstanding_debits_total'],
306
        }
307
    );
308
    my $total
309
        = ( $outstanding_debits->count )
310
        ? $outstanding_debits->next->get_column('outstanding_debits_total') + 0
311
        : 0;
312
313
    my $lines = Koha::Account::Lines->search(
314
        {
315
            borrowernumber    => $self->{patron_id},
316
            amountoutstanding => { '>' => 0 }
317
        }
318
    );
319
320
    return ( $total, $lines );
321
}
322
291
=head3 non_issues_charges
323
=head3 non_issues_charges
292
324
293
my $non_issues_charges = $self->non_issues_charges
325
my $non_issues_charges = $self->non_issues_charges
(-)a/t/db_dependent/Koha/Account.t (-1 / +59 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2018 Koha Development team
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>
19
20
use Modern::Perl;
21
22
use Test::More tests => 1;
23
24
use Koha::Account;
25
use Koha::Account::Lines;
26
27
use t::lib::TestBuilder;
28
29
my $schema  = Koha::Database->new->schema;
30
my $builder = t::lib::TestBuilder->new;
31
32
subtest 'outstanding_debits() tests' => sub {
33
34
    plan tests => 5;
35
36
    $schema->storage->txn_begin;
37
38
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
39
40
    my @generated_lines;
41
    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 1 })->store;
42
    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 2 })->store;
43
    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 3 })->store;
44
    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 4 })->store;
45
46
    my $account = Koha::Account->new({ patron_id => $patron->id });
47
    my ( $total, $lines ) = $account->outstanding_debits();
48
49
    is( $total, 10, 'Outstandig debits total is correctly calculated' );
50
51
    my $i = 0;
52
    foreach my $line ( @{ $lines->as_list } ) {
53
        my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
54
        is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
55
        $i++;
56
    }
57
58
    $schema->storage->txn_rollback;
59
};

Return to bug 20946