From 97dfdd5c5d565af65c19ac02c915fa98bbbe40d7 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 22 Oct 2023 06:56:46 +0200 Subject: [PATCH] Link currency to user and user group. --- app/Models/TransactionCurrency.php | 37 ++++++++++++ app/Models/UserGroup.php | 11 ++++ app/User.php | 11 ++++ ...10_21_113213_add_currency_pivot_tables.php | 60 +++++++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 database/migrations/2023_10_21_113213_add_currency_pivot_tables.php diff --git a/app/Models/TransactionCurrency.php b/app/Models/TransactionCurrency.php index 5d34e44419..713bbbb32f 100644 --- a/app/Models/TransactionCurrency.php +++ b/app/Models/TransactionCurrency.php @@ -24,8 +24,10 @@ declare(strict_types=1); namespace FireflyIII\Models; use Eloquent; +use FireflyIII\User; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Query\Builder; @@ -40,6 +42,8 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; * @property Carbon|null $updated_at * @property Carbon|null $deleted_at * @property bool $enabled + * @property bool $userDefault + * @property bool $userEnabled * @property string $code * @property string $name * @property string $symbol @@ -107,6 +111,39 @@ class TransactionCurrency extends Model throw new NotFoundHttpException(); } + /** + * @param User $user + * + * @return void + */ + public function refreshForUser(User $user) + { + $current = $user->currencies()->where('transaction_currencies.id', $this->id)->first(); + $default = app('amount')->getDefaultCurrencyByUser($user); + $this->userDefault = (int)$default->id === (int)$this->id; + $this->userEnabled = null !== $current; + } + + /** + * Link to users + * + * @return BelongsToMany + */ + public function users(): BelongsToMany + { + return $this->belongsToMany(User::class)->withTimestamps()->withPivot('default'); + } + + /** + * Link to user groups + * + * @return BelongsToMany + */ + public function userGroups(): BelongsToMany + { + return $this->belongsToMany(UserGroup::class)->withTimestamps()->withPivot('default'); + } + /** * @return HasMany */ diff --git a/app/Models/UserGroup.php b/app/Models/UserGroup.php index 5be2e89e81..ac8b3c8320 100644 --- a/app/Models/UserGroup.php +++ b/app/Models/UserGroup.php @@ -30,6 +30,7 @@ use FireflyIII\User; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Support\Carbon; @@ -129,6 +130,16 @@ class UserGroup extends Model return $this->hasMany(Account::class); } + /** + * Link to currencies + * + * @return BelongsToMany + */ + public function currencies(): BelongsToMany + { + return $this->belongsToMany(TransactionCurrency::class); + } + /** * Link to attachments. * diff --git a/app/User.php b/app/User.php index d4cfe96cf3..dc9af881bf 100644 --- a/app/User.php +++ b/app/User.php @@ -46,6 +46,7 @@ use FireflyIII\Models\Rule; use FireflyIII\Models\RuleGroup; use FireflyIII\Models\Tag; use FireflyIII\Models\Transaction; +use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionGroup; use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\UserGroup; @@ -231,6 +232,16 @@ class User extends Authenticatable return $this->hasMany(Account::class); } + /** + * Link to currencies + * + * @return BelongsToMany + */ + public function currencies(): BelongsToMany + { + return $this->belongsToMany(TransactionCurrency::class)->withTimestamps()->withPivot('default'); + } + /** * Link to attachments * diff --git a/database/migrations/2023_10_21_113213_add_currency_pivot_tables.php b/database/migrations/2023_10_21_113213_add_currency_pivot_tables.php new file mode 100644 index 0000000000..c20b4b1173 --- /dev/null +++ b/database/migrations/2023_10_21_113213_add_currency_pivot_tables.php @@ -0,0 +1,60 @@ +id(); + $table->timestamps(); + $table->integer('user_id', false, true); + $table->integer('transaction_currency_id', false, true); + $table->boolean('default')->default(false); + $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); + $table->foreign('transaction_currency_id')->references('id')->on('transaction_currencies')->onDelete('cascade'); + $table->unique(['user_id', 'transaction_currency_id'],'unique_combo'); + }); + } catch (QueryException $e) { + app('log')->error(sprintf('Could not create table "transaction_currency_user": %s', $e->getMessage())); + app('log')->error('If this table exists already (see the error message), this is not a problem. Other errors? Please open a discussion on GitHub.'); + } + } + + // transaction_currency_user_group + if (!Schema::hasTable('transaction_currency_user_group')) { + try { + Schema::create('transaction_currency_user_group', function (Blueprint $table) { + $table->id(); + $table->timestamps(); + $table->bigInteger('user_group_id', false, true); + $table->integer('transaction_currency_id', false, true); + $table->boolean('default')->default(false); + $table->foreign('user_group_id')->references('id')->on('user_groups')->onDelete('cascade'); + $table->foreign('transaction_currency_id')->references('id')->on('transaction_currencies')->onDelete('cascade'); + $table->unique(['user_group_id', 'transaction_currency_id'],'unique_combo'); + }); + } catch (QueryException $e) { + app('log')->error(sprintf('Could not create table "transaction_currency_user_group": %s', $e->getMessage())); + app('log')->error('If this table exists already (see the error message), this is not a problem. Other errors? Please open a discussion on GitHub.'); + } + } + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('transaction_currency_user'); + Schema::dropIfExists('transaction_currency_user_group'); + } +};