Link currency to user and user group.

This commit is contained in:
James Cole 2023-10-22 06:56:46 +02:00
parent eb5ee4d147
commit 97dfdd5c5d
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
4 changed files with 119 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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