This commit is contained in:
hackerESQ
2025-08-19 21:34:35 -05:00
parent e1c8c2c515
commit 7e2bf3430e
2 changed files with 56 additions and 67 deletions
+55 -66
View File
@@ -62,83 +62,72 @@ class DailyChange extends Model
{ {
$currency = auth()->user()?->getCurrency() ?? config('investbrain.base_currency'); $currency = auth()->user()?->getCurrency() ?? config('investbrain.base_currency');
$dividendSub = DB::table('holdings')
->join('dividends', 'dividends.symbol', '=', 'holdings.symbol')
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
$join->on(DB::raw('DATE(cr.date)'), '=', DB::raw('DATE(dividends.date)'))
->where('cr.currency', '=', $currency);
})
->join('transactions as tx', function ($join) {
$join->on('tx.symbol', '=', 'holdings.symbol')
->on('tx.portfolio_id', '=', 'holdings.portfolio_id')
->whereColumn('tx.date', '<=', 'dividends.date');
})
->select(['holdings.portfolio_id', 'dividends.date'])
->selectRaw("
((CASE WHEN tx.transaction_type = 'BUY'
THEN tx.quantity ELSE 0 END)
- (CASE WHEN tx.transaction_type = 'SELL'
THEN tx.quantity ELSE 0 END))
* SUM(
dividends.dividend_amount_base
* COALESCE(cr.rate, 1)
)
AS total_dividends_earned")
->groupBy(['holdings.portfolio_id', 'dividends.date', 'tx.transaction_type', 'tx.quantity']);
$costBasisSub = DB::table('transactions')
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
$join->on(DB::raw('DATE(cr.date)'), '=', DB::raw('DATE(transactions.date)'))
->where('cr.currency', $currency);
})
->select(['transactions.portfolio_id', 'transactions.date']);
return $query return $query
->select(['daily_change.date', 'daily_change.portfolio_id']) // Join currency rates to get the rate for each date
->selectRaw('daily_change.total_market_value * COALESCE(cr.rate, 1) AS total_market_value')
->leftJoin('currency_rates as cr', function ($join) use ($currency) { ->leftJoin('currency_rates as cr', function ($join) use ($currency) {
$join->on(DB::raw('DATE(cr.date)'), '=', DB::raw('DATE(daily_change.date)')) $join->on(DB::raw('DATE(cr.date)'), '=', DB::raw('DATE(daily_change.date)'))
->where('cr.currency', '=', $currency); ->where('cr.currency', '=', $currency);
}) })
->selectSub(function ($query) use ($costBasisSub) { ->select([
$query->fromSub( 'daily_change.date',
$costBasisSub->selectRaw(" 'daily_change.portfolio_id',
(CASE DB::raw('daily_change.total_market_value * COALESCE(cr.rate, 1) as total_market_value'),
WHEN transactions.transaction_type = 'BUY' 'total_cost_basis' => function ($query) use ($currency) {
THEN 1 ELSE -1 END $query->from('transactions')
) * transactions.cost_basis_base * transactions.quantity * COALESCE(cr.rate, 1) AS total_cost_basis"), ->leftJoin('currency_rates as cr', function ($join) use ($currency) {
'cb') $join->on(DB::raw('DATE(cr.date)'), '=', DB::raw('DATE(transactions.date)'))
->selectRaw('SUM(cb.total_cost_basis)') ->where('cr.currency', '=', $currency);
->whereColumn('cb.date', '<=', 'daily_change.date') })
->whereColumn('cb.portfolio_id', '=', 'daily_change.portfolio_id'); ->selectRaw('SUM(
}, 'total_cost_basis') (CASE WHEN transactions.transaction_type = \'BUY\' THEN 1 ELSE -1 END)
->selectSub(function ($query) use ($costBasisSub) { * transactions.cost_basis_base * transactions.quantity * COALESCE(cr.rate, 1)
$query->fromSub( )')
$costBasisSub->selectRaw(" ->whereColumn('transactions.portfolio_id', 'daily_change.portfolio_id')
(CASE ->whereColumn('transactions.date', '<=', 'daily_change.date');
WHEN transactions.transaction_type = 'SELL' },
THEN transactions.sale_price_base - transactions.cost_basis_base 'realized_gain_dollars' => function ($query) use ($currency) {
END $query->from('transactions')
) * transactions.quantity * COALESCE(cr.rate, 1) AS realized_gain_dollars"), ->leftJoin('currency_rates as cr', function ($join) use ($currency) {
'cb') $join->on(DB::raw('DATE(cr.date)'), '=', DB::raw('DATE(transactions.date)'))
->selectRaw('SUM(cb.realized_gain_dollars)') ->where('cr.currency', '=', $currency);
->whereColumn('cb.date', '<=', 'daily_change.date') })
->whereColumn('cb.portfolio_id', '=', 'daily_change.portfolio_id'); ->selectRaw('SUM(
}, 'realized_gain_dollars') (CASE WHEN transactions.transaction_type = \'SELL\'
->selectSub(function ($query) use ($dividendSub) { // todo: maybe costbasis uses this model? THEN (transactions.sale_price_base - transactions.cost_basis_base)
$query->fromSub($dividendSub, 'd') ELSE 0 END)
->selectRaw('SUM(d.total_dividends_earned)') * transactions.quantity * COALESCE(cr.rate, 1)
->whereColumn('d.date', '<=', 'daily_change.date') )')
->whereColumn('d.portfolio_id', '=', 'daily_change.portfolio_id'); ->whereColumn('transactions.portfolio_id', 'daily_change.portfolio_id')
}, 'total_dividends_earned') ->whereColumn('transactions.date', '<=', 'daily_change.date');
->addSelect('annotation') },
'total_dividends_earned' => function ($query) use ($currency) {
$query->from('holdings')
->join('dividends', 'dividends.symbol', '=', 'holdings.symbol')
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
$join->on(DB::raw('DATE(cr.date)'), '=', DB::raw('DATE(dividends.date)'))
->where('cr.currency', '=', $currency);
})
->join('transactions as tx', function ($join) {
$join->on('tx.symbol', '=', 'holdings.symbol')
->on('tx.portfolio_id', '=', 'holdings.portfolio_id')
->whereColumn('tx.date', '<=', 'dividends.date');
})
->selectRaw('SUM(
((CASE WHEN tx.transaction_type = \'BUY\' THEN tx.quantity ELSE 0 END)
- (CASE WHEN tx.transaction_type = \'SELL\' THEN tx.quantity ELSE 0 END))
* dividends.dividend_amount_base
* COALESCE(cr.rate, 1)
)')
->whereColumn('holdings.portfolio_id', 'daily_change.portfolio_id')
->whereColumn('dividends.date', '<=', 'daily_change.date');
},
])
->orderBy('daily_change.date'); ->orderBy('daily_change.date');
} }
public function scopeGetDailyPerformance($query) public function scopeGetDailyPerformance($query)
{ {
return $query->get() return $query->get()
->sortBy('date')
->groupBy('date') ->groupBy('date')
->map(function ($group) { ->map(function ($group) {
+1 -1
View File
@@ -402,7 +402,7 @@ class Holding extends Model
) )
->leftJoinSub($dividends_sub, 'dividends_display', ->leftJoinSub($dividends_sub, 'dividends_display',
function ($join) { function ($join) {
$join->on('holdings.symbol', '=', 'dividends_display.symbol'); $join->on('holdings.symbol', '=', 'dividends_display.symbol'); // todo: this isnt limiting to port ids
} }
); );