wip
This commit is contained in:
@@ -12,25 +12,10 @@ class DailyChange extends Model
|
|||||||
|
|
||||||
public $timestamps = false;
|
public $timestamps = false;
|
||||||
|
|
||||||
/**
|
|
||||||
* The primary key of the table.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $primaryKey = ['date', 'portfolio_id'];
|
protected $primaryKey = ['date', 'portfolio_id'];
|
||||||
|
|
||||||
/**
|
|
||||||
* Table name for the model
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $table = 'daily_change';
|
protected $table = 'daily_change';
|
||||||
|
|
||||||
/**
|
|
||||||
* The attributes that are mass assignable.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'portfolio_id',
|
'portfolio_id',
|
||||||
'date',
|
'date',
|
||||||
@@ -42,18 +27,8 @@ class DailyChange extends Model
|
|||||||
'notes',
|
'notes',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
|
||||||
* The attributes that should be hidden for arrays.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $hidden = [];
|
protected $hidden = [];
|
||||||
|
|
||||||
/**
|
|
||||||
* The attributes that should be cast to native types.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'date' => 'datetime',
|
'date' => 'datetime',
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -12,29 +12,14 @@ class Dividend extends Model
|
|||||||
use HasFactory;
|
use HasFactory;
|
||||||
use HasUuids;
|
use HasUuids;
|
||||||
|
|
||||||
/**
|
|
||||||
* The attributes that are mass assignable.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'symbol',
|
'symbol',
|
||||||
'date',
|
'date',
|
||||||
'dividend_amount',
|
'dividend_amount',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
|
||||||
* The attributes that should be hidden for arrays.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $hidden = [];
|
protected $hidden = [];
|
||||||
|
|
||||||
/**
|
|
||||||
* The attributes that should be cast to native types.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'date' => 'datetime',
|
'date' => 'datetime',
|
||||||
'first_date' => 'datetime',
|
'first_date' => 'datetime',
|
||||||
|
|||||||
@@ -31,24 +31,6 @@ class Holding extends Model
|
|||||||
'dividends_synced_at' => 'datetime',
|
'dividends_synced_at' => 'datetime',
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $appends = [
|
|
||||||
'market_gain_percent'
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Append the market gain / loss percent attribute
|
|
||||||
*
|
|
||||||
* @return int
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public function getMarketGainPercentAttribute()
|
|
||||||
{
|
|
||||||
|
|
||||||
return (int) !empty($this->market_data?->market_value) && !empty($this->average_cost_basis)
|
|
||||||
? (($this->market_data->market_value - $this->average_cost_basis) / $this->average_cost_basis) * 100
|
|
||||||
: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Market data for holding
|
* Market data for holding
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -14,11 +14,6 @@ class MarketData extends Model
|
|||||||
protected $keyType = 'string';
|
protected $keyType = 'string';
|
||||||
public $incrementing = false;
|
public $incrementing = false;
|
||||||
|
|
||||||
/**
|
|
||||||
* The attributes that are mass assignable.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'symbol',
|
'symbol',
|
||||||
'name',
|
'name',
|
||||||
|
|||||||
@@ -44,7 +44,19 @@ class Portfolio extends Model
|
|||||||
|
|
||||||
public function holdings()
|
public function holdings()
|
||||||
{
|
{
|
||||||
return $this->hasMany(Holding::class, 'portfolio_id', 'id');
|
return $this->hasMany(Holding::class, 'portfolio_id', 'id')
|
||||||
|
->withCount(['transactions as num_transactions' => function ($query) {
|
||||||
|
$query->portfolio($this->id);
|
||||||
|
}])
|
||||||
|
->withAggregate('market_data', 'name')
|
||||||
|
->withAggregate('market_data', 'market_value')
|
||||||
|
->withAggregate('market_data', 'fifty_two_week_low')
|
||||||
|
->withAggregate('market_data', 'fifty_two_week_high')
|
||||||
|
->withAggregate('market_data', 'updated_at')
|
||||||
|
->selectRaw('COALESCE(market_data.market_value * holdings.quantity, 0) AS total_market_value')
|
||||||
|
->selectRaw('COALESCE((market_data.market_value - holdings.average_cost_basis) * holdings.quantity, 0) AS market_gain_dollars')
|
||||||
|
->selectRaw('COALESCE(((market_data.market_value - holdings.average_cost_basis) / holdings.average_cost_basis) * 100, 0) AS market_gain_percent')
|
||||||
|
->join('market_data', 'holdings.symbol', 'market_data.symbol');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function transactions()
|
public function transactions()
|
||||||
|
|||||||
@@ -14,29 +14,14 @@ class Split extends Model
|
|||||||
use HasFactory;
|
use HasFactory;
|
||||||
use HasUuids;
|
use HasUuids;
|
||||||
|
|
||||||
/**
|
|
||||||
* The attributes that are mass assignable.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'symbol',
|
'symbol',
|
||||||
'date',
|
'date',
|
||||||
'split_amount',
|
'split_amount',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
|
||||||
* The attributes that should be hidden for arrays.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $hidden = [];
|
protected $hidden = [];
|
||||||
|
|
||||||
/**
|
|
||||||
* The attributes that should be cast to native types.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'date' => 'datetime',
|
'date' => 'datetime',
|
||||||
'first_date' => 'datetime',
|
'first_date' => 'datetime',
|
||||||
|
|||||||
+10
-21
@@ -24,22 +24,12 @@ class User extends Authenticatable
|
|||||||
use HasUuids;
|
use HasUuids;
|
||||||
use HasRelationships;
|
use HasRelationships;
|
||||||
|
|
||||||
/**
|
|
||||||
* The attributes that are mass assignable.
|
|
||||||
*
|
|
||||||
* @var array<int, string>
|
|
||||||
*/
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'name',
|
'name',
|
||||||
'email',
|
'email',
|
||||||
'password',
|
'password',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
|
||||||
* The attributes that should be hidden for serialization.
|
|
||||||
*
|
|
||||||
* @var array<int, string>
|
|
||||||
*/
|
|
||||||
protected $hidden = [
|
protected $hidden = [
|
||||||
'password',
|
'password',
|
||||||
'remember_token',
|
'remember_token',
|
||||||
@@ -47,20 +37,10 @@ class User extends Authenticatable
|
|||||||
'two_factor_secret',
|
'two_factor_secret',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
|
||||||
* The accessors to append to the model's array form.
|
|
||||||
*
|
|
||||||
* @var array<int, string>
|
|
||||||
*/
|
|
||||||
protected $appends = [
|
protected $appends = [
|
||||||
'profile_photo_url',
|
'profile_photo_url',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the attributes that should be cast.
|
|
||||||
*
|
|
||||||
* @return array<string, string>
|
|
||||||
*/
|
|
||||||
protected function casts(): array
|
protected function casts(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
@@ -76,7 +56,16 @@ class User extends Authenticatable
|
|||||||
|
|
||||||
public function holdings(): HasManyDeep
|
public function holdings(): HasManyDeep
|
||||||
{
|
{
|
||||||
return $this->hasManyDeep(Holding::class, ['portfolio_user', Portfolio::class]);
|
return $this->hasManyDeep(Holding::class, ['portfolio_user', Portfolio::class])
|
||||||
|
->withAggregate('market_data', 'name')
|
||||||
|
->withAggregate('market_data', 'market_value')
|
||||||
|
->withAggregate('market_data', 'fifty_two_week_low')
|
||||||
|
->withAggregate('market_data', 'fifty_two_week_high')
|
||||||
|
->withAggregate('market_data', 'updated_at')
|
||||||
|
->selectRaw('COALESCE(market_data.market_value * holdings.quantity, 0) AS total_market_value')
|
||||||
|
->selectRaw('COALESCE((market_data.market_value - holdings.average_cost_basis) * holdings.quantity, 0) AS market_gain_dollars')
|
||||||
|
->selectRaw('COALESCE(((market_data.market_value - holdings.average_cost_basis) / holdings.average_cost_basis) * 100, 0) AS market_gain_percent')
|
||||||
|
->join('market_data', 'holdings.symbol', 'market_data.symbol');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function transactions(): HasManyDeep
|
public function transactions(): HasManyDeep
|
||||||
|
|||||||
@@ -40,18 +40,6 @@ new class extends Component {
|
|||||||
{
|
{
|
||||||
return $this->portfolio
|
return $this->portfolio
|
||||||
->holdings()
|
->holdings()
|
||||||
->withCount(['transactions as num_transactions' => function ($query) {
|
|
||||||
$query->portfolio($this->portfolio->id);
|
|
||||||
}])
|
|
||||||
->withAggregate('market_data', 'name')
|
|
||||||
->withAggregate('market_data', 'market_value')
|
|
||||||
->withAggregate('market_data', 'fifty_two_week_low')
|
|
||||||
->withAggregate('market_data', 'fifty_two_week_high')
|
|
||||||
->withAggregate('market_data', 'updated_at')
|
|
||||||
->selectRaw('(market_data.market_value * holdings.quantity) AS total_market_value')
|
|
||||||
->selectRaw('((market_data.market_value - holdings.average_cost_basis) * holdings.quantity) AS market_gain_dollars')
|
|
||||||
->selectRaw('(((market_data.market_value - holdings.average_cost_basis) / holdings.average_cost_basis) * 100) AS market_gain_percent')
|
|
||||||
->join('market_data', 'holdings.symbol', 'market_data.symbol')
|
|
||||||
->orderBy(...array_values($this->sortBy))
|
->orderBy(...array_values($this->sortBy))
|
||||||
->where('quantity', '>', 0)
|
->where('quantity', '>', 0)
|
||||||
->get();
|
->get();
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
*
|
*
|
||||||
!public/
|
!public/
|
||||||
!.gitignore
|
!.gitignore
|
||||||
|
!market_data_seed.csv
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user