Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Link currency to user and user group.
James Cole97dfdd5c5d56
diff --git a/app/Models/TransactionCurrency.php b/app/Models/TransactionCurrency.phpindex 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 Modelthrow 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.phpindex 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 Modelreturn $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.phpindex 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 Authenticatablereturn $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.phpnew file mode 100644index 0000000000..c20b4b1173--- /dev/null+++ b/database/migrations/2023_10_21_113213_add_currency_pivot_tables.php@@ -0,0 +1,60 @@+<?php++use Illuminate\Database\Migrations\Migration;+use Illuminate\Database\Schema\Blueprint;+use Illuminate\Support\Facades\Schema;++return new class extends Migration {+ /**+ * Run the migrations.+ */+ public function up(): void+ {+ // transaction_currency_user+ if (!Schema::hasTable('transaction_currency_user')) {+ try {+ Schema::create('transaction_currency_user', function (Blueprint $table) {+ $table->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');+ }+};