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

112 lines
3.7 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;
2018-10-17 05:04:26 +02:00
use FireflyIII\Models\PiggyBank;
2018-08-11 19:21:58 +02:00
use FireflyIII\Models\RecurrenceTransaction;
2018-02-21 18:42:15 +01:00
use FireflyIII\Models\Transaction;
2018-03-02 16:31:02 +01:00
use FireflyIII\Models\TransactionJournal;
2018-08-11 19:21:58 +02:00
use Illuminate\Database\Eloquent\Builder;
2018-02-21 18:42:15 +01:00
use Log;
/**
* Class AccountDestroyService
*/
class AccountDestroyService
{
/**
* Constructor.
*/
public function __construct()
{
if ('testing' === env('APP_ENV')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
}
}
2018-02-21 18:42:15 +01:00
/**
* @param Account $account
* @param Account|null $moveTo
*
2018-03-25 09:01:43 +02:00
* @return void
2018-07-28 06:27:30 +02:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2018-02-21 18:42:15 +01:00
*/
2018-03-25 09:01:43 +02:00
public function destroy(Account $account, ?Account $moveTo): void
2018-02-21 18:42:15 +01:00
{
2018-08-11 19:21:58 +02:00
2018-02-21 18:42:15 +01:00
if (null !== $moveTo) {
DB::table('transactions')->where('account_id', $account->id)->update(['account_id' => $moveTo->id]);
2018-08-11 19:21:58 +02:00
// also update recurring transactions:
2018-08-17 06:45:57 +02:00
DB::table('recurrences_transactions')->where('source_id', $account->id)->update(['source_id' => $moveTo->id]);
DB::table('recurrences_transactions')->where('destination_id', $account->id)->update(['destination_id' => $moveTo->id]);
2018-02-21 18:42:15 +01:00
}
2018-07-28 06:27:30 +02:00
$service = app(JournalDestroyService::class);
2018-02-21 18:42:15 +01:00
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 */
2018-07-28 06:27:30 +02:00
2018-03-02 16:31:02 +01:00
$service->destroy($journal);
2018-02-21 18:42:15 +01:00
}
}
2018-08-11 19:21:58 +02:00
// delete recurring transactions with this account:
if (null === $moveTo) {
$recurrences = RecurrenceTransaction::
where(
function (Builder $q) use ($account) {
$q->where('source_id', $account->id);
$q->orWhere('destination_id', $account->id);
}
)->get(['recurrence_id'])->pluck('recurrence_id')->toArray();
$destroyService = new RecurrenceDestroyService();
foreach ($recurrences as $recurrenceId) {
$destroyService->destroyById((int)$recurrenceId);
}
}
2018-10-17 05:04:26 +02:00
// delete piggy banks:
PiggyBank::where('account_id', $account->id)->delete();
2018-02-21 18:42:15 +01:00
try {
$account->delete();
2018-03-02 16:31:02 +01:00
} catch (Exception $e) { // @codeCoverageIgnore
2018-03-10 22:38:20 +01:00
Log::error(sprintf('Could not delete account: %s', $e->getMessage())); // @codeCoverageIgnore
2018-02-21 18:42:15 +01:00
}
}
2018-03-05 19:35:58 +01:00
}