From 9a71396e9af5110ccc0c45fd418dd45db0d9a754 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 22 Dec 2021 18:17:37 -0300 Subject: [PATCH] Bug 29759: Make cancelling an AR refund This patch makes the Koha::ArticleRequest->cancel method perform a refund if it applies. The sequence is: - Find refundable (potentially partial) payments against the fee - Generate a refund for the refundable amount - Reduce the fee to zero => RESULT: the patron owes nothing, any credits applied to the debit are refunded. 1. Apply the unit tests patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/ArticleRequest.t => FAIL: Nothing is refunded 3. Apply this patch 4. Repeat 2 => SUCCESS: Tests pass! Refunds take place! 5. Try on the UI => SUCCESS: All good 6. Sign off :-D --- Koha/ArticleRequest.pm | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Koha/ArticleRequest.pm b/Koha/ArticleRequest.pm index 09b19455ae..83c88bee8c 100644 --- a/Koha/ArticleRequest.pm +++ b/Koha/ArticleRequest.pm @@ -138,6 +138,34 @@ sub cancel { $self->notes($notes) if $notes; $self->store(); $self->notify(); + + my $debit = $self->debit; + + if ( $debit ) { + # fees found, refund + my $account = $self->borrower->account; + + my $total_reversible = $debit->debit_offsets->filter_by_reversible->total; + if ( $total_reversible ) { + + $account->add_credit( + { + amount => abs $total_reversible, + interface => C4::Context->interface, + type => 'REFUND', + } + ); + } + + if ( $debit->amountoutstanding ) { + $debit->reduce({ + reduction_type => 'REFUND', + amount => $debit->amountoutstanding, + interface => C4::Context->interface, + })->discard_changes; + } + } + return $self; } -- 2.32.0