adds dividend re-investment feature

This commit is contained in:
hackerESQ
2024-10-18 20:46:22 -05:00
parent e4d45f391c
commit 51c33ebec0
14 changed files with 218 additions and 17 deletions
+27
View File
@@ -34,4 +34,31 @@ class DividendsTest extends TestCase
$this->assertEquals(4.95, $holding->dividends_earned);
}
/**
*/
public function test_new_dividends_are_reinvested(): void
{
$this->actingAs($user = User::factory()->create());
$portfolio = Portfolio::factory()->create();
Transaction::factory()->buy()->yearsAgo()->portfolio($portfolio->id)->symbol('ACME')->create();
$holding = Holding::query()->portfolio($portfolio->id)->symbol('ACME')->first();
$holding->reinvest_dividends = true;
$holding->save();
$this->assertEquals(0, $holding->dividends_earned);
Dividend::refreshDividendData('ACME');
$transactions = Transaction::where(['reinvested_dividend' => true])->symbol('ACME')->portfolio($portfolio->id)->get();
$dividendsReinvested = $transactions->reduce(function ($carry, $transaction) {
return $carry + ($transaction->cost_basis * $transaction->quantity);
}, 0);
$this->assertCount(3, $transactions);
$this->assertEqualsWithDelta(4.95, $dividendsReinvested, 0.01);
}
}