From 7e31929ccac0929fee93a01dda956307b0c380e2 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 --- C4/Circulation.pm | 78 ++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 35 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index f66554d70f..52c0781257 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -2328,45 +2328,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 $accountline = $accountlines->next; - if ($exemptfine) { - my $amountoutstanding = $accountline->amountoutstanding; + my $schema = Koha::Database->schema; - 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 $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 $accountline = $accountlines->next; + if ($exemptfine) { + my $amountoutstanding = $accountline->amountoutstanding; + + 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 + } + ); + + $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'); } - ); - - $credit->apply({ debits => $accountlines->reset, offset_type => 'Forgiven' }); - - $accountline->status('FORGIVEN'); - - 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.20.1