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

(-)a/Koha/Account.pm (-5 / +2 lines)
Lines 22-28 use Modern::Perl; Link Here
22
use Carp;
22
use Carp;
23
use Data::Dumper;
23
use Data::Dumper;
24
use List::MoreUtils qw( uniq );
24
use List::MoreUtils qw( uniq );
25
use List::Util qw( sum0 );
26
25
27
use C4::Log qw( logaction );
26
use C4::Log qw( logaction );
28
use C4::Stats qw( UpdateStats );
27
use C4::Stats qw( UpdateStats );
Lines 291-297 sub balance { Link Here
291
290
292
=head3 outstanding_debits
291
=head3 outstanding_debits
293
292
294
my ( $total, $lines ) = Koha::Account->new({ patron_id => $patron_id })->outstanding_debits;
293
my $lines = Koha::Account->new({ patron_id => $patron_id })->outstanding_debits;
295
294
296
=cut
295
=cut
297
296
Lines 305-313 sub outstanding_debits { Link Here
305
        }
304
        }
306
    );
305
    );
307
306
308
    my $total = sum0( $lines->get_column('amountoutstanding') );
307
    return $lines;
309
310
    return ( $total, $lines );
311
}
308
}
312
309
313
=head3 non_issues_charges
310
=head3 non_issues_charges
(-)a/Koha/Account/Lines.pm (-2 / +20 lines)
Lines 18-26 package Koha::Account::Lines; Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Carp;
20
use Carp;
21
use List::Util qw( sum0 );
21
22
22
use Koha::Database;
23
use Koha::Database;
23
24
use Koha::Account::Line;
24
use Koha::Account::Line;
25
25
26
use base qw(Koha::Objects);
26
use base qw(Koha::Objects);
Lines 34-42 Koha::Account::Lines - Koha Account Line Object set class Link Here
34
34
35
=head2 Class Methods
35
=head2 Class Methods
36
36
37
=head3 total_outstanding
38
39
    my $lines = Koha::Account::Lines->search({ ...  });
40
    my $total = $lines->total_outstanding;
41
42
Returns the sum of the outstanding amounts of the resultset. If the resultset is
43
empty it returns 0.
44
37
=cut
45
=cut
38
46
39
=head3 type
47
sub total_outstanding {
48
    my ( $self ) = @_;
49
50
    my $total = sum0( $self->get_column('amountoutstanding') );
51
52
    return $total;
53
}
54
55
=head2 Internal methods
56
57
=head3 _type
40
58
41
=cut
59
=cut
42
60
(-)a/members/pay.pl (-1 / +2 lines)
Lines 139-145 output_html_with_http_headers $input, $cookie, $template->output; Link Here
139
sub add_accounts_to_template {
139
sub add_accounts_to_template {
140
140
141
    my $patron = Koha::Patrons->find( $borrowernumber );
141
    my $patron = Koha::Patrons->find( $borrowernumber );
142
    my ( $total, $account_lines ) = Koha::Account->new( { patron_id => $borrowernumber } )->outstanding_debits;
142
    my $account_lines = $patron->account->outstanding_debits;
143
    my $total = $account_lines->total_outstanding;
143
    my @accounts;
144
    my @accounts;
144
    while ( my $account_line = $account_lines->next ) {
145
    while ( my $account_line = $account_lines->next ) {
145
        $account_line = $account_line->unblessed;
146
        $account_line = $account_line->unblessed;
(-)a/members/paycollect.pl (-2 / +3 lines)
Lines 58-65 my $borrower = $patron->unblessed; Link Here
58
my $category       = $patron->category;
58
my $category       = $patron->category;
59
my $user           = $input->remote_user;
59
my $user           = $input->remote_user;
60
60
61
my $branch         = C4::Context->userenv->{'branch'};
61
my $branch    = C4::Context->userenv->{'branch'};
62
my ( $total_due ) = Koha::Account->new( { patron_id => $borrowernumber } )->outstanding_debits;
62
my $total_due = $patron->account->outstanding_debits->total_outstanding;
63
63
my $total_paid = $input->param('paid');
64
my $total_paid = $input->param('paid');
64
65
65
my $individual   = $input->param('pay_individual');
66
my $individual   = $input->param('pay_individual');
(-)a/t/db_dependent/Koha/Account.t (-11 / +12 lines)
Lines 43-52 subtest 'outstanding_debits() tests' => sub { Link Here
43
    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 3 })->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;
44
    push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 4 })->store;
45
45
46
    my $account = Koha::Account->new({ patron_id => $patron->id });
46
    my $account = $patron->account;
47
    my ( $total, $lines ) = $account->outstanding_debits();
47
    my $lines   = $account->outstanding_debits();
48
48
49
    is( $total, 10, 'Outstandig debits total is correctly calculated' );
49
    is( $lines->total_outstanding, 10, 'Outstandig debits total is correctly calculated' );
50
50
51
    my $i = 0;
51
    my $i = 0;
52
    foreach my $line ( @{ $lines->as_list } ) {
52
    foreach my $line ( @{ $lines->as_list } ) {
Lines 55-70 subtest 'outstanding_debits() tests' => sub { Link Here
55
        $i++;
55
        $i++;
56
    }
56
    }
57
57
58
    ( $total, $lines ) =  Koha::Account->new({ patron_id => 'InvalidBorrowernumber' })->outstanding_debits();
59
    is( $total, 0, "Total if no outstanding debits is 0" );
60
    is( $lines->count, 0, "With no outstanding debits, we get back a Lines object with 0 lines" );
61
62
    my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
58
    my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
63
    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -2 })->store;
59
    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -2 })->store;
64
    my $just_one = Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding =>  3 })->store;
60
    my $just_one = Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding =>  3 })->store;
65
    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -6 })->store;
61
    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -6 })->store;
66
    ( $total, $lines ) =  Koha::Account->new({ patron_id => $patron_2->id })->outstanding_debits();
62
    $lines = $patron_2->account->outstanding_debits();
67
    is( $total, 3, "Total if some outstanding debits and some credits is only debits" );
63
    is( $lines->total_outstanding, 3, "Total if some outstanding debits and some credits is only debits" );
68
    is( $lines->count, 1, "With 1 outstanding debits, we get back a Lines object with 1 lines" );
64
    is( $lines->count, 1, "With 1 outstanding debits, we get back a Lines object with 1 lines" );
69
    my $the_line = Koha::Account::Lines->find( $just_one->id );
65
    my $the_line = Koha::Account::Lines->find( $just_one->id );
70
    is_deeply( $the_line->unblessed, $lines->next->unblessed, "We get back the one correct line");
66
    is_deeply( $the_line->unblessed, $lines->next->unblessed, "We get back the one correct line");
Lines 73-81 subtest 'outstanding_debits() tests' => sub { Link Here
73
    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -2 })->store;
69
    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -2 })->store;
74
    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -20 })->store;
70
    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -20 })->store;
75
    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -200 })->store;
71
    Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -200 })->store;
76
    ( $total, $lines ) =  Koha::Account->new({ patron_id => $patron_3->id })->outstanding_debits();
72
    $lines = $patron_3->account->outstanding_debits();
77
    is( $total, 0, "Total if no outstanding debits total is 0" );
73
    is( $lines->total_outstanding, 0, "Total if no outstanding debits total is 0" );
78
    is( $lines->count, 0, "With 0 outstanding debits, we get back a Lines object with 0 lines" );
74
    is( $lines->count, 0, "With 0 outstanding debits, we get back a Lines object with 0 lines" );
79
75
76
    my $patron_4 = $builder->build_object({ class => 'Koha::Patrons' });
77
    $lines = $patron_4->account->outstanding_debits();
78
    is( $lines->total_outstanding, 0, "Total if no outstanding debits is 0" );
79
    is( $lines->count, 0, "With no outstanding debits, we get back a Lines object with 0 lines" );
80
80
    $schema->storage->txn_rollback;
81
    $schema->storage->txn_rollback;
81
};
82
};
(-)a/t/db_dependent/Koha/Account/Lines.t (-32 / +96 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 1;
22
use Test::More tests => 2;
23
23
24
use Koha::Account::Lines;
24
use Koha::Account::Lines;
25
use Koha::Items;
25
use Koha::Items;
Lines 27-65 use Koha::Items; Link Here
27
use t::lib::TestBuilder;
27
use t::lib::TestBuilder;
28
28
29
my $schema = Koha::Database->new->schema;
29
my $schema = Koha::Database->new->schema;
30
$schema->storage->txn_begin;
31
32
my $builder = t::lib::TestBuilder->new;
30
my $builder = t::lib::TestBuilder->new;
33
31
34
subtest 'item' => sub {
32
subtest 'item' => sub {
35
        plan tests => 2;
33
36
34
    plan tests => 2;
37
        my $library = $builder->build( { source => 'Branch' } );
35
38
        my $biblioitem = $builder->build( { source => 'Biblioitem' } );
36
    $schema->storage->txn_begin;
39
        my $patron = $builder->build( { source => 'Borrower' } );
37
40
        my $item = Koha::Item->new(
38
    my $library = $builder->build( { source => 'Branch' } );
41
        {
39
    my $biblioitem = $builder->build( { source => 'Biblioitem' } );
42
            biblionumber     => $biblioitem->{biblionumber},
40
    my $patron = $builder->build( { source => 'Borrower' } );
43
            biblioitemnumber => $biblioitem->{biblioitemnumber},
41
    my $item = Koha::Item->new(
44
            homebranch       => $library->{branchcode},
42
    {
45
            holdingbranch    => $library->{branchcode},
43
        biblionumber     => $biblioitem->{biblionumber},
46
            barcode          => 'some_barcode_12',
44
        biblioitemnumber => $biblioitem->{biblioitemnumber},
47
            itype            => 'BK',
45
        homebranch       => $library->{branchcode},
48
        })->store;
46
        holdingbranch    => $library->{branchcode},
49
47
        barcode          => 'some_barcode_12',
50
        my $line = Koha::Account::Line->new(
48
        itype            => 'BK',
51
        {
49
    })->store;
52
            borrowernumber => $patron->{borrowernumber},
50
53
            itemnumber     => $item->itemnumber,
51
    my $line = Koha::Account::Line->new(
54
            accounttype    => "F",
52
    {
55
            amount         => 10,
53
        borrowernumber => $patron->{borrowernumber},
56
        })->store;
54
        itemnumber     => $item->itemnumber,
57
55
        accounttype    => "F",
58
        my $account_line_item = $line->item;
56
        amount         => 10,
59
        is( ref( $account_line_item ), 'Koha::Item', 'Koha::Account::Line->item should return a Koha::Item' );
57
    })->store;
60
        is( $line->itemnumber, $account_line_item->itemnumber, 'Koha::Account::Line->item should return the correct item' );
58
59
    my $account_line_item = $line->item;
60
    is( ref( $account_line_item ), 'Koha::Item', 'Koha::Account::Line->item should return a Koha::Item' );
61
    is( $line->itemnumber, $account_line_item->itemnumber, 'Koha::Account::Line->item should return the correct item' );
62
63
    $schema->storage->txn_rollback;
61
};
64
};
62
65
63
$schema->storage->txn_rollback;
66
subtest 'total_outstanding' => sub {
67
68
    plan tests => 5;
69
70
    $schema->storage->txn_begin;
71
72
    my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
73
74
    my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
75
    is( $lines->total_outstanding, 0, 'total_outstanding returns 0 if no lines (undef case)' );
64
76
65
1;
77
    my $debit_1 = Koha::Account::Line->new(
78
        {   borrowernumber    => $patron->id,
79
            accounttype       => "F",
80
            amount            => 10,
81
            amountoutstanding => 10
82
        }
83
    )->store;
84
85
    my $debit_2 = Koha::Account::Line->new(
86
        {   borrowernumber    => $patron->id,
87
            accounttype       => "F",
88
            amount            => 10,
89
            amountoutstanding => 10
90
        }
91
    )->store;
92
93
    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
94
    is( $lines->total_outstanding, 20, 'total_outstanding sums correctly' );
95
96
    my $credit_1 = Koha::Account::Line->new(
97
        {   borrowernumber    => $patron->id,
98
            accounttype       => "F",
99
            amount            => -10,
100
            amountoutstanding => -10
101
        }
102
    )->store;
103
104
    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
105
    is( $lines->total_outstanding, 10, 'total_outstanding sums correctly' );
106
107
    my $credit_2 = Koha::Account::Line->new(
108
        {   borrowernumber    => $patron->id,
109
            accounttype       => "F",
110
            amount            => -10,
111
            amountoutstanding => -10
112
        }
113
    )->store;
114
115
    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
116
    is( $lines->total_outstanding, 0, 'total_outstanding sums correctly' );
117
118
    my $credit_3 = Koha::Account::Line->new(
119
        {   borrowernumber    => $patron->id,
120
            accounttype       => "F",
121
            amount            => -100,
122
            amountoutstanding => -100
123
        }
124
    )->store;
125
126
    $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
127
    is( $lines->total_outstanding, -100, 'total_outstanding sums correctly' );
128
129
    $schema->storage->txn_rollback;
130
};
66
- 

Return to bug 20946