Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6d6f968f42 | |||
| 261c848ffd | |||
| 9bcc80078e |
@@ -68,7 +68,7 @@ class Dividend extends Model
|
||||
// nope, refresh forward looking only
|
||||
if ($dividends_meta->total_dividends) {
|
||||
|
||||
$start_date = $dividends_meta->last_dividend_update->addHours(24);
|
||||
$start_date = $dividends_meta->last_dividend_update;
|
||||
}
|
||||
|
||||
// skip refresh if there's already recent data
|
||||
@@ -90,7 +90,7 @@ class Dividend extends Model
|
||||
}
|
||||
|
||||
// insert records
|
||||
(new self)->insert($dividend_data->toArray());
|
||||
(new self)->insertOrIgnore($dividend_data->toArray());
|
||||
|
||||
// sync to holdings
|
||||
self::syncHoldings($symbol);
|
||||
|
||||
@@ -73,7 +73,7 @@ class Split extends Model
|
||||
if ($split_data->isNotEmpty()) {
|
||||
|
||||
// insert records
|
||||
(new self)->insert($split_data->map(function ($split) {
|
||||
(new self)->insertOrIgnore($split_data->map(function ($split) {
|
||||
|
||||
return [...$split, ...['id' => Str::uuid()->toString()]];
|
||||
})->toArray());
|
||||
|
||||
@@ -21,6 +21,8 @@ class CreateDividendsTable extends Migration
|
||||
$table->string('symbol', 25);
|
||||
$table->float('dividend_amount', 12, 4);
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['date', 'symbol']);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,8 @@ class CreateSplitsTable extends Migration
|
||||
$table->string('symbol', 25);
|
||||
$table->float('split_amount', 12, 4);
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['date', 'symbol']);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,6 @@ class CreateHoldingsTable extends Migration
|
||||
$table->float('total_cost_basis', 12, 4)->default(0);
|
||||
$table->float('realized_gain_dollars', 12, 4)->default(0);
|
||||
$table->float('dividends_earned', 12, 4)->default(0);
|
||||
$table->boolean('reinvest_dividends')->default(false);
|
||||
$table->timestamp('splits_synced_at')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
+8
-15
@@ -58,23 +58,16 @@ class DividendsTest extends TestCase
|
||||
$this->assertEqualsWithDelta(4.95, $dividendsReinvested * $market_data->market_value, 0.01);
|
||||
}
|
||||
|
||||
public function test_do_not_duplicate_recent_dividends(): void
|
||||
public function test_cannot_insert_duplicate_dividends(): 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();
|
||||
|
||||
Dividend::create([
|
||||
'symbol' => 'ACME',
|
||||
'date' => now()->subDay(2),
|
||||
'dividend_amount' => .01,
|
||||
]);
|
||||
|
||||
// first insert
|
||||
Dividend::refreshDividendData('ACME');
|
||||
|
||||
$this->assertCount(1, $holding->dividends);
|
||||
// try to duplicate
|
||||
Dividend::refreshDividendData('ACME');
|
||||
|
||||
$dividend_count = Dividend::count();
|
||||
|
||||
$this->assertEquals(3, $dividend_count);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user