Bug 21727: Unit tests for the refund case
[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 => 6;
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::Mocks;
31 use t::lib::TestBuilder;
32
33 my $schema = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
35
36 subtest 'item() tests' => sub {
37
38     plan tests => 2;
39
40     $schema->storage->txn_begin;
41
42     my $library = $builder->build( { source => 'Branch' } );
43     my $biblioitem = $builder->build( { source => 'Biblioitem' } );
44     my $patron = $builder->build( { source => 'Borrower' } );
45     my $item = Koha::Item->new(
46     {
47         biblionumber     => $biblioitem->{biblionumber},
48         biblioitemnumber => $biblioitem->{biblioitemnumber},
49         homebranch       => $library->{branchcode},
50         holdingbranch    => $library->{branchcode},
51         barcode          => 'some_barcode_12',
52         itype            => 'BK',
53     })->store;
54
55     my $line = Koha::Account::Line->new(
56     {
57         borrowernumber => $patron->{borrowernumber},
58         itemnumber     => $item->itemnumber,
59         accounttype    => "F",
60         amount         => 10,
61     })->store;
62
63     my $account_line_item = $line->item;
64     is( ref( $account_line_item ), 'Koha::Item', 'Koha::Account::Line->item should return a Koha::Item' );
65     is( $line->itemnumber, $account_line_item->itemnumber, 'Koha::Account::Line->item should return the correct item' );
66
67     $schema->storage->txn_rollback;
68 };
69
70 subtest 'total_outstanding() tests' => sub {
71
72     plan tests => 5;
73
74     $schema->storage->txn_begin;
75
76     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
77
78     my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
79     is( $lines->total_outstanding, 0, 'total_outstanding returns 0 if no lines (undef case)' );
80
81     my $debit_1 = Koha::Account::Line->new(
82         {   borrowernumber    => $patron->id,
83             accounttype       => "F",
84             amount            => 10,
85             amountoutstanding => 10
86         }
87     )->store;
88
89     my $debit_2 = Koha::Account::Line->new(
90         {   borrowernumber    => $patron->id,
91             accounttype       => "F",
92             amount            => 10,
93             amountoutstanding => 10
94         }
95     )->store;
96
97     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
98     is( $lines->total_outstanding, 20, 'total_outstanding sums correctly' );
99
100     my $credit_1 = Koha::Account::Line->new(
101         {   borrowernumber    => $patron->id,
102             accounttype       => "F",
103             amount            => -10,
104             amountoutstanding => -10
105         }
106     )->store;
107
108     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
109     is( $lines->total_outstanding, 10, 'total_outstanding sums correctly' );
110
111     my $credit_2 = Koha::Account::Line->new(
112         {   borrowernumber    => $patron->id,
113             accounttype       => "F",
114             amount            => -10,
115             amountoutstanding => -10
116         }
117     )->store;
118
119     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
120     is( $lines->total_outstanding, 0, 'total_outstanding sums correctly' );
121
122     my $credit_3 = Koha::Account::Line->new(
123         {   borrowernumber    => $patron->id,
124             accounttype       => "F",
125             amount            => -100,
126             amountoutstanding => -100
127         }
128     )->store;
129
130     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
131     is( $lines->total_outstanding, -100, 'total_outstanding sums correctly' );
132
133     $schema->storage->txn_rollback;
134 };
135
136 subtest 'is_credit() and is_debit() tests' => sub {
137
138     plan tests => 4;
139
140     $schema->storage->txn_begin;
141
142     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
143     my $account = $patron->account;
144
145     my $credit = $account->add_credit({ amount => 100, user_id => $patron->id });
146
147     ok( $credit->is_credit, 'is_credit detects credits' );
148     ok( !$credit->is_debit, 'is_debit detects credits' );
149
150     my $debit = Koha::Account::Line->new(
151     {
152         borrowernumber => $patron->id,
153         accounttype    => "F",
154         amount         => 10,
155     })->store;
156
157     ok( !$debit->is_credit, 'is_credit detects debits' );
158     ok( $debit->is_debit, 'is_debit detects debits');
159
160     $schema->storage->txn_rollback;
161 };
162
163 subtest 'apply() tests' => sub {
164
165     plan tests => 24;
166
167     $schema->storage->txn_begin;
168
169     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
170     my $account = $patron->account;
171
172     my $credit = $account->add_credit( { amount => 100, user_id => $patron->id } );
173
174     my $debit_1 = Koha::Account::Line->new(
175         {   borrowernumber    => $patron->id,
176             accounttype       => "F",
177             amount            => 10,
178             amountoutstanding => 10
179         }
180     )->store;
181
182     my $debit_2 = Koha::Account::Line->new(
183         {   borrowernumber    => $patron->id,
184             accounttype       => "F",
185             amount            => 100,
186             amountoutstanding => 100
187         }
188     )->store;
189
190     $credit->discard_changes;
191     $debit_1->discard_changes;
192
193     my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
194     my $remaining_credit = $credit->apply( { debits => $debits, offset_type => 'Manual Credit' } );
195     is( $remaining_credit * 1, 90, 'Remaining credit is correctly calculated' );
196     $credit->discard_changes;
197     is( $credit->amountoutstanding * -1, $remaining_credit, 'Remaining credit correctly stored' );
198
199     # re-read debit info
200     $debit_1->discard_changes;
201     is( $debit_1->amountoutstanding * 1, 0, 'Debit has been cancelled' );
202
203     my $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_1->id } );
204     is( $offsets->count, 1, 'Only one offset is generated' );
205     my $THE_offset = $offsets->next;
206     is( $THE_offset->amount * 1, -10, 'Amount was calculated correctly (less than the available credit)' );
207     is( $THE_offset->type, 'Manual Credit', 'Passed type stored correctly' );
208
209     $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
210     $remaining_credit = $credit->apply( { debits => $debits } );
211     is( $remaining_credit, 0, 'No remaining credit left' );
212     $credit->discard_changes;
213     is( $credit->amountoutstanding * 1, 0, 'No outstanding credit' );
214     $debit_2->discard_changes;
215     is( $debit_2->amountoutstanding * 1, 10, 'Outstanding amount decremented correctly' );
216
217     $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_2->id } );
218     is( $offsets->count, 1, 'Only one offset is generated' );
219     $THE_offset = $offsets->next;
220     is( $THE_offset->amount * 1, -90, 'Amount was calculated correctly (less than the available credit)' );
221     is( $THE_offset->type, 'Credit Applied', 'Defaults to \'Credit Applied\' offset type' );
222
223     $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
224     throws_ok
225         { $credit->apply({ debits => $debits }); }
226         'Koha::Exceptions::Account::NoAvailableCredit',
227         '->apply() can only be used with outstanding credits';
228
229     $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
230     throws_ok
231         { $debit_1->apply({ debits => $debits }); }
232         'Koha::Exceptions::Account::IsNotCredit',
233         '->apply() can only be used with credits';
234
235     $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
236     my $credit_3 = $account->add_credit({ amount => 1 });
237     throws_ok
238         { $credit_3->apply({ debits => $debits }); }
239         'Koha::Exceptions::Account::IsNotDebit',
240         '->apply() can only be applied to credits';
241
242     my $credit_2 = $account->add_credit({ amount => 20 });
243     my $debit_3  = Koha::Account::Line->new(
244         {   borrowernumber    => $patron->id,
245             accounttype       => "F",
246             amount            => 100,
247             amountoutstanding => 100
248         }
249     )->store;
250
251     $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id, $credit->id ] } });
252     throws_ok {
253         $credit_2->apply( { debits => $debits, offset_type => 'Manual Credit' } ); }
254         'Koha::Exceptions::Account::IsNotDebit',
255         '->apply() rolls back if any of the passed lines is not a debit';
256
257     is( $debit_1->discard_changes->amountoutstanding * 1,   0, 'No changes to already cancelled debit' );
258     is( $debit_2->discard_changes->amountoutstanding * 1,  10, 'Debit cancelled' );
259     is( $debit_3->discard_changes->amountoutstanding * 1, 100, 'Outstanding amount correctly calculated' );
260     is( $credit_2->discard_changes->amountoutstanding * -1, 20, 'No changes made' );
261
262     $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id ] } });
263     $remaining_credit = $credit_2->apply( { debits => $debits, offset_type => 'Manual Credit' } );
264
265     is( $debit_1->discard_changes->amountoutstanding * 1,  0, 'No changes to already cancelled debit' );
266     is( $debit_2->discard_changes->amountoutstanding * 1,  0, 'Debit cancelled' );
267     is( $debit_3->discard_changes->amountoutstanding * 1, 90, 'Outstanding amount correctly calculated' );
268     is( $credit_2->discard_changes->amountoutstanding * 1, 0, 'No remaining credit' );
269
270     $schema->storage->txn_rollback;
271 };
272
273 subtest 'Keep account info when a patron is deleted' => sub {
274
275     plan tests => 2;
276
277     $schema->storage->txn_begin;
278
279     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
280     my $item = $builder->build_object({ class => 'Koha::Items' });
281     my $line = Koha::Account::Line->new(
282     {
283         borrowernumber => $patron->borrowernumber,
284         itemnumber     => $item->itemnumber,
285         accounttype    => "F",
286         amount         => 10,
287     })->store;
288
289     $item->delete;
290     $line = $line->get_from_storage;
291     is( $line->itemnumber, undef, "The account line should not be deleted when the related item is delete");
292
293     $patron->delete;
294     $line = $line->get_from_storage;
295     is( $line->borrowernumber, undef, "The account line should not be deleted when the related patron is delete");
296
297     $schema->storage->txn_rollback;
298 };
299
300 subtest 'adjust() tests' => sub {
301
302     plan tests => 33;
303
304     $schema->storage->txn_begin;
305
306     # count logs before any actions
307     my $action_logs = $schema->resultset('ActionLog')->search()->count;
308
309     # Disable logs
310     t::lib::Mocks::mock_preference( 'FinesLog', 0 );
311
312     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
313     my $account = $patron->account;
314
315     my $debit_1 = Koha::Account::Line->new(
316         {   borrowernumber    => $patron->id,
317             accounttype       => "F",
318             amount            => 10,
319             amountoutstanding => 10
320         }
321     )->store;
322
323     my $debit_2 = Koha::Account::Line->new(
324         {   borrowernumber    => $patron->id,
325             accounttype       => "FU",
326             amount            => 100,
327             amountoutstanding => 100
328         }
329     )->store;
330
331     my $credit = $account->add_credit( { amount => 40, user_id => $patron->id } );
332
333     throws_ok { $debit_1->adjust( { amount => 50, type => 'bad' } ) }
334     qr/Update type not recognised/, 'Exception thrown for unrecognised type';
335
336     throws_ok { $debit_1->adjust( { amount => 50, type => 'fine_increment' } ) }
337     qr/Update type not allowed on this accounttype/,
338       'Exception thrown for type conflict';
339
340     # Increment an unpaid fine
341     $debit_2->adjust( { amount => 150, type => 'fine_increment' } )->discard_changes;
342
343     is( $debit_2->amount * 1, 150, 'Fine amount was updated in full' );
344     is( $debit_2->amountoutstanding * 1, 150, 'Fine amountoutstanding was update in full' );
345     isnt( $debit_2->date, undef, 'Date has been set' );
346     is( $debit_2->lastincrement * 1, 50, 'lastincrement is the to the right value' );
347
348     my $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
349     is( $offsets->count, 1, 'An offset is generated for the increment' );
350     my $THIS_offset = $offsets->next;
351     is( $THIS_offset->amount * 1, 50, 'Amount was calculated correctly (increment by 50)' );
352     is( $THIS_offset->type, 'Fine Update', 'Adjust type stored correctly' );
353
354     is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
355
356     # Update fine to partially paid
357     my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
358     $credit->apply( { debits => $debits, offset_type => 'Manual Credit' } );
359
360     $debit_2->discard_changes;
361     is( $debit_2->amount * 1, 150, 'Fine amount unaffected by partial payment' );
362     is( $debit_2->amountoutstanding * 1, 110, 'Fine amountoutstanding updated by partial payment' );
363
364     # Enable logs
365     t::lib::Mocks::mock_preference( 'FinesLog', 1 );
366
367     # Increment the partially paid fine
368     $debit_2->adjust( { amount => 160, type => 'fine_increment' } )->discard_changes;
369
370     is( $debit_2->amount * 1, 160, 'Fine amount was updated in full' );
371     is( $debit_2->amountoutstanding * 1, 120, 'Fine amountoutstanding was updated by difference' );
372     is( $debit_2->lastincrement * 1, 10, 'lastincrement is the to the right value' );
373
374     $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
375     is( $offsets->count, 3, 'An offset is generated for the increment' );
376     $THIS_offset = $offsets->last;
377     is( $THIS_offset->amount * 1, 10, 'Amount was calculated correctly (increment by 10)' );
378     is( $THIS_offset->type, 'Fine Update', 'Adjust type stored correctly' );
379
380     is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
381
382     # Decrement the partially paid fine, less than what was paid
383     $debit_2->adjust( { amount => 50, type => 'fine_increment' } )->discard_changes;
384
385     is( $debit_2->amount * 1, 50, 'Fine amount was updated in full' );
386     is( $debit_2->amountoutstanding * 1, 10, 'Fine amountoutstanding was updated by difference' );
387     is( $debit_2->lastincrement * 1, -110, 'lastincrement is the to the right value' );
388
389     $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
390     is( $offsets->count, 4, 'An offset is generated for the decrement' );
391     $THIS_offset = $offsets->last;
392     is( $THIS_offset->amount * 1, -110, 'Amount was calculated correctly (decrement by 110)' );
393     is( $THIS_offset->type, 'Fine Update', 'Adjust type stored correctly' );
394
395     # Decrement the partially paid fine, more than what was paid
396     $debit_2->adjust( { amount => 30, type => 'fine_increment' } )->discard_changes;
397     is( $debit_2->amount * 1, 30, 'Fine amount was updated in full' );
398     is( $debit_2->amountoutstanding * 1, 0, 'Fine amountoutstanding was zeroed (payment was 40)' );
399     is( $debit_2->lastincrement * 1, -20, 'lastincrement is the to the right value' );
400
401     $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
402     is( $offsets->count, 5, 'An offset is generated for the decrement' );
403     $THIS_offset = $offsets->last;
404     is( $THIS_offset->amount * 1, -20, 'Amount was calculated correctly (decrement by 20)' );
405     is( $THIS_offset->type, 'Fine Update', 'Adjust type stored correctly' );
406
407     my $overpayment_refund = $account->lines->last;
408     is( $overpayment_refund->amount * 1, -10, 'A new credit has been added' );
409     is( $overpayment_refund->description, 'Overpayment refund', 'Credit generated with the expected description' );
410
411     $schema->storage->txn_rollback;
412 };
413
414 1;