wip
This commit is contained in:
@@ -55,9 +55,9 @@ class Holding extends Model
|
||||
*/
|
||||
public function transactions()
|
||||
{
|
||||
return $this->hasMany(Transaction::class, 'symbol', 'symbol')
|
||||
->where('portfolio_id', $this->portfolio_id)
|
||||
->withAggregate('portfolio', 'title');
|
||||
|
||||
return $this->hasManyThrough(Transaction::class, Portfolio::class, 'id', 'portfolio_id', 'portfolio_id', 'id')
|
||||
->where('symbol', $this->symbol);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -127,12 +127,12 @@ class Holding extends Model
|
||||
|
||||
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');
|
||||
return $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)
|
||||
|
||||
@@ -42,14 +42,11 @@ class Portfolio extends Model
|
||||
|
||||
public function holdings()
|
||||
{
|
||||
return $this->hasMany(Holding::class, 'portfolio_id', 'id')
|
||||
->withCount(['transactions as num_transactions' => function ($query) {
|
||||
$query->portfolio($this->id);
|
||||
}])
|
||||
->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');
|
||||
return $this->hasMany(Holding::class, 'portfolio_id')
|
||||
->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');
|
||||
}
|
||||
|
||||
public function transactions()
|
||||
|
||||
@@ -89,7 +89,7 @@ class Transaction extends Model
|
||||
|
||||
public function scopeWithMarketData($query)
|
||||
{
|
||||
$query->withAggregate('market_data', 'name')
|
||||
return $query->withAggregate('market_data', 'name')
|
||||
->withAggregate('market_data', 'market_value')
|
||||
->withAggregate('market_data', 'fifty_two_week_low')
|
||||
->withAggregate('market_data', 'fifty_two_week_high')
|
||||
|
||||
Reference in New Issue
Block a user