From 696035229137ffb139446ba99f8ad41dab94a199 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 25 Apr 2019 12:35:24 +0100 Subject: [PATCH] Bug 22200: (follow-up) Wrap accountline creation in a transaction Signed-off-by: Martin Renvoize Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi Signed-off-by: Nick Clemens --- C4/Circulation.pm | 72 ++++++++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index 51dde19697..94e9dfa708 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -2338,45 +2338,53 @@ sub _FixOverduesOnReturn { return; } - # check for overdue fine - my $accountlines = Koha::Account::Lines->search( - { - borrowernumber => $borrowernumber, - itemnumber => $item, - accounttype => 'OVERDUE', - status => 'UNRETURNED' - } - ); - return 0 unless $accountlines->count; # no warning, there's just nothing to fix + my $schema = Koha::Database->schema; - my $accountline = $accountlines->next; - if ($exemptfine) { - my $amountoutstanding = $accountline->amountoutstanding; + my $result = $schema->txn_do( + sub { + # check for overdue fine + my $accountlines = Koha::Account::Lines->search( + { + borrowernumber => $borrowernumber, + itemnumber => $item, + accounttype => 'OVERDUE', + status => 'UNRETURNED' + } + ); + return 0 unless $accountlines->count; # no warning, there's just nothing to fix - my $account = Koha::Account->new({patron_id => $borrowernumber}); - my $credit = $account->add_credit( - { - amount => $amountoutstanding, - user_id => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef, - library_id => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef, - interface => C4::Context->interface, - type => 'forgiven', - item_id => $item - } - ); + my $accountline = $accountlines->next; + if ($exemptfine) { + my $amountoutstanding = $accountline->amountoutstanding; - $credit->apply({ debits => $accountlines->reset, offset_type => 'Forgiven' }); + my $account = Koha::Account->new({patron_id => $borrowernumber}); + my $credit = $account->add_credit( + { + amount => $amountoutstanding, + user_id => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef, + library_id => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef, + interface => C4::Context->interface, + type => 'forgiven', + item_id => $item + } + ); - $accountline->status('FORGIVEN'); + $credit->apply({ debits => $accountlines->reset, offset_type => 'Forgiven' }); + + $accountline->status('FORGIVEN'); + + if (C4::Context->preference("FinesLog")) { + &logaction("FINES", 'MODIFY',$borrowernumber,"Overdue forgiven: item $item"); + } + } else { + $accountline->status('RETURNED'); + } - if (C4::Context->preference("FinesLog")) { - &logaction("FINES", 'MODIFY',$borrowernumber,"Overdue forgiven: item $item"); + return $accountline->store(); } - } else { - $accountline->status('RETURNED'); - } + ); - return $accountline->store(); + return $result; } =head2 _FixAccountForLostAndReturned -- 2.39.5