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

106 lines
3.3 KiB
PHP
Raw Normal View History

2019-03-20 18:31:00 +01:00
<?php
2019-03-20 18:31:00 +01:00
/**
* BudgetLimitCurrency.php
2020-01-23 20:35:02 +01:00
* Copyright (c) 2020 james@firefly-iii.org
2019-03-20 18:31:00 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2019-03-20 18:31:00 +01:00
*
* 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.
2019-03-20 18:31:00 +01:00
*
* This program is distributed in the hope that it will be useful,
2019-03-20 18:31:00 +01:00
* 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.
2019-03-20 18:31:00 +01:00
*
* 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-03-20 18:31:00 +01:00
*/
declare(strict_types=1);
namespace FireflyIII\Console\Commands\Upgrade;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
2021-09-18 10:20:19 +02:00
use FireflyIII\Exceptions\FireflyException;
2023-11-05 16:11:09 +01:00
use FireflyIII\Models\Budget;
2019-03-20 18:31:00 +01:00
use FireflyIII\Models\BudgetLimit;
2023-11-05 16:11:09 +01:00
use FireflyIII\User;
2019-03-20 18:31:00 +01:00
use Illuminate\Console\Command;
/**
* Class BudgetLimitCurrency
*/
class BudgetLimitCurrency extends Command
{
use ShowsFriendlyMessages;
2023-12-02 12:56:48 +01:00
public const string CONFIG_NAME = '480_bl_currency';
2023-11-05 09:54:53 +01:00
protected $description = 'Give budget limits a currency';
2023-11-05 09:54:53 +01:00
protected $signature = 'firefly-iii:bl-currency {--F|force : Force the execution of this command.}';
2019-03-20 18:31:00 +01:00
/**
* Execute the console command.
*
2021-09-18 10:20:19 +02:00
* @throws FireflyException
2019-03-20 18:31:00 +01:00
*/
public function handle(): int
{
if ($this->isExecuted() && true !== $this->option('force')) {
$this->friendlyInfo('This command has already been executed.');
2019-03-20 18:31:00 +01:00
return 0;
}
2021-04-07 07:28:43 +02:00
2019-03-23 18:58:06 +01:00
$count = 0;
2019-03-20 18:31:00 +01:00
$budgetLimits = BudgetLimit::get();
2023-12-20 19:35:52 +01:00
2019-03-20 18:31:00 +01:00
/** @var BudgetLimit $budgetLimit */
foreach ($budgetLimits as $budgetLimit) {
if (null === $budgetLimit->transaction_currency_id) {
2023-12-20 19:35:52 +01:00
/** @var null|Budget $budget */
2019-03-20 18:31:00 +01:00
$budget = $budgetLimit->budget;
if (null !== $budget) {
2023-12-20 19:35:52 +01:00
/** @var null|User $user */
2019-03-20 18:31:00 +01:00
$user = $budget->user;
if (null !== $user) {
2023-10-28 06:58:33 +02:00
$currency = app('amount')->getDefaultCurrencyByUserGroup($user->userGroup);
2019-03-20 18:31:00 +01:00
$budgetLimit->transaction_currency_id = $currency->id;
$budgetLimit->save();
$this->friendlyInfo(
2019-03-20 18:31:00 +01:00
sprintf('Budget limit #%d (part of budget "%s") now has a currency setting (%s).', $budgetLimit->id, $budget->name, $currency->name)
);
2023-12-20 19:35:52 +01:00
++$count;
2019-03-20 18:31:00 +01:00
}
}
}
}
2019-03-23 18:58:06 +01:00
if (0 === $count) {
$this->friendlyPositive('All budget limits are OK.');
2019-03-23 18:58:06 +01:00
}
2019-03-20 18:31:00 +01:00
$this->markAsExecuted();
2020-03-21 15:43:41 +01:00
2019-03-20 18:31:00 +01:00
return 0;
}
private function isExecuted(): bool
{
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
if (null !== $configVar) {
2022-10-30 12:24:51 +01:00
return (bool)$configVar->data;
2019-03-20 18:31:00 +01:00
}
return false;
2019-03-20 18:31:00 +01:00
}
private function markAsExecuted(): void
{
app('fireflyconfig')->set(self::CONFIG_NAME, true);
}
2019-08-17 12:09:03 +02:00
}