Bug 20997: (follow-up) Add Koha::Account::Line->is_debit method
[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 => 3;
23 use Test::Exception;
24
25 use Koha::Account;
26 use Koha::Account::Lines;
27 use Koha::Account::Offsets;
28 use Koha::Items;
29
30 use t::lib::TestBuilder;
31
32 my $schema = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
34
35 subtest 'item() tests' => sub {
36
37     plan tests => 2;
38
39     $schema->storage->txn_begin;
40
41     my $library = $builder->build( { source => 'Branch' } );
42     my $biblioitem = $builder->build( { source => 'Biblioitem' } );
43     my $patron = $builder->build( { source => 'Borrower' } );
44     my $item = Koha::Item->new(
45     {
46         biblionumber     => $biblioitem->{biblionumber},
47         biblioitemnumber => $biblioitem->{biblioitemnumber},
48         homebranch       => $library->{branchcode},
49         holdingbranch    => $library->{branchcode},
50         barcode          => 'some_barcode_12',
51         itype            => 'BK',
52     })->store;
53
54     my $line = Koha::Account::Line->new(
55     {
56         borrowernumber => $patron->{borrowernumber},
57         itemnumber     => $item->itemnumber,
58         accounttype    => "F",
59         amount         => 10,
60     })->store;
61
62     my $account_line_item = $line->item;
63     is( ref( $account_line_item ), 'Koha::Item', 'Koha::Account::Line->item should return a Koha::Item' );
64     is( $line->itemnumber, $account_line_item->itemnumber, 'Koha::Account::Line->item should return the correct item' );
65
66     $schema->storage->txn_rollback;
67 };
68
69 subtest 'total_outstanding() tests' => sub {
70
71     plan tests => 5;
72
73     $schema->storage->txn_begin;
74
75     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
76
77     my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
78     is( $lines->total_outstanding, 0, 'total_outstanding returns 0 if no lines (undef case)' );
79
80     my $debit_1 = Koha::Account::Line->new(
81         {   borrowernumber    => $patron->id,
82             accounttype       => "F",
83             amount            => 10,
84             amountoutstanding => 10
85         }
86     )->store;
87
88     my $debit_2 = Koha::Account::Line->new(
89         {   borrowernumber    => $patron->id,
90             accounttype       => "F",
91             amount            => 10,
92             amountoutstanding => 10
93         }
94     )->store;
95
96     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
97     is( $lines->total_outstanding, 20, 'total_outstanding sums correctly' );
98
99     my $credit_1 = Koha::Account::Line->new(
100         {   borrowernumber    => $patron->id,
101             accounttype       => "F",
102             amount            => -10,
103             amountoutstanding => -10
104         }
105     )->store;
106
107     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
108     is( $lines->total_outstanding, 10, 'total_outstanding sums correctly' );
109
110     my $credit_2 = Koha::Account::Line->new(
111         {   borrowernumber    => $patron->id,
112             accounttype       => "F",
113             amount            => -10,
114             amountoutstanding => -10
115         }
116     )->store;
117
118     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
119     is( $lines->total_outstanding, 0, 'total_outstanding sums correctly' );
120
121     my $credit_3 = Koha::Account::Line->new(
122         {   borrowernumber    => $patron->id,
123             accounttype       => "F",
124             amount            => -100,
125             amountoutstanding => -100
126         }
127     )->store;
128
129     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
130     is( $lines->total_outstanding, -100, 'total_outstanding sums correctly' );
131
132     $schema->storage->txn_rollback;
133 };
134
135 subtest 'is_credit() and is_debit() tests' => sub {
136
137     plan tests => 4;
138
139     $schema->storage->txn_begin;
140
141     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
142     my $account = $patron->account;
143
144     my $credit = $account->add_credit({ amount => 100, user_id => $patron->id });
145
146     ok( $credit->is_credit, 'is_credit detects credits' );
147     ok( !$credit->is_debit, 'is_debit detects credits' );
148
149     my $debit = Koha::Account::Line->new(
150     {
151         borrowernumber => $patron->id,
152         accounttype    => "F",
153         amount         => 10,
154     })->store;
155
156     ok( !$debit->is_credit, 'is_credit detects debits' );
157     ok( $debit->is_debit, 'is_debit detects debits');
158
159     $schema->storage->txn_rollback;
160 };
161
162 subtest 'apply() tests' => sub {
163
164     plan tests => 12;
165
166     $schema->storage->txn_begin;
167
168     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
169     my $account = $patron->account;
170
171     my $credit = $account->add_credit( { amount => 100, user_id => $patron->id } );
172
173     my $debit_1 = Koha::Account::Line->new(
174         {   borrowernumber    => $patron->id,
175             accounttype       => "F",
176             amount            => 10,
177             amountoutstanding => 10
178         }
179     )->store;
180
181     my $debit_2 = Koha::Account::Line->new(
182         {   borrowernumber    => $patron->id,
183             accounttype       => "F",
184             amount            => 100,
185             amountoutstanding => 100
186         }
187     )->store;
188
189     $credit->discard_changes;
190     $debit_1->discard_changes;
191
192     my $remaining_credit = $credit->apply( { debit => $debit_1, offset_type => 'Manual Credit' } );
193     is( $remaining_credit, 90, 'Remaining credit is correctly calculated' );
194     $credit->discard_changes;
195     is( $credit->amountoutstanding * -1, $remaining_credit, 'Remaining credit correctly stored' );
196
197     # re-read debit info
198     $debit_1->discard_changes;
199     is( $debit_1->amountoutstanding * 1, 0, 'Debit has been cancelled' );
200
201     my $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_1->id } );
202     is( $offsets->count, 1, 'Only one offset is generated' );
203     my $THE_offest = $offsets->next;
204     is( $THE_offest->amount * 1, 10, 'Amount was calculated correctly (less than the available credit)' );
205     is( $THE_offest->type, 'Manual Credit', 'Passed type stored correctly' );
206
207     $remaining_credit = $credit->apply( { debit => $debit_2, offset_type => 'Manual Credit' } );
208     is( $remaining_credit, 0, 'No remaining credit left' );
209     $credit->discard_changes;
210     is( $credit->amountoutstanding * 1, 0, 'No outstanding credit' );
211     $debit_2->discard_changes;
212     is( $debit_2->amountoutstanding * 1, 10, 'Outstanding amount decremented correctly' );
213
214     throws_ok
215         { $debit_1->apply({ debit => $credit }); }
216         'Koha::Exceptions::Account::IsNotCredit',
217         '->apply() can only be used with credits';
218
219     throws_ok
220         { $credit->apply({ debit => $credit }); }
221         'Koha::Exceptions::Account::IsNotDebit',
222         '->apply() can only be applied to credits';
223
224     throws_ok
225         { $credit->apply({ debit => $debit_1 }); }
226         'Koha::Exceptions::Account::NoAvailableCredit',
227         '->apply() can only be used with outstanding credits';
228
229
230     $schema->storage->txn_rollback;
231 };