@@ -421,6 +421,14 @@ class Holding extends Model
|
||||
->selectRaw("SUM(CASE WHEN transaction_type = 'BUY' THEN (quantity * cost_basis) ELSE 0 END) AS total_cost_basis")
|
||||
->first();
|
||||
|
||||
// delete holding if no transactions
|
||||
if (empty($query->qty_purchases + $query->qty_sales)) {
|
||||
|
||||
$this->delete();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$total_quantity = round($query->qty_purchases - $query->qty_sales, 4);
|
||||
|
||||
$average_cost_basis = (
|
||||
|
||||
@@ -59,4 +59,19 @@ class HoldingsTest extends TestCase
|
||||
$holding = Holding::query()->getPortfolioMetrics();
|
||||
$this->assertEquals(400, $holding->get('total_cost_basis'));
|
||||
}
|
||||
|
||||
public function test_delete_holding_on_sync_if_no_transactions(): void
|
||||
{
|
||||
$this->actingAs($user = User::factory()->create());
|
||||
|
||||
$portfolio = Portfolio::factory()->create();
|
||||
|
||||
$transaction = Transaction::factory()->buy()->lastYear()->costBasis(100)->portfolio($portfolio->id)->symbol('AAPL')->create();
|
||||
|
||||
$this->assertDatabaseCount('holdings', 1);
|
||||
|
||||
$transaction->delete();
|
||||
|
||||
$this->assertDatabaseEmpty('holdings');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,18 +78,20 @@ class ImportExportTest extends TestCase
|
||||
{
|
||||
$this->actingAs($user = User::factory()->create());
|
||||
|
||||
Portfolio::create([
|
||||
$portfolio = Portfolio::create([
|
||||
'id' => '9e792bb8-94e7-4ed3-b8cc-43b50d34c337',
|
||||
'title' => 'Test Portfolio',
|
||||
]);
|
||||
|
||||
$holding = Holding::create([
|
||||
'portfolio_id' => '9e792bb8-94e7-4ed3-b8cc-43b50d34c337',
|
||||
'portfolio_id' => $portfolio->id,
|
||||
'symbol' => 'ACME',
|
||||
'quantity' => 0,
|
||||
'reinvest_dividends' => false,
|
||||
]);
|
||||
|
||||
Transaction::factory()->buy()->lastYear()->costBasis(100)->portfolio($portfolio->id)->symbol('AAPL')->create();
|
||||
|
||||
$this->assertEquals(false, $holding->reinvest_dividends);
|
||||
|
||||
BackupImportModel::create([
|
||||
|
||||
Reference in New Issue
Block a user