Files
firefly-iii/app/Services/Internal/Destroy/AccountDestroyService.php

72 lines
2.3 KiB
PHP
Raw Normal View History

2018-02-21 18:42:15 +01:00
<?php
/**
* AccountDestroyService.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Services\Internal\Destroy;
use DB;
2018-03-02 16:31:02 +01:00
use Exception;
2018-02-21 18:42:15 +01:00
use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
2018-03-02 16:31:02 +01:00
use FireflyIII\Models\TransactionJournal;
2018-02-21 18:42:15 +01:00
use Log;
/**
* Class AccountDestroyService
*/
class AccountDestroyService
{
/**
* @param Account $account
* @param Account|null $moveTo
*
* @return bool
*/
public function destroy(Account $account, ?Account $moveTo): bool
{
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);
2018-03-02 16:31:02 +01:00
/** @var TransactionJournal $journal */
2018-02-21 18:42:15 +01:00
$journal = $transaction->transactionJournal()->first();
if (null !== $journal) {
Log::debug('Call for deletion of journal #' . $journal->id);
2018-03-02 16:31:02 +01:00
/** @var JournalDestroyService $service */
$service = app(JournalDestroyService::class);
$service->destroy($journal);
2018-02-21 18:42:15 +01:00
}
}
try {
$account->delete();
2018-03-02 16:31:02 +01:00
} catch (Exception $e) { // @codeCoverageIgnore
Log::error(sprintf('Could not delete account: %s',$e->getMessage())); // @codeCoverageIgnore
2018-02-21 18:42:15 +01:00
}
return true;
}
}