Optimize currency search.

This commit is contained in:
James Cole
2025-09-07 14:49:49 +02:00
parent cce5a73dd2
commit 4835b05304
30 changed files with 211 additions and 157 deletions

View File

@@ -25,9 +25,11 @@ declare(strict_types=1);
namespace FireflyIII\Console\Commands\Upgrade;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Preference;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\UserGroup;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\User;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
@@ -38,9 +40,9 @@ class UpgradesCurrencyPreferences extends Command
public const string CONFIG_NAME = '610_upgrade_currency_prefs';
protected $description = 'Upgrade user currency preferences';
protected $description = 'Upgrade user currency preferences';
protected $signature = 'upgrade:610-currency-preferences {--F|force : Force the execution of this command.}';
protected $signature = 'upgrade:610-currency-preferences {--F|force : Force the execution of this command.}';
/**
* Execute the console command.
@@ -65,7 +67,7 @@ class UpgradesCurrencyPreferences extends Command
{
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
if (null !== $configVar) {
return (bool) $configVar->data;
return (bool)$configVar->data;
}
return false;
@@ -80,7 +82,7 @@ class UpgradesCurrencyPreferences extends Command
$this->upgradeGroupPreferences($group);
}
$users = User::get();
$users = User::get();
/** @var User $user */
foreach ($users as $user) {
@@ -104,8 +106,8 @@ class UpgradesCurrencyPreferences extends Command
private function upgradeUserPreferences(User $user): void
{
$currencies = TransactionCurrency::get();
$enabled = new Collection();
$currencies = TransactionCurrency::get();
$enabled = new Collection();
/** @var TransactionCurrency $currency */
foreach ($currencies as $currency) {
@@ -116,10 +118,10 @@ class UpgradesCurrencyPreferences extends Command
$user->currencies()->sync($enabled->pluck('id')->toArray());
// set the default currency for the user and for the group:
$preference = $this->getPreference($user);
$primaryCurrency = TransactionCurrency::where('code', $preference)->first();
if (null === $primaryCurrency) {
// get EUR
$preference = $this->getPreference($user);
try {
$primaryCurrency = Amount::getTransactionCurrencyByCode($preference);
} catch (FireflyException) {
$primaryCurrency = TransactionCurrency::where('code', 'EUR')->first();
}
$user->currencies()->updateExistingPivot($primaryCurrency->id, ['user_default' => true]);
@@ -135,7 +137,7 @@ class UpgradesCurrencyPreferences extends Command
}
if (null !== $preference->data && !is_array($preference->data)) {
return (string) $preference->data;
return (string)$preference->data;
}
return 'EUR';