fix: cost basis for holding calculations
This commit is contained in:
+6
-13
@@ -323,19 +323,12 @@ class Holding extends Model
|
|||||||
AS rate"
|
AS rate"
|
||||||
)
|
)
|
||||||
->selectRaw(
|
->selectRaw(
|
||||||
"(CASE
|
"CASE
|
||||||
WHEN transactions.transaction_type = 'BUY'
|
WHEN transactions.transaction_type = 'BUY'
|
||||||
THEN AVG(transactions.cost_basis_base)
|
THEN transactions.quantity
|
||||||
ELSE (
|
ELSE -transactions.quantity
|
||||||
SELECT
|
END
|
||||||
AVG(-buy.cost_basis_base)
|
AS remaining_quantity"
|
||||||
FROM transactions as buy
|
|
||||||
WHERE buy.symbol = transactions.symbol
|
|
||||||
AND buy.portfolio_id = transactions.portfolio_id
|
|
||||||
AND buy.transaction_type = 'BUY'
|
|
||||||
AND buy.date <= transactions.date
|
|
||||||
) END)
|
|
||||||
AS cost_basis_base"
|
|
||||||
)
|
)
|
||||||
->groupBy([
|
->groupBy([
|
||||||
'transactions.symbol',
|
'transactions.symbol',
|
||||||
@@ -353,7 +346,7 @@ class Holding extends Model
|
|||||||
"SUM(CASE WHEN transactions.transaction_type = 'SELL' THEN (transactions.sale_price_base - transactions.cost_basis_base) * transactions.quantity * COALESCE(cr.rate, 1) ELSE 0 END) AS realized_gain_dollars"
|
"SUM(CASE WHEN transactions.transaction_type = 'SELL' THEN (transactions.sale_price_base - transactions.cost_basis_base) * transactions.quantity * COALESCE(cr.rate, 1) ELSE 0 END) AS realized_gain_dollars"
|
||||||
)
|
)
|
||||||
->selectRaw(
|
->selectRaw(
|
||||||
'SUM(cost_basis_display.cost_basis_base * cost_basis_display.quantity * cost_basis_display.rate) AS total_cost_basis'
|
"SUM(CASE WHEN transactions.transaction_type = 'BUY' THEN transactions.cost_basis_base * transactions.quantity * cost_basis_display.rate END) / SUM(CASE WHEN transactions.transaction_type = 'BUY' THEN transactions.quantity END) * SUM(cost_basis_display.remaining_quantity) AS total_cost_basis"
|
||||||
)
|
)
|
||||||
->groupBy(['transactions.symbol', 'transactions.portfolio_id']),
|
->groupBy(['transactions.symbol', 'transactions.portfolio_id']),
|
||||||
'transactions_display',
|
'transactions_display',
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests;
|
||||||
|
|
||||||
|
use App\Models\Holding;
|
||||||
|
use App\Models\Portfolio;
|
||||||
|
use App\Models\Transaction;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
|
||||||
|
class HoldingsTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
public function test_calculates_cost_basis(): void
|
||||||
|
{
|
||||||
|
$this->actingAs($user = User::factory()->create());
|
||||||
|
|
||||||
|
$portfolio = Portfolio::factory()->create();
|
||||||
|
|
||||||
|
Transaction::factory()->buy()->lastYear()->costBasis(200)->portfolio($portfolio->id)->symbol('AAPL')->create();
|
||||||
|
Transaction::factory()->buy()->lastMonth()->costBasis(300)->portfolio($portfolio->id)->symbol('AAPL')->create();
|
||||||
|
$holding = Holding::query()->getPortfolioMetrics();
|
||||||
|
$this->assertEquals(500, $holding->get('total_cost_basis'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_calculates_cost_basis_after_multiple_sales(): void
|
||||||
|
{
|
||||||
|
$this->actingAs($user = User::factory()->create());
|
||||||
|
|
||||||
|
$portfolio = Portfolio::factory()->create();
|
||||||
|
|
||||||
|
Transaction::factory()->buy()->lastYear()->costBasis(200)->portfolio($portfolio->id)->symbol('AAPL')->create();
|
||||||
|
Transaction::factory()->buy()->lastMonth()->costBasis(300)->portfolio($portfolio->id)->symbol('AAPL')->create();
|
||||||
|
|
||||||
|
Transaction::factory()->sell()->recent()->costBasis(250)->portfolio($portfolio->id)->symbol('AAPL')->create();
|
||||||
|
$holding = Holding::query()->getPortfolioMetrics();
|
||||||
|
$this->assertEquals(250, $holding->get('total_cost_basis'));
|
||||||
|
|
||||||
|
Transaction::factory()->sell()->recent()->costBasis(250)->portfolio($portfolio->id)->symbol('AAPL')->create();
|
||||||
|
$holding = Holding::query()->getPortfolioMetrics();
|
||||||
|
$this->assertEquals(0, $holding->get('total_cost_basis'));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user