Bug 20990: (follow-up) Fix test description
[srvgit] / t / db_dependent / Koha / Account.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 => 3;
23
24 use Koha::Account;
25 use Koha::Account::Lines;
26 use Koha::Account::Offsets;
27
28 use t::lib::Mocks;
29 use t::lib::TestBuilder;
30
31 my $schema  = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new;
33
34 subtest 'outstanding_debits() tests' => sub {
35
36     plan tests => 12;
37
38     $schema->storage->txn_begin;
39
40     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
41
42     my @generated_lines;
43     push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 1 })->store;
44     push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 2 })->store;
45     push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 3 })->store;
46     push @generated_lines, Koha::Account::Line->new({ borrowernumber => $patron->id, amountoutstanding => 4 })->store;
47
48     my $account = $patron->account;
49     my $lines   = $account->outstanding_debits();
50
51     is( $lines->total_outstanding, 10, 'Outstandig debits total is correctly calculated' );
52
53     my $i = 0;
54     foreach my $line ( @{ $lines->as_list } ) {
55         my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
56         is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
57         $i++;
58     }
59
60     my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
61     Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -2 })->store;
62     my $just_one = Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding =>  3 })->store;
63     Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -6 })->store;
64     $lines = $patron_2->account->outstanding_debits();
65     is( $lines->total_outstanding, 3, "Total if some outstanding debits and some credits is only debits" );
66     is( $lines->count, 1, "With 1 outstanding debits, we get back a Lines object with 1 lines" );
67     my $the_line = Koha::Account::Lines->find( $just_one->id );
68     is_deeply( $the_line->unblessed, $lines->next->unblessed, "We get back the one correct line");
69
70     my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
71     Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -2 })->store;
72     Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -20 })->store;
73     Koha::Account::Line->new({ borrowernumber => $patron_2->id, amountoutstanding => -200 })->store;
74     $lines = $patron_3->account->outstanding_debits();
75     is( $lines->total_outstanding, 0, "Total if no outstanding debits total is 0" );
76     is( $lines->count, 0, "With 0 outstanding debits, we get back a Lines object with 0 lines" );
77
78     my $patron_4 = $builder->build_object({ class => 'Koha::Patrons' });
79     $lines = $patron_4->account->outstanding_debits();
80     is( $lines->total_outstanding, 0, "Total if no outstanding debits is 0" );
81     is( $lines->count, 0, "With no outstanding debits, we get back a Lines object with 0 lines" );
82
83     $schema->storage->txn_rollback;
84 };
85
86 subtest 'outstanding_credits() tests' => sub {
87
88     plan tests => 5;
89
90     $schema->storage->txn_begin;
91
92     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
93     my $account = Koha::Account->new({ patron_id => $patron->id });
94
95     my @generated_lines;
96     push @generated_lines, $account->add_credit({ amount => 1 });
97     push @generated_lines, $account->add_credit({ amount => 2 });
98     push @generated_lines, $account->add_credit({ amount => 3 });
99     push @generated_lines, $account->add_credit({ amount => 4 });
100
101     my ( $total, $lines ) = $account->outstanding_credits();
102
103     is( $total, -10, 'Outstandig credits total is correctly calculated' );
104
105     my $i = 0;
106     foreach my $line ( @{ $lines->as_list } ) {
107         my $fetched_line = Koha::Account::Lines->find( $generated_lines[$i]->id );
108         is_deeply( $line->unblessed, $fetched_line->unblessed, "Fetched line matches the generated one ($i)" );
109         $i++;
110     }
111
112     $schema->storage->txn_rollback;
113 };
114
115 subtest 'add_credit() tests' => sub {
116
117     plan tests => 15;
118
119     $schema->storage->txn_begin;
120
121     # delete logs and statistics
122     my $action_logs = $schema->resultset('ActionLog')->search()->count;
123     my $statistics = $schema->resultset('Statistic')->search()->count;
124
125     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
126     my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
127
128     is( $account->balance, 0, 'Patron has no balance' );
129
130     # Disable logs
131     t::lib::Mocks::mock_preference( 'FinesLog', 0 );
132
133     my $line_1 = $account->add_credit(
134         {   amount      => 25,
135             description => 'Payment of 25',
136             library_id  => $patron->branchcode,
137             note        => 'not really important',
138             type        => 'payment',
139             user_id     => $patron->id
140         }
141     );
142
143     is( $account->balance, -25, 'Patron has a balance of -25' );
144     is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
145     is( $schema->resultset('Statistic')->count(), $statistics + 1, 'Action added to statistics' );
146     is( $line_1->accounttype, $Koha::Account::account_type->{'payment'}, 'Account type is correctly set' );
147
148     # Enable logs
149     t::lib::Mocks::mock_preference( 'FinesLog', 1 );
150
151     my $sip_code = "1";
152     my $line_2 = $account->add_credit(
153         {   amount      => 37,
154             description => 'Payment of 37',
155             library_id  => $patron->branchcode,
156             note        => 'not really important',
157             user_id     => $patron->id,
158             sip         => $sip_code
159         }
160     );
161
162     is( $account->balance, -62, 'Patron has a balance of -25' );
163     is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
164     is( $schema->resultset('Statistic')->count(), $statistics + 2, 'Action added to statistics' );
165     is( $line_2->accounttype, $Koha::Account::account_type->{'payment'} . $sip_code, 'Account type is correctly set' );
166
167     # offsets have the credit_id set to accountlines_id, and debit_id is undef
168     my $offset_1 = Koha::Account::Offsets->search({ credit_id => $line_1->id })->next;
169     my $offset_2 = Koha::Account::Offsets->search({ credit_id => $line_2->id })->next;
170
171     is( $offset_1->credit_id, $line_1->id, 'No debit_id is set for credits' );
172     is( $offset_1->debit_id, undef, 'No debit_id is set for credits' );
173     is( $offset_2->credit_id, $line_2->id, 'No debit_id is set for credits' );
174     is( $offset_2->debit_id, undef, 'No debit_id is set for credits' );
175
176     my $line_3 = $account->add_credit(
177         {   amount      => 20,
178             description => 'Manual credit applied',
179             library_id  => $patron->branchcode,
180             user_id     => $patron->id,
181             type        => 'forgiven'
182         }
183     );
184
185     is( $schema->resultset('ActionLog')->count(), $action_logs + 2, 'Log was added' );
186     is( $schema->resultset('Statistic')->count(), $statistics + 2, 'No action added to statistics, because of credit type' );
187
188     $schema->storage->txn_rollback;
189 };