Skip to content

Firefly III: a personal finances manager

Fix default currencies.

James Cole2f110ab9a889
diff --git a/app/Console/Commands/Correction/EnableCurrencies.php b/app/Console/Commands/Correction/EnableCurrencies.php
index 1a48199667..b94927f048 100644
--- a/app/Console/Commands/Correction/EnableCurrencies.php
+++ b/app/Console/Commands/Correction/EnableCurrencies.php
@@ -31,7 +31,6 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\UserGroup;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
-use FireflyIII\User;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
@@ -72,8 +71,11 @@ class EnableCurrencies extends Command
$repos = app(CurrencyRepositoryInterface::class);
$repos->setUserGroup($userGroup);
+ // first check if the user has any default currency (not necessarily the case, so can be forced).
+ $defaultCurrency = app('amount')->getDefaultCurrencyByUserGroup($userGroup);
+
Log::debug(sprintf('Now correcting currencies for user group #%d', $userGroup->id));
- $found = [];
+ $found = [$defaultCurrency->id];
// get all meta entries
/** @var Collection $meta */
$meta = AccountMeta
@@ -122,7 +124,6 @@ class EnableCurrencies extends Command
)
);
-
$valid = new Collection();
/** @var int $currencyId */
foreach ($found as $currencyId) {
@@ -133,7 +134,6 @@ class EnableCurrencies extends Command
}
$ids = $valid->pluck('id')->toArray();
Log::debug(sprintf('Found currencies for user group #%d: %s', $userGroup->id, join(', ', $ids)));
-
$userGroup->currencies()->sync($ids);
/** @var GroupMembership $membership */
foreach ($userGroup->groupMemberships()->get() as $membership) {
diff --git a/app/Http/Controllers/TransactionCurrency/IndexController.php b/app/Http/Controllers/TransactionCurrency/IndexController.php
index 1aa032d016..f73d6b7304 100644
--- a/app/Http/Controllers/TransactionCurrency/IndexController.php
+++ b/app/Http/Controllers/TransactionCurrency/IndexController.php
@@ -85,8 +85,8 @@ class IndexController extends Controller
// order so default is on top:
$collection = $collection->sortBy(
static function (TransactionCurrency $currency) {
- $default = true === $currency->userDefault ? 0 : 1;
- $enabled = true === $currency->userEnabled ? 0 : 1;
+ $default = true === $currency->userGroupDefault ? 0 : 1;
+ $enabled = true === $currency->userGroupEnabled ? 0 : 1;
return sprintf('%s-%s-%s', $default, $enabled, $currency->code);
}
);
diff --git a/app/Models/TransactionCurrency.php b/app/Models/TransactionCurrency.php
index ca84cce988..f9f80553e3 100644
--- a/app/Models/TransactionCurrency.php
+++ b/app/Models/TransactionCurrency.php
@@ -44,8 +44,8 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @property Carbon|null $updated_at
* @property Carbon|null $deleted_at
* @property bool $enabled
- * @property bool|null $userDefault
- * @property bool|null $userEnabled
+ * @property bool|null $userGroupDefault
+ * @property bool|null $userGroupEnabled
* @property string $code
* @property string $name
* @property string $symbol
@@ -82,9 +82,9 @@ class TransactionCurrency extends Model
use ReturnsIntegerIdTrait;
use SoftDeletes;
- public ?bool $userDefault;
- public ?bool $userEnabled;
- protected $casts
+ public ?bool $userGroupDefault;
+ public ?bool $userGroupEnabled;
+ protected $casts
= [
'created_at' => 'datetime',
'updated_at' => 'datetime',
@@ -123,10 +123,10 @@ class TransactionCurrency extends Model
*/
public function refreshForUser(User $user)
{
- $current = $user->userGroup->currencies()->where('transaction_currencies.id', $this->id)->first();
- $default = app('amount')->getDefaultCurrencyByUserGroup($user->userGroup);
- $this->userDefault = $default->id === $this->id;
- $this->userEnabled = null !== $current;
+ $current = $user->userGroup->currencies()->where('transaction_currencies.id', $this->id)->first();
+ $default = app('amount')->getDefaultCurrencyByUserGroup($user->userGroup);
+ $this->userGroupDefault = $default->id === $this->id;
+ $this->userGroupEnabled = null !== $current;
}
/**
diff --git a/app/Repositories/UserGroups/Currency/CurrencyRepository.php b/app/Repositories/UserGroups/Currency/CurrencyRepository.php
index 27cb8dbe0c..6ccba7c949 100644
--- a/app/Repositories/UserGroups/Currency/CurrencyRepository.php
+++ b/app/Repositories/UserGroups/Currency/CurrencyRepository.php
@@ -186,8 +186,8 @@ class CurrencyRepository implements CurrencyRepositoryInterface
$isDefault = $local->contains(static function (TransactionCurrency $entry) use ($current) {
return 1 === (int)$entry->pivot->group_default && $entry->id === $current->id;
});
- $current->userEnabled = $hasId;
- $current->userDefault = $isDefault;
+ $current->userGroupEnabled = $hasId;
+ $current->userGroupDefault = $isDefault;
return $current;
});
}
@@ -199,8 +199,8 @@ class CurrencyRepository implements CurrencyRepositoryInterface
{
$all = $this->userGroup->currencies()->orderBy('code', 'ASC')->withPivot(['group_default'])->get();
$all->map(static function (TransactionCurrency $current) {
- $current->userEnabled = true;
- $current->userDefault = 1 === (int)$current->pivot->group_default;
+ $current->userGroupEnabled = true;
+ $current->userGroupDefault = 1 === (int)$current->pivot->group_default;
return $current;
});
return $all;
diff --git a/app/Services/Internal/Update/CurrencyUpdateService.php b/app/Services/Internal/Update/CurrencyUpdateService.php
index 02faa86a1f..d68d9d4e5d 100644
--- a/app/Services/Internal/Update/CurrencyUpdateService.php
+++ b/app/Services/Internal/Update/CurrencyUpdateService.php
@@ -57,8 +57,8 @@ class CurrencyUpdateService
if (array_key_exists('decimal_places', $data) && is_int($data['decimal_places'])) {
$currency->decimal_places = $data['decimal_places'];
}
- $currency->userEnabled = null;
- $currency->userDefault = null;
+ $currency->userGroupEnabled = null;
+ $currency->userGroupDefault = null;
$currency->save();
return $currency;
diff --git a/app/Transformers/CurrencyTransformer.php b/app/Transformers/CurrencyTransformer.php
index 771f881c8b..e87de47aa0 100644
--- a/app/Transformers/CurrencyTransformer.php
+++ b/app/Transformers/CurrencyTransformer.php
@@ -43,8 +43,8 @@ class CurrencyTransformer extends AbstractTransformer
'id' => $currency->id,
'created_at' => $currency->created_at->toAtomString(),
'updated_at' => $currency->updated_at->toAtomString(),
- 'default' => $currency->userDefault,
- 'enabled' => $currency->userEnabled,
+ 'default' => $currency->userGroupDefault,
+ 'enabled' => $currency->userGroupEnabled,
'name' => $currency->name,
'code' => $currency->code,
'symbol' => $currency->symbol,
diff --git a/app/Transformers/V2/CurrencyTransformer.php b/app/Transformers/V2/CurrencyTransformer.php
index 108a758b5f..18b28588f9 100644
--- a/app/Transformers/V2/CurrencyTransformer.php
+++ b/app/Transformers/V2/CurrencyTransformer.php
@@ -49,8 +49,8 @@ class CurrencyTransformer extends AbstractTransformer
'id' => $currency->id,
'created_at' => $currency->created_at->toAtomString(),
'updated_at' => $currency->updated_at->toAtomString(),
- 'default' => $currency->userDefault,
- 'enabled' => $currency->userEnabled,
+ 'default' => $currency->userGroupDefault,
+ 'enabled' => $currency->userGroupEnabled,
'name' => $currency->name,
'code' => $currency->code,
'symbol' => $currency->symbol,
diff --git a/resources/views/currencies/index.twig b/resources/views/currencies/index.twig
index 1a30c963ad..93b30071d9 100644
--- a/resources/views/currencies/index.twig
+++ b/resources/views/currencies/index.twig
@@ -43,7 +43,7 @@
class="fa fa-fw fa-trash"></span></a>
{% endif %}
{# Disable the currency. #}
- {% if currency.userEnabled %}
+ {% if currency.userGroupEnabled %}
<a class="btn btn-default disable-currency" data-code="{{ currency.code }}"
href="#">
<span class="fa fa-fw fa-square-o"></span>
@@ -51,7 +51,7 @@
{% endif %}
{# Enable the currency. #}
- {% if not currency.userEnabled %}
+ {% if not currency.userGroupEnabled %}
<a class="btn btn-default enable-currency" data-code="{{ currency.code }}"
href="#">
<span class="fa fa-fw fa-check-square-o"></span>
@@ -66,19 +66,19 @@
</div>
</td>
<td>
- {% if currency.userEnabled == false %}
+ {% if currency.userGroupEnabled == false %}
<span class="text-muted">
{% endif %}
{{ currency.name }} ({{ currency.code }}) ({{ currency.symbol|raw }})
{% if currency.id == defaultCurrency.id %}
<span class="label label-success" id="default-currency">{{ 'default_currency'|_ }}</span>
{% endif %}
- {% if currency.userEnabled == false %}
+ {% if currency.userGroupEnabled == false %}
<span class="label label-default">{{ 'currency_is_disabled'|_ }}</span>
{% endif %}
- {% if currency.userEnabled == false %}
+ {% if currency.userGroupEnabled == false %}
</span>
{% endif %}
</td>