Files
firefly-iii/app/Console/Commands/Correction/EnableCurrencies.php

141 lines
5.1 KiB
PHP
Raw Normal View History

2019-03-23 08:10:59 +01:00
<?php
/**
* EnableCurrencies.php
2020-01-23 20:35:02 +01:00
* Copyright (c) 2020 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/>.
*/
2019-08-17 12:09:03 +02:00
declare(strict_types=1);
2019-03-23 08:10:59 +01:00
namespace FireflyIII\Console\Commands\Correction;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
2019-03-23 08:10:59 +01:00
use FireflyIII\Models\AccountMeta;
use FireflyIII\Models\BudgetLimit;
2023-12-16 15:56:35 +01:00
use FireflyIII\Models\GroupMembership;
2019-03-23 08:10:59 +01:00
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
2023-12-16 15:56:35 +01:00
use FireflyIII\Models\UserGroup;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
2019-03-23 08:10:59 +01:00
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
2023-12-16 15:56:35 +01:00
use Illuminate\Support\Facades\Log;
use Symfony\Component\Console\Command\Command as CommandAlias;
2019-03-23 08:10:59 +01:00
/**
* Class EnableCurrencies
*/
class EnableCurrencies extends Command
{
use ShowsFriendlyMessages;
2019-03-23 08:10:59 +01:00
protected $description = 'Enables all currencies in use.';
protected $signature = 'firefly-iii:enable-currencies';
2019-03-23 08:10:59 +01:00
/**
* Execute the console command.
*/
public function handle(): int
{
2023-12-16 15:56:35 +01:00
$userGroups = UserGroup::get();
foreach ($userGroups as $userGroup) {
$this->correctCurrencies($userGroup);
}
2023-12-20 19:35:52 +01:00
2023-12-16 15:56:35 +01:00
return CommandAlias::SUCCESS;
}
private function correctCurrencies(UserGroup $userGroup): void
{
/** @var CurrencyRepositoryInterface $repos */
$repos = app(CurrencyRepositoryInterface::class);
2023-12-16 16:06:23 +01:00
// first check if the user has any default currency (not necessarily the case, so can be forced).
$defaultCurrency = app('amount')->getDefaultCurrencyByUserGroup($userGroup);
2023-12-16 15:56:35 +01:00
Log::debug(sprintf('Now correcting currencies for user group #%d', $userGroup->id));
2023-12-16 16:06:23 +01:00
$found = [$defaultCurrency->id];
2023-12-20 19:35:52 +01:00
2019-03-23 08:10:59 +01:00
// get all meta entries
/** @var Collection $meta */
2023-12-20 19:35:52 +01:00
$meta = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id')
->where('accounts.user_group_id', $userGroup->id)
->where('account_meta.name', 'currency_id')->groupBy('data')->get(['data'])
;
2019-03-23 08:10:59 +01:00
foreach ($meta as $entry) {
2022-10-30 12:24:51 +01:00
$found[] = (int)$entry->data;
2019-03-23 08:10:59 +01:00
}
// get all from journals:
2023-12-20 19:35:52 +01:00
$journals = TransactionJournal::where('user_group_id', $userGroup->id)
->groupBy('transaction_currency_id')->get(['transaction_currency_id'])
;
2019-03-23 08:10:59 +01:00
foreach ($journals as $entry) {
2022-10-30 12:24:51 +01:00
$found[] = (int)$entry->transaction_currency_id;
2019-03-23 08:10:59 +01:00
}
// get all from transactions
2023-12-20 19:35:52 +01:00
$transactions = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.user_group_id', $userGroup->id)
->groupBy('transactions.transaction_currency_id', 'transactions.foreign_currency_id')
->get(['transactions.transaction_currency_id', 'transactions.foreign_currency_id'])
;
2019-03-23 08:10:59 +01:00
foreach ($transactions as $entry) {
2022-10-30 12:24:51 +01:00
$found[] = (int)$entry->transaction_currency_id;
$found[] = (int)$entry->foreign_currency_id;
2019-03-23 08:10:59 +01:00
}
// get all from budget limits
2023-12-20 19:35:52 +01:00
$limits = BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
->groupBy('transaction_currency_id')
->get(['budget_limits.transaction_currency_id'])
;
2019-03-23 08:10:59 +01:00
foreach ($limits as $entry) {
2023-11-05 19:41:37 +01:00
$found[] = $entry->transaction_currency_id;
2019-03-23 08:10:59 +01:00
}
2023-12-16 15:56:35 +01:00
$found = array_values(array_unique($found));
$found = array_values(
2021-03-12 06:30:40 +01:00
array_filter(
2022-10-30 12:23:16 +01:00
$found,
2023-11-04 14:18:49 +01:00
static function (int $currencyId) {
2023-12-20 19:35:52 +01:00
return 0 !== $currencyId;
2022-10-30 12:23:16 +01:00
}
2021-03-12 06:30:40 +01:00
)
);
2023-12-16 15:56:35 +01:00
$valid = new Collection();
2023-12-20 19:35:52 +01:00
2023-12-16 15:56:35 +01:00
/** @var int $currencyId */
foreach ($found as $currencyId) {
$currency = $repos->find($currencyId);
if (null !== $currency) {
$valid->push($currency);
}
2019-06-10 20:14:00 +02:00
}
2023-12-16 15:56:35 +01:00
$ids = $valid->pluck('id')->toArray();
2023-12-16 19:33:13 +01:00
Log::debug(sprintf('Found currencies for user group #%d: %s', $userGroup->id, implode(', ', $ids)));
2023-12-16 15:56:35 +01:00
$userGroup->currencies()->sync($ids);
2023-12-20 19:35:52 +01:00
2023-12-16 15:56:35 +01:00
/** @var GroupMembership $membership */
foreach ($userGroup->groupMemberships()->get() as $membership) {
// make sure no individual different preferences.
$membership->user->currencies()->sync([]);
2019-03-23 08:10:59 +01:00
}
}
}