Bug 20946: (QA follow-up) make outstanding_debits return the account lines only
[koha.git] / t / db_dependent / Koha / Account / Lines.t
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 => 2;
23
24 use Koha::Account::Lines;
25 use Koha::Items;
26
27 use t::lib::TestBuilder;
28
29 my $schema = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
31
32 subtest 'item' => sub {
33
34     plan tests => 2;
35
36     $schema->storage->txn_begin;
37
38     my $library = $builder->build( { source => 'Branch' } );
39     my $biblioitem = $builder->build( { source => 'Biblioitem' } );
40     my $patron = $builder->build( { source => 'Borrower' } );
41     my $item = Koha::Item->new(
42     {
43         biblionumber     => $biblioitem->{biblionumber},
44         biblioitemnumber => $biblioitem->{biblioitemnumber},
45         homebranch       => $library->{branchcode},
46         holdingbranch    => $library->{branchcode},
47         barcode          => 'some_barcode_12',
48         itype            => 'BK',
49     })->store;
50
51     my $line = Koha::Account::Line->new(
52     {
53         borrowernumber => $patron->{borrowernumber},
54         itemnumber     => $item->itemnumber,
55         accounttype    => "F",
56         amount         => 10,
57     })->store;
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;
64 };
65
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)' );
76
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 };