fix splits command

and add splits test
This commit is contained in:
hackerESQ
2024-09-09 19:39:38 -05:00
parent 0c27ebaba5
commit 75fbd60a54
6 changed files with 80 additions and 32 deletions
+26 -20
View File
@@ -3,6 +3,7 @@
namespace App\Models; namespace App\Models;
use App\Models\Transaction; use App\Models\Transaction;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use App\Interfaces\MarketData\MarketDataInterface; use App\Interfaces\MarketData\MarketDataInterface;
@@ -68,8 +69,12 @@ class Split extends Model
} }
if ($split_data->isNotEmpty()) { if ($split_data->isNotEmpty()) {
// insert records // insert records
(new self)->insert($split_data->toArray()); (new self)->insert($split_data->map(function($split) {
return [...$split, ...['id' => Str::uuid()->toString()]];
})->toArray());
} }
// sync to transactions // sync to transactions
@@ -84,31 +89,34 @@ class Split extends Model
*/ */
public static function syncToTransactions($symbol) public static function syncToTransactions($symbol)
{ {
// get relevant split data // get splits joined with matching holdings
$splits = self::where([ $splits = self::select([
'splits.symbol' => $symbol, 'splits.date',
]) 'splits.symbol',
->whereDate('transactions.date', '>', DB::raw('IFNULL(holdings.splits_synced_at, "0000-00-00")')) 'splits.split_amount',
->select([ 'holdings.portfolio_id'
'splits.date', ])
'splits.symbol', ->where([
'splits.split_amount', 'splits.symbol' => $symbol,
'transactions.portfolio_id' ])
]) ->whereDate('splits.date', '>', DB::raw('IFNULL(holdings.splits_synced_at, "0000-00-00")'))
->join('transactions', 'transactions.symbol', 'splits.symbol') ->where('holdings.quantity', '>', 0)
->join('holdings', 'transactions.symbol', 'holdings.symbol') ->join('holdings', 'splits.symbol', 'holdings.symbol')
->orderBy('splits.date', 'ASC') ->orderBy('splits.date', 'ASC')
->get(); ->get();
foreach($splits as $split) { foreach($splits as $split) {
// get qty owned when split was issued
$qty_owned = Transaction::where([ $qty_owned = Transaction::where([
'symbol' => $split->symbol, 'symbol' => $split->symbol,
'portfolio_id' => $split->portfolio_id 'portfolio_id' => $split->portfolio_id
]) ])
->whereDate('transactions.date', '<', $split->date->format('Y-m-d')) ->whereDate('transactions.date', '<', $split->date->format('Y-m-d'))
->sum('quantity'); ->selectRaw('SUM(CASE WHEN transaction_type = "BUY" THEN quantity ELSE 0 END) -
SUM(CASE WHEN transaction_type = "SELL" THEN quantity ELSE 0 END) AS qty_owned')
->value('qty_owned');
if ($qty_owned > 0) { if ($qty_owned > 0) {
Transaction::create([ Transaction::create([
@@ -132,6 +140,4 @@ class Split extends Model
} }
} }
} }
} }
+3 -1
View File
@@ -21,7 +21,8 @@ class Transaction extends Model
'transaction_type', 'transaction_type',
'quantity', 'quantity',
'cost_basis', 'cost_basis',
'sale_price' 'sale_price',
'split',
]; ];
protected $hidden = []; protected $hidden = [];
@@ -185,6 +186,7 @@ class Transaction extends Model
'quantity' => $this->quantity, 'quantity' => $this->quantity,
'average_cost_basis' => $this->cost_basis, 'average_cost_basis' => $this->cost_basis,
'total_cost_basis' => $this->quantity * $this->cost_basis, 'total_cost_basis' => $this->quantity * $this->cost_basis,
'splits_synced_at' => now(),
])->syncTransactionsAndDividends(); ])->syncTransactionsAndDividends();
} }
} }
@@ -36,6 +36,13 @@ class TransactionFactory extends Factory
]; ];
} }
public function yearsAgo(): static
{
return $this->state(fn (array $attributes) => [
'date' => $this->faker->date('Y-m-d', '-3 years'),
]);
}
public function symbol($symbol): static public function symbol($symbol): static
{ {
return $this->state(fn (array $attributes) => [ return $this->state(fn (array $attributes) => [
@@ -92,7 +92,9 @@ new class extends Component {
@endscope @endscope
@scope('cell_transaction_type', $row) @scope('cell_transaction_type', $row)
<x-badge <x-badge
:value="$row->transaction_type" :value="$row->split
? 'SPLIT'
: $row->transaction_type"
class="{{ $row->transaction_type == 'BUY' class="{{ $row->transaction_type == 'BUY'
? 'badge-success' ? 'badge-success'
: 'badge-error' }} badge-sm mr-3" : 'badge-error' }} badge-sm mr-3"
+4 -8
View File
@@ -17,8 +17,7 @@ class DashboardTest extends TestCase
*/ */
public function test_user_has_portfolios(): void public function test_user_has_portfolios(): void
{ {
$user = User::factory()->create(); $this->actingAs($user = User::factory()->create());
$this->actingAs($user);
Portfolio::factory(5)->create(); Portfolio::factory(5)->create();
@@ -29,8 +28,7 @@ class DashboardTest extends TestCase
*/ */
public function test_user_has_transactions(): void public function test_user_has_transactions(): void
{ {
$user = User::factory()->create(); $this->actingAs($user = User::factory()->create());
$this->actingAs($user);
Transaction::factory(10)->create(); Transaction::factory(10)->create();
@@ -41,8 +39,7 @@ class DashboardTest extends TestCase
*/ */
public function test_user_has_holdings(): void public function test_user_has_holdings(): void
{ {
$user = User::factory()->create(); $this->actingAs($user = User::factory()->create());
$this->actingAs($user);
$portfolio = Portfolio::factory()->create(); $portfolio = Portfolio::factory()->create();
@@ -55,8 +52,7 @@ class DashboardTest extends TestCase
*/ */
public function test_user_has_dashboard_metrics(): void public function test_user_has_dashboard_metrics(): void
{ {
$user = User::factory()->create(); $this->actingAs($user = User::factory()->create());
$this->actingAs($user);
$portfolio = Portfolio::factory()->create(); $portfolio = Portfolio::factory()->create();
+37 -2
View File
@@ -3,6 +3,11 @@
namespace Tests; namespace Tests;
use Tests\TestCase; use Tests\TestCase;
use App\Models\User;
use App\Models\Split;
use App\Models\Holding;
use App\Models\Portfolio;
use App\Models\Transaction;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
class SplitsTest extends TestCase class SplitsTest extends TestCase
@@ -11,8 +16,38 @@ class SplitsTest extends TestCase
/** /**
*/ */
public function test_user_has_dashboard_metrics(): void public function test_splits_create_new_transaction(): void
{ {
// $this->actingAs($user = User::factory()->create());
$portfolio = Portfolio::factory()->create();
Transaction::factory()->buy()->yearsAgo()->portfolio($portfolio->id)->symbol('ACME')->create();
// manually reset the split last sync date (which is set when the holding is created)
Holding::query()->portfolio($portfolio->id)->symbol('ACME')->update([
'splits_synced_at' => null
]);
Split::refreshSplitData('ACME');
$transactions = Transaction::query()->symbol('ACME')->portfolio($portfolio->id)->get();
$this->assertCount(2, $transactions);
}
/**
*/
public function test_splits_do_not_create_new_transaction_if_already_synced(): void
{
$this->actingAs($user = User::factory()->create());
$portfolio = Portfolio::factory()->create();
Transaction::factory()->buy()->yearsAgo()->portfolio($portfolio->id)->symbol('ACME')->create();
Split::refreshSplitData('ACME');
$transactions = Transaction::query()->symbol('ACME')->portfolio($portfolio->id)->get();
$this->assertCount(1, $transactions);
} }
} }