dividend earnings not shared between portfolios

This commit is contained in:
hackerESQ
2025-08-22 16:37:33 -05:00
parent ac310735df
commit 65710e2791
4 changed files with 31 additions and 4 deletions
+4 -3
View File
@@ -341,11 +341,11 @@ class Holding extends Model
->on('cr.date', '=', 'dividends.date')
->where('cr.currency', '=', $currency);
})
->select(['dividends.symbol'])
->select(['dividends.symbol', 'tx.portfolio_id'])
->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"
)
->groupBy(['dividends.symbol']);
->groupBy(['dividends.symbol', 'tx.portfolio_id']);
return $query->select([
'holdings.symbol',
@@ -402,7 +402,8 @@ class Holding extends Model
)
->leftJoinSub($dividends_sub, 'dividends_display',
function ($join) {
$join->on('holdings.symbol', '=', 'dividends_display.symbol'); // todo: this isnt limiting to port ids
$join->on('holdings.symbol', '=', 'dividends_display.symbol') // todo: this isnt limiting to port ids
->on('holdings.portfolio_id', '=', 'dividends_display.portfolio_id');
}
);
+1 -1
View File
@@ -220,7 +220,7 @@ class Portfolio extends Model
}
cache()->forget('graph-YTD-'.$this->id);
cache()->forget('graph-YTD-'.request()->user()->id);
cache()->forget('graph-YTD-'.request()->user()?->id);
}
protected function getMostRecentCloseData($history, $date, $i = 0, $max_attempts = 5)
@@ -59,6 +59,13 @@ class TransactionFactory extends Factory
]);
}
public function sixMonthsAgo(): static
{
return $this->state(fn (array $attributes) => [
'date' => now()->subMonths(6)->toDateString(),
]);
}
public function today(): static
{
return $this->state(fn (array $attributes) => [
+19
View File
@@ -70,4 +70,23 @@ class DividendsTest extends TestCase
$this->assertEquals(3, $dividend_count);
}
public function test_dividend_earnings_are_not_shared_between_portfolios(): void
{
$this->actingAs($user = User::factory()->create());
$portfolioOne = Portfolio::factory()->create();
Transaction::factory()->buy()->yearsAgo()->portfolio($portfolioOne->id)->symbol('ACME')->create();
$portfolioTwo = Portfolio::factory()->create();
Transaction::factory(2)->buy()->sixMonthsAgo()->portfolio($portfolioTwo->id)->symbol('ACME')->create();
Dividend::refreshDividendData('ACME');
$holdingOne = Holding::query()->portfolio($portfolioOne->id)->symbol('ACME')->first();
$holdingTwo = Holding::query()->portfolio($portfolioTwo->id)->symbol('ACME')->first();
$this->assertEquals(4.95, $holdingOne->dividends_earned);
$this->assertEquals(8, $holdingTwo->dividends_earned);
}
}