From 2cea54fe2bd22328e8af7f77e7e74302cb560969 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 2 Apr 2019 10:08:40 +0100 Subject: [PATCH] Bug 22377: Add test case --- t/db_dependent/Circulation.t | 104 ++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t index b459ddadad..eeffd5158a 100755 --- a/t/db_dependent/Circulation.t +++ b/t/db_dependent/Circulation.t @@ -1983,7 +1983,7 @@ subtest 'AddReturn | is_overdue' => sub { subtest '_FixAccountForLostAndReturned' => sub { - plan tests => 5; + plan tests => 6; t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 ); t::lib::Mocks::mock_preference( 'WhenLostForgiveFine', 0 ); @@ -2367,6 +2367,108 @@ subtest '_FixAccountForLostAndReturned' => sub { my $manual_debit = Koha::Account::Lines->search({ borrowernumber => $patron->id, accounttype => 'OVERDUE', status => 'UNRETURNED' })->next; is( $manual_debit->amountoutstanding + 0, $manual_debit_amount - $payment_amount, 'reconcile_balance was called' ); }; + + subtest 'Test that refund is applied to the correct debt' => sub { + + plan tests => 13; + + my $patron1 = $builder->build_object( { class => 'Koha::Patrons' } ); + my $patron2 = $builder->build_object( { class => 'Koha::Patrons' } ); + my $manager = $builder->build_object({ class => "Koha::Patrons" }); + t::lib::Mocks::mock_userenv({ patron => $manager, branchcode => $manager->branchcode }); + + my $item = $builder->build_sample_item( + { + biblionumber => $biblio->biblionumber, + library => $library->branchcode, + replacementprice => 23.00, + replacementprice => $replacement_amount, + itype => $item_type->itemtype + } + ); + + # Add overdue fine for patron2 (order of these actions is meaningful) + my $account2 = $patron2->account; + my $fine2 = $account2->add_debit( + { + type => 'overdue', + amount => '4.00', + item_id => $item->itemnumber, + user_id => $manager->borrowernumber, + interface => 'commandline', + library_id => $manager->branchcode + } + )->status('RETURNED')->store; + + my $overdue_fee_lines = Koha::Account::Lines->search( + { borrowernumber => $patron2->id, itemnumber => $item->itemnumber, accounttype => 'OVERDUE' } ); + is( $overdue_fee_lines->count, 1, 'Only one overdue item fee produced for patron 2' ); + my $overdue_fee_line = $overdue_fee_lines->next; + is( $overdue_fee_line->amount + 0, '4', 'The right OVERDUE amount is generated for patron 2' ); + is( $overdue_fee_line->amountoutstanding + 0, + '4', 'The right OVERDUE amountountstanding is generated for patron 2' ); + + # Issue and mark as lost for patron1 (order of these actions is meaningful) + AddIssue( $patron1->unblessed, $item->barcode ); + ModItem( { itemlost => 3 }, $biblio->biblionumber, $item->itemnumber ); + LostItem( $item->itemnumber, 1 ); + + # Writeoff overdue fine for patron2 (order of these actions is meaningful) + my $writeoff2 = $account2->pay( + { + amount => '4.00', + library_id => $manager->branchcode, + lines => [$fine2], + type => 'writeoff' + } + ); + + my $processing_fee_lines = Koha::Account::Lines->search( + { borrowernumber => $patron1->id, itemnumber => $item->itemnumber, accounttype => 'PF' } ); + is( $processing_fee_lines->count, 1, 'Only one processing fee produced for patron 1' ); + my $processing_fee_line = $processing_fee_lines->next; + is( $processing_fee_line->amount + 0, + $processfee_amount, 'The right PF amount is generated for patron 1' ); + is( $processing_fee_line->amountoutstanding + 0, + $processfee_amount, 'The right PF amountoutstanding is generated for patron 1' ); + + my $lost_fee_lines = Koha::Account::Lines->search( + { borrowernumber => $patron1->id, itemnumber => $item->itemnumber, accounttype => 'LOST' } ); + is( $lost_fee_lines->count, 1, 'Only one lost item fee produced for patron 1' ); + my $lost_fee_line = $lost_fee_lines->next; + is( $lost_fee_line->amount + 0, $replacement_amount, 'The right LOST amount is generated for patron 1' ); + is( $lost_fee_line->amountoutstanding + 0, + $replacement_amount, 'The right LOST amountountstanding is generated for patron 1' ); + + my $overdue_writeoff_lines = Koha::Account::Lines->search( + { borrowernumber => $patron2->id, itemnumber => $item->itemnumber, accounttype => 'W' } ); + is( $overdue_writeoff_lines->count, 1, 'Only one overdue item writeoff produced for patron 2' ); + my $overdue_writeoff_line = $overdue_writeoff_lines->next; + is( $overdue_writeoff_line->amount + 0, '-4', 'The right W amount is generated for patron 2' ); + is( $overdue_writeoff_line->amountoutstanding + 0, + '0', 'The right W amountountstanding is generated for patron 2' ); + is( $overdue_fee_line->discard_changes->amountoutstanding + 0, 0, 'The OVERDUE fine amountoutstanding has been reduced to zero for patron 2'); + + is ( DateTime->compare($overdue_fee_line->date, $lost_fee_line->date), -1, 'The overdue fine for patron 2 was added before the lost fee for patron 1'); + is ( DateTime->compare($lost_fee_line->date, $overdue_writeoff_line->date), -1, 'The lost item fee for patron 1 was added before the overdue fine writeoff for patron 2'); + + my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item->itemnumber, undef ); + my $credit_return = Koha::Account::Lines->find($credit_return_id); + + is( $credit_return->accounttype, 'CR', 'An account line of type CR is added' ); + is( $credit_return->borrowernumber, $patron1->borrowernumber, 'The account line of type CR is associated with patron 1' ); + is( $credit_return->amount + 0, -99.00, 'The account line of type CR has an amount of -99' ); + is( $credit_return->amountoutstanding + 0, 0, 'The account line of type CR has an amountoutstanding of 0' ); + + $lost_fee_line->discard_changes; + is( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' ); + is( $lost_fee_line->accounttype, + 'LOST', 'Lost fee now still has account type of LOST' ); + is( $lost_fee_line->status, 'RETURNED', "Lost fee now has account status of RETURNED"); + + is( $patron1->account->balance, 20, 'The patron balance is 20, still owes the processing fee' ); + }; + }; subtest '_FixOverduesOnReturn' => sub { -- 2.20.1