chore: clean up
This commit is contained in:
+60
-55
@@ -243,49 +243,7 @@ class Holding extends Model
|
|||||||
{
|
{
|
||||||
$currency = $currency ?? auth()->user()->getCurrency();
|
$currency = $currency ?? auth()->user()->getCurrency();
|
||||||
|
|
||||||
return $query->select([
|
$cost_basis_sub = DB::table('transactions')
|
||||||
'holdings.symbol',
|
|
||||||
'holdings.portfolio_id',
|
|
||||||
'dividends_display.total_dividends_earned',
|
|
||||||
])
|
|
||||||
->groupBy([
|
|
||||||
'holdings.symbol',
|
|
||||||
'holdings.quantity',
|
|
||||||
'holdings.portfolio_id',
|
|
||||||
'cr.rate',
|
|
||||||
'dividends_display.total_dividends_earned',
|
|
||||||
'market_data.market_value_base',
|
|
||||||
])
|
|
||||||
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
|
|
||||||
$join->where('cr.currency', '=', $currency);
|
|
||||||
|
|
||||||
if (config('database.default') === 'sqlite') {
|
|
||||||
$join->whereRaw("strftime('%Y-%m-%d', cr.date) = ?", [
|
|
||||||
now()->toDateString(),
|
|
||||||
]);
|
|
||||||
} else {
|
|
||||||
$join->on('cr.date', '=', DB::raw("'".now()->toDateString()."'"));
|
|
||||||
}
|
|
||||||
})
|
|
||||||
->leftJoin('market_data', function ($join) {
|
|
||||||
$join->on('market_data.symbol', '=', 'holdings.symbol');
|
|
||||||
})
|
|
||||||
->selectRaw(
|
|
||||||
'holdings.quantity * market_data.market_value_base * COALESCE(cr.rate, 1) AS total_market_value'
|
|
||||||
)
|
|
||||||
->selectRaw(
|
|
||||||
'SUM(transactions_display.realized_gain_dollars) AS realized_gain_dollars'
|
|
||||||
)
|
|
||||||
->selectRaw(
|
|
||||||
'(SUM(transactions_display.total_cost_basis) / SUM(transactions_display.total_purchases)) * holdings.quantity AS total_cost_basis'
|
|
||||||
)
|
|
||||||
->selectRaw(
|
|
||||||
'(
|
|
||||||
holdings.quantity * market_data.market_value_base * COALESCE(cr.rate, 1)
|
|
||||||
) - (SUM(transactions_display.total_cost_basis) / SUM(transactions_display.total_purchases)) * holdings.quantity AS total_gain_dollars'
|
|
||||||
)
|
|
||||||
->leftJoinSub(
|
|
||||||
DB::table('transactions')
|
|
||||||
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
|
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
|
||||||
$join
|
$join
|
||||||
->on('cr.date', '=', 'transactions.date')
|
->on('cr.date', '=', 'transactions.date')
|
||||||
@@ -370,16 +328,9 @@ class Holding extends Model
|
|||||||
'transactions.quantity',
|
'transactions.quantity',
|
||||||
'cost_basis_display.rate',
|
'cost_basis_display.rate',
|
||||||
'cr.rate',
|
'cr.rate',
|
||||||
]),
|
]);
|
||||||
'transactions_display',
|
|
||||||
function ($join) {
|
$dividends_sub = DB::table('dividends')
|
||||||
$join
|
|
||||||
->on('holdings.symbol', '=', 'transactions_display.symbol')
|
|
||||||
->on('holdings.portfolio_id', '=', 'transactions_display.portfolio_id');
|
|
||||||
}
|
|
||||||
)
|
|
||||||
->leftJoinSub(
|
|
||||||
DB::table('dividends')
|
|
||||||
->join('transactions as tx', function ($join) {
|
->join('transactions as tx', function ($join) {
|
||||||
$join
|
$join
|
||||||
->on('tx.symbol', '=', 'dividends.symbol')
|
->on('tx.symbol', '=', 'dividends.symbol')
|
||||||
@@ -394,8 +345,62 @@ class Holding extends Model
|
|||||||
->selectRaw(
|
->selectRaw(
|
||||||
"SUM(((CASE WHEN transaction_type = 'BUY' THEN tx.quantity ELSE 0 END) - (CASE WHEN transaction_type = 'SELL' THEN tx.quantity ELSE 0 END)) * dividends.dividend_amount_base * COALESCE(cr.rate, 1)) AS total_dividends_earned"
|
"SUM(((CASE WHEN transaction_type = 'BUY' THEN tx.quantity ELSE 0 END) - (CASE WHEN transaction_type = 'SELL' THEN tx.quantity ELSE 0 END)) * dividends.dividend_amount_base * COALESCE(cr.rate, 1)) AS total_dividends_earned"
|
||||||
)
|
)
|
||||||
->groupBy(['dividends.symbol']),
|
->groupBy(['dividends.symbol']);
|
||||||
'dividends_display',
|
|
||||||
|
return $query->select([
|
||||||
|
'holdings.symbol',
|
||||||
|
'holdings.portfolio_id',
|
||||||
|
'dividends_display.total_dividends_earned',
|
||||||
|
])
|
||||||
|
->groupBy([
|
||||||
|
'holdings.symbol',
|
||||||
|
'holdings.quantity',
|
||||||
|
'holdings.portfolio_id',
|
||||||
|
'cr.rate',
|
||||||
|
'dividends_display.total_dividends_earned',
|
||||||
|
'market_data.market_value_base',
|
||||||
|
])
|
||||||
|
->leftJoin('currency_rates as cr', function ($join) use ($currency) {
|
||||||
|
$join->where('cr.currency', '=', $currency);
|
||||||
|
|
||||||
|
if (config('database.default') === 'sqlite') {
|
||||||
|
$join->whereRaw("strftime('%Y-%m-%d', cr.date) = ?", [
|
||||||
|
now()->toDateString(),
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
$join->on('cr.date', '=', DB::raw("'".now()->toDateString()."'"));
|
||||||
|
}
|
||||||
|
})
|
||||||
|
->leftJoin('market_data', function ($join) {
|
||||||
|
$join->on('market_data.symbol', '=', 'holdings.symbol');
|
||||||
|
})
|
||||||
|
->selectRaw('
|
||||||
|
holdings.quantity * market_data.market_value_base * COALESCE(cr.rate, 1)
|
||||||
|
AS total_market_value
|
||||||
|
')
|
||||||
|
->selectRaw('
|
||||||
|
SUM(transactions_display.realized_gain_dollars)
|
||||||
|
AS realized_gain_dollars
|
||||||
|
')
|
||||||
|
->selectRaw('
|
||||||
|
(SUM(transactions_display.total_cost_basis) / SUM(transactions_display.total_purchases))
|
||||||
|
* holdings.quantity
|
||||||
|
AS total_cost_basis
|
||||||
|
')
|
||||||
|
->selectRaw('
|
||||||
|
(holdings.quantity * market_data.market_value_base * COALESCE(cr.rate, 1))
|
||||||
|
- (SUM(transactions_display.total_cost_basis) / SUM(transactions_display.total_purchases))
|
||||||
|
* holdings.quantity
|
||||||
|
AS total_gain_dollars
|
||||||
|
')
|
||||||
|
->leftJoinSub($cost_basis_sub, 'transactions_display',
|
||||||
|
function ($join) {
|
||||||
|
$join
|
||||||
|
->on('holdings.symbol', '=', 'transactions_display.symbol')
|
||||||
|
->on('holdings.portfolio_id', '=', 'transactions_display.portfolio_id');
|
||||||
|
}
|
||||||
|
)
|
||||||
|
->leftJoinSub($dividends_sub, 'dividends_display',
|
||||||
function ($join) {
|
function ($join) {
|
||||||
$join->on('holdings.symbol', '=', 'dividends_display.symbol');
|
$join->on('holdings.symbol', '=', 'dividends_display.symbol');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user