3 # Copyright 2018 Koha Development team
5 # This file is part of Koha
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.
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.
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>
22 use Test::More tests => 15;
28 use C4::Circulation qw( AddRenewal CanBookBeRenewed LostItem AddIssue AddReturn );
30 use Koha::Account::Lines;
31 use Koha::Account::Offsets;
33 use Koha::DateUtils qw( dt_from_string );
36 use t::lib::TestBuilder;
38 my $schema = Koha::Database->new->schema;
39 my $builder = t::lib::TestBuilder->new;
41 subtest 'patron() tests' => sub {
45 $schema->storage->txn_begin;
47 my $library = $builder->build( { source => 'Branch' } );
48 my $patron = $builder->build( { source => 'Borrower' } );
50 my $line = Koha::Account::Line->new(
52 borrowernumber => $patron->{borrowernumber},
53 debit_type_code => "OVERDUE",
56 interface => 'commandline',
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' );
63 $line->borrowernumber(undef)->store;
64 is( $line->patron, undef, 'Koha::Account::Line->patron should return undef if no patron linked' );
66 $schema->storage->txn_rollback;
69 subtest 'manager() tests' => sub {
73 $schema->storage->txn_begin;
75 my $library = $builder->build( { source => 'Branch' } );
76 my $manager = $builder->build( { source => 'Borrower' } );
78 my $line = Koha::Account::Line->new(
80 manager_id => $manager->{borrowernumber},
81 debit_type_code => "OVERDUE",
84 interface => 'commandline',
87 my $account_line_manager = $line->manager;
88 is( ref( $account_line_manager ), 'Koha::Patron', 'Koha::Account::Line->manager should return a Koha::Patron' );
89 is( $line->manager_id, $account_line_manager->borrowernumber, 'Koha::Account::Line->manager should return the correct staff' );
91 $line->manager_id(undef)->store;
92 is( $line->manager, undef, 'Koha::Account::Line->manager should return undef if no staff linked' );
94 $schema->storage->txn_rollback;
97 subtest 'item() tests' => sub {
101 $schema->storage->txn_begin;
103 my $library = $builder->build( { source => 'Branch' } );
104 my $patron = $builder->build( { source => 'Borrower' } );
105 my $item = $builder->build_sample_item(
107 library => $library->{branchcode},
108 barcode => 'some_barcode_12',
113 my $line = Koha::Account::Line->new(
115 borrowernumber => $patron->{borrowernumber},
116 itemnumber => $item->itemnumber,
117 debit_type_code => "OVERDUE",
118 status => "RETURNED",
120 interface => 'commandline',
123 my $account_line_item = $line->item;
124 is( ref( $account_line_item ), 'Koha::Item', 'Koha::Account::Line->item should return a Koha::Item' );
125 is( $line->itemnumber, $account_line_item->itemnumber, 'Koha::Account::Line->item should return the correct item' );
127 $line->itemnumber(undef)->store;
128 is( $line->item, undef, 'Koha::Account::Line->item should return undef if no item linked' );
130 $schema->storage->txn_rollback;
133 subtest 'library() tests' => sub {
137 $schema->storage->txn_begin;
139 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
140 my $patron = $builder->build( { source => 'Borrower' } );
142 my $line = Koha::Account::Line->new(
144 borrowernumber => $patron->{borrowernumber},
145 branchcode => $library->branchcode,
146 debit_type_code => "OVERDUE",
147 status => "RETURNED",
149 interface => 'commandline',
153 my $account_line_library = $line->library;
154 is( ref($account_line_library),
156 'Koha::Account::Line->library should return a Koha::Library' );
159 $account_line_library->branchcode,
160 'Koha::Account::Line->library should return the correct library'
163 # Test ON DELETE SET NULL
165 my $found = Koha::Account::Lines->find( $line->accountlines_id );
166 ok( $found, "Koha::Account::Line not deleted when the linked library is deleted" );
168 is( $found->library, undef,
169 'Koha::Account::Line->library should return undef if linked library has been deleted'
172 $schema->storage->txn_rollback;
175 subtest 'is_credit() and is_debit() tests' => sub {
179 $schema->storage->txn_begin;
181 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
182 my $account = $patron->account;
184 my $credit = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' });
186 ok( $credit->is_credit, 'is_credit detects credits' );
187 ok( !$credit->is_debit, 'is_debit detects credits' );
189 my $debit = Koha::Account::Line->new(
191 borrowernumber => $patron->id,
192 debit_type_code => "OVERDUE",
193 status => "RETURNED",
195 interface => 'commandline',
198 ok( !$debit->is_credit, 'is_credit detects debits' );
199 ok( $debit->is_debit, 'is_debit detects debits');
201 $schema->storage->txn_rollback;
204 subtest 'apply() tests' => sub {
208 $schema->storage->txn_begin;
210 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
211 my $account = $patron->account;
213 my $credit = $account->add_credit( { amount => 100, user_id => $patron->id, interface => 'commandline' } );
215 my $debit_1 = Koha::Account::Line->new(
216 { borrowernumber => $patron->id,
217 debit_type_code => "OVERDUE",
218 status => "RETURNED",
220 amountoutstanding => 10,
221 interface => 'commandline',
225 my $debit_2 = Koha::Account::Line->new(
226 { borrowernumber => $patron->id,
227 debit_type_code => "OVERDUE",
228 status => "RETURNED",
230 amountoutstanding => 100,
231 interface => 'commandline',
235 $credit->discard_changes;
236 $debit_1->discard_changes;
238 my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
239 $credit = $credit->apply( { debits => [ $debits->as_list ] } );
240 is( ref($credit), 'Koha::Account::Line', '->apply returns the updated Koha::Account::Line credit object');
241 is( $credit->amountoutstanding * -1, 90, 'Remaining credit is correctly calculated' );
244 $debit_1->discard_changes;
245 is( $debit_1->amountoutstanding * 1, 0, 'Debit has been cancelled' );
247 my $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_1->id } );
248 is( $offsets->count, 1, 'Only one offset is generated' );
249 my $THE_offset = $offsets->next;
250 is( $THE_offset->amount * 1, -10, 'Amount was calculated correctly (less than the available credit)' );
251 is( $THE_offset->type, 'APPLY', 'Passed type stored correctly' );
253 $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
254 $credit = $credit->apply( { debits => [ $debits->as_list ] } );
255 is( $credit->amountoutstanding * 1, 0, 'No remaining credit' );
256 $debit_2->discard_changes;
257 is( $debit_2->amountoutstanding * 1, 10, 'Outstanding amount decremented correctly' );
259 $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_2->id } );
260 is( $offsets->count, 1, 'Only one offset is generated' );
261 $THE_offset = $offsets->next;
262 is( $THE_offset->amount * 1, -90, 'Amount was calculated correctly (less than the available credit)' );
263 is( $THE_offset->type, 'APPLY', 'Defaults to \'APPLY\' offset type' );
265 $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
267 { $credit->apply({ debits => [ $debits->as_list ] }); }
268 'Koha::Exceptions::Account::NoAvailableCredit',
269 '->apply() can only be used with outstanding credits';
271 $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
273 { $debit_1->apply({ debits => [ $debits->as_list ] }); }
274 'Koha::Exceptions::Account::IsNotCredit',
275 '->apply() can only be used with credits';
277 $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
278 my $credit_3 = $account->add_credit({ amount => 1, interface => 'commandline' });
280 { $credit_3->apply({ debits => [ $debits->as_list ] }); }
281 'Koha::Exceptions::Account::IsNotDebit',
282 '->apply() can only be applied to credits';
284 my $credit_2 = $account->add_credit({ amount => 20, interface => 'commandline' });
285 my $debit_3 = Koha::Account::Line->new(
286 { borrowernumber => $patron->id,
287 debit_type_code => "OVERDUE",
288 status => "RETURNED",
290 amountoutstanding => 100,
291 interface => 'commandline',
295 $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id, $credit->id ] } });
297 $credit_2->apply( { debits => [ $debits->as_list ] }); }
298 'Koha::Exceptions::Account::IsNotDebit',
299 '->apply() rolls back if any of the passed lines is not a debit';
301 is( $debit_1->discard_changes->amountoutstanding * 1, 0, 'No changes to already cancelled debit' );
302 is( $debit_2->discard_changes->amountoutstanding * 1, 10, 'Debit cancelled' );
303 is( $debit_3->discard_changes->amountoutstanding * 1, 100, 'Outstanding amount correctly calculated' );
304 is( $credit_2->discard_changes->amountoutstanding * -1, 20, 'No changes made' );
306 $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id ] } });
307 $credit_2 = $credit_2->apply( { debits => [ $debits->as_list ] } );
309 is( $debit_1->discard_changes->amountoutstanding * 1, 0, 'No changes to already cancelled debit' );
310 is( $debit_2->discard_changes->amountoutstanding * 1, 0, 'Debit cancelled' );
311 is( $debit_3->discard_changes->amountoutstanding * 1, 90, 'Outstanding amount correctly calculated' );
312 is( $credit_2->amountoutstanding * 1, 0, 'No remaining credit' );
314 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
315 my $biblio = $builder->build_sample_biblio();
317 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
318 my $now = dt_from_string();
319 my $seven_weeks = DateTime::Duration->new(weeks => 7);
320 my $five_weeks = DateTime::Duration->new(weeks => 5);
321 my $seven_weeks_ago = $now - $seven_weeks;
322 my $five_weeks_ago = $now - $five_weeks;
324 my $checkout = Koha::Checkout->new(
326 borrowernumber => $patron->id,
327 itemnumber => $item->id,
328 date_due => $five_weeks_ago,
329 branchcode => $library->id,
330 issuedate => $seven_weeks_ago
334 my $accountline = Koha::Account::Line->new(
336 issue_id => $checkout->id,
337 borrowernumber => $patron->id,
338 itemnumber => $item->id,
339 branchcode => $library->id,
341 debit_type_code => 'OVERDUE',
342 status => 'UNRETURNED',
345 amountoutstanding => '1',
349 my $a = $checkout->account_lines->next;
350 is( $a->id, $accountline->id, "Koha::Checkout::account_lines returns the related acountline" );
352 # Enable renewing upon fine payment
353 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 1 );
355 my $module = Test::MockModule->new('C4::Circulation');
356 $module->mock('AddRenewal', sub { $called = 1; });
357 $module->mock('CanBookBeRenewed', sub { return 1; });
358 my $credit_forgive = $account->add_credit(
361 user_id => $patron->id,
366 my $debits_renew = Koha::Account::Lines->search({ accountlines_id => $accountline->id })->as_list;
367 $credit_forgive = $credit_forgive->apply( { debits => $debits_renew } );
368 is( $called, 0, 'C4::Circulation::AddRenew NOT called when RenewAccruingItemWhenPaid enabled but credit type is "FORGIVEN"' );
370 $accountline = Koha::Account::Line->new(
372 issue_id => $checkout->id,
373 borrowernumber => $patron->id,
374 itemnumber => $item->id,
375 branchcode => $library->id,
377 debit_type_code => 'OVERDUE',
378 status => 'UNRETURNED',
381 amountoutstanding => '1',
384 my $credit_renew = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' });
385 $debits_renew = Koha::Account::Lines->search({ accountlines_id => $accountline->id })->as_list;
386 $credit_renew = $credit_renew->apply( { debits => $debits_renew } );
387 is( $called, 1, 'RenewAccruingItemWhenPaid causes C4::Circulation::AddRenew to be called when appropriate' );
389 my @messages = @{$credit_renew->object_messages};
390 is( $messages[0]->type, 'info', 'Info message added for renewal' );
391 is( $messages[0]->message, 'renewal', 'Message is "renewal"' );
392 is( $messages[0]->payload->{itemnumber}, $item->id, 'itemnumber found in payload' );
393 is( $messages[0]->payload->{due_date}, 1, 'due_date key in payload' );
394 is( $messages[0]->payload->{success}, 1, "'success' key in payload" );
396 t::lib::Mocks::mock_preference( 'MarkLostItemsAsReturned', 'onpayment');
397 my $loser = $builder->build_object( { class => 'Koha::Patrons' } );
398 my $loser_account = $loser->account;
400 my $lost_item = $builder->build_sample_item();
401 my $lost_checkout = Koha::Checkout->new(
403 borrowernumber => $loser->id,
404 itemnumber => $lost_item->id,
405 date_due => $five_weeks_ago,
406 branchcode => $library->id,
407 issuedate => $seven_weeks_ago
411 $lost_item->itemlost(1)->store;
412 my $processing_fee = Koha::Account::Line->new(
414 issue_id => $lost_checkout->id,
415 borrowernumber => $loser->id,
416 itemnumber => $lost_item->id,
417 branchcode => $library->id,
419 debit_type_code => 'PROCESSING',
421 interface => 'intranet',
423 amountoutstanding => '15',
426 my $lost_fee = Koha::Account::Line->new(
428 issue_id => $lost_checkout->id,
429 borrowernumber => $loser->id,
430 itemnumber => $lost_item->id,
431 branchcode => $library->id,
433 debit_type_code => 'LOST',
435 interface => 'intranet',
437 amountoutstanding => '12.63',
440 my $pay_lost = $loser_account->add_credit({ amount => 27.630000, user_id => $loser->id, interface => 'intranet' });
441 my $pay_lines = [ $processing_fee, $lost_fee ];
442 $pay_lost->apply( { debits => $pay_lines, offset_type => 'Credit applied' } );
444 is( $loser->checkouts->next, undef, "Item has been returned");
448 $schema->storage->txn_rollback;
451 subtest 'Keep account info when related patron, staff, item or cash_register is deleted' => sub {
455 $schema->storage->txn_begin;
457 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
458 my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
459 my $item = $builder->build_sample_item;
460 my $issue = $builder->build_object(
462 class => 'Koha::Checkouts',
463 value => { itemnumber => $item->itemnumber }
466 my $register = $builder->build_object({ class => 'Koha::Cash::Registers' });
468 my $line = Koha::Account::Line->new(
470 borrowernumber => $patron->borrowernumber,
471 manager_id => $staff->borrowernumber,
472 itemnumber => $item->itemnumber,
473 debit_type_code => "OVERDUE",
474 status => "RETURNED",
476 interface => 'commandline',
477 register_id => $register->id
482 $line = $line->get_from_storage;
483 is( $line->itemnumber, undef, "The account line should not be deleted when the related item is delete");
486 $line = $line->get_from_storage;
487 is( $line->manager_id, undef, "The account line should not be deleted when the related staff is delete");
490 $line = $line->get_from_storage;
491 is( $line->borrowernumber, undef, "The account line should not be deleted when the related patron is delete");
494 $line = $line->get_from_storage;
495 is( $line->register_id, undef, "The account line should not be deleted when the related cash register is delete");
497 $schema->storage->txn_rollback;
500 subtest 'Renewal related tests' => sub {
504 $schema->storage->txn_begin;
506 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
507 my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
508 my $item = $builder->build_sample_item;
509 my $issue = $builder->build_object(
511 class => 'Koha::Checkouts',
513 itemnumber => $item->itemnumber,
514 borrowernumber => $patron->borrowernumber,
515 onsite_checkout => 0,
516 renewals_count => 99,
521 my $line = Koha::Account::Line->new(
523 borrowernumber => $patron->borrowernumber,
524 manager_id => $staff->borrowernumber,
525 itemnumber => $item->itemnumber,
526 debit_type_code => "OVERDUE",
527 status => "UNRETURNED",
528 amountoutstanding => 0,
530 interface => 'commandline',
533 is( $line->is_renewable, 1, "Item is returned as renewable when it meets the conditions" );
534 $line->amountoutstanding(5);
535 is( $line->is_renewable, 0, "Item is returned as unrenewable when it has outstanding fine" );
536 $line->amountoutstanding(0);
537 $line->debit_type_code("VOID");
538 is( $line->is_renewable, 0, "Item is returned as unrenewable when it has the wrong account type" );
539 $line->debit_type_code("OVERDUE");
540 $line->status("RETURNED");
541 is( $line->is_renewable, 0, "Item is returned as unrenewable when it has the wrong account status" );
544 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 0 );
545 is ($line->renew_item({ interface => 'intranet' }), undef, 'Attempt to renew fails when syspref is not set');
546 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 1 );
547 t::lib::Mocks::mock_preference( 'RenewAccruingItemInOpac', 0 );
548 is ($line->renew_item({ interface => 'opac' }), undef, 'Attempt to renew fails when syspref is not set - OPAC');
549 t::lib::Mocks::mock_preference( 'RenewAccruingItemInOpac', 1 );
551 $line->renew_item({ interface => 'intranet' }),
553 itemnumber => $item->itemnumber,
557 'Attempt to renew fails when CanBookBeRenewed returns false'
560 $issue = $builder->build_object(
562 class => 'Koha::Checkouts',
564 itemnumber => $item->itemnumber,
565 onsite_checkout => 0,
572 my $module = Test::MockModule->new('C4::Circulation');
573 $module->mock('CanBookBeRenewed', sub { return 1; });
575 my $r = Koha::Checkouts::Renewals->find({ checkout_id => $issue->id });
576 is( $r->seen, 0, "RenewAccruingItemWhenPaid triggers an unseen renewal" );
578 $schema->storage->txn_rollback;
581 subtest 'adjust() tests' => sub {
585 $schema->storage->txn_begin;
587 # count logs before any actions
588 my $action_logs = $schema->resultset('ActionLog')->search()->count;
591 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
593 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
594 my $account = $patron->account;
596 my $debit_1 = Koha::Account::Line->new(
597 { borrowernumber => $patron->id,
598 debit_type_code => "OVERDUE",
599 status => "RETURNED",
601 amountoutstanding => 10,
602 interface => 'commandline',
606 my $debit_2 = Koha::Account::Line->new(
607 { borrowernumber => $patron->id,
608 debit_type_code => "OVERDUE",
609 status => "UNRETURNED",
611 amountoutstanding => 100,
612 interface => 'commandline'
616 my $credit = $account->add_credit( { amount => 40, user_id => $patron->id, interface => 'commandline' } );
618 throws_ok { $debit_1->adjust( { amount => 50, type => 'bad', interface => 'commandline' } ) }
619 qr/Update type not recognised/, 'Exception thrown for unrecognised type';
621 throws_ok { $debit_1->adjust( { amount => 50, type => 'overdue_update', interface => 'commandline' } ) }
622 qr/Update type not allowed on this debit_type/,
623 'Exception thrown for type conflict';
625 # Increment an unpaid fine
626 $debit_2->adjust( { amount => 150, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
628 is( $debit_2->amount * 1, 150, 'Fine amount was updated in full' );
629 is( $debit_2->amountoutstanding * 1, 150, 'Fine amountoutstanding was update in full' );
630 isnt( $debit_2->date, undef, 'Date has been set' );
632 my $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
633 is( $offsets->count, 1, 'An offset is generated for the increment' );
634 my $THIS_offset = $offsets->next;
635 is( $THIS_offset->amount * 1, 50, 'Amount was calculated correctly (increment by 50)' );
636 is( $THIS_offset->type, 'OVERDUE_INCREASE', 'Adjust type stored correctly' );
638 is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
640 # Update fine to partially paid
641 my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
642 $credit->apply( { debits => [ $debits->as_list ] } );
644 $debit_2->discard_changes;
645 is( $debit_2->amount * 1, 150, 'Fine amount unaffected by partial payment' );
646 is( $debit_2->amountoutstanding * 1, 110, 'Fine amountoutstanding updated by partial payment' );
649 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
651 # Increment the partially paid fine
652 $debit_2->adjust( { amount => 160, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
654 is( $debit_2->amount * 1, 160, 'Fine amount was updated in full' );
655 is( $debit_2->amountoutstanding * 1, 120, 'Fine amountoutstanding was updated by difference' );
657 $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
658 is( $offsets->count, 3, 'An offset is generated for the increment' );
659 $THIS_offset = $offsets->last;
660 is( $THIS_offset->amount * 1, 10, 'Amount was calculated correctly (increment by 10)' );
661 is( $THIS_offset->type, 'OVERDUE_INCREASE', 'Adjust type stored correctly' );
663 is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
665 # Decrement the partially paid fine, less than what was paid
666 $debit_2->adjust( { amount => 50, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
668 is( $debit_2->amount * 1, 50, 'Fine amount was updated in full' );
669 is( $debit_2->amountoutstanding * 1, 10, 'Fine amountoutstanding was updated by difference' );
671 $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
672 is( $offsets->count, 4, 'An offset is generated for the decrement' );
673 $THIS_offset = $offsets->last;
674 is( $THIS_offset->amount * 1, -110, 'Amount was calculated correctly (decrement by 110)' );
675 is( $THIS_offset->type, 'OVERDUE_DECREASE', 'Adjust type stored correctly' );
677 # Decrement the partially paid fine, more than what was paid
678 $debit_2->adjust( { amount => 30, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
679 is( $debit_2->amount * 1, 30, 'Fine amount was updated in full' );
680 is( $debit_2->amountoutstanding * 1, 0, 'Fine amountoutstanding was zeroed (payment was 40)' );
682 $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
683 is( $offsets->count, 5, 'An offset is generated for the decrement' );
684 $THIS_offset = $offsets->last;
685 is( $THIS_offset->amount * 1, -20, 'Amount was calculated correctly (decrement by 20)' );
686 is( $THIS_offset->type, 'OVERDUE_DECREASE', 'Adjust type stored correctly' );
688 my $overpayment_refund = $account->lines->last;
689 is( $overpayment_refund->amount * 1, -10, 'A new credit has been added' );
690 is( $overpayment_refund->credit_type_code, 'OVERPAYMENT', 'Credit generated with the expected credit_type_code' );
692 $schema->storage->txn_rollback;
695 subtest 'checkout() tests' => sub {
698 $schema->storage->txn_begin;
700 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
701 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
702 my $item = $builder->build_sample_item;
703 my $account = $patron->account;
705 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
706 my $checkout = AddIssue( $patron->unblessed, $item->barcode );
708 my $line = $account->add_debit({
710 interface => 'commandline',
711 item_id => $item->itemnumber,
712 issue_id => $checkout->issue_id,
714 status => 'UNRETURNED'
717 my $line_checkout = $line->checkout;
718 is( ref($line_checkout), 'Koha::Checkout', 'Result type is correct' );
719 is( $line_checkout->issue_id, $checkout->issue_id, 'Koha::Account::Line->checkout should return the correct checkout');
721 # Prevent re-calculation of fines at check-in for the test; Since bug 8338 the recalculation would result in a '0'
722 # fine which would subsequently be removed by _FixOverduesOnReturn
723 t::lib::Mocks::mock_preference( 'finesMode', 'off' );
725 my ( $returned, undef, $old_checkout) = C4::Circulation::AddReturn( $item->barcode, $library->branchcode );
726 is( $returned, 1, 'The item should have been returned' );
728 $line = $line->get_from_storage;
729 my $old_line_checkout = $line->checkout;
730 is( ref($old_line_checkout), 'Koha::Old::Checkout', 'Result type is correct' );
731 is( $old_line_checkout->issue_id, $old_checkout->issue_id, 'Koha::Account::Line->checkout should return the correct old_checkout' );
733 $line->issue_id(undef)->store;
734 is( $line->checkout, undef, 'Koha::Account::Line->checkout should return undef if no checkout linked' );
736 $schema->storage->txn_rollback;
739 subtest 'credits() and debits() tests' => sub {
742 $schema->storage->txn_begin;
744 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
745 my $account = $patron->account;
747 my $debit1 = $account->add_debit({
749 interface => 'commandline',
752 my $debit2 = $account->add_debit({
754 interface => 'commandline',
757 my $credit1 = $account->add_credit({
759 interface => 'commandline',
762 my $credit2 = $account->add_credit({
764 interface => 'commandline',
768 $credit1->apply({ debits => [ $debit1 ] });
769 $credit2->apply({ debits => [ $debit1, $debit2 ] });
771 my $credits = $debit1->credits;
772 is($credits->count, 2, '2 Credits applied to debit 1');
773 my $credit = $credits->next;
774 is($credit->amount + 0, -5, 'Correct first credit');
775 $credit = $credits->next;
776 is($credit->amount + 0, -10, 'Correct second credit');
778 $credits = $debit2->credits;
779 is($credits->count, 1, '1 Credits applied to debit 2');
780 $credit = $credits->next;
781 is($credit->amount + 0, -10, 'Correct first credit');
783 my $debits = $credit1->debits;
784 is($debits->count, 1, 'Credit 1 applied to 1 debit');
785 my $debit = $debits->next;
786 is($debit->amount + 0, 8, 'Correct first debit');
788 $debits = $credit2->debits;
789 is($debits->count, 2, 'Credit 2 applied to 2 debits');
790 $debit = $debits->next;
791 is($debit->amount + 0, 8, 'Correct first debit');
792 $debit = $debits->next;
793 is($debit->amount + 0, 12, 'Correct second debit');
797 'Koha::Exceptions::Account::IsNotCredit',
798 'Exception is thrown when requesting debits linked to debit';
801 { $credit1->credits; }
802 'Koha::Exceptions::Account::IsNotDebit',
803 'Exception is thrown when requesting credits linked to credit';
806 $schema->storage->txn_rollback;
809 subtest "void() tests" => sub {
813 $schema->storage->txn_begin;
816 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
817 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
819 my $borrower = Koha::Patron->new( {
820 cardnumber => 'dariahall',
822 firstname => 'Daria',
824 $borrower->categorycode( $categorycode );
825 $borrower->branchcode( $branchcode );
828 my $account = Koha::Account->new({ patron_id => $borrower->id });
830 my $line1 = Koha::Account::Line->new(
832 borrowernumber => $borrower->borrowernumber,
834 amountoutstanding => 10,
835 interface => 'commandline',
836 debit_type_code => 'OVERDUE'
839 my $line2 = Koha::Account::Line->new(
841 borrowernumber => $borrower->borrowernumber,
843 amountoutstanding => 20,
844 interface => 'commandline',
845 debit_type_code => 'OVERDUE'
849 is( $account->balance(), 30, "Account balance is 30" );
850 is( $line1->amountoutstanding, 10, 'First fee has amount outstanding of 10' );
851 is( $line2->amountoutstanding, 20, 'Second fee has amount outstanding of 20' );
853 my $id = $account->pay(
855 lines => [$line1, $line2],
860 my $account_payment = Koha::Account::Lines->find( $id );
862 is( $account->balance(), 0, "Account balance is 0" );
864 $line1->_result->discard_changes();
865 $line2->_result->discard_changes();
866 is( $line1->amountoutstanding+0, 0, 'First fee has amount outstanding of 0' );
867 is( $line2->amountoutstanding+0, 0, 'Second fee has amount outstanding of 0' );
870 $line1->void( { interface => 'test' } );
872 'Koha::Exceptions::Account::IsNotCredit',
873 '->void() can only be used with credits';
876 $account_payment->void();
878 'Koha::Exceptions::MissingParameter',
879 "->void() requires the `interface` parameter is passed";
882 $account_payment->void( { interface => 'intranet' } );
884 'Koha::Exceptions::MissingParameter',
885 "->void() requires the `staff_id` parameter is passed when `interface` equals 'intranet'";
887 $account_payment->void( { interface => 'intranet', staff_id => $borrower->borrowernumber } );
889 'Koha::Exceptions::MissingParameter',
890 "->void() requires the `branch` parameter is passed when `interface` equals 'intranet'";
892 my $void = $account_payment->void({ interface => 'test' });
894 is( ref($void), 'Koha::Account::Line', 'Void returns the account line' );
895 is( $void->debit_type_code, 'VOID', 'Void returns the VOID account line' );
896 is( $void->manager_id, undef, 'Void proceeds without manager_id OK if interface is not "intranet"' );
897 is( $void->branchcode, undef, 'Void proceeds without branchcode OK if interface is not "intranet"' );
898 is( $account->balance(), 30, "Account balance is again 30" );
900 $account_payment->_result->discard_changes();
901 $line1->_result->discard_changes();
902 $line2->_result->discard_changes();
904 is( $account_payment->credit_type_code, 'PAYMENT', 'Voided payment credit_type_code is still PAYMENT' );
905 is( $account_payment->status, 'VOID', 'Voided payment status is VOID' );
906 is( $account_payment->amount+0, -30, 'Voided payment amount is still -30' );
907 is( $account_payment->amountoutstanding+0, 0, 'Voided payment amount outstanding is 0' );
909 is( $line1->amountoutstanding+0, 10, 'First fee again has amount outstanding of 10' );
910 is( $line2->amountoutstanding+0, 20, 'Second fee again has amount outstanding of 20' );
912 my $credit2 = $account->add_credit( { interface => 'test', amount => 10 } );
913 $void = $credit2->void(
915 interface => 'intranet',
916 staff_id => $borrower->borrowernumber,
917 branch => $branchcode
920 is( $void->manager_id, $borrower->borrowernumber, "->void stores the manager_id when it's passed");
921 is( $void->branchcode, $branchcode, "->void stores the branchcode when it's passed");
923 $schema->storage->txn_rollback;
926 subtest "payout() tests" => sub {
930 $schema->storage->txn_begin;
934 $builder->build( { source => 'Category' } )->{categorycode};
935 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
937 my $borrower = Koha::Patron->new(
939 cardnumber => 'dariahall',
941 firstname => 'Daria',
944 $borrower->categorycode($categorycode);
945 $borrower->branchcode($branchcode);
948 my $staff = Koha::Patron->new(
950 cardnumber => 'bobby',
952 firstname => 'Bobby',
955 $staff->categorycode($categorycode);
956 $staff->branchcode($branchcode);
959 my $account = Koha::Account->new( { patron_id => $borrower->id } );
961 my $debit1 = Koha::Account::Line->new(
963 borrowernumber => $borrower->borrowernumber,
965 amountoutstanding => 10,
966 interface => 'commandline',
967 debit_type_code => 'OVERDUE'
970 my $credit1 = Koha::Account::Line->new(
972 borrowernumber => $borrower->borrowernumber,
974 amountoutstanding => -20,
975 interface => 'commandline',
976 credit_type_code => 'CREDIT'
980 is( $account->balance(), -10, "Account balance is -10" );
981 is( $debit1->amountoutstanding + 0,
982 10, 'Overdue fee has an amount outstanding of 10' );
983 is( $credit1->amountoutstanding + 0,
984 -20, 'Credit has an amount outstanding of -20' );
987 interface => 'intranet',
988 staff_id => $staff->borrowernumber,
989 branch => $branchcode,
990 payout_type => 'CASH',
994 throws_ok { $debit1->payout($pay_params); }
995 'Koha::Exceptions::Account::IsNotCredit',
996 '->payout() can only be used with credits';
999 ( 'interface', 'staff_id', 'branch', 'payout_type', 'amount' );
1000 for my $required (@required) {
1001 my $params = {%$pay_params};
1002 delete( $params->{$required} );
1004 $credit1->payout($params);
1006 'Koha::Exceptions::MissingParameter',
1007 "->payout() requires the `$required` parameter is passed";
1013 interface => 'intranet',
1014 staff_id => $staff->borrowernumber,
1015 branch => $branchcode,
1016 payout_type => 'CASH',
1021 'Koha::Exceptions::ParameterTooHigh',
1022 '->payout() cannot pay out more than the amountoutstanding';
1024 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
1028 interface => 'intranet',
1029 staff_id => $staff->borrowernumber,
1030 branch => $branchcode,
1031 payout_type => 'CASH',
1036 'Koha::Exceptions::Account::RegisterRequired',
1037 '->payout() requires a cash_register if payout_type is `CASH`';
1039 t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
1040 my $payout = $credit1->payout(
1042 interface => 'intranet',
1043 staff_id => $staff->borrowernumber,
1044 branch => $branchcode,
1045 payout_type => 'CASH',
1050 is( ref($payout), 'Koha::Account::Line',
1051 '->payout() returns a Koha::Account::Line' );
1052 is( $payout->amount() + 0, 10, "Payout amount is 10" );
1053 is( $payout->amountoutstanding() + 0, 0, "Payout amountoutstanding is 0" );
1054 is( $account->balance() + 0, 0, "Account balance is 0" );
1055 is( $debit1->amountoutstanding + 0,
1056 10, 'Overdue fee still has an amount outstanding of 10' );
1057 is( $credit1->amountoutstanding + 0,
1058 -10, 'Credit has an new amount outstanding of -10' );
1059 is( $credit1->status(), 'PAID', "Credit has a new status of PAID" );
1061 $schema->storage->txn_rollback;
1064 subtest "reduce() tests" => sub {
1068 $schema->storage->txn_begin;
1072 $builder->build( { source => 'Category' } )->{categorycode};
1073 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
1075 my $borrower = Koha::Patron->new(
1077 cardnumber => 'dariahall',
1079 firstname => 'Daria',
1082 $borrower->categorycode($categorycode);
1083 $borrower->branchcode($branchcode);
1086 my $staff = Koha::Patron->new(
1088 cardnumber => 'bobby',
1089 surname => 'Bloggs',
1090 firstname => 'Bobby',
1093 $staff->categorycode($categorycode);
1094 $staff->branchcode($branchcode);
1097 my $account = Koha::Account->new( { patron_id => $borrower->id } );
1099 my $debit1 = Koha::Account::Line->new(
1101 borrowernumber => $borrower->borrowernumber,
1103 amountoutstanding => 20,
1104 interface => 'commandline',
1105 debit_type_code => 'LOST'
1108 my $credit1 = Koha::Account::Line->new(
1110 borrowernumber => $borrower->borrowernumber,
1112 amountoutstanding => -20,
1113 interface => 'commandline',
1114 credit_type_code => 'CREDIT'
1118 is( $account->balance(), 0, "Account balance is 0" );
1119 is( $debit1->amountoutstanding,
1120 20, 'Overdue fee has an amount outstanding of 20' );
1121 is( $credit1->amountoutstanding,
1122 -20, 'Credit has an amount outstanding of -20' );
1124 my $reduce_params = {
1125 interface => 'commandline',
1126 reduction_type => 'DISCOUNT',
1128 staff_id => $staff->borrowernumber,
1129 branch => $branchcode
1132 throws_ok { $credit1->reduce($reduce_params); }
1133 'Koha::Exceptions::Account::IsNotDebit',
1134 '->reduce() can only be used with debits';
1136 my @required = ( 'interface', 'reduction_type', 'amount' );
1137 for my $required (@required) {
1138 my $params = {%$reduce_params};
1139 delete( $params->{$required} );
1141 $debit1->reduce($params);
1143 'Koha::Exceptions::MissingParameter',
1144 "->reduce() requires the `$required` parameter is passed";
1147 $reduce_params->{interface} = 'intranet';
1148 my @dependant_required = ( 'staff_id', 'branch' );
1149 for my $d (@dependant_required) {
1150 my $params = {%$reduce_params};
1151 delete( $params->{$d} );
1153 $debit1->reduce($params);
1155 'Koha::Exceptions::MissingParameter',
1156 "->reduce() requires the `$d` parameter is passed when interface is intranet";
1162 interface => 'intranet',
1163 staff_id => $staff->borrowernumber,
1164 branch => $branchcode,
1165 reduction_type => 'REFUND',
1170 'Koha::Exceptions::ParameterTooHigh',
1171 '->reduce() cannot reduce more than original amount';
1174 # (Discount 5 on debt of 20)
1175 my $reduction = $debit1->reduce($reduce_params);
1177 is( ref($reduction), 'Koha::Account::Line',
1178 '->reduce() returns a Koha::Account::Line' );
1179 is( $reduction->amount() * 1, -5, "Reduce amount is -5" );
1180 is( $reduction->amountoutstanding() * 1,
1181 0, "Reduce amountoutstanding is 0" );
1182 is( $debit1->amountoutstanding() * 1,
1183 15, "Debit amountoutstanding reduced by 5 to 15" );
1184 is( $debit1->status(), 'DISCOUNTED', "Debit status updated to DISCOUNTED");
1185 is( $account->balance() * 1, -5, "Account balance is -5" );
1186 is( $reduction->status(), 'APPLIED', "Reduction status is 'APPLIED'" );
1188 my $offsets = Koha::Account::Offsets->search(
1189 { credit_id => $reduction->id } );
1190 is( $offsets->count, 2, 'Two offsets generated' );
1191 my $THE_offset = $offsets->next;
1192 is( $THE_offset->type, 'CREATE', 'CREATE offset added for discount line');
1193 is( $THE_offset->amount * 1,
1194 -5, 'Correct offset amount recorded');
1195 $THE_offset = $offsets->next;
1196 is( $THE_offset->type, 'APPLY', "APPLY offset added for 'DISCOUNT'" );
1197 is( $THE_offset->amount * 1, -5, 'Correct amount offset against debt');
1198 is( $THE_offset->debit_id, $debit1->accountlines_id, 'APPLY offset recorded the correct debit_id');
1200 # Zero offset created when zero outstanding
1201 # (Refund another 5 on paid debt of 20)
1202 $credit1->apply( { debits => [$debit1] } );
1203 is( $debit1->amountoutstanding + 0,
1204 0, 'Debit1 amountoutstanding reduced to 0' );
1205 $reduce_params->{reduction_type} = 'REFUND';
1206 $reduction = $debit1->reduce($reduce_params);
1207 is( $reduction->amount() * 1, -5, "Reduce amount is -5" );
1208 is( $reduction->amountoutstanding() * 1,
1209 -5, "Reduce amountoutstanding is -5" );
1210 is( $debit1->status(), 'REFUNDED', "Debit status updated to REFUNDED");
1212 $offsets = Koha::Account::Offsets->search(
1213 { credit_id => $reduction->id } );
1214 is( $offsets->count, 2, 'Two offsets generated' );
1215 $THE_offset = $offsets->next;
1216 is( $THE_offset->type, 'CREATE', 'CREATE offset added for refund line');
1217 is( $THE_offset->amount * 1,
1218 -5, 'Correct offset amount recorded');
1219 $THE_offset = $offsets->next;
1220 is( $THE_offset->type, 'APPLY', "APPLY offset added for 'REFUND'" );
1221 is( $THE_offset->amount * 1,
1222 0, 'Zero offset created for already paid off debit' );
1224 # Compound reduction should not allow more than original amount
1225 # (Reduction of 5 + 5 + 20 > 20)
1226 $reduce_params->{amount} = 20;
1228 $debit1->reduce($reduce_params);
1230 'Koha::Exceptions::ParameterTooHigh',
1231 '->reduce cannot reduce more than the original amount (combined reductions test)';
1233 # Throw exception if attempting to reduce a payout
1234 my $payout = $reduction->payout(
1236 interface => 'intranet',
1237 staff_id => $staff->borrowernumber,
1238 branch => $branchcode,
1239 payout_type => 'CASH',
1244 $payout->reduce($reduce_params);
1246 'Koha::Exceptions::Account::IsNotDebit',
1247 '->reduce() cannot be used on a payout debit';
1249 $schema->storage->txn_rollback;
1252 subtest "cancel() tests" => sub {
1255 $schema->storage->txn_begin;
1257 my $library = $builder->build_object( { class => 'Koha::Libraries' });
1258 my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->branchcode } });
1259 my $staff = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->branchcode } });
1261 t::lib::Mocks::mock_userenv({ patron => $patron });
1263 my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
1265 my $debit1 = Koha::Account::Line->new(
1267 borrowernumber => $patron->borrowernumber,
1269 amountoutstanding => 10,
1270 interface => 'commandline',
1271 debit_type_code => 'OVERDUE',
1274 my $debit2 = Koha::Account::Line->new(
1276 borrowernumber => $patron->borrowernumber,
1278 amountoutstanding => 20,
1279 interface => 'commandline',
1280 debit_type_code => 'OVERDUE',
1284 my $ret = $account->pay(
1290 my $credit = Koha::Account::Lines->find({ accountlines_id => $ret->{payment_id} });
1292 is( $account->balance(), 25, "Account balance is 25" );
1293 is( $debit1->amountoutstanding + 0,
1294 10, 'First fee has amount outstanding of 10' );
1295 is( $debit2->amountoutstanding + 0,
1296 15, 'Second fee has amount outstanding of 15' );
1299 { staff_id => $staff->borrowernumber, branch => $library->branchcode } );
1301 'Koha::Exceptions::Account::IsNotDebit',
1302 '->cancel() can only be used with debits';
1305 $debit1->reduce( { staff_id => $staff->borrowernumber } );
1307 'Koha::Exceptions::MissingParameter',
1308 "->cancel() requires the `branch` parameter is passed";
1310 $debit1->reduce( { branch => $library->branchcode } );
1312 'Koha::Exceptions::MissingParameter',
1313 "->cancel() requires the `staff_id` parameter is passed";
1317 { staff_id => $staff->borrowernumber, branch => $library->branchcode } );
1319 'Koha::Exceptions::Account',
1320 '->cancel() can only be used with debits that have not been offset';
1322 my $cancellation = $debit1->cancel(
1323 { staff_id => $staff->borrowernumber, branch => $library->branchcode } );
1324 is( ref($cancellation), 'Koha::Account::Line',
1325 'Cancel returns an account line' );
1327 $cancellation->amount() * 1,
1328 $debit1->amount * -1,
1329 "Cancellation amount is " . $debit1->amount
1331 is( $cancellation->amountoutstanding() * 1,
1332 0, "Cancellation amountoutstanding is 0" );
1333 is( $debit1->amountoutstanding() * 1,
1334 0, "Debit amountoutstanding reduced to 0" );
1335 is( $debit1->status(), 'CANCELLED', "Debit status updated to CANCELLED" );
1336 is( $account->balance() * 1, 15, "Account balance is 15" );
1338 my $offsets = Koha::Account::Offsets->search(
1339 { credit_id => $cancellation->id } );
1340 is( $offsets->count, 2, 'Two offsets are generated' );
1341 my $THE_offset = $offsets->next;
1342 is( $THE_offset->type, 'CREATE', 'CREATE offset added for cancel line');
1343 is( $THE_offset->amount * 1, -10, 'Correct offset amount recorded' );
1344 $THE_offset = $offsets->next;
1345 is( $THE_offset->type, 'APPLY', "APPLY offset added" );
1346 is( $THE_offset->amount * 1,
1347 -10, 'Correct amount was applied against debit' );
1349 $schema->storage->txn_rollback;