2024-08-01 13:53:10 -05:00
|
|
|
<?php
|
|
|
|
|
|
2025-01-28 17:33:54 -06:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2024-08-01 13:53:10 -05:00
|
|
|
namespace App\Models;
|
|
|
|
|
|
2024-10-19 23:11:04 -05:00
|
|
|
use App\Traits\HasConnectedAccounts;
|
2025-09-26 17:41:28 -05:00
|
|
|
use App\Traits\HasProfilePhoto;
|
2025-01-28 17:14:49 -06:00
|
|
|
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
|
|
|
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
2024-08-01 13:53:10 -05:00
|
|
|
use Illuminate\Notifications\Notifiable;
|
2025-04-09 19:25:15 -05:00
|
|
|
use Illuminate\Support\Arr;
|
2024-08-01 13:53:10 -05:00
|
|
|
use Laravel\Fortify\TwoFactorAuthenticatable;
|
2025-01-28 17:14:49 -06:00
|
|
|
use Laravel\Sanctum\HasApiTokens;
|
2024-08-22 22:45:17 -05:00
|
|
|
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
|
|
|
|
|
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
|
2024-08-01 13:53:10 -05:00
|
|
|
|
2024-10-19 23:11:04 -05:00
|
|
|
class User extends Authenticatable implements MustVerifyEmail
|
2024-08-01 13:53:10 -05:00
|
|
|
{
|
|
|
|
|
use HasApiTokens;
|
2025-01-28 17:14:49 -06:00
|
|
|
use HasConnectedAccounts;
|
2024-08-01 13:53:10 -05:00
|
|
|
use HasFactory;
|
|
|
|
|
use HasProfilePhoto;
|
2025-01-28 17:14:49 -06:00
|
|
|
use HasRelationships;
|
|
|
|
|
use HasUuids;
|
2024-08-01 13:53:10 -05:00
|
|
|
use Notifiable;
|
|
|
|
|
use TwoFactorAuthenticatable;
|
|
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'name',
|
|
|
|
|
'email',
|
|
|
|
|
'password',
|
2025-04-09 19:25:15 -05:00
|
|
|
'options',
|
2024-08-01 13:53:10 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $hidden = [
|
2024-11-05 15:39:29 -06:00
|
|
|
'admin',
|
2024-08-01 13:53:10 -05:00
|
|
|
'password',
|
|
|
|
|
'remember_token',
|
|
|
|
|
'two_factor_recovery_codes',
|
|
|
|
|
'two_factor_secret',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $appends = [
|
|
|
|
|
'profile_photo_url',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected function casts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'email_verified_at' => 'datetime',
|
|
|
|
|
'password' => 'hashed',
|
2025-04-09 19:25:15 -05:00
|
|
|
'admin' => 'boolean',
|
|
|
|
|
'options' => 'json',
|
2024-08-01 13:53:10 -05:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function portfolios()
|
|
|
|
|
{
|
2024-10-21 22:23:20 -05:00
|
|
|
return $this->belongsToMany(Portfolio::class)->withPivot(['owner', 'full_access', 'invite_accepted_at']);
|
2024-08-01 13:53:10 -05:00
|
|
|
}
|
|
|
|
|
|
2024-08-27 22:06:10 -05:00
|
|
|
public function daily_changes()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasManyDeep(DailyChange::class, ['portfolio_user', Portfolio::class]);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 22:45:17 -05:00
|
|
|
public function holdings(): HasManyDeep
|
|
|
|
|
{
|
2024-08-24 22:51:45 -05:00
|
|
|
return $this->hasManyDeep(Holding::class, ['portfolio_user', Portfolio::class])
|
2024-08-28 15:19:20 -05:00
|
|
|
->withMarketData()
|
2025-01-28 17:14:49 -06:00
|
|
|
->withPerformance();
|
2024-08-22 22:45:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function transactions(): HasManyDeep
|
|
|
|
|
{
|
2024-08-26 19:49:43 -05:00
|
|
|
return $this->hasManyDeep(Transaction::class, ['portfolio_user', Portfolio::class])
|
2024-08-28 15:19:20 -05:00
|
|
|
->withMarketData()
|
2024-08-28 22:06:47 -05:00
|
|
|
->withAggregate('portfolio', 'title')
|
2024-08-27 21:17:54 -05:00
|
|
|
->selectRaw('
|
|
|
|
|
CASE
|
|
|
|
|
WHEN transaction_type = \'SELL\'
|
|
|
|
|
THEN COALESCE(transactions.sale_price - transactions.cost_basis, 0)
|
|
|
|
|
ELSE COALESCE(market_data.market_value - transactions.cost_basis, 0)
|
2025-01-28 17:14:49 -06:00
|
|
|
END AS gain_dollars');
|
2024-08-22 22:45:17 -05:00
|
|
|
}
|
2025-04-09 19:25:15 -05:00
|
|
|
|
|
|
|
|
public function getCurrency(): string
|
|
|
|
|
{
|
|
|
|
|
return Arr::get($this->options, 'display_currency') ?? config('investbrain.base_currency');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getLocale(): string
|
|
|
|
|
{
|
|
|
|
|
$available_locales = Arr::pluck(config('app.available_locales'), 'locale');
|
|
|
|
|
|
|
|
|
|
return Arr::get($this->options, 'locale') ?? request()->getPreferredLanguage($available_locales) ?? config('app.locale');
|
|
|
|
|
}
|
2025-04-10 20:47:35 -05:00
|
|
|
|
2025-07-22 21:52:04 -05:00
|
|
|
public function setOption(mixed $key, ?string $value = null): self
|
2025-04-10 20:47:35 -05:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
$options = is_array($key) ? $key : [$key => $value];
|
|
|
|
|
|
2025-04-11 21:49:06 -05:00
|
|
|
$this->options = array_merge($this->options ?? [], $options);
|
2025-04-10 20:47:35 -05:00
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
2024-08-01 13:53:10 -05:00
|
|
|
}
|