Details
-
Type:
Improvement
-
Status: Closed
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: None
-
Fix Version/s: 5.5.0
-
Component/s: None
-
Labels:None
Description
Database transactions are not nestable (mostly) and thus should be avoided when possible. We can remove one from Transactions::applyFromCredits() by using Transactions::unapply()
Change app/models/transactions.php around line 875 from
// Begin a transaction
$this->begin();
// Apply all credits
foreach ($apply_amounts as $transaction_id => $trans_amounts) {
$this->apply($transaction_id, ['amounts' => $trans_amounts]);
if (($errors = $this->errors())) {
// Roll back
$this->rollBack();
return;
}
}
// Commit transaction
$this->commit();
To
// Apply all credits $last_transaction_id = null; $errors = null; foreach ($apply_amounts as $transaction_id => $trans_amounts) { $this->apply($transaction_id, ['amounts' => $trans_amounts]); if (($errors = $this->errors())) { break; } $last_transaction_id = $transaction_id; } if ($errors && $last_transaction_id) { foreach ($apply_amounts as $transaction_id => $trans_amounts) { $invoice_ids = array_map(function ($value) { return $value['invoice_id']; }, $trans_amounts); $this->unapply($transaction_id, $invoice_ids); if ($last_transaction_id == $transaction_id) { break; } } }