Bug 17600: Standardize our EXPORT_OK
[srvgit] / t / db_dependent / Koha / Account / Line.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 => 14;
23 use Test::Exception;
24 use Test::MockModule;
25
26 use DateTime;
27
28 use C4::Circulation qw( AddRenewal CanBookBeRenewed LostItem AddIssue AddReturn );
29 use Koha::Account;
30 use Koha::Account::Lines;
31 use Koha::Account::Offsets;
32 use Koha::Items;
33 use Koha::DateUtils qw( dt_from_string );
34
35 use t::lib::Mocks;
36 use t::lib::TestBuilder;
37
38 my $schema = Koha::Database->new->schema;
39 my $builder = t::lib::TestBuilder->new;
40
41 subtest 'patron() tests' => sub {
42
43     plan tests => 3;
44
45     $schema->storage->txn_begin;
46
47     my $library = $builder->build( { source => 'Branch' } );
48     my $patron = $builder->build( { source => 'Borrower' } );
49
50     my $line = Koha::Account::Line->new(
51     {
52         borrowernumber => $patron->{borrowernumber},
53         debit_type_code    => "OVERDUE",
54         status         => "RETURNED",
55         amount         => 10,
56         interface      => 'commandline',
57     })->store;
58
59     my $account_line_patron = $line->patron;
60     is( ref( $account_line_patron ), 'Koha::Patron', 'Koha::Account::Line->patron should return a Koha::Patron' );
61     is( $line->borrowernumber, $account_line_patron->borrowernumber, 'Koha::Account::Line->patron should return the correct borrower' );
62
63     $line->borrowernumber(undef)->store;
64     is( $line->patron, undef, 'Koha::Account::Line->patron should return undef if no patron linked' );
65
66     $schema->storage->txn_rollback;
67 };
68
69 subtest 'item() tests' => sub {
70
71     plan tests => 3;
72
73     $schema->storage->txn_begin;
74
75     my $library = $builder->build( { source => 'Branch' } );
76     my $patron = $builder->build( { source => 'Borrower' } );
77     my $item = $builder->build_sample_item(
78         {
79             library => $library->{branchcode},
80             barcode => 'some_barcode_12',
81             itype   => 'BK',
82         }
83     );
84
85     my $line = Koha::Account::Line->new(
86     {
87         borrowernumber => $patron->{borrowernumber},
88         itemnumber     => $item->itemnumber,
89         debit_type_code    => "OVERDUE",
90         status         => "RETURNED",
91         amount         => 10,
92         interface      => 'commandline',
93     })->store;
94
95     my $account_line_item = $line->item;
96     is( ref( $account_line_item ), 'Koha::Item', 'Koha::Account::Line->item should return a Koha::Item' );
97     is( $line->itemnumber, $account_line_item->itemnumber, 'Koha::Account::Line->item should return the correct item' );
98
99     $line->itemnumber(undef)->store;
100     is( $line->item, undef, 'Koha::Account::Line->item should return undef if no item linked' );
101
102     $schema->storage->txn_rollback;
103 };
104
105 subtest 'library() tests' => sub {
106
107     plan tests => 4;
108
109     $schema->storage->txn_begin;
110
111     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
112     my $patron  = $builder->build( { source => 'Borrower' } );
113
114     my $line = Koha::Account::Line->new(
115         {
116             borrowernumber  => $patron->{borrowernumber},
117             branchcode      => $library->branchcode,
118             debit_type_code => "OVERDUE",
119             status          => "RETURNED",
120             amount          => 10,
121             interface       => 'commandline',
122         }
123     )->store;
124
125     my $account_line_library = $line->library;
126     is( ref($account_line_library),
127         'Koha::Library',
128         'Koha::Account::Line->library should return a Koha::Library' );
129     is(
130         $line->branchcode,
131         $account_line_library->branchcode,
132         'Koha::Account::Line->library should return the correct library'
133     );
134
135     # Test ON DELETE SET NULL
136     $library->delete;
137     my $found = Koha::Account::Lines->find( $line->accountlines_id );
138     ok( $found, "Koha::Account::Line not deleted when the linked library is deleted" );
139
140     is( $found->library, undef,
141 'Koha::Account::Line->library should return undef if linked library has been deleted'
142     );
143
144     $schema->storage->txn_rollback;
145 };
146
147 subtest 'is_credit() and is_debit() tests' => sub {
148
149     plan tests => 4;
150
151     $schema->storage->txn_begin;
152
153     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
154     my $account = $patron->account;
155
156     my $credit = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' });
157
158     ok( $credit->is_credit, 'is_credit detects credits' );
159     ok( !$credit->is_debit, 'is_debit detects credits' );
160
161     my $debit = Koha::Account::Line->new(
162     {
163         borrowernumber => $patron->id,
164         debit_type_code    => "OVERDUE",
165         status         => "RETURNED",
166         amount         => 10,
167         interface      => 'commandline',
168     })->store;
169
170     ok( !$debit->is_credit, 'is_credit detects debits' );
171     ok( $debit->is_debit, 'is_debit detects debits');
172
173     $schema->storage->txn_rollback;
174 };
175
176 subtest 'apply() tests' => sub {
177
178     plan tests => 31;
179
180     $schema->storage->txn_begin;
181
182     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
183     my $account = $patron->account;
184
185     my $credit = $account->add_credit( { amount => 100, user_id => $patron->id, interface => 'commandline' } );
186
187     my $debit_1 = Koha::Account::Line->new(
188         {   borrowernumber    => $patron->id,
189             debit_type_code       => "OVERDUE",
190             status            => "RETURNED",
191             amount            => 10,
192             amountoutstanding => 10,
193             interface         => 'commandline',
194         }
195     )->store;
196
197     my $debit_2 = Koha::Account::Line->new(
198         {   borrowernumber    => $patron->id,
199             debit_type_code       => "OVERDUE",
200             status            => "RETURNED",
201             amount            => 100,
202             amountoutstanding => 100,
203             interface         => 'commandline',
204         }
205     )->store;
206
207     $credit->discard_changes;
208     $debit_1->discard_changes;
209
210     my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
211     $credit = $credit->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
212     is( ref($credit), 'Koha::Account::Line', '->apply returns the updated Koha::Account::Line credit object');
213     is( $credit->amountoutstanding * -1, 90, 'Remaining credit is correctly calculated' );
214
215     # re-read debit info
216     $debit_1->discard_changes;
217     is( $debit_1->amountoutstanding * 1, 0, 'Debit has been cancelled' );
218
219     my $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_1->id } );
220     is( $offsets->count, 1, 'Only one offset is generated' );
221     my $THE_offset = $offsets->next;
222     is( $THE_offset->amount * 1, -10, 'Amount was calculated correctly (less than the available credit)' );
223     is( $THE_offset->type, 'Manual Credit', 'Passed type stored correctly' );
224
225     $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
226     $credit = $credit->apply( { debits => [ $debits->as_list ] } );
227     is( $credit->amountoutstanding * 1, 0, 'No remaining credit' );
228     $debit_2->discard_changes;
229     is( $debit_2->amountoutstanding * 1, 10, 'Outstanding amount decremented correctly' );
230
231     $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_2->id } );
232     is( $offsets->count, 1, 'Only one offset is generated' );
233     $THE_offset = $offsets->next;
234     is( $THE_offset->amount * 1, -90, 'Amount was calculated correctly (less than the available credit)' );
235     is( $THE_offset->type, 'Credit Applied', 'Defaults to \'Credit Applied\' offset type' );
236
237     $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
238     throws_ok
239         { $credit->apply({ debits => [ $debits->as_list ] }); }
240         'Koha::Exceptions::Account::NoAvailableCredit',
241         '->apply() can only be used with outstanding credits';
242
243     $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
244     throws_ok
245         { $debit_1->apply({ debits => [ $debits->as_list ] }); }
246         'Koha::Exceptions::Account::IsNotCredit',
247         '->apply() can only be used with credits';
248
249     $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
250     my $credit_3 = $account->add_credit({ amount => 1, interface => 'commandline' });
251     throws_ok
252         { $credit_3->apply({ debits => [ $debits->as_list ] }); }
253         'Koha::Exceptions::Account::IsNotDebit',
254         '->apply() can only be applied to credits';
255
256     my $credit_2 = $account->add_credit({ amount => 20, interface => 'commandline' });
257     my $debit_3  = Koha::Account::Line->new(
258         {   borrowernumber    => $patron->id,
259             debit_type_code       => "OVERDUE",
260             status            => "RETURNED",
261             amount            => 100,
262             amountoutstanding => 100,
263             interface         => 'commandline',
264         }
265     )->store;
266
267     $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id, $credit->id ] } });
268     throws_ok {
269         $credit_2->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } ); }
270         'Koha::Exceptions::Account::IsNotDebit',
271         '->apply() rolls back if any of the passed lines is not a debit';
272
273     is( $debit_1->discard_changes->amountoutstanding * 1,   0, 'No changes to already cancelled debit' );
274     is( $debit_2->discard_changes->amountoutstanding * 1,  10, 'Debit cancelled' );
275     is( $debit_3->discard_changes->amountoutstanding * 1, 100, 'Outstanding amount correctly calculated' );
276     is( $credit_2->discard_changes->amountoutstanding * -1, 20, 'No changes made' );
277
278     $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id ] } });
279     $credit_2 = $credit_2->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
280
281     is( $debit_1->discard_changes->amountoutstanding * 1,  0, 'No changes to already cancelled debit' );
282     is( $debit_2->discard_changes->amountoutstanding * 1,  0, 'Debit cancelled' );
283     is( $debit_3->discard_changes->amountoutstanding * 1, 90, 'Outstanding amount correctly calculated' );
284     is( $credit_2->amountoutstanding * 1, 0, 'No remaining credit' );
285
286     my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
287     my $biblio = $builder->build_sample_biblio();
288     my $item =
289         $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
290     my $now = dt_from_string();
291     my $seven_weeks = DateTime::Duration->new(weeks => 7);
292     my $five_weeks = DateTime::Duration->new(weeks => 5);
293     my $seven_weeks_ago = $now - $seven_weeks;
294     my $five_weeks_ago = $now - $five_weeks;
295
296     my $checkout = Koha::Checkout->new(
297         {
298             borrowernumber => $patron->id,
299             itemnumber     => $item->id,
300             date_due       => $five_weeks_ago,
301             branchcode     => $library->id,
302             issuedate      => $seven_weeks_ago
303         }
304     )->store();
305
306     my $accountline = Koha::Account::Line->new(
307         {
308             issue_id       => $checkout->id,
309             borrowernumber => $patron->id,
310             itemnumber     => $item->id,
311             branchcode     => $library->id,
312             date           => \'NOW()',
313             debit_type_code => 'OVERDUE',
314             status         => 'UNRETURNED',
315             interface      => 'cli',
316             amount => '1',
317             amountoutstanding => '1',
318         }
319     )->store();
320
321     # Enable renewing upon fine payment
322     t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 1 );
323     my $called = 0;
324     my $module = Test::MockModule->new('C4::Circulation');
325     $module->mock('AddRenewal', sub { $called = 1; });
326     $module->mock('CanBookBeRenewed', sub { return 1; });
327     my $credit_forgive = $account->add_credit(
328         {
329             amount    => 1,
330             user_id   => $patron->id,
331             interface => 'cli',
332             type      => 'FORGIVEN'
333         }
334     );
335     my $debits_renew = Koha::Account::Lines->search({ accountlines_id => $accountline->id })->as_list;
336     $credit_forgive = $credit_forgive->apply( { debits => $debits_renew, offset_type => 'Forgiven' } );
337     is( $called, 0, 'C4::Circulation::AddRenew NOT called when RenewAccruingItemWhenPaid enabled but credit type is "FORGIVEN"' );
338
339     $accountline = Koha::Account::Line->new(
340         {
341             issue_id       => $checkout->id,
342             borrowernumber => $patron->id,
343             itemnumber     => $item->id,
344             branchcode     => $library->id,
345             date           => \'NOW()',
346             debit_type_code => 'OVERDUE',
347             status         => 'UNRETURNED',
348             interface      => 'cli',
349             amount => '1',
350             amountoutstanding => '1',
351         }
352     )->store();
353     my $credit_renew = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' });
354     $debits_renew = Koha::Account::Lines->search({ accountlines_id => $accountline->id })->as_list;
355     $credit_renew = $credit_renew->apply( { debits => $debits_renew, offset_type => 'Manual Credit' } );
356     is( $called, 1, 'RenewAccruingItemWhenPaid causes C4::Circulation::AddRenew to be called when appropriate' );
357
358     my @messages = @{$credit_renew->messages};
359     is( $messages[0]->type, 'info', 'Info message added for renewal' );
360     is( $messages[0]->message, 'renewal', 'Message is "renewal"' );
361     is( $messages[0]->payload->{itemnumber}, $item->id, 'itemnumber found in payload' );
362     is( $messages[0]->payload->{due_date}, 1, 'due_date key in payload' );
363     is( $messages[0]->payload->{success}, 1, "'success' key in payload" );
364
365     t::lib::Mocks::mock_preference( 'MarkLostItemsAsReturned', 'onpayment');
366     my $loser  = $builder->build_object( { class => 'Koha::Patrons' } );
367     my $loser_account = $loser->account;
368
369     my $lost_item = $builder->build_sample_item();
370     my $lost_checkout = Koha::Checkout->new(
371         {
372             borrowernumber => $loser->id,
373             itemnumber     => $lost_item->id,
374             date_due       => $five_weeks_ago,
375             branchcode     => $library->id,
376             issuedate      => $seven_weeks_ago
377         }
378     )->store();
379
380     $lost_item->itemlost(1)->store;
381     my $processing_fee = Koha::Account::Line->new(
382         {
383             issue_id       => $lost_checkout->id,
384             borrowernumber => $loser->id,
385             itemnumber     => $lost_item->id,
386             branchcode     => $library->id,
387             date           => \'NOW()',
388             debit_type_code => 'PROCESSING',
389             status         => undef,
390             interface      => 'intranet',
391             amount => '15',
392             amountoutstanding => '15',
393         }
394     )->store();
395     my $lost_fee = Koha::Account::Line->new(
396         {
397             issue_id       => $lost_checkout->id,
398             borrowernumber => $loser->id,
399             itemnumber     => $lost_item->id,
400             branchcode     => $library->id,
401             date           => \'NOW()',
402             debit_type_code => 'LOST',
403             status         => undef,
404             interface      => 'intranet',
405             amount => '12.63',
406             amountoutstanding => '12.63',
407         }
408     )->store();
409     my $pay_lost = $loser_account->add_credit({ amount => 27.630000, user_id => $loser->id, interface => 'intranet' });
410     my $pay_lines = [ $processing_fee, $lost_fee ];
411     $pay_lost->apply( { debits => $pay_lines, offset_type => 'Credit applied' } );
412
413     is( $loser->checkouts->next, undef, "Item has been returned");
414
415
416
417     $schema->storage->txn_rollback;
418 };
419
420 subtest 'Keep account info when related patron, staff, item or cash_register is deleted' => sub {
421
422     plan tests => 4;
423
424     $schema->storage->txn_begin;
425
426     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
427     my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
428     my $item = $builder->build_sample_item;
429     my $issue = $builder->build_object(
430         {
431             class => 'Koha::Checkouts',
432             value => { itemnumber => $item->itemnumber }
433         }
434     );
435     my $register = $builder->build_object({ class => 'Koha::Cash::Registers' });
436
437     my $line = Koha::Account::Line->new(
438     {
439         borrowernumber => $patron->borrowernumber,
440         manager_id     => $staff->borrowernumber,
441         itemnumber     => $item->itemnumber,
442         debit_type_code    => "OVERDUE",
443         status         => "RETURNED",
444         amount         => 10,
445         interface      => 'commandline',
446         register_id    => $register->id
447     })->store;
448
449     $issue->delete;
450     $item->delete;
451     $line = $line->get_from_storage;
452     is( $line->itemnumber, undef, "The account line should not be deleted when the related item is delete");
453
454     $staff->delete;
455     $line = $line->get_from_storage;
456     is( $line->manager_id, undef, "The account line should not be deleted when the related staff is delete");
457
458     $patron->delete;
459     $line = $line->get_from_storage;
460     is( $line->borrowernumber, undef, "The account line should not be deleted when the related patron is delete");
461
462     $register->delete;
463     $line = $line->get_from_storage;
464     is( $line->register_id, undef, "The account line should not be deleted when the related cash register is delete");
465
466     $schema->storage->txn_rollback;
467 };
468
469 subtest 'Renewal related tests' => sub {
470
471     plan tests => 8;
472
473     $schema->storage->txn_begin;
474
475     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
476     my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
477     my $item = $builder->build_object({ class => 'Koha::Items' });
478     my $issue = $builder->build_object(
479         {
480             class => 'Koha::Checkouts',
481             value => {
482                 itemnumber      => $item->itemnumber,
483                 onsite_checkout => 0,
484                 renewals        => 99,
485                 auto_renew      => 0
486             }
487         }
488     );
489     my $line = Koha::Account::Line->new(
490     {
491         borrowernumber    => $patron->borrowernumber,
492         manager_id        => $staff->borrowernumber,
493         itemnumber        => $item->itemnumber,
494         debit_type_code   => "OVERDUE",
495         status            => "UNRETURNED",
496         amountoutstanding => 0,
497         interface         => 'commandline',
498     })->store;
499
500     is( $line->is_renewable, 1, "Item is returned as renewable when it meets the conditions" );
501     $line->amountoutstanding(5);
502     is( $line->is_renewable, 0, "Item is returned as unrenewable when it has outstanding fine" );
503     $line->amountoutstanding(0);
504     $line->debit_type_code("VOID");
505     is( $line->is_renewable, 0, "Item is returned as unrenewable when it has the wrong account type" );
506     $line->debit_type_code("OVERDUE");
507     $line->status("RETURNED");
508     is( $line->is_renewable, 0, "Item is returned as unrenewable when it has the wrong account status" );
509
510
511     t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 0 );
512     is ($line->renew_item({ interface => 'intranet' }), undef, 'Attempt to renew fails when syspref is not set');
513     t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 1 );
514     t::lib::Mocks::mock_preference( 'RenewAccruingItemInOpac', 0 );
515     is ($line->renew_item({ interface => 'opac' }), undef, 'Attempt to renew fails when syspref is not set - OPAC');
516     t::lib::Mocks::mock_preference( 'RenewAccruingItemInOpac', 1 );
517     is_deeply(
518         $line->renew_item({ interface => 'intranet' }),
519         {
520             itemnumber => $item->itemnumber,
521             error      => 'too_many',
522             success    => 0
523         },
524         'Attempt to renew fails when CanBookBeRenewed returns false'
525     );
526     $issue->delete;
527     $issue = $builder->build_object(
528         {
529             class => 'Koha::Checkouts',
530             value => {
531                 itemnumber      => $item->itemnumber,
532                 onsite_checkout => 0,
533                 renewals        => 0,
534                 auto_renew      => 0
535             }
536         }
537     );
538     my $called = 0;
539     my $module = Test::MockModule->new('C4::Circulation');
540     $module->mock('AddRenewal', sub { $called = 1; });
541     $module->mock('CanBookBeRenewed', sub { return 1; });
542     $line->renew_item;
543     is( $called, 1, 'Attempt to renew succeeds when conditions are met' );
544
545     $schema->storage->txn_rollback;
546 };
547
548 subtest 'adjust() tests' => sub {
549
550     plan tests => 29;
551
552     $schema->storage->txn_begin;
553
554     # count logs before any actions
555     my $action_logs = $schema->resultset('ActionLog')->search()->count;
556
557     # Disable logs
558     t::lib::Mocks::mock_preference( 'FinesLog', 0 );
559
560     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
561     my $account = $patron->account;
562
563     my $debit_1 = Koha::Account::Line->new(
564         {   borrowernumber    => $patron->id,
565             debit_type_code       => "OVERDUE",
566             status            => "RETURNED",
567             amount            => 10,
568             amountoutstanding => 10,
569             interface         => 'commandline',
570         }
571     )->store;
572
573     my $debit_2 = Koha::Account::Line->new(
574         {   borrowernumber    => $patron->id,
575             debit_type_code       => "OVERDUE",
576             status            => "UNRETURNED",
577             amount            => 100,
578             amountoutstanding => 100,
579             interface         => 'commandline'
580         }
581     )->store;
582
583     my $credit = $account->add_credit( { amount => 40, user_id => $patron->id, interface => 'commandline' } );
584
585     throws_ok { $debit_1->adjust( { amount => 50, type => 'bad', interface => 'commandline' } ) }
586     qr/Update type not recognised/, 'Exception thrown for unrecognised type';
587
588     throws_ok { $debit_1->adjust( { amount => 50, type => 'overdue_update', interface => 'commandline' } ) }
589     qr/Update type not allowed on this debit_type/,
590       'Exception thrown for type conflict';
591
592     # Increment an unpaid fine
593     $debit_2->adjust( { amount => 150, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
594
595     is( $debit_2->amount * 1, 150, 'Fine amount was updated in full' );
596     is( $debit_2->amountoutstanding * 1, 150, 'Fine amountoutstanding was update in full' );
597     isnt( $debit_2->date, undef, 'Date has been set' );
598
599     my $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
600     is( $offsets->count, 1, 'An offset is generated for the increment' );
601     my $THIS_offset = $offsets->next;
602     is( $THIS_offset->amount * 1, 50, 'Amount was calculated correctly (increment by 50)' );
603     is( $THIS_offset->type, 'OVERDUE_INCREASE', 'Adjust type stored correctly' );
604
605     is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
606
607     # Update fine to partially paid
608     my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
609     $credit->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
610
611     $debit_2->discard_changes;
612     is( $debit_2->amount * 1, 150, 'Fine amount unaffected by partial payment' );
613     is( $debit_2->amountoutstanding * 1, 110, 'Fine amountoutstanding updated by partial payment' );
614
615     # Enable logs
616     t::lib::Mocks::mock_preference( 'FinesLog', 1 );
617
618     # Increment the partially paid fine
619     $debit_2->adjust( { amount => 160, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
620
621     is( $debit_2->amount * 1, 160, 'Fine amount was updated in full' );
622     is( $debit_2->amountoutstanding * 1, 120, 'Fine amountoutstanding was updated by difference' );
623
624     $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
625     is( $offsets->count, 3, 'An offset is generated for the increment' );
626     $THIS_offset = $offsets->last;
627     is( $THIS_offset->amount * 1, 10, 'Amount was calculated correctly (increment by 10)' );
628     is( $THIS_offset->type, 'OVERDUE_INCREASE', 'Adjust type stored correctly' );
629
630     is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
631
632     # Decrement the partially paid fine, less than what was paid
633     $debit_2->adjust( { amount => 50, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
634
635     is( $debit_2->amount * 1, 50, 'Fine amount was updated in full' );
636     is( $debit_2->amountoutstanding * 1, 10, 'Fine amountoutstanding was updated by difference' );
637
638     $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
639     is( $offsets->count, 4, 'An offset is generated for the decrement' );
640     $THIS_offset = $offsets->last;
641     is( $THIS_offset->amount * 1, -110, 'Amount was calculated correctly (decrement by 110)' );
642     is( $THIS_offset->type, 'OVERDUE_DECREASE', 'Adjust type stored correctly' );
643
644     # Decrement the partially paid fine, more than what was paid
645     $debit_2->adjust( { amount => 30, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
646     is( $debit_2->amount * 1, 30, 'Fine amount was updated in full' );
647     is( $debit_2->amountoutstanding * 1, 0, 'Fine amountoutstanding was zeroed (payment was 40)' );
648
649     $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
650     is( $offsets->count, 5, 'An offset is generated for the decrement' );
651     $THIS_offset = $offsets->last;
652     is( $THIS_offset->amount * 1, -20, 'Amount was calculated correctly (decrement by 20)' );
653     is( $THIS_offset->type, 'OVERDUE_DECREASE', 'Adjust type stored correctly' );
654
655     my $overpayment_refund = $account->lines->last;
656     is( $overpayment_refund->amount * 1, -10, 'A new credit has been added' );
657     is( $overpayment_refund->credit_type_code, 'OVERPAYMENT', 'Credit generated with the expected credit_type_code' );
658
659     $schema->storage->txn_rollback;
660 };
661
662 subtest 'checkout() tests' => sub {
663     plan tests => 6;
664
665     $schema->storage->txn_begin;
666
667     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
668     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
669     my $item = $builder->build_sample_item;
670     my $account = $patron->account;
671
672     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
673     my $checkout = AddIssue( $patron->unblessed, $item->barcode );
674
675     my $line = $account->add_debit({
676         amount    => 10,
677         interface => 'commandline',
678         item_id   => $item->itemnumber,
679         issue_id  => $checkout->issue_id,
680         type      => 'OVERDUE',
681         status    => 'UNRETURNED'
682     });
683
684     my $line_checkout = $line->checkout;
685     is( ref($line_checkout), 'Koha::Checkout', 'Result type is correct' );
686     is( $line_checkout->issue_id, $checkout->issue_id, 'Koha::Account::Line->checkout should return the correct checkout');
687
688     # Prevent re-calculation of fines at check-in for the test; Since bug 8338 the recalculation would result in a '0'
689     # fine which would subsequently be removed by _FixOverduesOnReturn
690     t::lib::Mocks::mock_preference( 'finesMode', 'off' );
691
692     my ( $returned, undef, $old_checkout) = C4::Circulation::AddReturn( $item->barcode, $library->branchcode );
693     is( $returned, 1, 'The item should have been returned' );
694
695     $line = $line->get_from_storage;
696     my $old_line_checkout = $line->checkout;
697     is( ref($old_line_checkout), 'Koha::Old::Checkout', 'Result type is correct' );
698     is( $old_line_checkout->issue_id, $old_checkout->issue_id, 'Koha::Account::Line->checkout should return the correct old_checkout' );
699
700     $line->issue_id(undef)->store;
701     is( $line->checkout, undef, 'Koha::Account::Line->checkout should return undef if no checkout linked' );
702
703     $schema->storage->txn_rollback;
704 };
705
706 subtest 'credits() and debits() tests' => sub {
707     plan tests => 12;
708
709     $schema->storage->txn_begin;
710
711     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
712     my $account = $patron->account;
713
714     my $debit1 = $account->add_debit({
715         amount    => 8,
716         interface => 'commandline',
717         type      => 'ACCOUNT',
718     });
719     my $debit2 = $account->add_debit({
720         amount    => 12,
721         interface => 'commandline',
722         type      => 'ACCOUNT',
723     });
724     my $credit1 = $account->add_credit({
725         amount    => 5,
726         interface => 'commandline',
727         type      => 'CREDIT',
728     });
729     my $credit2 = $account->add_credit({
730         amount    => 10,
731         interface => 'commandline',
732         type      => 'CREDIT',
733     });
734
735     $credit1->apply({ debits => [ $debit1 ] });
736     $credit2->apply({ debits => [ $debit1, $debit2 ] });
737
738     my $credits = $debit1->credits;
739     is($credits->count, 2, '2 Credits applied to debit 1');
740     my $credit = $credits->next;
741     is($credit->amount + 0, -5, 'Correct first credit');
742     $credit = $credits->next;
743     is($credit->amount + 0, -10, 'Correct second credit');
744
745     $credits = $debit2->credits;
746     is($credits->count, 1, '1 Credits applied to debit 2');
747     $credit = $credits->next;
748     is($credit->amount + 0, -10, 'Correct first credit');
749
750     my $debits = $credit1->debits;
751     is($debits->count, 1, 'Credit 1 applied to 1 debit');
752     my $debit = $debits->next;
753     is($debit->amount + 0, 8, 'Correct first debit');
754
755     $debits = $credit2->debits;
756     is($debits->count, 2, 'Credit 2 applied to 2 debits');
757     $debit = $debits->next;
758     is($debit->amount + 0, 8, 'Correct first debit');
759     $debit = $debits->next;
760     is($debit->amount + 0, 12, 'Correct second debit');
761
762     throws_ok
763         { $debit1->debits; }
764         'Koha::Exceptions::Account::IsNotCredit',
765         'Exception is thrown when requesting debits linked to debit';
766
767     throws_ok
768         { $credit1->credits; }
769         'Koha::Exceptions::Account::IsNotDebit',
770         'Exception is thrown when requesting credits linked to credit';
771
772
773     $schema->storage->txn_rollback;
774 };
775
776 subtest "void() tests" => sub {
777
778     plan tests => 23;
779
780     $schema->storage->txn_begin;
781
782     # Create a borrower
783     my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
784     my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
785
786     my $borrower = Koha::Patron->new( {
787         cardnumber => 'dariahall',
788         surname => 'Hall',
789         firstname => 'Daria',
790     } );
791     $borrower->categorycode( $categorycode );
792     $borrower->branchcode( $branchcode );
793     $borrower->store;
794
795     my $account = Koha::Account->new({ patron_id => $borrower->id });
796
797     my $line1 = Koha::Account::Line->new(
798         {
799             borrowernumber    => $borrower->borrowernumber,
800             amount            => 10,
801             amountoutstanding => 10,
802             interface         => 'commandline',
803             debit_type_code   => 'OVERDUE'
804         }
805     )->store();
806     my $line2 = Koha::Account::Line->new(
807         {
808             borrowernumber    => $borrower->borrowernumber,
809             amount            => 20,
810             amountoutstanding => 20,
811             interface         => 'commandline',
812             debit_type_code   => 'OVERDUE'
813         }
814     )->store();
815
816     is( $account->balance(), 30, "Account balance is 30" );
817     is( $line1->amountoutstanding, 10, 'First fee has amount outstanding of 10' );
818     is( $line2->amountoutstanding, 20, 'Second fee has amount outstanding of 20' );
819
820     my $id = $account->pay(
821         {
822             lines  => [$line1, $line2],
823             amount => 30,
824         }
825     )->{payment_id};
826
827     my $account_payment = Koha::Account::Lines->find( $id );
828
829     is( $account->balance(), 0, "Account balance is 0" );
830
831     $line1->_result->discard_changes();
832     $line2->_result->discard_changes();
833     is( $line1->amountoutstanding+0, 0, 'First fee has amount outstanding of 0' );
834     is( $line2->amountoutstanding+0, 0, 'Second fee has amount outstanding of 0' );
835
836     throws_ok {
837         $line1->void( { interface => 'test' } );
838     }
839     'Koha::Exceptions::Account::IsNotCredit',
840       '->void() can only be used with credits';
841
842     throws_ok {
843         $account_payment->void();
844     }
845     'Koha::Exceptions::MissingParameter',
846       "->void() requires the `interface` parameter is passed";
847
848     throws_ok {
849         $account_payment->void( { interface => 'intranet' } );
850     }
851     'Koha::Exceptions::MissingParameter',
852       "->void() requires the `staff_id` parameter is passed when `interface` equals 'intranet'";
853     throws_ok {
854         $account_payment->void( { interface => 'intranet', staff_id => $borrower->borrowernumber } );
855     }
856     'Koha::Exceptions::MissingParameter',
857       "->void() requires the `branch` parameter is passed when `interface` equals 'intranet'";
858
859     my $void = $account_payment->void({ interface => 'test' });
860
861     is( ref($void), 'Koha::Account::Line', 'Void returns the account line' );
862     is( $void->debit_type_code, 'VOID', 'Void returns the VOID account line' );
863     is( $void->manager_id, undef, 'Void proceeds without manager_id OK if interface is not "intranet"' );
864     is( $void->branchcode, undef, 'Void proceeds without branchcode OK if interface is not "intranet"' );
865     is( $account->balance(), 30, "Account balance is again 30" );
866
867     $account_payment->_result->discard_changes();
868     $line1->_result->discard_changes();
869     $line2->_result->discard_changes();
870
871     is( $account_payment->credit_type_code, 'PAYMENT', 'Voided payment credit_type_code is still PAYMENT' );
872     is( $account_payment->status, 'VOID', 'Voided payment status is VOID' );
873     is( $account_payment->amount+0, -30, 'Voided payment amount is still -30' );
874     is( $account_payment->amountoutstanding+0, 0, 'Voided payment amount outstanding is 0' );
875
876     is( $line1->amountoutstanding+0, 10, 'First fee again has amount outstanding of 10' );
877     is( $line2->amountoutstanding+0, 20, 'Second fee again has amount outstanding of 20' );
878
879     my $credit2 = $account->add_credit( { interface => 'test', amount => 10 } );
880     $void = $credit2->void(
881         {
882             interface => 'intranet',
883             staff_id  => $borrower->borrowernumber,
884             branch    => $branchcode
885         }
886     );
887     is( $void->manager_id, $borrower->borrowernumber, "->void stores the manager_id when it's passed");
888     is( $void->branchcode, $branchcode, "->void stores the branchcode when it's passed");
889
890     $schema->storage->txn_rollback;
891 };
892
893 subtest "payout() tests" => sub {
894
895     plan tests => 18;
896
897     $schema->storage->txn_begin;
898
899     # Create a borrower
900     my $categorycode =
901       $builder->build( { source => 'Category' } )->{categorycode};
902     my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
903
904     my $borrower = Koha::Patron->new(
905         {
906             cardnumber => 'dariahall',
907             surname    => 'Hall',
908             firstname  => 'Daria',
909         }
910     );
911     $borrower->categorycode($categorycode);
912     $borrower->branchcode($branchcode);
913     $borrower->store;
914
915     my $staff = Koha::Patron->new(
916         {
917             cardnumber => 'bobby',
918             surname    => 'Bloggs',
919             firstname  => 'Bobby',
920         }
921     );
922     $staff->categorycode($categorycode);
923     $staff->branchcode($branchcode);
924     $staff->store;
925
926     my $account = Koha::Account->new( { patron_id => $borrower->id } );
927
928     my $debit1 = Koha::Account::Line->new(
929         {
930             borrowernumber    => $borrower->borrowernumber,
931             amount            => 10,
932             amountoutstanding => 10,
933             interface         => 'commandline',
934             debit_type_code   => 'OVERDUE'
935         }
936     )->store();
937     my $credit1 = Koha::Account::Line->new(
938         {
939             borrowernumber    => $borrower->borrowernumber,
940             amount            => -20,
941             amountoutstanding => -20,
942             interface         => 'commandline',
943             credit_type_code  => 'CREDIT'
944         }
945     )->store();
946
947     is( $account->balance(), -10, "Account balance is -10" );
948     is( $debit1->amountoutstanding + 0,
949         10, 'Overdue fee has an amount outstanding of 10' );
950     is( $credit1->amountoutstanding + 0,
951         -20, 'Credit has an amount outstanding of -20' );
952
953     my $pay_params = {
954         interface   => 'intranet',
955         staff_id    => $staff->borrowernumber,
956         branch      => $branchcode,
957         payout_type => 'CASH',
958         amount      => 20
959     };
960
961     throws_ok { $debit1->payout($pay_params); }
962     'Koha::Exceptions::Account::IsNotCredit',
963       '->payout() can only be used with credits';
964
965     my @required =
966       ( 'interface', 'staff_id', 'branch', 'payout_type', 'amount' );
967     for my $required (@required) {
968         my $params = {%$pay_params};
969         delete( $params->{$required} );
970         throws_ok {
971             $credit1->payout($params);
972         }
973         'Koha::Exceptions::MissingParameter',
974           "->payout() requires the `$required` parameter is passed";
975     }
976
977     throws_ok {
978         $credit1->payout(
979             {
980                 interface   => 'intranet',
981                 staff_id    => $staff->borrowernumber,
982                 branch      => $branchcode,
983                 payout_type => 'CASH',
984                 amount      => 25
985             }
986         );
987     }
988     'Koha::Exceptions::ParameterTooHigh',
989       '->payout() cannot pay out more than the amountoutstanding';
990
991     t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
992     throws_ok {
993         $credit1->payout(
994             {
995                 interface   => 'intranet',
996                 staff_id    => $staff->borrowernumber,
997                 branch      => $branchcode,
998                 payout_type => 'CASH',
999                 amount      => 10
1000             }
1001         );
1002     }
1003     'Koha::Exceptions::Account::RegisterRequired',
1004       '->payout() requires a cash_register if payout_type is `CASH`';
1005
1006     t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
1007     my $payout = $credit1->payout(
1008         {
1009             interface   => 'intranet',
1010             staff_id    => $staff->borrowernumber,
1011             branch      => $branchcode,
1012             payout_type => 'CASH',
1013             amount      => 10
1014         }
1015     );
1016
1017     is( ref($payout), 'Koha::Account::Line',
1018         '->payout() returns a Koha::Account::Line' );
1019     is( $payout->amount() + 0,            10, "Payout amount is 10" );
1020     is( $payout->amountoutstanding() + 0, 0,  "Payout amountoutstanding is 0" );
1021     is( $account->balance() + 0,          0,  "Account balance is 0" );
1022     is( $debit1->amountoutstanding + 0,
1023         10, 'Overdue fee still has an amount outstanding of 10' );
1024     is( $credit1->amountoutstanding + 0,
1025         -10, 'Credit has an new amount outstanding of -10' );
1026     is( $credit1->status(), 'PAID', "Credit has a new status of PAID" );
1027
1028     $schema->storage->txn_rollback;
1029 };
1030
1031 subtest "reduce() tests" => sub {
1032
1033     plan tests => 29;
1034
1035     $schema->storage->txn_begin;
1036
1037     # Create a borrower
1038     my $categorycode =
1039       $builder->build( { source => 'Category' } )->{categorycode};
1040     my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
1041
1042     my $borrower = Koha::Patron->new(
1043         {
1044             cardnumber => 'dariahall',
1045             surname    => 'Hall',
1046             firstname  => 'Daria',
1047         }
1048     );
1049     $borrower->categorycode($categorycode);
1050     $borrower->branchcode($branchcode);
1051     $borrower->store;
1052
1053     my $staff = Koha::Patron->new(
1054         {
1055             cardnumber => 'bobby',
1056             surname    => 'Bloggs',
1057             firstname  => 'Bobby',
1058         }
1059     );
1060     $staff->categorycode($categorycode);
1061     $staff->branchcode($branchcode);
1062     $staff->store;
1063
1064     my $account = Koha::Account->new( { patron_id => $borrower->id } );
1065
1066     my $debit1 = Koha::Account::Line->new(
1067         {
1068             borrowernumber    => $borrower->borrowernumber,
1069             amount            => 20,
1070             amountoutstanding => 20,
1071             interface         => 'commandline',
1072             debit_type_code   => 'LOST'
1073         }
1074     )->store();
1075     my $credit1 = Koha::Account::Line->new(
1076         {
1077             borrowernumber    => $borrower->borrowernumber,
1078             amount            => -20,
1079             amountoutstanding => -20,
1080             interface         => 'commandline',
1081             credit_type_code  => 'CREDIT'
1082         }
1083     )->store();
1084
1085     is( $account->balance(), 0, "Account balance is 0" );
1086     is( $debit1->amountoutstanding,
1087         20, 'Overdue fee has an amount outstanding of 20' );
1088     is( $credit1->amountoutstanding,
1089         -20, 'Credit has an amount outstanding of -20' );
1090
1091     my $reduce_params = {
1092         interface      => 'commandline',
1093         reduction_type => 'DISCOUNT',
1094         amount         => 5,
1095         staff_id       => $staff->borrowernumber,
1096         branch         => $branchcode
1097     };
1098
1099     throws_ok { $credit1->reduce($reduce_params); }
1100     'Koha::Exceptions::Account::IsNotDebit',
1101       '->reduce() can only be used with debits';
1102
1103     my @required = ( 'interface', 'reduction_type', 'amount' );
1104     for my $required (@required) {
1105         my $params = {%$reduce_params};
1106         delete( $params->{$required} );
1107         throws_ok {
1108             $debit1->reduce($params);
1109         }
1110         'Koha::Exceptions::MissingParameter',
1111           "->reduce() requires the `$required` parameter is passed";
1112     }
1113
1114     $reduce_params->{interface} = 'intranet';
1115     my @dependant_required = ( 'staff_id', 'branch' );
1116     for my $d (@dependant_required) {
1117         my $params = {%$reduce_params};
1118         delete( $params->{$d} );
1119         throws_ok {
1120             $debit1->reduce($params);
1121         }
1122         'Koha::Exceptions::MissingParameter',
1123 "->reduce() requires the `$d` parameter is passed when interface is intranet";
1124     }
1125
1126     throws_ok {
1127         $debit1->reduce(
1128             {
1129                 interface      => 'intranet',
1130                 staff_id       => $staff->borrowernumber,
1131                 branch         => $branchcode,
1132                 reduction_type => 'REFUND',
1133                 amount         => 25
1134             }
1135         );
1136     }
1137     'Koha::Exceptions::ParameterTooHigh',
1138       '->reduce() cannot reduce more than original amount';
1139
1140     # Partial Reduction
1141     # (Discount 5 on debt of 20)
1142     my $reduction = $debit1->reduce($reduce_params);
1143
1144     is( ref($reduction), 'Koha::Account::Line',
1145         '->reduce() returns a Koha::Account::Line' );
1146     is( $reduction->amount() * 1, -5, "Reduce amount is -5" );
1147     is( $reduction->amountoutstanding() * 1,
1148         0, "Reduce amountoutstanding is 0" );
1149     is( $debit1->amountoutstanding() * 1,
1150         15, "Debit amountoutstanding reduced by 5 to 15" );
1151     is( $debit1->status(), 'DISCOUNTED', "Debit status updated to DISCOUNTED");
1152     is( $account->balance() * 1, -5,        "Account balance is -5" );
1153     is( $reduction->status(),    'APPLIED', "Reduction status is 'APPLIED'" );
1154
1155     my $offsets = Koha::Account::Offsets->search(
1156         { credit_id => $reduction->id, debit_id => $debit1->id } );
1157     is( $offsets->count, 1, 'Only one offset is generated' );
1158     my $THE_offset = $offsets->next;
1159     is( $THE_offset->amount * 1,
1160         -5, 'Correct amount was applied against debit' );
1161     is( $THE_offset->type, 'DISCOUNT', "Offset type set to 'DISCOUNT'" );
1162
1163     # Zero offset created when zero outstanding
1164     # (Refund another 5 on paid debt of 20)
1165     $credit1->apply( { debits => [$debit1] } );
1166     is( $debit1->amountoutstanding + 0,
1167         0, 'Debit1 amountoutstanding reduced to 0' );
1168     $reduce_params->{reduction_type} = 'REFUND';
1169     $reduction = $debit1->reduce($reduce_params);
1170     is( $reduction->amount() * 1, -5, "Reduce amount is -5" );
1171     is( $reduction->amountoutstanding() * 1,
1172         -5, "Reduce amountoutstanding is -5" );
1173     is( $debit1->status(), 'REFUNDED', "Debit status updated to REFUNDED");
1174
1175     $offsets = Koha::Account::Offsets->search(
1176         { credit_id => $reduction->id, debit_id => $debit1->id } );
1177     is( $offsets->count, 1, 'Only one new offset is generated' );
1178     $THE_offset = $offsets->next;
1179     is( $THE_offset->amount * 1,
1180         0, 'Zero offset created for already paid off debit' );
1181     is( $THE_offset->type, 'REFUND', "Offset type set to 'REFUND'" );
1182
1183     # Compound reduction should not allow more than original amount
1184     # (Reduction of 5 + 5 + 20 > 20)
1185     $reduce_params->{amount} = 20;
1186     throws_ok {
1187         $debit1->reduce($reduce_params);
1188     }
1189     'Koha::Exceptions::ParameterTooHigh',
1190 '->reduce cannot reduce more than the original amount (combined reductions test)';
1191
1192     # Throw exception if attempting to reduce a payout
1193     my $payout = $reduction->payout(
1194         {
1195             interface   => 'intranet',
1196             staff_id    => $staff->borrowernumber,
1197             branch      => $branchcode,
1198             payout_type => 'CASH',
1199             amount      => 5
1200         }
1201     );
1202     throws_ok {
1203         $payout->reduce($reduce_params);
1204     }
1205     'Koha::Exceptions::Account::IsNotDebit',
1206       '->reduce() cannot be used on a payout debit';
1207
1208     $schema->storage->txn_rollback;
1209 };
1210
1211 subtest "cancel() tests" => sub {
1212     plan tests => 16;
1213
1214     $schema->storage->txn_begin;
1215
1216     my $library = $builder->build_object( { class => 'Koha::Libraries' });
1217     my $patron  = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->branchcode } });
1218     my $staff   = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->branchcode } });
1219
1220     t::lib::Mocks::mock_userenv({ patron => $patron });
1221
1222     my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
1223
1224     my $debit1 = Koha::Account::Line->new(
1225         {
1226             borrowernumber    => $patron->borrowernumber,
1227             amount            => 10,
1228             amountoutstanding => 10,
1229             interface         => 'commandline',
1230             debit_type_code   => 'OVERDUE',
1231         }
1232     )->store();
1233     my $debit2 = Koha::Account::Line->new(
1234         {
1235             borrowernumber    => $patron->borrowernumber,
1236             amount            => 20,
1237             amountoutstanding => 20,
1238             interface         => 'commandline',
1239             debit_type_code   => 'OVERDUE',
1240         }
1241     )->store();
1242
1243     my $ret = $account->pay(
1244         {
1245             lines  => [$debit2],
1246             amount => 5,
1247         }
1248     );
1249     my $credit = Koha::Account::Lines->find({ accountlines_id => $ret->{payment_id} });
1250
1251     is( $account->balance(), 25, "Account balance is 25" );
1252     is( $debit1->amountoutstanding + 0,
1253         10, 'First fee has amount outstanding of 10' );
1254     is( $debit2->amountoutstanding + 0,
1255         15, 'Second fee has amount outstanding of 15' );
1256     throws_ok {
1257         $credit->cancel(
1258             { staff_id => $staff->borrowernumber, branch => $library->branchcode } );
1259     }
1260     'Koha::Exceptions::Account::IsNotDebit',
1261       '->cancel() can only be used with debits';
1262
1263     throws_ok {
1264         $debit1->reduce( { staff_id => $staff->borrowernumber } );
1265     }
1266     'Koha::Exceptions::MissingParameter',
1267       "->cancel() requires the `branch` parameter is passed";
1268     throws_ok {
1269         $debit1->reduce( { branch => $library->branchcode } );
1270     }
1271     'Koha::Exceptions::MissingParameter',
1272       "->cancel() requires the `staff_id` parameter is passed";
1273
1274     throws_ok {
1275         $debit2->cancel(
1276             { staff_id => $staff->borrowernumber, branch => $library->branchcode } );
1277     }
1278     'Koha::Exceptions::Account',
1279       '->cancel() can only be used with debits that have not been offset';
1280
1281     my $cancellation = $debit1->cancel(
1282         { staff_id => $staff->borrowernumber, branch => $library->branchcode } );
1283     is( ref($cancellation), 'Koha::Account::Line',
1284         'Cancel returns an account line' );
1285     is(
1286         $cancellation->amount() * 1,
1287         $debit1->amount * -1,
1288         "Cancellation amount is " . $debit1->amount
1289     );
1290     is( $cancellation->amountoutstanding() * 1,
1291         0, "Cancellation amountoutstanding is 0" );
1292     is( $debit1->amountoutstanding() * 1,
1293         0, "Debit amountoutstanding reduced to 0" );
1294     is( $debit1->status(), 'CANCELLED', "Debit status updated to CANCELLED" );
1295     is( $account->balance() * 1, 15, "Account balance is 15" );
1296
1297     my $offsets = Koha::Account::Offsets->search(
1298         { credit_id => $cancellation->id, debit_id => $debit1->id } );
1299     is( $offsets->count, 1, 'Only one offset is generated' );
1300     my $THE_offset = $offsets->next;
1301     is( $THE_offset->amount * 1,
1302         -10, 'Correct amount was applied against debit' );
1303     is( $THE_offset->type, 'CANCELLATION',
1304         "Offset type set to 'CANCELLATION'" );
1305
1306     $schema->storage->txn_rollback;
1307 };
1308
1309 1;