wip
This commit is contained in:
@@ -12,25 +12,10 @@ class DailyChange extends Model
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
/**
|
||||
* The primary key of the table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = ['date', 'portfolio_id'];
|
||||
|
||||
/**
|
||||
* Table name for the model
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'daily_change';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'portfolio_id',
|
||||
'date',
|
||||
@@ -42,18 +27,8 @@ class DailyChange extends Model
|
||||
'notes',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for arrays.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $hidden = [];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'date' => 'datetime',
|
||||
];
|
||||
|
||||
@@ -12,29 +12,14 @@ class Dividend extends Model
|
||||
use HasFactory;
|
||||
use HasUuids;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'symbol',
|
||||
'date',
|
||||
'dividend_amount',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for arrays.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $hidden = [];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'date' => 'datetime',
|
||||
'first_date' => 'datetime',
|
||||
|
||||
@@ -31,24 +31,6 @@ class Holding extends Model
|
||||
'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
|
||||
*
|
||||
|
||||
@@ -14,11 +14,6 @@ class MarketData extends Model
|
||||
protected $keyType = 'string';
|
||||
public $incrementing = false;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'symbol',
|
||||
'name',
|
||||
|
||||
@@ -44,7 +44,19 @@ class Portfolio extends Model
|
||||
|
||||
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()
|
||||
|
||||
@@ -14,29 +14,14 @@ class Split extends Model
|
||||
use HasFactory;
|
||||
use HasUuids;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'symbol',
|
||||
'date',
|
||||
'split_amount',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for arrays.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $hidden = [];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'date' => 'datetime',
|
||||
'first_date' => 'datetime',
|
||||
|
||||
+10
-21
@@ -24,22 +24,12 @@ class User extends Authenticatable
|
||||
use HasUuids;
|
||||
use HasRelationships;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for serialization.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
@@ -47,20 +37,10 @@ class User extends Authenticatable
|
||||
'two_factor_secret',
|
||||
];
|
||||
|
||||
/**
|
||||
* The accessors to append to the model's array form.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $appends = [
|
||||
'profile_photo_url',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
@@ -76,7 +56,16 @@ class User extends Authenticatable
|
||||
|
||||
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
|
||||
|
||||
@@ -40,18 +40,6 @@ new class extends Component {
|
||||
{
|
||||
return $this->portfolio
|
||||
->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))
|
||||
->where('quantity', '>', 0)
|
||||
->get();
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
*
|
||||
!public/
|
||||
!.gitignore
|
||||
!market_data_seed.csv
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user