Fix default currencies.

This commit is contained in:
James Cole
2023-12-16 16:06:23 +01:00
parent a1dfb3a99e
commit 2f110ab9a8
8 changed files with 30 additions and 30 deletions

View File

@@ -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) {

View File

@@ -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);
}
);

View File

@@ -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;
}
/**

View File

@@ -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;

View File

@@ -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;

View File

@@ -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,

View File

@@ -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,

View File

@@ -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>