. */ declare(strict_types=1); namespace FireflyIII\Console\Commands\Upgrade; use FireflyIII\Console\Commands\ShowsFriendlyMessages; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Budget; use FireflyIII\Models\BudgetLimit; use FireflyIII\User; use Illuminate\Console\Command; class UpgradesBudgetLimits extends Command { use ShowsFriendlyMessages; public const string CONFIG_NAME = '480_bl_currency'; protected $description = 'Give budget limits a currency'; protected $signature = 'upgrade:480-budget-limit-currencies {--F|force : Force the execution of this command.}'; /** * Execute the console command. * * @throws FireflyException */ public function handle(): int { if ($this->isExecuted() && true !== $this->option('force')) { $this->friendlyInfo('This command has already been executed.'); return 0; } $count = 0; $budgetLimits = BudgetLimit::get(); /** @var BudgetLimit $budgetLimit */ foreach ($budgetLimits as $budgetLimit) { if (null === $budgetLimit->transaction_currency_id) { /** @var null|Budget $budget */ $budget = $budgetLimit->budget; if (null !== $budget) { /** @var null|User $user */ $user = $budget->user; if (null !== $user) { $currency = app('amount')->getNativeCurrencyByUserGroup($user->userGroup); $budgetLimit->transaction_currency_id = $currency->id; $budgetLimit->save(); $this->friendlyInfo( sprintf('Budget limit #%d (part of budget "%s") now has a currency setting (%s).', $budgetLimit->id, $budget->name, $currency->name) ); ++$count; } } } } $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 markAsExecuted(): void { app('fireflyconfig')->set(self::CONFIG_NAME, true); } }