Bug 20997: ->apply should be applied to Koha::Account::Lines
[koha-equinox.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 => 4;
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 => 24;
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 $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
193     my $remaining_credit = $credit->apply( { debits => $debits, offset_type => 'Manual Credit' } );
194     is( $remaining_credit * 1, 90, 'Remaining credit is correctly calculated' );
195     $credit->discard_changes;
196     is( $credit->amountoutstanding * -1, $remaining_credit, 'Remaining credit correctly stored' );
197
198     # re-read debit info
199     $debit_1->discard_changes;
200     is( $debit_1->amountoutstanding * 1, 0, 'Debit has been cancelled' );
201
202     my $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_1->id } );
203     is( $offsets->count, 1, 'Only one offset is generated' );
204     my $THE_offset = $offsets->next;
205     is( $THE_offset->amount * 1, 10, 'Amount was calculated correctly (less than the available credit)' );
206     is( $THE_offset->type, 'Manual Credit', 'Passed type stored correctly' );
207
208     $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
209     $remaining_credit = $credit->apply( { debits => $debits } );
210     is( $remaining_credit, 0, 'No remaining credit left' );
211     $credit->discard_changes;
212     is( $credit->amountoutstanding * 1, 0, 'No outstanding credit' );
213     $debit_2->discard_changes;
214     is( $debit_2->amountoutstanding * 1, 10, 'Outstanding amount decremented correctly' );
215
216     $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_2->id } );
217     is( $offsets->count, 1, 'Only one offset is generated' );
218     $THE_offset = $offsets->next;
219     is( $THE_offset->amount * 1, 90, 'Amount was calculated correctly (less than the available credit)' );
220     is( $THE_offset->type, 'credit_applied', 'Defaults to credit_applied offset type' );
221
222     $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
223     throws_ok
224         { $credit->apply({ debits => $debits }); }
225         'Koha::Exceptions::Account::NoAvailableCredit',
226         '->apply() can only be used with outstanding credits';
227
228     $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
229     throws_ok
230         { $debit_1->apply({ debits => $debits }); }
231         'Koha::Exceptions::Account::IsNotCredit',
232         '->apply() can only be used with credits';
233
234     $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
235     my $credit_3 = $account->add_credit({ amount => 1 });
236     throws_ok
237         { $credit_3->apply({ debits => $debits }); }
238         'Koha::Exceptions::Account::IsNotDebit',
239         '->apply() can only be applied to credits';
240
241     my $credit_2 = $account->add_credit({ amount => 20 });
242     my $debit_3  = Koha::Account::Line->new(
243         {   borrowernumber    => $patron->id,
244             accounttype       => "F",
245             amount            => 100,
246             amountoutstanding => 100
247         }
248     )->store;
249
250     $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id, $credit->id ] } });
251     throws_ok {
252         $credit_2->apply( { debits => $debits, offset_type => 'Manual Credit' } ); }
253         'Koha::Exceptions::Account::IsNotDebit',
254         '->apply() rolls back if any of the passed lines is not a debit';
255
256     is( $debit_1->discard_changes->amountoutstanding * 1,   0, 'No changes to already cancelled debit' );
257     is( $debit_2->discard_changes->amountoutstanding * 1,  10, 'Debit cancelled' );
258     is( $debit_3->discard_changes->amountoutstanding * 1, 100, 'Outstanding amount correctly calculated' );
259     is( $credit_2->discard_changes->amountoutstanding * -1, 20, 'No changes made' );
260
261     $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id ] } });
262     $remaining_credit = $credit_2->apply( { debits => $debits, offset_type => 'Manual Credit' } );
263
264     is( $debit_1->discard_changes->amountoutstanding * 1,  0, 'No changes to already cancelled debit' );
265     is( $debit_2->discard_changes->amountoutstanding * 1,  0, 'Debit cancelled' );
266     is( $debit_3->discard_changes->amountoutstanding * 1, 90, 'Outstanding amount correctly calculated' );
267     is( $credit_2->discard_changes->amountoutstanding * 1, 0, 'No remaining credit' );
268
269     $schema->storage->txn_rollback;
270 };