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

100 lines
3.4 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;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
/**
* 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.
*
* @return int
2019-03-23 08:10:59 +01:00
*/
public function handle(): int
{
$found = [];
// get all meta entries
/** @var Collection $meta */
$meta = AccountMeta::where('name', 'currency_id')->groupBy('data')->get(['data']);
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:
2020-10-23 18:15:37 +02:00
$journals = TransactionJournal::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
2021-03-12 06:30:40 +01:00
$transactions = Transaction::groupBy('transaction_currency_id', 'foreign_currency_id')->get(['transaction_currency_id', '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
$limits = BudgetLimit::groupBy('transaction_currency_id')->get(['transaction_currency_id']);
foreach ($limits as $entry) {
2022-10-30 12:24:51 +01:00
$found[] = (int)$entry->transaction_currency_id;
2019-03-23 08:10:59 +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,
function (int $currencyId) {
return $currencyId !== 0;
}
2021-03-12 06:30:40 +01:00
)
);
2019-03-23 08:10:59 +01:00
$disabled = TransactionCurrency::whereIn('id', $found)->where('enabled', false)->count();
if ($disabled > 0) {
$this->friendlyInfo(sprintf('%d currencies were (was) disabled while in use by transactions. This has been corrected.', $disabled));
2019-06-10 20:14:00 +02:00
}
if (0 === $disabled) {
$this->friendlyPositive('All currencies are correctly enabled or disabled.');
2019-03-23 08:10:59 +01:00
}
TransactionCurrency::whereIn('id', $found)->update(['enabled' => true]);
return 0;
}
}