2021-04-22 18:33:53 +02:00
|
|
|
<?php
|
|
|
|
|
2021-04-23 18:48:15 +02:00
|
|
|
/*
|
|
|
|
* FixFrontpageAccounts.php
|
|
|
|
* Copyright (c) 2021 james@firefly-iii.org
|
|
|
|
*
|
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-04-22 18:33:53 +02:00
|
|
|
namespace FireflyIII\Console\Commands\Correction;
|
|
|
|
|
2023-06-20 07:16:56 +02:00
|
|
|
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
|
2021-04-22 18:33:53 +02:00
|
|
|
use FireflyIII\Models\AccountType;
|
|
|
|
use FireflyIII\Models\Preference;
|
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
|
|
|
use FireflyIII\User;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class FixFrontpageAccounts
|
|
|
|
*/
|
|
|
|
class FixFrontpageAccounts extends Command
|
|
|
|
{
|
2023-06-20 07:16:56 +02:00
|
|
|
use ShowsFriendlyMessages;
|
|
|
|
|
2021-04-22 18:33:53 +02:00
|
|
|
protected $description = 'Fixes a preference that may include deleted accounts or accounts of another type.';
|
2023-06-02 07:36:17 +02:00
|
|
|
protected $signature = 'firefly-iii:fix-frontpage-accounts';
|
2021-04-22 18:33:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*/
|
|
|
|
public function handle(): int
|
|
|
|
{
|
|
|
|
$users = User::get();
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2021-04-22 18:33:53 +02:00
|
|
|
/** @var User $user */
|
|
|
|
foreach ($users as $user) {
|
2023-10-29 06:22:57 +01:00
|
|
|
$preference = app('preferences')->getForUser($user, 'frontPageAccounts');
|
2021-04-22 18:33:53 +02:00
|
|
|
if (null !== $preference) {
|
|
|
|
$this->fixPreference($preference);
|
|
|
|
}
|
|
|
|
}
|
2023-06-20 07:16:56 +02:00
|
|
|
$this->friendlyPositive('Account preferences are OK');
|
2021-04-22 18:33:53 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function fixPreference(Preference $preference): void
|
|
|
|
{
|
2024-01-01 14:43:56 +01:00
|
|
|
$fixed = [];
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2021-04-22 18:33:53 +02:00
|
|
|
/** @var AccountRepositoryInterface $repository */
|
|
|
|
$repository = app(AccountRepositoryInterface::class);
|
|
|
|
if (null === $preference->user) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$repository->setUser($preference->user);
|
2024-01-01 14:43:56 +01:00
|
|
|
$data = $preference->data;
|
2021-04-22 18:33:53 +02:00
|
|
|
if (is_array($data)) {
|
|
|
|
/** @var string $accountId */
|
|
|
|
foreach ($data as $accountId) {
|
2022-10-30 12:24:51 +01:00
|
|
|
$accountIdInt = (int)$accountId;
|
2021-06-30 06:17:38 +02:00
|
|
|
$account = $repository->find($accountIdInt);
|
2021-05-24 08:06:56 +02:00
|
|
|
if (null !== $account
|
2022-04-13 16:23:02 +02:00
|
|
|
&& in_array($account->accountType->type, [AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE], true)
|
2021-05-24 08:06:56 +02:00
|
|
|
&& true === $account->active) {
|
|
|
|
$fixed[] = $account->id;
|
2021-04-22 18:33:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-29 12:10:03 +01:00
|
|
|
app('preferences')->setForUser($preference->user, 'frontPageAccounts', $fixed);
|
2021-04-22 18:33:53 +02:00
|
|
|
}
|
|
|
|
}
|