@@ -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")
|
->selectRaw("SUM(CASE WHEN transaction_type = 'BUY' THEN (quantity * cost_basis) ELSE 0 END) AS total_cost_basis")
|
||||||
->first();
|
->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);
|
$total_quantity = round($query->qty_purchases - $query->qty_sales, 4);
|
||||||
|
|
||||||
$average_cost_basis = (
|
$average_cost_basis = (
|
||||||
|
|||||||
@@ -59,4 +59,19 @@ class HoldingsTest extends TestCase
|
|||||||
$holding = Holding::query()->getPortfolioMetrics();
|
$holding = Holding::query()->getPortfolioMetrics();
|
||||||
$this->assertEquals(400, $holding->get('total_cost_basis'));
|
$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());
|
$this->actingAs($user = User::factory()->create());
|
||||||
|
|
||||||
Portfolio::create([
|
$portfolio = Portfolio::create([
|
||||||
'id' => '9e792bb8-94e7-4ed3-b8cc-43b50d34c337',
|
'id' => '9e792bb8-94e7-4ed3-b8cc-43b50d34c337',
|
||||||
'title' => 'Test Portfolio',
|
'title' => 'Test Portfolio',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$holding = Holding::create([
|
$holding = Holding::create([
|
||||||
'portfolio_id' => '9e792bb8-94e7-4ed3-b8cc-43b50d34c337',
|
'portfolio_id' => $portfolio->id,
|
||||||
'symbol' => 'ACME',
|
'symbol' => 'ACME',
|
||||||
'quantity' => 0,
|
'quantity' => 0,
|
||||||
'reinvest_dividends' => false,
|
'reinvest_dividends' => false,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
Transaction::factory()->buy()->lastYear()->costBasis(100)->portfolio($portfolio->id)->symbol('AAPL')->create();
|
||||||
|
|
||||||
$this->assertEquals(false, $holding->reinvest_dividends);
|
$this->assertEquals(false, $holding->reinvest_dividends);
|
||||||
|
|
||||||
BackupImportModel::create([
|
BackupImportModel::create([
|
||||||
|
|||||||
Reference in New Issue
Block a user