. */ declare(strict_types=1); namespace FireflyIII\Services\Internal\Destroy; use DB; use Exception; use FireflyIII\Models\Account; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use Log; /** * Class AccountDestroyService */ class AccountDestroyService { /** * @param Account $account * @param Account|null $moveTo * * @return void */ public function destroy(Account $account, ?Account $moveTo): void { if (null !== $moveTo) { DB::table('transactions')->where('account_id', $account->id)->update(['account_id' => $moveTo->id]); } Log::debug('Now trigger account delete response #' . $account->id); /** @var Transaction $transaction */ foreach ($account->transactions()->get() as $transaction) { Log::debug('Now at transaction #' . $transaction->id); /** @var TransactionJournal $journal */ $journal = $transaction->transactionJournal()->first(); if (null !== $journal) { Log::debug('Call for deletion of journal #' . $journal->id); /** @var JournalDestroyService $service */ $service = app(JournalDestroyService::class); $service->destroy($journal); } } try { $account->delete(); } catch (Exception $e) { // @codeCoverageIgnore Log::error(sprintf('Could not delete account: %s', $e->getMessage())); // @codeCoverageIgnore } } }