From 49c9e93292b2f694857edc66e913b9f65d081e8b Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 29 Nov 2018 16:46:36 -0300 Subject: [PATCH] Bug 21915: Reconcile balance on _FixAccountForLostAndReturned call This patch makes _FixAccountForLostAndReturned reconcile the patron's account balance, when the AccountAutoReconcile syspref is set. To test: - Apply this patch - Run: $ kshell k$ prove t/db_dependent/Circulation.t => SUCCESS: Tests pass, peace \o/ - Sign off :-D Sponsored-by: ByWater Solutions Signed-off-by: Kyle M Hall Signed-off-by: Martin Renvoize Signed-off-by: Nick Clemens --- C4/Circulation.pm | 4 ++ t/db_dependent/Circulation.t | 80 +++++++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index a544bc22c4..84565a316e 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -2448,6 +2448,10 @@ sub _FixAccountForLostAndReturned { ModItem( { paidfor => '' }, undef, $itemnumber, { log_action => 0 } ); + if ( defined $account and C4::Context->preference('AccountAutoReconcile') ) { + $account->reconcile_balance; + } + return ($credit) ? $credit->id : undef; } diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t index 00274ca486..aaf508391c 100755 --- a/t/db_dependent/Circulation.t +++ b/t/db_dependent/Circulation.t @@ -27,6 +27,7 @@ use POSIX qw( floor ); use t::lib::Mocks; use t::lib::TestBuilder; +use C4::Accounts; use C4::Calendar; use C4::Circulation; use C4::Biblio; @@ -1976,7 +1977,7 @@ subtest 'AddReturn | is_overdue' => sub { subtest '_FixAccountForLostAndReturned' => sub { - plan tests => 4; + plan tests => 5; t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 ); t::lib::Mocks::mock_preference( 'WhenLostForgiveFine', 0 ); @@ -2280,6 +2281,83 @@ subtest '_FixAccountForLostAndReturned' => sub { 'The patron balance is the difference between the PF and the credit' ); }; + + subtest 'Partial payement, existing debits and AccountAutoReconcile' => sub { + + plan tests => 8; + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + my $barcode = 'KD123456793'; + my $replacement_amount = 100; + my $processfee_amount = 20; + + my $item_type = $builder->build_object( + { class => 'Koha::ItemTypes', + value => { + notforloan => undef, + rentalcharge => 0, + defaultreplacecost => undef, + processfee => 0 + } + } + ); + my ( undef, undef, $item_id ) = AddItem( + { homebranch => $library->branchcode, + holdingbranch => $library->branchcode, + barcode => $barcode, + replacementprice => $replacement_amount, + itype => $item_type->itemtype + }, + $biblionumber + ); + + AddIssue( $patron->unblessed, $barcode ); + + # Simulate item marked as lost + ModItem( { itemlost => 1 }, $biblionumber, $item_id ); + LostItem( $item_id, 1 ); + + my $lost_fee_lines = Koha::Account::Lines->search( + { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'L' } ); + is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' ); + my $lost_fee_line = $lost_fee_lines->next; + is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' ); + is( $lost_fee_line->amountoutstanding + 0, + $replacement_amount, 'The right L amountountstanding is generated' ); + + my $account = $patron->account; + is( $account->balance, $replacement_amount, 'Balance is L' ); + + # Partially pay fee + my $payment_amount = 27; + my $payment = $account->add_credit( + { amount => $payment_amount, + type => 'payment' + } + ); + $payment->apply({ debits => $lost_fee_lines->reset, offset_type => 'Payment' }); + + is( $account->balance, + $replacement_amount - $payment_amount, + 'Payment applied' + ); + + # TODO use add_debit when time comes + my $manual_debit_amount = 80; + C4::Accounts::manualinvoice( $patron->id, undef, undef, 'FU', $manual_debit_amount ); + + is( $account->balance, $manual_debit_amount + $replacement_amount - $payment_amount, 'Manual debit applied' ); + + t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 1 ); + + my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item_id, $patron->id ); + my $credit_return = Koha::Account::Lines->find($credit_return_id); + + is( $account->balance, $manual_debit_amount - $payment_amount, 'Balance is PF - payment (CR)' ); + + my $manual_debit = Koha::Account::Lines->search({ borrowernumber => $patron->id, accounttype => 'FU' })->next; + is( $manual_debit->amountoutstanding + 0, $manual_debit_amount - $payment_amount, 'reconcile_balance was called' ); + }; }; subtest '_FixOverduesOnReturn' => sub { -- 2.39.5