Bug 22200: (follow-up) Wrap accountline creation in a transaction
authorMartin Renvoize <martin.renvoize@ptfs-europe.com>
Thu, 25 Apr 2019 11:35:24 +0000 (12:35 +0100)
committerNick Clemens <nick@bywatersolutions.com>
Fri, 26 Apr 2019 10:28:48 +0000 (10:28 +0000)
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

C4/Circulation.pm

index 51dde19..94e9dfa 100644 (file)
@@ -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