diff --git a/app/Models/Holding.php b/app/Models/Holding.php index 355d3cc..6abd830 100644 --- a/app/Models/Holding.php +++ b/app/Models/Holding.php @@ -16,8 +16,6 @@ class Holding extends Model use HasFactory; use HasUuids; - protected $with = ['market_data']; - protected $fillable = [ 'portfolio_id', 'symbol', @@ -68,9 +66,6 @@ class Holding extends Model */ public function dividends() { - - - return $this->hasMany(Dividend::class, 'symbol', 'symbol') ->select([ 'dividends.symbol', @@ -129,6 +124,16 @@ class Holding extends Model ->orderBy('date', 'DESC'); } + public function scopeWithMarketData($query) + { + $query->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') + ->join('market_data', 'holdings.symbol', 'market_data.symbol'); + } + public function scopePortfolio($query, $portfolio) { return $query->where('portfolio_id', $portfolio); diff --git a/app/Models/Portfolio.php b/app/Models/Portfolio.php index 51b082d..41847cb 100644 --- a/app/Models/Portfolio.php +++ b/app/Models/Portfolio.php @@ -46,15 +46,10 @@ class Portfolio extends Model ->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') + ->withMarketData() ->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), 0) AS market_gain_percent') - ->join('market_data', 'holdings.symbol', 'market_data.symbol'); + ->selectRaw('COALESCE(((market_data.market_value - holdings.average_cost_basis) / holdings.average_cost_basis), 0) AS market_gain_percent'); } public function transactions() diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index 06c7ab9..5fa9dbc 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -86,6 +86,17 @@ class Transaction extends Model { return $this->belongsTo(Portfolio::class); } + + public function scopeWithMarketData($query) + { + $query->withAggregate('market_data', 'name') + ->withAggregate('portfolio', 'title') + ->withAggregate('market_data', 'market_value') + ->withAggregate('market_data', 'fifty_two_week_low') + ->withAggregate('market_data', 'fifty_two_week_high') + ->withAggregate('market_data', 'updated_at') + ->join('market_data', 'transactions.symbol', 'market_data.symbol'); + } public function scopePortfolio($query, $portfolio) { diff --git a/app/Models/User.php b/app/Models/User.php index aa11828..a6097d1 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -62,32 +62,21 @@ class User extends Authenticatable public function holdings(): HasManyDeep { 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') + ->withMarketData() ->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), 0) AS market_gain_percent') - ->join('market_data', 'holdings.symbol', 'market_data.symbol'); + ->selectRaw('COALESCE(((market_data.market_value - holdings.average_cost_basis) / holdings.average_cost_basis), 0) AS market_gain_percent'); } public function transactions(): HasManyDeep { return $this->hasManyDeep(Transaction::class, ['portfolio_user', Portfolio::class]) - ->withAggregate('market_data', 'name') - ->withAggregate('portfolio', 'title') - ->withAggregate('market_data', 'market_value') - ->withAggregate('market_data', 'fifty_two_week_low') - ->withAggregate('market_data', 'fifty_two_week_high') - ->withAggregate('market_data', 'updated_at') + ->withMarketData() ->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) - END AS gain_dollars') - ->join('market_data', 'transactions.symbol', 'market_data.symbol');; + END AS gain_dollars'); } }