Files
firefly-iii/app/Console/Commands/Upgrade/UpgradeCurrencyPreferences.php

152 lines
4.6 KiB
PHP
Raw Normal View History

<?php
2023-10-28 17:17:09 +02:00
/*
* UpgradeCurrencyPreferences.php
* Copyright (c) 2023 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);
namespace FireflyIII\Console\Commands\Upgrade;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use FireflyIII\Models\Preference;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\UserGroup;
use FireflyIII\User;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
/**
* Class UpgradeCurrencyPreferences
*/
class UpgradeCurrencyPreferences extends Command
{
use ShowsFriendlyMessages;
2023-12-02 12:56:48 +01:00
public const string CONFIG_NAME = '610_upgrade_currency_prefs';
2023-11-05 09:54:53 +01:00
protected $description = 'Upgrade user currency preferences';
2023-11-05 09:54:53 +01:00
protected $signature = 'firefly-iii:upgrade-currency-preferences {--F|force : Force the execution of this command.}';
/**
* Execute the console command.
*/
public function handle(): int
{
if ($this->isExecuted() && true !== $this->option('force')) {
$this->friendlyInfo('This command has already been executed.');
return 0;
}
$this->runUpgrade();
$this->friendlyPositive('Currency preferences migrated.');
2023-11-01 18:45:15 +01:00
$this->markAsExecuted();
return 0;
}
private function isExecuted(): bool
{
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
if (null !== $configVar) {
return (bool)$configVar->data;
}
return false;
}
private function runUpgrade(): void
{
$groups = UserGroup::get();
2023-12-20 19:35:52 +01:00
/** @var UserGroup $group */
foreach ($groups as $group) {
$this->upgradeGroupPreferences($group);
}
$users = User::get();
2023-12-20 19:35:52 +01:00
/** @var User $user */
foreach ($users as $user) {
$this->upgradeUserPreferences($user);
}
}
2023-12-21 04:59:23 +01:00
private function upgradeGroupPreferences(UserGroup $group): void
2023-10-28 15:03:33 +02:00
{
$currencies = TransactionCurrency::get();
$enabled = new Collection();
2023-12-20 19:35:52 +01:00
2023-10-28 15:03:33 +02:00
/** @var TransactionCurrency $currency */
foreach ($currencies as $currency) {
if ($currency->enabled) {
$enabled->push($currency);
}
}
$group->currencies()->sync($enabled->pluck('id')->toArray());
}
private function upgradeUserPreferences(User $user): void
{
$currencies = TransactionCurrency::get();
$enabled = new Collection();
2023-12-20 19:35:52 +01:00
/** @var TransactionCurrency $currency */
foreach ($currencies as $currency) {
if ($currency->enabled) {
$enabled->push($currency);
}
}
$user->currencies()->sync($enabled->pluck('id')->toArray());
// set the default currency for the user and for the group:
$preference = $this->getPreference($user);
$defaultCurrency = TransactionCurrency::where('code', $preference)->first();
2023-10-29 06:22:57 +01:00
if (null === $defaultCurrency) {
// get EUR
$defaultCurrency = TransactionCurrency::where('code', 'EUR')->first();
}
$user->currencies()->updateExistingPivot($defaultCurrency->id, ['user_default' => true]);
$user->userGroup->currencies()->updateExistingPivot($defaultCurrency->id, ['group_default' => true]);
}
2023-10-28 15:03:33 +02:00
private function getPreference(User $user): string
{
2023-10-28 15:03:33 +02:00
$preference = Preference::where('user_id', $user->id)->where('name', 'currencyPreference')->first(['id', 'user_id', 'name', 'data', 'updated_at', 'created_at']);
2023-11-26 16:20:24 +01:00
if (null === $preference) {
return 'EUR';
}
2023-11-26 12:10:42 +01:00
if (null !== $preference->data && !is_array($preference->data)) {
2023-10-28 15:03:33 +02:00
return (string)$preference->data;
}
2023-12-20 19:35:52 +01:00
2023-10-28 15:03:33 +02:00
return 'EUR';
}
private function markAsExecuted(): void
{
app('fireflyconfig')->set(self::CONFIG_NAME, true);
}
}