From 1e0a149ded30bddd313dba85a5390db54abee5c8 Mon Sep 17 00:00:00 2001 From: hackerESQ Date: Wed, 28 Aug 2024 23:32:01 -0500 Subject: [PATCH] wip --- app/Http/Controllers/HoldingController.php | 14 ++++++++------ app/Models/Holding.php | 18 +++++++++--------- app/Models/Portfolio.php | 13 +++++-------- app/Models/Transaction.php | 2 +- resources/views/holding/show.blade.php | 2 +- .../views/livewire/holdings-table.blade.php | 4 ++++ 6 files changed, 28 insertions(+), 25 deletions(-) diff --git a/app/Http/Controllers/HoldingController.php b/app/Http/Controllers/HoldingController.php index 1f71376..f5056fc 100644 --- a/app/Http/Controllers/HoldingController.php +++ b/app/Http/Controllers/HoldingController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Models\Holding; use App\Models\Portfolio; use Illuminate\Http\Request; @@ -14,12 +15,13 @@ class HoldingController extends Controller public function show(Request $request, Portfolio $portfolio, String $symbol) { - $holding = $portfolio->holdings() - ->with(['market_data']) - ->symbol($symbol) - ->portfolio($portfolio->id) - ->first(); + $holding = Holding::with(['market_data']) + ->symbol($symbol) + ->portfolio($portfolio->id) + ->firstOrFail(); - return view('holding.show', compact(['portfolio', 'holding'])); + $transactions = $holding->transactions; + + return view('holding.show', compact(['portfolio', 'holding', 'transactions'])); } } diff --git a/app/Models/Holding.php b/app/Models/Holding.php index 0c776cb..56caf68 100644 --- a/app/Models/Holding.php +++ b/app/Models/Holding.php @@ -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) diff --git a/app/Models/Portfolio.php b/app/Models/Portfolio.php index 0c2a145..8a9bfff 100644 --- a/app/Models/Portfolio.php +++ b/app/Models/Portfolio.php @@ -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() diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index 1e5489e..38783b7 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -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') diff --git a/resources/views/holding/show.blade.php b/resources/views/holding/show.blade.php index ee28cb8..b23ab87 100644 --- a/resources/views/holding/show.blade.php +++ b/resources/views/holding/show.blade.php @@ -114,7 +114,7 @@ @livewire('transactions-list', [ 'portfolio' => $holding->portfolio, - 'transactions' => $holding->transactions, + 'transactions' => $transactions, 'shouldGoToHolding' => false ]) diff --git a/resources/views/livewire/holdings-table.blade.php b/resources/views/livewire/holdings-table.blade.php index a9461d9..cf269ca 100644 --- a/resources/views/livewire/holdings-table.blade.php +++ b/resources/views/livewire/holdings-table.blade.php @@ -1,5 +1,6 @@ portfolio ->holdings() + ->withCount(['transactions as num_transactions' => function ($query) { + $query->where('portfolio_id', $this->portfolio->id); + }]) ->orderBy(...array_values($this->sortBy)) ->where('quantity', '>', 0) ->get();