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 => 13;
28 use C4::Circulation qw/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 $biblioitem = $builder->build( { source => 'Biblioitem' } );
77 my $patron = $builder->build( { source => 'Borrower' } );
78 my $item = Koha::Item->new(
80 biblionumber => $biblioitem->{biblionumber},
81 biblioitemnumber => $biblioitem->{biblioitemnumber},
82 homebranch => $library->{branchcode},
83 holdingbranch => $library->{branchcode},
84 barcode => 'some_barcode_12',
88 my $line = Koha::Account::Line->new(
90 borrowernumber => $patron->{borrowernumber},
91 itemnumber => $item->itemnumber,
92 debit_type_code => "OVERDUE",
95 interface => 'commandline',
98 my $account_line_item = $line->item;
99 is( ref( $account_line_item ), 'Koha::Item', 'Koha::Account::Line->item should return a Koha::Item' );
100 is( $line->itemnumber, $account_line_item->itemnumber, 'Koha::Account::Line->item should return the correct item' );
102 $line->itemnumber(undef)->store;
103 is( $line->item, undef, 'Koha::Account::Line->item should return undef if no item linked' );
105 $schema->storage->txn_rollback;
108 subtest 'library() tests' => sub {
112 $schema->storage->txn_begin;
114 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
115 my $patron = $builder->build( { source => 'Borrower' } );
117 my $line = Koha::Account::Line->new(
119 borrowernumber => $patron->{borrowernumber},
120 branchcode => $library->branchcode,
121 debit_type_code => "OVERDUE",
122 status => "RETURNED",
124 interface => 'commandline',
128 my $account_line_library = $line->library;
129 is( ref($account_line_library),
131 'Koha::Account::Line->library should return a Koha::Library' );
134 $account_line_library->branchcode,
135 'Koha::Account::Line->library should return the correct library'
138 # Test ON DELETE SET NULL
140 my $found = Koha::Account::Lines->find( $line->accountlines_id );
141 ok( $found, "Koha::Account::Line not deleted when the linked library is deleted" );
143 is( $found->library, undef,
144 'Koha::Account::Line->library should return undef if linked library has been deleted'
147 $schema->storage->txn_rollback;
150 subtest 'is_credit() and is_debit() tests' => sub {
154 $schema->storage->txn_begin;
156 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
157 my $account = $patron->account;
159 my $credit = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' });
161 ok( $credit->is_credit, 'is_credit detects credits' );
162 ok( !$credit->is_debit, 'is_debit detects credits' );
164 my $debit = Koha::Account::Line->new(
166 borrowernumber => $patron->id,
167 debit_type_code => "OVERDUE",
168 status => "RETURNED",
170 interface => 'commandline',
173 ok( !$debit->is_credit, 'is_credit detects debits' );
174 ok( $debit->is_debit, 'is_debit detects debits');
176 $schema->storage->txn_rollback;
179 subtest 'apply() tests' => sub {
183 $schema->storage->txn_begin;
185 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
186 my $account = $patron->account;
188 my $credit = $account->add_credit( { amount => 100, user_id => $patron->id, interface => 'commandline' } );
190 my $debit_1 = Koha::Account::Line->new(
191 { borrowernumber => $patron->id,
192 debit_type_code => "OVERDUE",
193 status => "RETURNED",
195 amountoutstanding => 10,
196 interface => 'commandline',
200 my $debit_2 = Koha::Account::Line->new(
201 { borrowernumber => $patron->id,
202 debit_type_code => "OVERDUE",
203 status => "RETURNED",
205 amountoutstanding => 100,
206 interface => 'commandline',
210 $credit->discard_changes;
211 $debit_1->discard_changes;
213 my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
214 my $remaining_credit = $credit->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
215 is( $remaining_credit * 1, 90, 'Remaining credit is correctly calculated' );
216 $credit->discard_changes;
217 is( $credit->amountoutstanding * -1, $remaining_credit, 'Remaining credit correctly stored' );
220 $debit_1->discard_changes;
221 is( $debit_1->amountoutstanding * 1, 0, 'Debit has been cancelled' );
223 my $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_1->id } );
224 is( $offsets->count, 1, 'Only one offset is generated' );
225 my $THE_offset = $offsets->next;
226 is( $THE_offset->amount * 1, -10, 'Amount was calculated correctly (less than the available credit)' );
227 is( $THE_offset->type, 'Manual Credit', 'Passed type stored correctly' );
229 $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
230 $remaining_credit = $credit->apply( { debits => [ $debits->as_list ] } );
231 is( $remaining_credit, 0, 'No remaining credit left' );
232 $credit->discard_changes;
233 is( $credit->amountoutstanding * 1, 0, 'No outstanding credit' );
234 $debit_2->discard_changes;
235 is( $debit_2->amountoutstanding * 1, 10, 'Outstanding amount decremented correctly' );
237 $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_2->id } );
238 is( $offsets->count, 1, 'Only one offset is generated' );
239 $THE_offset = $offsets->next;
240 is( $THE_offset->amount * 1, -90, 'Amount was calculated correctly (less than the available credit)' );
241 is( $THE_offset->type, 'Credit Applied', 'Defaults to \'Credit Applied\' offset type' );
243 $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
245 { $credit->apply({ debits => [ $debits->as_list ] }); }
246 'Koha::Exceptions::Account::NoAvailableCredit',
247 '->apply() can only be used with outstanding credits';
249 $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
251 { $debit_1->apply({ debits => [ $debits->as_list ] }); }
252 'Koha::Exceptions::Account::IsNotCredit',
253 '->apply() can only be used with credits';
255 $debits = Koha::Account::Lines->search({ accountlines_id => $credit->id });
256 my $credit_3 = $account->add_credit({ amount => 1, interface => 'commandline' });
258 { $credit_3->apply({ debits => [ $debits->as_list ] }); }
259 'Koha::Exceptions::Account::IsNotDebit',
260 '->apply() can only be applied to credits';
262 my $credit_2 = $account->add_credit({ amount => 20, interface => 'commandline' });
263 my $debit_3 = Koha::Account::Line->new(
264 { borrowernumber => $patron->id,
265 debit_type_code => "OVERDUE",
266 status => "RETURNED",
268 amountoutstanding => 100,
269 interface => 'commandline',
273 $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id, $credit->id ] } });
275 $credit_2->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } ); }
276 'Koha::Exceptions::Account::IsNotDebit',
277 '->apply() rolls back if any of the passed lines is not a debit';
279 is( $debit_1->discard_changes->amountoutstanding * 1, 0, 'No changes to already cancelled debit' );
280 is( $debit_2->discard_changes->amountoutstanding * 1, 10, 'Debit cancelled' );
281 is( $debit_3->discard_changes->amountoutstanding * 1, 100, 'Outstanding amount correctly calculated' );
282 is( $credit_2->discard_changes->amountoutstanding * -1, 20, 'No changes made' );
284 $debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id ] } });
285 $remaining_credit = $credit_2->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
287 is( $debit_1->discard_changes->amountoutstanding * 1, 0, 'No changes to already cancelled debit' );
288 is( $debit_2->discard_changes->amountoutstanding * 1, 0, 'Debit cancelled' );
289 is( $debit_3->discard_changes->amountoutstanding * 1, 90, 'Outstanding amount correctly calculated' );
290 is( $credit_2->discard_changes->amountoutstanding * 1, 0, 'No remaining credit' );
292 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
293 my $biblio = $builder->build_sample_biblio();
295 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
296 my $now = dt_from_string();
297 my $seven_weeks = DateTime::Duration->new(weeks => 7);
298 my $five_weeks = DateTime::Duration->new(weeks => 5);
299 my $seven_weeks_ago = $now - $seven_weeks;
300 my $five_weeks_ago = $now - $five_weeks;
302 my $checkout = Koha::Checkout->new(
304 borrowernumber => $patron->id,
305 itemnumber => $item->id,
306 date_due => $five_weeks_ago,
307 branchcode => $library->id,
308 issuedate => $seven_weeks_ago
312 my $accountline = Koha::Account::Line->new(
314 issue_id => $checkout->id,
315 borrowernumber => $patron->id,
316 itemnumber => $item->id,
317 branchcode => $library->id,
319 debit_type_code => 'OVERDUE',
320 status => 'UNRETURNED',
323 amountoutstanding => '1',
327 # Enable renewing upon fine payment
328 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 1 );
330 my $module = new Test::MockModule('C4::Circulation');
331 $module->mock('AddRenewal', sub { $called = 1; });
332 $module->mock('CanBookBeRenewed', sub { return 1; });
333 my $credit_forgive = $account->add_credit(
336 user_id => $patron->id,
341 my $debits_renew = Koha::Account::Lines->search({ accountlines_id => $accountline->id })->as_list;
342 $credit_forgive->apply( { debits => $debits_renew, offset_type => 'Forgiven' } );
343 is( $called, 0, 'C4::Circulation::AddRenew NOT called when RenewAccruingItemWhenPaid enabled but credit type is "FORGIVEN"' );
345 $accountline = Koha::Account::Line->new(
347 issue_id => $checkout->id,
348 borrowernumber => $patron->id,
349 itemnumber => $item->id,
350 branchcode => $library->id,
352 debit_type_code => 'OVERDUE',
353 status => 'UNRETURNED',
356 amountoutstanding => '1',
359 my $credit_renew = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' });
360 $debits_renew = Koha::Account::Lines->search({ accountlines_id => $accountline->id })->as_list;
361 $credit_renew->apply( { debits => $debits_renew, offset_type => 'Manual Credit' } );
362 is( $called, 1, 'RenewAccruingItemWhenPaid causes C4::Circulation::AddRenew to be called when appropriate' );
364 t::lib::Mocks::mock_preference( 'MarkLostItemsAsReturned', 'onpayment');
365 my $loser = $builder->build_object( { class => 'Koha::Patrons' } );
366 my $loser_account = $loser->account;
368 my $lost_item = $builder->build_sample_item();
369 my $lost_checkout = Koha::Checkout->new(
371 borrowernumber => $loser->id,
372 itemnumber => $lost_item->id,
373 date_due => $five_weeks_ago,
374 branchcode => $library->id,
375 issuedate => $seven_weeks_ago
379 $lost_item->itemlost(1)->store;
380 my $processing_fee = Koha::Account::Line->new(
382 issue_id => $lost_checkout->id,
383 borrowernumber => $loser->id,
384 itemnumber => $lost_item->id,
385 branchcode => $library->id,
387 debit_type_code => 'PROCESSING',
389 interface => 'intranet',
391 amountoutstanding => '15',
394 my $lost_fee = Koha::Account::Line->new(
396 issue_id => $lost_checkout->id,
397 borrowernumber => $loser->id,
398 itemnumber => $lost_item->id,
399 branchcode => $library->id,
401 debit_type_code => 'LOST',
403 interface => 'intranet',
405 amountoutstanding => '12.63',
408 my $pay_lost = $loser_account->add_credit({ amount => 27.630000, user_id => $loser->id, interface => 'intranet' });
409 my $pay_lines = [ $processing_fee, $lost_fee ];
410 $pay_lost->apply( { debits => $pay_lines, offset_type => 'Credit applied' } );
412 is( $loser->checkouts->next, undef, "Item has been returned");
414 $schema->storage->txn_rollback;
417 subtest 'Keep account info when related patron, staff, item or cash_register is deleted' => sub {
421 $schema->storage->txn_begin;
423 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
424 my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
425 my $item = $builder->build_object({ class => 'Koha::Items' });
426 my $issue = $builder->build_object(
428 class => 'Koha::Checkouts',
429 value => { itemnumber => $item->itemnumber }
432 my $register = $builder->build_object({ class => 'Koha::Cash::Registers' });
434 my $line = Koha::Account::Line->new(
436 borrowernumber => $patron->borrowernumber,
437 manager_id => $staff->borrowernumber,
438 itemnumber => $item->itemnumber,
439 debit_type_code => "OVERDUE",
440 status => "RETURNED",
442 interface => 'commandline',
443 register_id => $register->id
448 $line = $line->get_from_storage;
449 is( $line->itemnumber, undef, "The account line should not be deleted when the related item is delete");
452 $line = $line->get_from_storage;
453 is( $line->manager_id, undef, "The account line should not be deleted when the related staff is delete");
456 $line = $line->get_from_storage;
457 is( $line->borrowernumber, undef, "The account line should not be deleted when the related patron is delete");
460 $line = $line->get_from_storage;
461 is( $line->register_id, undef, "The account line should not be deleted when the related cash register is delete");
463 $schema->storage->txn_rollback;
466 subtest 'Renewal related tests' => sub {
470 $schema->storage->txn_begin;
472 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
473 my $staff = $builder->build_object( { class => 'Koha::Patrons' } );
474 my $item = $builder->build_object({ class => 'Koha::Items' });
475 my $issue = $builder->build_object(
477 class => 'Koha::Checkouts',
479 itemnumber => $item->itemnumber,
480 onsite_checkout => 0,
486 my $line = Koha::Account::Line->new(
488 borrowernumber => $patron->borrowernumber,
489 manager_id => $staff->borrowernumber,
490 itemnumber => $item->itemnumber,
491 debit_type_code => "OVERDUE",
492 status => "UNRETURNED",
493 amountoutstanding => 0,
494 interface => 'commandline',
497 is( $line->is_renewable, 1, "Item is returned as renewable when it meets the conditions" );
498 $line->amountoutstanding(5);
499 is( $line->is_renewable, 0, "Item is returned as unrenewable when it has outstanding fine" );
500 $line->amountoutstanding(0);
501 $line->debit_type_code("VOID");
502 is( $line->is_renewable, 0, "Item is returned as unrenewable when it has the wrong account type" );
503 $line->debit_type_code("OVERDUE");
504 $line->status("RETURNED");
505 is( $line->is_renewable, 0, "Item is returned as unrenewable when it has the wrong account status" );
508 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 0 );
509 is ($line->renew_item({ interface => 'intranet' }), undef, 'Attempt to renew fails when syspref is not set');
510 t::lib::Mocks::mock_preference( 'RenewAccruingItemWhenPaid', 1 );
511 t::lib::Mocks::mock_preference( 'RenewAccruingItemInOpac', 0 );
512 is ($line->renew_item({ interface => 'opac' }), undef, 'Attempt to renew fails when syspref is not set - OPAC');
513 t::lib::Mocks::mock_preference( 'RenewAccruingItemInOpac', 1 );
515 $line->renew_item({ interface => 'intranet' }),
517 itemnumber => $item->itemnumber,
521 'Attempt to renew fails when CanBookBeRenewed returns false'
524 $issue = $builder->build_object(
526 class => 'Koha::Checkouts',
528 itemnumber => $item->itemnumber,
529 onsite_checkout => 0,
536 my $module = new Test::MockModule('C4::Circulation');
537 $module->mock('AddRenewal', sub { $called = 1; });
538 $module->mock('CanBookBeRenewed', sub { return 1; });
540 is( $called, 1, 'Attempt to renew succeeds when conditions are met' );
542 $schema->storage->txn_rollback;
545 subtest 'adjust() tests' => sub {
549 $schema->storage->txn_begin;
551 # count logs before any actions
552 my $action_logs = $schema->resultset('ActionLog')->search()->count;
555 t::lib::Mocks::mock_preference( 'FinesLog', 0 );
557 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
558 my $account = $patron->account;
560 my $debit_1 = Koha::Account::Line->new(
561 { borrowernumber => $patron->id,
562 debit_type_code => "OVERDUE",
563 status => "RETURNED",
565 amountoutstanding => 10,
566 interface => 'commandline',
570 my $debit_2 = Koha::Account::Line->new(
571 { borrowernumber => $patron->id,
572 debit_type_code => "OVERDUE",
573 status => "UNRETURNED",
575 amountoutstanding => 100,
576 interface => 'commandline'
580 my $credit = $account->add_credit( { amount => 40, user_id => $patron->id, interface => 'commandline' } );
582 throws_ok { $debit_1->adjust( { amount => 50, type => 'bad', interface => 'commandline' } ) }
583 qr/Update type not recognised/, 'Exception thrown for unrecognised type';
585 throws_ok { $debit_1->adjust( { amount => 50, type => 'overdue_update', interface => 'commandline' } ) }
586 qr/Update type not allowed on this debit_type/,
587 'Exception thrown for type conflict';
589 # Increment an unpaid fine
590 $debit_2->adjust( { amount => 150, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
592 is( $debit_2->amount * 1, 150, 'Fine amount was updated in full' );
593 is( $debit_2->amountoutstanding * 1, 150, 'Fine amountoutstanding was update in full' );
594 isnt( $debit_2->date, undef, 'Date has been set' );
596 my $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
597 is( $offsets->count, 1, 'An offset is generated for the increment' );
598 my $THIS_offset = $offsets->next;
599 is( $THIS_offset->amount * 1, 50, 'Amount was calculated correctly (increment by 50)' );
600 is( $THIS_offset->type, 'OVERDUE_INCREASE', 'Adjust type stored correctly' );
602 is( $schema->resultset('ActionLog')->count(), $action_logs + 0, 'No log was added' );
604 # Update fine to partially paid
605 my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
606 $credit->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
608 $debit_2->discard_changes;
609 is( $debit_2->amount * 1, 150, 'Fine amount unaffected by partial payment' );
610 is( $debit_2->amountoutstanding * 1, 110, 'Fine amountoutstanding updated by partial payment' );
613 t::lib::Mocks::mock_preference( 'FinesLog', 1 );
615 # Increment the partially paid fine
616 $debit_2->adjust( { amount => 160, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
618 is( $debit_2->amount * 1, 160, 'Fine amount was updated in full' );
619 is( $debit_2->amountoutstanding * 1, 120, 'Fine amountoutstanding was updated by difference' );
621 $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
622 is( $offsets->count, 3, 'An offset is generated for the increment' );
623 $THIS_offset = $offsets->last;
624 is( $THIS_offset->amount * 1, 10, 'Amount was calculated correctly (increment by 10)' );
625 is( $THIS_offset->type, 'OVERDUE_INCREASE', 'Adjust type stored correctly' );
627 is( $schema->resultset('ActionLog')->count(), $action_logs + 1, 'Log was added' );
629 # Decrement the partially paid fine, less than what was paid
630 $debit_2->adjust( { amount => 50, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
632 is( $debit_2->amount * 1, 50, 'Fine amount was updated in full' );
633 is( $debit_2->amountoutstanding * 1, 10, 'Fine amountoutstanding was updated by difference' );
635 $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
636 is( $offsets->count, 4, 'An offset is generated for the decrement' );
637 $THIS_offset = $offsets->last;
638 is( $THIS_offset->amount * 1, -110, 'Amount was calculated correctly (decrement by 110)' );
639 is( $THIS_offset->type, 'OVERDUE_DECREASE', 'Adjust type stored correctly' );
641 # Decrement the partially paid fine, more than what was paid
642 $debit_2->adjust( { amount => 30, type => 'overdue_update', interface => 'commandline' } )->discard_changes;
643 is( $debit_2->amount * 1, 30, 'Fine amount was updated in full' );
644 is( $debit_2->amountoutstanding * 1, 0, 'Fine amountoutstanding was zeroed (payment was 40)' );
646 $offsets = Koha::Account::Offsets->search( { debit_id => $debit_2->id } );
647 is( $offsets->count, 5, 'An offset is generated for the decrement' );
648 $THIS_offset = $offsets->last;
649 is( $THIS_offset->amount * 1, -20, 'Amount was calculated correctly (decrement by 20)' );
650 is( $THIS_offset->type, 'OVERDUE_DECREASE', 'Adjust type stored correctly' );
652 my $overpayment_refund = $account->lines->last;
653 is( $overpayment_refund->amount * 1, -10, 'A new credit has been added' );
654 is( $overpayment_refund->description, 'Overpayment refund', 'Credit generated with the expected description' );
656 $schema->storage->txn_rollback;
659 subtest 'checkout() tests' => sub {
662 $schema->storage->txn_begin;
664 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
665 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
666 my $item = $builder->build_sample_item;
667 my $account = $patron->account;
669 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
670 my $checkout = AddIssue( $patron->unblessed, $item->barcode );
672 my $line = $account->add_debit({
674 interface => 'commandline',
675 item_id => $item->itemnumber,
676 issue_id => $checkout->issue_id,
678 status => 'UNRETURNED'
681 my $line_checkout = $line->checkout;
682 is( ref($line_checkout), 'Koha::Checkout', 'Result type is correct' );
683 is( $line_checkout->issue_id, $checkout->issue_id, 'Koha::Account::Line->checkout should return the correct checkout');
685 # Prevent re-calculation of fines at check-in for the test; Since bug 8338 the recalculation would result in a '0'
686 # fine which would subsequently be removed by _FixOverduesOnReturn
687 t::lib::Mocks::mock_preference( 'finesMode', 'off' );
689 my ( $returned, undef, $old_checkout) = C4::Circulation::AddReturn( $item->barcode, $library->branchcode );
690 is( $returned, 1, 'The item should have been returned' );
692 $line = $line->get_from_storage;
693 my $old_line_checkout = $line->checkout;
694 is( ref($old_line_checkout), 'Koha::Old::Checkout', 'Result type is correct' );
695 is( $old_line_checkout->issue_id, $old_checkout->issue_id, 'Koha::Account::Line->checkout should return the correct old_checkout' );
697 $line->issue_id(undef)->store;
698 is( $line->checkout, undef, 'Koha::Account::Line->checkout should return undef if no checkout linked' );
700 $schema->storage->txn_rollback;
703 subtest 'credits() and debits() tests' => sub {
706 $schema->storage->txn_begin;
708 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
709 my $account = $patron->account;
711 my $debit1 = $account->add_debit({
713 interface => 'commandline',
716 my $debit2 = $account->add_debit({
718 interface => 'commandline',
721 my $credit1 = $account->add_credit({
723 interface => 'commandline',
726 my $credit2 = $account->add_credit({
728 interface => 'commandline',
732 $credit1->apply({ debits => [ $debit1 ] });
733 $credit2->apply({ debits => [ $debit1, $debit2 ] });
735 my $credits = $debit1->credits;
736 is($credits->count, 2, '2 Credits applied to debit 1');
737 my $credit = $credits->next;
738 is($credit->amount + 0, -5, 'Correct first credit');
739 $credit = $credits->next;
740 is($credit->amount + 0, -10, 'Correct second credit');
742 $credits = $debit2->credits;
743 is($credits->count, 1, '1 Credits applied to debit 2');
744 $credit = $credits->next;
745 is($credit->amount + 0, -10, 'Correct first credit');
747 my $debits = $credit1->debits;
748 is($debits->count, 1, 'Credit 1 applied to 1 debit');
749 my $debit = $debits->next;
750 is($debit->amount + 0, 8, 'Correct first debit');
752 $debits = $credit2->debits;
753 is($debits->count, 2, 'Credit 2 applied to 2 debits');
754 $debit = $debits->next;
755 is($debit->amount + 0, 8, 'Correct first debit');
756 $debit = $debits->next;
757 is($debit->amount + 0, 12, 'Correct second debit');
759 $schema->storage->txn_rollback;
762 subtest "void() tests" => sub {
766 $schema->storage->txn_begin;
769 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
770 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
772 my $borrower = Koha::Patron->new( {
773 cardnumber => 'dariahall',
775 firstname => 'Daria',
777 $borrower->categorycode( $categorycode );
778 $borrower->branchcode( $branchcode );
781 my $account = Koha::Account->new({ patron_id => $borrower->id });
783 my $line1 = Koha::Account::Line->new(
785 borrowernumber => $borrower->borrowernumber,
787 amountoutstanding => 10,
788 interface => 'commandline',
789 debit_type_code => 'OVERDUE'
792 my $line2 = Koha::Account::Line->new(
794 borrowernumber => $borrower->borrowernumber,
796 amountoutstanding => 20,
797 interface => 'commandline',
798 debit_type_code => 'OVERDUE'
802 is( $account->balance(), 30, "Account balance is 30" );
803 is( $line1->amountoutstanding, 10, 'First fee has amount outstanding of 10' );
804 is( $line2->amountoutstanding, 20, 'Second fee has amount outstanding of 20' );
806 my $id = $account->pay(
808 lines => [$line1, $line2],
813 my $account_payment = Koha::Account::Lines->find( $id );
815 is( $account->balance(), 0, "Account balance is 0" );
817 $line1->_result->discard_changes();
818 $line2->_result->discard_changes();
819 is( $line1->amountoutstanding+0, 0, 'First fee has amount outstanding of 0' );
820 is( $line2->amountoutstanding+0, 0, 'Second fee has amount outstanding of 0' );
822 my $ret = $account_payment->void();
824 is( ref($ret), 'Koha::Account::Line', 'Void returns the account line' );
825 is( $account->balance(), 30, "Account balance is again 30" );
827 $account_payment->_result->discard_changes();
828 $line1->_result->discard_changes();
829 $line2->_result->discard_changes();
831 is( $account_payment->credit_type_code, 'PAYMENT', 'Voided payment credit_type_code is still PAYMENT' );
832 is( $account_payment->status, 'VOID', 'Voided payment status is VOID' );
833 is( $account_payment->amount+0, 0, 'Voided payment amount is 0' );
834 is( $account_payment->amountoutstanding+0, 0, 'Voided payment amount outstanding is 0' );
836 is( $line1->amountoutstanding+0, 10, 'First fee again has amount outstanding of 10' );
837 is( $line2->amountoutstanding+0, 20, 'Second fee again has amount outstanding of 20' );
839 # Accountlines that are not credits should be un-voidable
840 my $line1_pre = $line1->unblessed();
841 $ret = $line1->void();
842 $line1->_result->discard_changes();
843 my $line1_post = $line1->unblessed();
844 is( $ret, undef, 'Attempted void on non-credit returns undef' );
845 is_deeply( $line1_pre, $line1_post, 'Non-credit account line cannot be voided' );
847 $schema->storage->txn_rollback;
850 subtest "payout() tests" => sub {
854 $schema->storage->txn_begin;
858 $builder->build( { source => 'Category' } )->{categorycode};
859 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
861 my $borrower = Koha::Patron->new(
863 cardnumber => 'dariahall',
865 firstname => 'Daria',
868 $borrower->categorycode($categorycode);
869 $borrower->branchcode($branchcode);
872 my $staff = Koha::Patron->new(
874 cardnumber => 'bobby',
876 firstname => 'Bobby',
879 $staff->categorycode($categorycode);
880 $staff->branchcode($branchcode);
883 my $account = Koha::Account->new( { patron_id => $borrower->id } );
885 my $debit1 = Koha::Account::Line->new(
887 borrowernumber => $borrower->borrowernumber,
889 amountoutstanding => 10,
890 interface => 'commandline',
891 debit_type_code => 'OVERDUE'
894 my $credit1 = Koha::Account::Line->new(
896 borrowernumber => $borrower->borrowernumber,
898 amountoutstanding => -20,
899 interface => 'commandline',
900 credit_type_code => 'CREDIT'
904 is( $account->balance(), -10, "Account balance is -10" );
905 is( $debit1->amountoutstanding + 0,
906 10, 'Overdue fee has an amount outstanding of 10' );
907 is( $credit1->amountoutstanding + 0,
908 -20, 'Credit has an amount outstanding of -20' );
911 interface => 'intranet',
912 staff_id => $staff->borrowernumber,
913 branch => $branchcode,
914 payout_type => 'CASH',
918 throws_ok { $debit1->payout($pay_params); }
919 'Koha::Exceptions::Account::IsNotCredit',
920 '->payout() can only be used with credits';
923 ( 'interface', 'staff_id', 'branch', 'payout_type', 'amount' );
924 for my $required (@required) {
925 my $params = {%$pay_params};
926 delete( $params->{$required} );
928 $credit1->payout($params);
930 'Koha::Exceptions::MissingParameter',
931 "->payout() requires the `$required` parameter is passed";
937 interface => 'intranet',
938 staff_id => $staff->borrowernumber,
939 branch => $branchcode,
940 payout_type => 'CASH',
945 'Koha::Exceptions::ParameterTooHigh',
946 '->payout() cannot pay out more than the amountoutstanding';
948 t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 );
952 interface => 'intranet',
953 staff_id => $staff->borrowernumber,
954 branch => $branchcode,
955 payout_type => 'CASH',
960 'Koha::Exceptions::Account::RegisterRequired',
961 '->payout() requires a cash_register if payout_type is `CASH`';
963 t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 );
964 my $payout = $credit1->payout(
966 interface => 'intranet',
967 staff_id => $staff->borrowernumber,
968 branch => $branchcode,
969 payout_type => 'CASH',
974 is( ref($payout), 'Koha::Account::Line',
975 '->payout() returns a Koha::Account::Line' );
976 is( $payout->amount() + 0, 10, "Payout amount is 10" );
977 is( $payout->amountoutstanding() + 0, 0, "Payout amountoutstanding is 0" );
978 is( $account->balance() + 0, 0, "Account balance is 0" );
979 is( $debit1->amountoutstanding + 0,
980 10, 'Overdue fee still has an amount outstanding of 10' );
981 is( $credit1->amountoutstanding + 0,
982 -10, 'Credit has an new amount outstanding of -10' );
983 is( $credit1->status(), 'PAID', "Credit has a new status of PAID" );
985 $schema->storage->txn_rollback;
988 subtest "reduce() tests" => sub {
992 $schema->storage->txn_begin;
996 $builder->build( { source => 'Category' } )->{categorycode};
997 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
999 my $borrower = Koha::Patron->new(
1001 cardnumber => 'dariahall',
1003 firstname => 'Daria',
1006 $borrower->categorycode($categorycode);
1007 $borrower->branchcode($branchcode);
1010 my $staff = Koha::Patron->new(
1012 cardnumber => 'bobby',
1013 surname => 'Bloggs',
1014 firstname => 'Bobby',
1017 $staff->categorycode($categorycode);
1018 $staff->branchcode($branchcode);
1021 my $account = Koha::Account->new( { patron_id => $borrower->id } );
1023 my $debit1 = Koha::Account::Line->new(
1025 borrowernumber => $borrower->borrowernumber,
1027 amountoutstanding => 20,
1028 interface => 'commandline',
1029 debit_type_code => 'LOST'
1032 my $credit1 = Koha::Account::Line->new(
1034 borrowernumber => $borrower->borrowernumber,
1036 amountoutstanding => -20,
1037 interface => 'commandline',
1038 credit_type_code => 'CREDIT'
1042 is( $account->balance(), 0, "Account balance is 0" );
1043 is( $debit1->amountoutstanding,
1044 20, 'Overdue fee has an amount outstanding of 20' );
1045 is( $credit1->amountoutstanding,
1046 -20, 'Credit has an amount outstanding of -20' );
1048 my $reduce_params = {
1049 interface => 'commandline',
1050 reduction_type => 'REFUND',
1052 staff_id => $staff->borrowernumber,
1053 branch => $branchcode
1056 throws_ok { $credit1->reduce($reduce_params); }
1057 'Koha::Exceptions::Account::IsNotDebit',
1058 '->reduce() can only be used with debits';
1060 my @required = ( 'interface', 'reduction_type', 'amount' );
1061 for my $required (@required) {
1062 my $params = {%$reduce_params};
1063 delete( $params->{$required} );
1065 $debit1->reduce($params);
1067 'Koha::Exceptions::MissingParameter',
1068 "->reduce() requires the `$required` parameter is passed";
1071 $reduce_params->{interface} = 'intranet';
1072 my @dependant_required = ( 'staff_id', 'branch' );
1073 for my $d (@dependant_required) {
1074 my $params = {%$reduce_params};
1075 delete( $params->{$d} );
1077 $debit1->reduce($params);
1079 'Koha::Exceptions::MissingParameter',
1080 "->reduce() requires the `$d` parameter is passed when interface is intranet";
1086 interface => 'intranet',
1087 staff_id => $staff->borrowernumber,
1088 branch => $branchcode,
1089 reduction_type => 'REFUND',
1094 'Koha::Exceptions::ParameterTooHigh',
1095 '->reduce() cannot reduce more than original amount';
1098 # (Refund 5 on debt of 20)
1099 my $reduction = $debit1->reduce($reduce_params);
1101 is( ref($reduction), 'Koha::Account::Line',
1102 '->reduce() returns a Koha::Account::Line' );
1103 is( $reduction->amount() * 1, -5, "Reduce amount is -5" );
1104 is( $reduction->amountoutstanding() * 1,
1105 0, "Reduce amountoutstanding is 0" );
1106 is( $debit1->amountoutstanding() * 1,
1107 15, "Debit amountoutstanding reduced by 5 to 15" );
1108 is( $account->balance() * 1, -5, "Account balance is -5" );
1109 is( $reduction->status(), 'APPLIED', "Reduction status is 'APPLIED'" );
1111 my $offsets = Koha::Account::Offsets->search(
1112 { credit_id => $reduction->id, debit_id => $debit1->id } );
1113 is( $offsets->count, 1, 'Only one offset is generated' );
1114 my $THE_offset = $offsets->next;
1115 is( $THE_offset->amount * 1,
1116 -5, 'Correct amount was applied against debit' );
1117 is( $THE_offset->type, 'REFUND', "Offset type set to 'REFUND'" );
1119 # Zero offset created when zero outstanding
1120 # (Refund another 5 on paid debt of 20)
1121 $credit1->apply( { debits => [$debit1] } );
1122 is( $debit1->amountoutstanding + 0,
1123 0, 'Debit1 amountoutstanding reduced to 0' );
1124 $reduction = $debit1->reduce($reduce_params);
1125 is( $reduction->amount() * 1, -5, "Reduce amount is -5" );
1126 is( $reduction->amountoutstanding() * 1,
1127 -5, "Reduce amountoutstanding is -5" );
1129 $offsets = Koha::Account::Offsets->search(
1130 { credit_id => $reduction->id, debit_id => $debit1->id } );
1131 is( $offsets->count, 1, 'Only one new offset is generated' );
1132 $THE_offset = $offsets->next;
1133 is( $THE_offset->amount * 1,
1134 0, 'Zero offset created for already paid off debit' );
1135 is( $THE_offset->type, 'REFUND', "Offset type set to 'REFUND'" );
1137 # Compound reduction should not allow more than original amount
1138 # (Reduction of 5 + 5 + 20 > 20)
1139 $reduce_params->{amount} = 20;
1141 $debit1->reduce($reduce_params);
1143 'Koha::Exceptions::ParameterTooHigh',
1144 '->reduce cannot reduce more than the original amount (combined reductions test)';
1146 # Throw exception if attempting to reduce a payout
1147 my $payout = $reduction->payout(
1149 interface => 'intranet',
1150 staff_id => $staff->borrowernumber,
1151 branch => $branchcode,
1152 payout_type => 'CASH',
1157 $payout->reduce($reduce_params);
1159 'Koha::Exceptions::Account::IsNotDebit',
1160 '->reduce() cannot be used on a payout debit';
1162 $schema->storage->txn_rollback;