From 596d57891256065b2f9ca88b7c5e051cc383075e 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 --- C4/Circulation.pm | 7 +++- t/db_dependent/Circulation.t | 80 +++++++++++++++++++++++++++++++++++- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index 0999a970fd..356487e9be 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -2390,6 +2390,7 @@ sub _FixAccountForLostAndReturned { my $borrowernumber = @_ ? shift : undef; my $item_id = @_ ? shift : $itemnumber; # Send the barcode if you want that logged in the description + my $account; my $credit; # check for charge made for lost book @@ -2423,7 +2424,7 @@ sub _FixAccountForLostAndReturned { : 0; if ( $total_to_refund > 0 ) { - my $account = Koha::Patrons->find( $accountline->borrowernumber )->account; + $account = Koha::Patrons->find( $accountline->borrowernumber )->account; $credit = $account->add_credit( { amount => $total_to_refund, @@ -2443,6 +2444,10 @@ sub _FixAccountForLostAndReturned { $accountline->amountoutstanding(0); $accountline->store(); + 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 93f62571c6..a3b5d097f3 100755 --- a/t/db_dependent/Circulation.t +++ b/t/db_dependent/Circulation.t @@ -25,6 +25,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; @@ -1992,7 +1993,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 ); @@ -2288,6 +2289,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.19.2