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 => 14;
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 'item() tests' => sub {
73 $schema->storage->txn_begin;
75 my $library = $builder->build( { source => 'Branch' } );
76 my $patron = $builder->build( { source => 'Borrower' } );
77 my $item = $builder->build_sample_item(
79 library => $library->{branchcode},
80 barcode => 'some_barcode_12',
85 my $line = Koha::Account::Line->new(
87 borrowernumber => $patron->{borrowernumber},
88 itemnumber => $item->itemnumber,
89 debit_type_code => "OVERDUE",
92 interface => 'commandline',
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' );
99 $line->itemnumber(undef)->store;
100 is( $line->item, undef, 'Koha::Account::Line->item should return undef if no item linked' );
102 $schema->storage->txn_rollback;
105 subtest 'library() tests' => sub {
109 $schema->storage->txn_begin;
111 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
112 my $patron = $builder->build( { source => 'Borrower' } );
114 my $line = Koha::Account::Line->new(
116 borrowernumber => $patron->{borrowernumber},
117 branchcode => $library->branchcode,
118 debit_type_code => "OVERDUE",
119 status => "RETURNED",
121 interface => 'commandline',
125 my $account_line_library = $line->library;
126 is( ref($account_line_library),
128 'Koha::Account::Line->library should return a Koha::Library' );
131 $account_line_library->branchcode,
132 'Koha::Account::Line->library should return the correct library'
135 # Test ON DELETE SET NULL
137 my $found = Koha::Account::Lines->find( $line->accountlines_id );
138 ok( $found, "Koha::Account::Line not deleted when the linked library is deleted" );
140 is( $found->library, undef,
141 'Koha::Account::Line->library should return undef if linked library has been deleted'
144 $schema->storage->txn_rollback;
147 subtest 'is_credit() and is_debit() tests' => sub {
151 $schema->storage->txn_begin;
153 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
154 my $account = $patron->account;
156 my $credit = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' });
158 ok( $credit->is_credit, 'is_credit detects credits' );
159 ok( !$credit->is_debit, 'is_debit detects credits' );
161 my $debit = Koha::Account::Line->new(
163 borrowernumber => $patron->id,
164 debit_type_code => "OVERDUE",
165 status => "RETURNED",
167 interface => 'commandline',
170 ok( !$debit->is_credit, 'is_credit detects debits' );
171 ok( $debit->is_debit, 'is_debit detects debits');
173 $schema->storage->txn_rollback;
176 subtest 'apply() tests' => sub {
180 $schema->storage->txn_begin;
182 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
183 my $account = $patron->account;
185 my $credit = $account->add_credit( { amount => 100, user_id => $patron->id, interface => 'commandline' } );
187 my $debit_1 = Koha::Account::Line->new(
188 { borrowernumber => $patron->id,
189 debit_type_code => "OVERDUE",
190 status => "RETURNED",
192 amountoutstanding => 10,
193 interface => 'commandline',
197 my $debit_2 = Koha::Account::Line->new(
198 { borrowernumber => $patron->id,
199 debit_type_code => "OVERDUE",
200 status => "RETURNED",
202 amountoutstanding => 100,
203 interface => 'commandline',
207 $credit->discard_changes;
208 $debit_1->discard_changes;
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' );
216 $debit_1->discard_changes;
217 is( $debit_1->amountoutstanding * 1, 0, 'Debit has been cancelled' );
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' );
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' );
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' );
237 $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
239 { $credit->apply({ debits => [ $debits->as_list ] }); }
240 'Koha::Exceptions::Account::NoAvailableCredit',
241 '->apply() can only be used with outstanding credits';
243 $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
245 { $debit_1->apply({ debits => [ $debits->as_list ] }); }
246 'Koha::Exceptions::Account::IsNotCredit',
247 '->apply() can only be used with credits';
249 $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
250 my $credit_3 = $account->add_credit({ amount => 1, interface => 'commandline' });
252 { $credit_3->apply({ debits => [ $debits->as_list ] }); }
253 'Koha::Exceptions::Account::IsNotDebit',
254 '->apply() can only be applied to credits';
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",
262 amountoutstanding => 100,
263 interface => 'commandline',
267 $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id, $credit->id ] } });
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';
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' );
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' } );
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' );
286 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
287 my $biblio = $builder->build_sample_biblio();
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;
296 my $checkout = Koha::Checkout->new(
298 borrowernumber => $patron->id,
299 itemnumber => $item->id,
300 date_due => $five_weeks_ago,
301 branchcode => $library->id,
302 issuedate => $seven_weeks_ago
306 my $accountline = Koha::Account::Line->new(
308 issue_id => $checkout->id,
309 borrowernumber => $patron->id,
310 itemnumber => $item->id,
311 branchcode => $library->id,
313 debit_type_code => 'OVERDUE',
314 status => 'UNRETURNED',
317 amountoutstanding => '1',
321 # Enable renewing upon fine payment
322 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 1 );
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(
330 user_id => $patron->id,
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"' );
339 $accountline = Koha::Account::Line->new(
341 issue_id => $checkout->id,
342 borrowernumber => $patron->id,
343 itemnumber => $item->id,
344 branchcode => $library->id,
346 debit_type_code => 'OVERDUE',
347 status => 'UNRETURNED',
350 amountoutstanding => '1',
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' );
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" );
365 t::lib::Mocks::mock_preference( 'MarkLostItemsAsReturned', 'onpayment');
366 my $loser = $builder->build_object( { class => 'Koha::Patrons' } );
367 my $loser_account = $loser->account;
369 my $lost_item = $builder->build_sample_item();
370 my $lost_checkout = Koha::Checkout->new(
372 borrowernumber => $loser->id,
373 itemnumber => $lost_item->id,
374 date_due => $five_weeks_ago,
375 branchcode => $library->id,
376 issuedate => $seven_weeks_ago
380 $lost_item->itemlost(1)->store;
381 my $processing_fee = Koha::Account::Line->new(
383 issue_id => $lost_checkout->id,
384 borrowernumber => $loser->id,
385 itemnumber => $lost_item->id,
386 branchcode => $library->id,
388 debit_type_code => 'PROCESSING',
390 interface => 'intranet',
392 amountoutstanding => '15',
395 my $lost_fee = Koha::Account::Line->new(
397 issue_id => $lost_checkout->id,
398 borrowernumber => $loser->id,
399 itemnumber => $lost_item->id,
400 branchcode => $library->id,
402 debit_type_code => 'LOST',
404 interface => 'intranet',
406 amountoutstanding => '12.63',
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' } );
413 is( $loser->checkouts->next, undef, "Item has been returned");
417 $schema->storage->txn_rollback;
420 subtest 'Keep account info when related patron, staff, item or cash_register is deleted' => sub {
424 $schema->storage->txn_begin;
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(
431 class => 'Koha::Checkouts',
432 value => { itemnumber => $item->itemnumber }
435 my $register = $builder->build_object({ class => 'Koha::Cash::Registers' });
437 my $line = Koha::Account::Line->new(
439 borrowernumber => $patron->borrowernumber,
440 manager_id => $staff->borrowernumber,
441 itemnumber => $item->itemnumber,
442 debit_type_code => "OVERDUE",
443 status => "RETURNED",
445 interface => 'commandline',
446 register_id => $register->id
451 $line = $line->get_from_storage;
452 is( $line->itemnumber, undef, "The account line should not be deleted when the related item is 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");
459 $line = $line->get_from_storage;
460 is( $line->borrowernumber, undef, "The account line should not be deleted when the related patron is 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");
466 $schema->storage->txn_rollback;
469 subtest 'Renewal related tests' => sub {
473 $schema->storage->txn_begin;
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(
480 class => 'Koha::Checkouts',
482 itemnumber => $item->itemnumber,
483 onsite_checkout => 0,
489 my $line = Koha::Account::Line->new(
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',
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" );
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 );
518 $line->renew_item({ interface => 'intranet' }),
520 itemnumber => $item->itemnumber,
524 'Attempt to renew fails when CanBookBeRenewed returns false'
527 $issue = $builder->build_object(
529 class => 'Koha::Checkouts',
531 itemnumber => $item->itemnumber,
532 onsite_checkout => 0,
539 my $module = Test::MockModule->new('C4::Circulation');
540 $module->mock('AddRenewal', sub { $called = 1; });
541 $module->mock('CanBookBeRenewed', sub { return 1; });
543 is( $called, 1, 'Attempt to renew succeeds when conditions are met' );
545 $schema->storage->txn_rollback;
548 subtest 'adjust() tests' => sub {
552 $schema->storage->txn_begin;
554 # count logs before any actions
555 my $action_logs = $schema->resultset('ActionLog')->search()->count;
558 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
560 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
561 my $account = $patron->account;
563 my $debit_1 = Koha::Account::Line->new(
564 { borrowernumber => $patron->id,
565 debit_type_code => "OVERDUE",
566 status => "RETURNED",
568 amountoutstanding => 10,
569 interface => 'commandline',
573 my $debit_2 = Koha::Account::Line->new(
574 { borrowernumber => $patron->id,
575 debit_type_code => "OVERDUE",
576 status => "UNRETURNED",
578 amountoutstanding => 100,
579 interface => 'commandline'
583 my $credit = $account->add_credit( { amount => 40, user_id => $patron->id, interface => 'commandline' } );
585 throws_ok { $debit_1->adjust( { amount => 50, type => 'bad', interface => 'commandline' } ) }
586 qr/Update type not recognised/, 'Exception thrown for unrecognised type';
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';
592 # Increment an unpaid fine
593 $debit_2->adjust( { amount => 150, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
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' );
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' );
605 is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
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' } );
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' );
616 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
618 # Increment the partially paid fine
619 $debit_2->adjust( { amount => 160, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
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' );
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' );
630 is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
632 # Decrement the partially paid fine, less than what was paid
633 $debit_2->adjust( { amount => 50, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
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' );
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' );
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)' );
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' );
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' );
659 $schema->storage->txn_rollback;
662 subtest 'checkout() tests' => sub {
665 $schema->storage->txn_begin;
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;
672 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
673 my $checkout = AddIssue( $patron->unblessed, $item->barcode );
675 my $line = $account->add_debit({
677 interface => 'commandline',
678 item_id => $item->itemnumber,
679 issue_id => $checkout->issue_id,
681 status => 'UNRETURNED'
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');
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' );
692 my ( $returned, undef, $old_checkout) = C4::Circulation::AddReturn( $item->barcode, $library->branchcode );
693 is( $returned, 1, 'The item should have been returned' );
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' );
700 $line->issue_id(undef)->store;
701 is( $line->checkout, undef, 'Koha::Account::Line->checkout should return undef if no checkout linked' );
703 $schema->storage->txn_rollback;
706 subtest 'credits() and debits() tests' => sub {
709 $schema->storage->txn_begin;
711 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
712 my $account = $patron->account;
714 my $debit1 = $account->add_debit({
716 interface => 'commandline',
719 my $debit2 = $account->add_debit({
721 interface => 'commandline',
724 my $credit1 = $account->add_credit({
726 interface => 'commandline',
729 my $credit2 = $account->add_credit({
731 interface => 'commandline',
735 $credit1->apply({ debits => [ $debit1 ] });
736 $credit2->apply({ debits => [ $debit1, $debit2 ] });
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');
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');
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');
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');
764 'Koha::Exceptions::Account::IsNotCredit',
765 'Exception is thrown when requesting debits linked to debit';
768 { $credit1->credits; }
769 'Koha::Exceptions::Account::IsNotDebit',
770 'Exception is thrown when requesting credits linked to credit';
773 $schema->storage->txn_rollback;
776 subtest "void() tests" => sub {
780 $schema->storage->txn_begin;
783 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
784 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
786 my $borrower = Koha::Patron->new( {
787 cardnumber => 'dariahall',
789 firstname => 'Daria',
791 $borrower->categorycode( $categorycode );
792 $borrower->branchcode( $branchcode );
795 my $account = Koha::Account->new({ patron_id => $borrower->id });
797 my $line1 = Koha::Account::Line->new(
799 borrowernumber => $borrower->borrowernumber,
801 amountoutstanding => 10,
802 interface => 'commandline',
803 debit_type_code => 'OVERDUE'
806 my $line2 = Koha::Account::Line->new(
808 borrowernumber => $borrower->borrowernumber,
810 amountoutstanding => 20,
811 interface => 'commandline',
812 debit_type_code => 'OVERDUE'
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' );
820 my $id = $account->pay(
822 lines => [$line1, $line2],
827 my $account_payment = Koha::Account::Lines->find( $id );
829 is( $account->balance(), 0, "Account balance is 0" );
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' );
837 $line1->void( { interface => 'test' } );
839 'Koha::Exceptions::Account::IsNotCredit',
840 '->void() can only be used with credits';
843 $account_payment->void();
845 'Koha::Exceptions::MissingParameter',
846 "->void() requires the `interface` parameter is passed";
849 $account_payment->void( { interface => 'intranet' } );
851 'Koha::Exceptions::MissingParameter',
852 "->void() requires the `staff_id` parameter is passed when `interface` equals 'intranet'";
854 $account_payment->void( { interface => 'intranet', staff_id => $borrower->borrowernumber } );
856 'Koha::Exceptions::MissingParameter',
857 "->void() requires the `branch` parameter is passed when `interface` equals 'intranet'";
859 my $void = $account_payment->void({ interface => 'test' });
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" );
867 $account_payment->_result->discard_changes();
868 $line1->_result->discard_changes();
869 $line2->_result->discard_changes();
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' );
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' );
879 my $credit2 = $account->add_credit( { interface => 'test', amount => 10 } );
880 $void = $credit2->void(
882 interface => 'intranet',
883 staff_id => $borrower->borrowernumber,
884 branch => $branchcode
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");
890 $schema->storage->txn_rollback;
893 subtest "payout() tests" => sub {
897 $schema->storage->txn_begin;
901 $builder->build( { source => 'Category' } )->{categorycode};
902 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
904 my $borrower = Koha::Patron->new(
906 cardnumber => 'dariahall',
908 firstname => 'Daria',
911 $borrower->categorycode($categorycode);
912 $borrower->branchcode($branchcode);
915 my $staff = Koha::Patron->new(
917 cardnumber => 'bobby',
919 firstname => 'Bobby',
922 $staff->categorycode($categorycode);
923 $staff->branchcode($branchcode);
926 my $account = Koha::Account->new( { patron_id => $borrower->id } );
928 my $debit1 = Koha::Account::Line->new(
930 borrowernumber => $borrower->borrowernumber,
932 amountoutstanding => 10,
933 interface => 'commandline',
934 debit_type_code => 'OVERDUE'
937 my $credit1 = Koha::Account::Line->new(
939 borrowernumber => $borrower->borrowernumber,
941 amountoutstanding => -20,
942 interface => 'commandline',
943 credit_type_code => 'CREDIT'
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' );
954 interface => 'intranet',
955 staff_id => $staff->borrowernumber,
956 branch => $branchcode,
957 payout_type => 'CASH',
961 throws_ok { $debit1->payout($pay_params); }
962 'Koha::Exceptions::Account::IsNotCredit',
963 '->payout() can only be used with credits';
966 ( 'interface', 'staff_id', 'branch', 'payout_type', 'amount' );
967 for my $required (@required) {
968 my $params = {%$pay_params};
969 delete( $params->{$required} );
971 $credit1->payout($params);
973 'Koha::Exceptions::MissingParameter',
974 "->payout() requires the `$required` parameter is passed";
980 interface => 'intranet',
981 staff_id => $staff->borrowernumber,
982 branch => $branchcode,
983 payout_type => 'CASH',
988 'Koha::Exceptions::ParameterTooHigh',
989 '->payout() cannot pay out more than the amountoutstanding';
991 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
995 interface => 'intranet',
996 staff_id => $staff->borrowernumber,
997 branch => $branchcode,
998 payout_type => 'CASH',
1003 'Koha::Exceptions::Account::RegisterRequired',
1004 '->payout() requires a cash_register if payout_type is `CASH`';
1006 t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
1007 my $payout = $credit1->payout(
1009 interface => 'intranet',
1010 staff_id => $staff->borrowernumber,
1011 branch => $branchcode,
1012 payout_type => 'CASH',
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" );
1028 $schema->storage->txn_rollback;
1031 subtest "reduce() tests" => sub {
1035 $schema->storage->txn_begin;
1039 $builder->build( { source => 'Category' } )->{categorycode};
1040 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
1042 my $borrower = Koha::Patron->new(
1044 cardnumber => 'dariahall',
1046 firstname => 'Daria',
1049 $borrower->categorycode($categorycode);
1050 $borrower->branchcode($branchcode);
1053 my $staff = Koha::Patron->new(
1055 cardnumber => 'bobby',
1056 surname => 'Bloggs',
1057 firstname => 'Bobby',
1060 $staff->categorycode($categorycode);
1061 $staff->branchcode($branchcode);
1064 my $account = Koha::Account->new( { patron_id => $borrower->id } );
1066 my $debit1 = Koha::Account::Line->new(
1068 borrowernumber => $borrower->borrowernumber,
1070 amountoutstanding => 20,
1071 interface => 'commandline',
1072 debit_type_code => 'LOST'
1075 my $credit1 = Koha::Account::Line->new(
1077 borrowernumber => $borrower->borrowernumber,
1079 amountoutstanding => -20,
1080 interface => 'commandline',
1081 credit_type_code => 'CREDIT'
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' );
1091 my $reduce_params = {
1092 interface => 'commandline',
1093 reduction_type => 'DISCOUNT',
1095 staff_id => $staff->borrowernumber,
1096 branch => $branchcode
1099 throws_ok { $credit1->reduce($reduce_params); }
1100 'Koha::Exceptions::Account::IsNotDebit',
1101 '->reduce() can only be used with debits';
1103 my @required = ( 'interface', 'reduction_type', 'amount' );
1104 for my $required (@required) {
1105 my $params = {%$reduce_params};
1106 delete( $params->{$required} );
1108 $debit1->reduce($params);
1110 'Koha::Exceptions::MissingParameter',
1111 "->reduce() requires the `$required` parameter is passed";
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} );
1120 $debit1->reduce($params);
1122 'Koha::Exceptions::MissingParameter',
1123 "->reduce() requires the `$d` parameter is passed when interface is intranet";
1129 interface => 'intranet',
1130 staff_id => $staff->borrowernumber,
1131 branch => $branchcode,
1132 reduction_type => 'REFUND',
1137 'Koha::Exceptions::ParameterTooHigh',
1138 '->reduce() cannot reduce more than original amount';
1141 # (Discount 5 on debt of 20)
1142 my $reduction = $debit1->reduce($reduce_params);
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'" );
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'" );
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");
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'" );
1183 # Compound reduction should not allow more than original amount
1184 # (Reduction of 5 + 5 + 20 > 20)
1185 $reduce_params->{amount} = 20;
1187 $debit1->reduce($reduce_params);
1189 'Koha::Exceptions::ParameterTooHigh',
1190 '->reduce cannot reduce more than the original amount (combined reductions test)';
1192 # Throw exception if attempting to reduce a payout
1193 my $payout = $reduction->payout(
1195 interface => 'intranet',
1196 staff_id => $staff->borrowernumber,
1197 branch => $branchcode,
1198 payout_type => 'CASH',
1203 $payout->reduce($reduce_params);
1205 'Koha::Exceptions::Account::IsNotDebit',
1206 '->reduce() cannot be used on a payout debit';
1208 $schema->storage->txn_rollback;
1211 subtest "cancel() tests" => sub {
1214 $schema->storage->txn_begin;
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 } });
1220 t::lib::Mocks::mock_userenv({ patron => $patron });
1222 my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
1224 my $debit1 = Koha::Account::Line->new(
1226 borrowernumber => $patron->borrowernumber,
1228 amountoutstanding => 10,
1229 interface => 'commandline',
1230 debit_type_code => 'OVERDUE',
1233 my $debit2 = Koha::Account::Line->new(
1235 borrowernumber => $patron->borrowernumber,
1237 amountoutstanding => 20,
1238 interface => 'commandline',
1239 debit_type_code => 'OVERDUE',
1243 my $ret = $account->pay(
1249 my $credit = Koha::Account::Lines->find({ accountlines_id => $ret->{payment_id} });
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' );
1258 { staff_id => $staff->borrowernumber, branch => $library->branchcode } );
1260 'Koha::Exceptions::Account::IsNotDebit',
1261 '->cancel() can only be used with debits';
1264 $debit1->reduce( { staff_id => $staff->borrowernumber } );
1266 'Koha::Exceptions::MissingParameter',
1267 "->cancel() requires the `branch` parameter is passed";
1269 $debit1->reduce( { branch => $library->branchcode } );
1271 'Koha::Exceptions::MissingParameter',
1272 "->cancel() requires the `staff_id` parameter is passed";
1276 { staff_id => $staff->borrowernumber, branch => $library->branchcode } );
1278 'Koha::Exceptions::Account',
1279 '->cancel() can only be used with debits that have not been offset';
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' );
1286 $cancellation->amount() * 1,
1287 $debit1->amount * -1,
1288 "Cancellation amount is " . $debit1->amount
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" );
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'" );
1306 $schema->storage->txn_rollback;