From de94af8b5aa88ad574072ae13d11883852312438 Mon Sep 17 00:00:00 2001 From: Emmi Takkinen Date: Wed, 10 Nov 2021 11:45:00 +0200 Subject: [PATCH] Bug 16223: Call DelDebarmentsAfterPayment from Koha::Account::Line::apply Calling function from Koha::Account::pay missed several payment cases. Moving it to Koha::Account::Line::apply should cover all payments. Also reworked tests no just to test DelDebarmentsAfterPayment but to test if debartments are lifted after pay. To test repeat test plan from previous patches. Also prove t/db_dependent/Patron/Borrower_Debartments.t Sponsored-by: Koha-Suomi Oy --- Koha/Account.pm | 2 - Koha/Account/Line.pm | 2 + t/db_dependent/Patron/Borrower_Debarments.t | 56 +++++++++++++++++---- 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/Koha/Account.pm b/Koha/Account.pm index 36f5b93298..50454b21db 100644 --- a/Koha/Account.pm +++ b/Koha/Account.pm @@ -102,8 +102,6 @@ sub pay { } ); - Koha::Patron::Debarments::DelDebarmentsAfterPayment({ borrowernumber => $self->{patron_id} }); - # NOTE: Pay historically always applied as much credit as it could to all # existing outstanding debits, whether passed specific debits or otherwise. if ( $payment->amountoutstanding ) { diff --git a/Koha/Account/Line.pm b/Koha/Account/Line.pm index 9b402a1b39..85dc393158 100644 --- a/Koha/Account/Line.pm +++ b/Koha/Account/Line.pm @@ -701,6 +701,8 @@ sub apply { } }); + Koha::Patron::Debarments::DelDebarmentsAfterPayment({ borrowernumber => $self->borrowernumber }); + return $self; } diff --git a/t/db_dependent/Patron/Borrower_Debarments.t b/t/db_dependent/Patron/Borrower_Debarments.t index 74616a19f1..b3427e42d0 100755 --- a/t/db_dependent/Patron/Borrower_Debarments.t +++ b/t/db_dependent/Patron/Borrower_Debarments.t @@ -197,22 +197,58 @@ is( Koha::Patrons->find($borrowernumber3)->debarredcomment, # Test removing debartments after payment my $debarmentsRulesPref = C4::Context->preference("DebarmentsToLiftAfterPayment"); -C4::Context->set_preference("DebarmentsToLiftAfterPayment", "Test debarment:\n outstanding: 0\nTest debarment 2:"); +C4::Context->set_preference("DebarmentsToLiftAfterPayment", "Test debarment:\n outstanding: 0\nTest debarment 2:\n outstanding: 2"); -AddDebarment({ - borrowernumber => $borrowernumber, +my $register = $builder->build_object( { class => 'Koha::Cash::Registers' } ); +my $patron2 = $builder->build_object( { class => 'Koha::Patrons' } ); +my $account = $patron2->account; +my $fine = $account->add_debit( + { + amount => '5.00', + type => 'OVERDUE', + interface => 'cron' + } +); + +Koha::Patron::Debarments::AddDebarment({ + borrowernumber => $patron2->borrowernumber, comment => 'Test debarment', }); -AddDebarment({ - borrowernumber => $borrowernumber, +Koha::Patron::Debarments::AddDebarment({ + borrowernumber => $patron2->borrowernumber, comment => 'Test debarment 2', }); -$debarments = GetDebarments({ borrowernumber => $borrowernumber }); +$debarments = Koha::Patron::Debarments::GetDebarments({ borrowernumber => $patron2->borrowernumber }); is( @$debarments, 2, "GetDebarments returns 2 debarments before payment" ); -DelDebarmentsAfterPayment({ borrowernumber => $borrowernumber }); -C4::Context->set_preference("DebarmentsToLiftAfterPayment", $debarmentsRulesPref); +my $payment1 = $account->pay( + { + cash_register => $register->id, + amount => '4.00', + credit_type => 'PAYMENT', + lines => [$fine] + } +); + +$debarments = Koha::Patron::Debarments::GetDebarments({ borrowernumber => $patron2->borrowernumber }); +is( @$debarments, 1, "GetDebarments returns 1 debarment after first payment" ); +my $debarment_comment; +foreach my $debarment (@$debarments){ + $debarment_comment = $debarment->{comment}; +} +is( $debarment_comment, "Test debarment", "Returned debartments comment is 'Test debarment'" ); + +my $payment2 = $account->pay( + { + cash_register => $register->id, + amount => '1.00', + credit_type => 'PAYMENT', + lines => [$fine] + } +); + +$debarments = Koha::Patron::Debarments::GetDebarments({ borrowernumber => $patron2->borrowernumber }); +is( @$debarments, 0, "GetDebarments returns 0 debarments after second payment" ); -$debarments = GetDebarments({ borrowernumber => $borrowernumber }); -is( @$debarments, 0, "GetDebarments returns 0 debarments after payment" ); \ No newline at end of file +=cut \ No newline at end of file -- 2.25.1