Bug 22511: Update tests
[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 => 7;
23 use Test::Exception;
24
25 use C4::Circulation qw/AddIssue AddReturn/;
26 use Koha::Account;
27 use Koha::Account::Lines;
28 use Koha::Account::Offsets;
29 use Koha::Items;
30
31 use t::lib::Mocks;
32 use t::lib::TestBuilder;
33
34 my $schema = Koha::Database->new->schema;
35 my $builder = t::lib::TestBuilder->new;
36
37 subtest 'item() tests' => sub {
38
39     plan tests => 3;
40
41     $schema->storage->txn_begin;
42
43     my $library = $builder->build( { source => 'Branch' } );
44     my $biblioitem = $builder->build( { source => 'Biblioitem' } );
45     my $patron = $builder->build( { source => 'Borrower' } );
46     my $item = Koha::Item->new(
47     {
48         biblionumber     => $biblioitem->{biblionumber},
49         biblioitemnumber => $biblioitem->{biblioitemnumber},
50         homebranch       => $library->{branchcode},
51         holdingbranch    => $library->{branchcode},
52         barcode          => 'some_barcode_12',
53         itype            => 'BK',
54     })->store;
55
56     my $line = Koha::Account::Line->new(
57     {
58         borrowernumber => $patron->{borrowernumber},
59         itemnumber     => $item->itemnumber,
60         accounttype    => "OVERDUE",
61         status         => "RETURNED",
62         amount         => 10,
63         interface      => 'commandline',
64     })->store;
65
66     my $account_line_item = $line->item;
67     is( ref( $account_line_item ), 'Koha::Item', 'Koha::Account::Line->item should return a Koha::Item' );
68     is( $line->itemnumber, $account_line_item->itemnumber, 'Koha::Account::Line->item should return the correct item' );
69
70     $line->itemnumber(undef)->store;
71     is( $line->item, undef, 'Koha::Account::Line->item should return undef if no item linked' );
72
73     $schema->storage->txn_rollback;
74 };
75
76 subtest 'total_outstanding() tests' => sub {
77
78     plan tests => 5;
79
80     $schema->storage->txn_begin;
81
82     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
83
84     my $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
85     is( $lines->total_outstanding, 0, 'total_outstanding returns 0 if no lines (undef case)' );
86
87     my $debit_1 = Koha::Account::Line->new(
88         {   borrowernumber    => $patron->id,
89             accounttype       => "OVERDUE",
90             status            => "RETURNED",
91             amount            => 10,
92             amountoutstanding => 10,
93             interface         => 'commandline',
94         }
95     )->store;
96
97     my $debit_2 = Koha::Account::Line->new(
98         {   borrowernumber    => $patron->id,
99             accounttype       => "OVERDUE",
100             status            => "RETURNED",
101             amount            => 10,
102             amountoutstanding => 10,
103             interface         => 'commandline',
104         }
105     )->store;
106
107     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
108     is( $lines->total_outstanding, 20, 'total_outstanding sums correctly' );
109
110     my $credit_1 = Koha::Account::Line->new(
111         {   borrowernumber    => $patron->id,
112             accounttype       => "OVERDUE",
113             status            => "RETURNED",
114             amount            => -10,
115             amountoutstanding => -10,
116             interface         => 'commandline',
117         }
118     )->store;
119
120     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
121     is( $lines->total_outstanding, 10, 'total_outstanding sums correctly' );
122
123     my $credit_2 = Koha::Account::Line->new(
124         {   borrowernumber    => $patron->id,
125             accounttype       => "OVERDUE",
126             status            => "RETURNED",
127             amount            => -10,
128             amountoutstanding => -10,
129             interface         => 'commandline',
130         }
131     )->store;
132
133     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
134     is( $lines->total_outstanding, 0, 'total_outstanding sums correctly' );
135
136     my $credit_3 = Koha::Account::Line->new(
137         {   borrowernumber    => $patron->id,
138             accounttype       => "OVERDUE",
139             status            => "RETURNED",
140             amount            => -100,
141             amountoutstanding => -100,
142             interface         => 'commandline',
143         }
144     )->store;
145
146     $lines = Koha::Account::Lines->search({ borrowernumber => $patron->id });
147     is( $lines->total_outstanding, -100, 'total_outstanding sums correctly' );
148
149     $schema->storage->txn_rollback;
150 };
151
152 subtest 'is_credit() and is_debit() tests' => sub {
153
154     plan tests => 4;
155
156     $schema->storage->txn_begin;
157
158     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
159     my $account = $patron->account;
160
161     my $credit = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' });
162
163     ok( $credit->is_credit, 'is_credit detects credits' );
164     ok( !$credit->is_debit, 'is_debit detects credits' );
165
166     my $debit = Koha::Account::Line->new(
167     {
168         borrowernumber => $patron->id,
169         accounttype    => "OVERDUE",
170         status         => "RETURNED",
171         amount         => 10,
172         interface      => 'commandline',
173     })->store;
174
175     ok( !$debit->is_credit, 'is_credit detects debits' );
176     ok( $debit->is_debit, 'is_debit detects debits');
177
178     $schema->storage->txn_rollback;
179 };
180
181 subtest 'apply() tests' => sub {
182
183     plan tests => 24;
184
185     $schema->storage->txn_begin;
186
187     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
188     my $account = $patron->account;
189
190     my $credit = $account->add_credit( { amount => 100, user_id => $patron->id, interface => 'commandline' } );
191
192     my $debit_1 = Koha::Account::Line->new(
193         {   borrowernumber    => $patron->id,
194             accounttype       => "OVERDUE",
195             status            => "RETURNED",
196             amount            => 10,
197             amountoutstanding => 10,
198             interface         => 'commandline',
199         }
200     )->store;
201
202     my $debit_2 = Koha::Account::Line->new(
203         {   borrowernumber    => $patron->id,
204             accounttype       => "OVERDUE",
205             status            => "RETURNED",
206             amount            => 100,
207             amountoutstanding => 100,
208             interface         => 'commandline',
209         }
210     )->store;
211
212     $credit->discard_changes;
213     $debit_1->discard_changes;
214
215     my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
216     my $remaining_credit = $credit->apply( { debits => $debits, offset_type => 'Manual Credit' } );
217     is( $remaining_credit * 1, 90, 'Remaining credit is correctly calculated' );
218     $credit->discard_changes;
219     is( $credit->amountoutstanding * -1, $remaining_credit, 'Remaining credit correctly stored' );
220
221     # re-read debit info
222     $debit_1->discard_changes;
223     is( $debit_1->amountoutstanding * 1, 0, 'Debit has been cancelled' );
224
225     my $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_1->id } );
226     is( $offsets->count, 1, 'Only one offset is generated' );
227     my $THE_offset = $offsets->next;
228     is( $THE_offset->amount * 1, -10, 'Amount was calculated correctly (less than the available credit)' );
229     is( $THE_offset->type, 'Manual Credit', 'Passed type stored correctly' );
230
231     $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
232     $remaining_credit = $credit->apply( { debits => $debits } );
233     is( $remaining_credit, 0, 'No remaining credit left' );
234     $credit->discard_changes;
235     is( $credit->amountoutstanding * 1, 0, 'No outstanding credit' );
236     $debit_2->discard_changes;
237     is( $debit_2->amountoutstanding * 1, 10, 'Outstanding amount decremented correctly' );
238
239     $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_2->id } );
240     is( $offsets->count, 1, 'Only one offset is generated' );
241     $THE_offset = $offsets->next;
242     is( $THE_offset->amount * 1, -90, 'Amount was calculated correctly (less than the available credit)' );
243     is( $THE_offset->type, 'Credit Applied', 'Defaults to \'Credit Applied\' offset type' );
244
245     $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
246     throws_ok
247         { $credit->apply({ debits => $debits }); }
248         'Koha::Exceptions::Account::NoAvailableCredit',
249         '->apply() can only be used with outstanding credits';
250
251     $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
252     throws_ok
253         { $debit_1->apply({ debits => $debits }); }
254         'Koha::Exceptions::Account::IsNotCredit',
255         '->apply() can only be used with credits';
256
257     $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
258     my $credit_3 = $account->add_credit({ amount => 1, interface => 'commandline' });
259     throws_ok
260         { $credit_3->apply({ debits => $debits }); }
261         'Koha::Exceptions::Account::IsNotDebit',
262         '->apply() can only be applied to credits';
263
264     my $credit_2 = $account->add_credit({ amount => 20, interface => 'commandline' });
265     my $debit_3  = Koha::Account::Line->new(
266         {   borrowernumber    => $patron->id,
267             accounttype       => "OVERDUE",
268             status            => "RETURNED",
269             amount            => 100,
270             amountoutstanding => 100,
271             interface         => 'commandline',
272         }
273     )->store;
274
275     $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id, $credit->id ] } });
276     throws_ok {
277         $credit_2->apply( { debits => $debits, offset_type => 'Manual Credit' } ); }
278         'Koha::Exceptions::Account::IsNotDebit',
279         '->apply() rolls back if any of the passed lines is not a debit';
280
281     is( $debit_1->discard_changes->amountoutstanding * 1,   0, 'No changes to already cancelled debit' );
282     is( $debit_2->discard_changes->amountoutstanding * 1,  10, 'Debit cancelled' );
283     is( $debit_3->discard_changes->amountoutstanding * 1, 100, 'Outstanding amount correctly calculated' );
284     is( $credit_2->discard_changes->amountoutstanding * -1, 20, 'No changes made' );
285
286     $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id ] } });
287     $remaining_credit = $credit_2->apply( { debits => $debits, offset_type => 'Manual Credit' } );
288
289     is( $debit_1->discard_changes->amountoutstanding * 1,  0, 'No changes to already cancelled debit' );
290     is( $debit_2->discard_changes->amountoutstanding * 1,  0, 'Debit cancelled' );
291     is( $debit_3->discard_changes->amountoutstanding * 1, 90, 'Outstanding amount correctly calculated' );
292     is( $credit_2->discard_changes->amountoutstanding * 1, 0, 'No remaining credit' );
293
294     $schema->storage->txn_rollback;
295 };
296
297 subtest 'Keep account info when related patron, staff or item is deleted' => sub {
298
299     plan tests => 3;
300
301     $schema->storage->txn_begin;
302
303     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
304     my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
305     my $item = $builder->build_object({ class => 'Koha::Items' });
306     my $issue = $builder->build_object(
307         {
308             class => 'Koha::Checkouts',
309             value => { itemnumber => $item->itemnumber }
310         }
311     );
312     my $line = Koha::Account::Line->new(
313     {
314         borrowernumber => $patron->borrowernumber,
315         manager_id     => $staff->borrowernumber,
316         itemnumber     => $item->itemnumber,
317         accounttype    => "OVERDUE",
318         status         => "RETURNED",
319         amount         => 10,
320         interface      => 'commandline',
321     })->store;
322
323     $issue->delete;
324     $item->delete;
325     $line = $line->get_from_storage;
326     is( $line->itemnumber, undef, "The account line should not be deleted when the related item is delete");
327
328     $staff->delete;
329     $line = $line->get_from_storage;
330     is( $line->manager_id, undef, "The account line should not be deleted when the related staff is delete");
331
332     $patron->delete;
333     $line = $line->get_from_storage;
334     is( $line->borrowernumber, undef, "The account line should not be deleted when the related patron is delete");
335
336     $schema->storage->txn_rollback;
337 };
338
339 subtest 'adjust() tests' => sub {
340
341     plan tests => 29;
342
343     $schema->storage->txn_begin;
344
345     # count logs before any actions
346     my $action_logs = $schema->resultset('ActionLog')->search()->count;
347
348     # Disable logs
349     t::lib::Mocks::mock_preference( 'FinesLog', 0 );
350
351     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
352     my $account = $patron->account;
353
354     my $debit_1 = Koha::Account::Line->new(
355         {   borrowernumber    => $patron->id,
356             accounttype       => "OVERDUE",
357             status            => "RETURNED",
358             amount            => 10,
359             amountoutstanding => 10,
360             interface         => 'commandline',
361         }
362     )->store;
363
364     my $debit_2 = Koha::Account::Line->new(
365         {   borrowernumber    => $patron->id,
366             accounttype       => "OVERDUE",
367             status            => "UNRETURNED",
368             amount            => 100,
369             amountoutstanding => 100,
370             interface         => 'commandline'
371         }
372     )->store;
373
374     my $credit = $account->add_credit( { amount => 40, user_id => $patron->id, interface => 'commandline' } );
375
376     throws_ok { $debit_1->adjust( { amount => 50, type => 'bad', interface => 'commandline' } ) }
377     qr/Update type not recognised/, 'Exception thrown for unrecognised type';
378
379     throws_ok { $debit_1->adjust( { amount => 50, type => 'overdue_update', interface => 'commandline' } ) }
380     qr/Update type not allowed on this accounttype/,
381       'Exception thrown for type conflict';
382
383     # Increment an unpaid fine
384     $debit_2->adjust( { amount => 150, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
385
386     is( $debit_2->amount * 1, 150, 'Fine amount was updated in full' );
387     is( $debit_2->amountoutstanding * 1, 150, 'Fine amountoutstanding was update in full' );
388     isnt( $debit_2->date, undef, 'Date has been set' );
389
390     my $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
391     is( $offsets->count, 1, 'An offset is generated for the increment' );
392     my $THIS_offset = $offsets->next;
393     is( $THIS_offset->amount * 1, 50, 'Amount was calculated correctly (increment by 50)' );
394     is( $THIS_offset->type, 'OVERDUE_INCREASE', 'Adjust type stored correctly' );
395
396     is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
397
398     # Update fine to partially paid
399     my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
400     $credit->apply( { debits => $debits, offset_type => 'Manual Credit' } );
401
402     $debit_2->discard_changes;
403     is( $debit_2->amount * 1, 150, 'Fine amount unaffected by partial payment' );
404     is( $debit_2->amountoutstanding * 1, 110, 'Fine amountoutstanding updated by partial payment' );
405
406     # Enable logs
407     t::lib::Mocks::mock_preference( 'FinesLog', 1 );
408
409     # Increment the partially paid fine
410     $debit_2->adjust( { amount => 160, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
411
412     is( $debit_2->amount * 1, 160, 'Fine amount was updated in full' );
413     is( $debit_2->amountoutstanding * 1, 120, 'Fine amountoutstanding was updated by difference' );
414
415     $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
416     is( $offsets->count, 3, 'An offset is generated for the increment' );
417     $THIS_offset = $offsets->last;
418     is( $THIS_offset->amount * 1, 10, 'Amount was calculated correctly (increment by 10)' );
419     is( $THIS_offset->type, 'OVERDUE_INCREASE', 'Adjust type stored correctly' );
420
421     is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
422
423     # Decrement the partially paid fine, less than what was paid
424     $debit_2->adjust( { amount => 50, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
425
426     is( $debit_2->amount * 1, 50, 'Fine amount was updated in full' );
427     is( $debit_2->amountoutstanding * 1, 10, 'Fine amountoutstanding was updated by difference' );
428
429     $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
430     is( $offsets->count, 4, 'An offset is generated for the decrement' );
431     $THIS_offset = $offsets->last;
432     is( $THIS_offset->amount * 1, -110, 'Amount was calculated correctly (decrement by 110)' );
433     is( $THIS_offset->type, 'OVERDUE_DECREASE', 'Adjust type stored correctly' );
434
435     # Decrement the partially paid fine, more than what was paid
436     $debit_2->adjust( { amount => 30, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
437     is( $debit_2->amount * 1, 30, 'Fine amount was updated in full' );
438     is( $debit_2->amountoutstanding * 1, 0, 'Fine amountoutstanding was zeroed (payment was 40)' );
439
440     $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
441     is( $offsets->count, 5, 'An offset is generated for the decrement' );
442     $THIS_offset = $offsets->last;
443     is( $THIS_offset->amount * 1, -20, 'Amount was calculated correctly (decrement by 20)' );
444     is( $THIS_offset->type, 'OVERDUE_DECREASE', 'Adjust type stored correctly' );
445
446     my $overpayment_refund = $account->lines->last;
447     is( $overpayment_refund->amount * 1, -10, 'A new credit has been added' );
448     is( $overpayment_refund->description, 'Overpayment refund', 'Credit generated with the expected description' );
449
450     $schema->storage->txn_rollback;
451 };
452
453 subtest 'checkout() tests' => sub {
454     plan tests => 6;
455
456     $schema->storage->txn_begin;
457
458     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
459     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
460     my $item = $builder->build_sample_item;
461     my $account = $patron->account;
462
463     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
464     my $checkout = AddIssue( $patron->unblessed, $item->barcode );
465
466     my $line = $account->add_debit({
467         amount    => 10,
468         interface => 'commandline',
469         item_id   => $item->itemnumber,
470         issue_id  => $checkout->issue_id,
471         type      => 'overdue',
472     });
473
474     my $line_checkout = $line->checkout;
475     is( ref($line_checkout), 'Koha::Checkout', 'Result type is correct' );
476     is( $line_checkout->issue_id, $checkout->issue_id, 'Koha::Account::Line->checkout should return the correct checkout');
477
478     my ( $returned, undef, $old_checkout) = C4::Circulation::AddReturn( $item->barcode, $library->branchcode );
479     is( $returned, 1, 'The item should have been returned' );
480
481     $line = $line->get_from_storage;
482     my $old_line_checkout = $line->checkout;
483     is( ref($old_line_checkout), 'Koha::Old::Checkout', 'Result type is correct' );
484     is( $old_line_checkout->issue_id, $old_checkout->issue_id, 'Koha::Account::Line->checkout should return the correct old_checkout' );
485
486     $line->issue_id(undef)->store;
487     is( $line->checkout, undef, 'Koha::Account::Line->checkout should return undef if no checkout linked' );
488
489     $schema->storage->txn_rollback;
490 };
491
492 subtest "void() tests" => sub {
493
494     plan tests => 15;
495
496     # Create a borrower
497     my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
498     my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
499
500     my $borrower = Koha::Patron->new( {
501         cardnumber => 'dariahall',
502         surname => 'Hall',
503         firstname => 'Daria',
504     } );
505     $borrower->categorycode( $categorycode );
506     $borrower->branchcode( $branchcode );
507     $borrower->store;
508
509     my $account = Koha::Account->new({ patron_id => $borrower->id });
510
511     my $line1 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 10, amountoutstanding => 10, interface => 'commandline' })->store();
512     my $line2 = Koha::Account::Line->new({ borrowernumber => $borrower->borrowernumber, amount => 20, amountoutstanding => 20, interface => 'commandline' })->store();
513
514     is( $account->balance(), 30, "Account balance is 30" );
515     is( $line1->amountoutstanding, 10, 'First fee has amount outstanding of 10' );
516     is( $line2->amountoutstanding, 20, 'Second fee has amount outstanding of 20' );
517
518     my $id = $account->pay(
519         {
520             lines  => [$line1, $line2],
521             amount => 30,
522         }
523     );
524
525     my $account_payment = Koha::Account::Lines->find( $id );
526
527     is( $account->balance(), 0, "Account balance is 0" );
528
529     $line1->_result->discard_changes();
530     $line2->_result->discard_changes();
531     is( $line1->amountoutstanding+0, 0, 'First fee has amount outstanding of 0' );
532     is( $line2->amountoutstanding+0, 0, 'Second fee has amount outstanding of 0' );
533
534     my $ret = $account_payment->void();
535
536     is( ref($ret), 'Koha::Account::Line', 'Void returns the account line' );
537     is( $account->balance(), 30, "Account balance is again 30" );
538
539     $account_payment->_result->discard_changes();
540     $line1->_result->discard_changes();
541     $line2->_result->discard_changes();
542
543     is( $account_payment->accounttype, 'Pay', 'Voided payment accounttype is still Pay' );
544     is( $account_payment->status, 'VOID', 'Voided payment status is VOID' );
545     is( $account_payment->amount+0, 0, 'Voided payment amount is 0' );
546     is( $account_payment->amountoutstanding+0, 0, 'Voided payment amount outstanding is 0' );
547
548     is( $line1->amountoutstanding+0, 10, 'First fee again has amount outstanding of 10' );
549     is( $line2->amountoutstanding+0, 20, 'Second fee again has amount outstanding of 20' );
550
551     # Accountlines that are not credits should be un-voidable
552     my $line1_pre = $line1->unblessed();
553     $ret = $line1->void();
554     $line1->_result->discard_changes();
555     my $line1_post = $line1->unblessed();
556     is( $ret, undef, 'Attempted void on non-credit returns undef' );
557     is_deeply( $line1_pre, $line1_post, 'Non-credit account line cannot be voided' )
558 };
559
560 1;