Files
investbrain/app/Models/Split.php
T

149 lines
4.4 KiB
PHP
Raw Normal View History

2024-08-10 13:30:19 -05:00
<?php
2025-01-28 17:33:54 -06:00
declare(strict_types=1);
2024-08-10 13:30:19 -05:00
namespace App\Models;
use App\Interfaces\MarketData\MarketDataInterface;
2025-04-09 19:25:15 -05:00
use App\Traits\HasMarketData;
2024-08-17 18:40:50 -05:00
use Illuminate\Database\Eloquent\Concerns\HasUuids;
2024-08-10 13:30:19 -05:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2025-01-28 17:14:49 -06:00
use Illuminate\Database\Eloquent\Model;
2025-04-09 19:25:15 -05:00
use Illuminate\Database\Eloquent\Relations\HasMany;
2025-01-28 17:14:49 -06:00
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
2024-08-10 13:30:19 -05:00
class Split extends Model
{
use HasFactory;
2025-04-09 19:25:15 -05:00
use HasMarketData;
2024-08-17 18:40:50 -05:00
use HasUuids;
2024-08-10 13:30:19 -05:00
protected $fillable = [
'symbol',
'date',
'split_amount',
];
protected $hidden = [];
protected $casts = [
'date' => 'datetime',
'last_date' => 'datetime',
];
2025-04-09 19:25:15 -05:00
public function holdings(): HasMany
2025-01-28 17:14:49 -06:00
{
2024-08-30 21:58:38 -05:00
return $this->hasMany(Holding::class, 'symbol', 'symbol');
}
2025-04-09 19:25:15 -05:00
public function transactions(): HasMany
2025-01-28 17:14:49 -06:00
{
2024-08-30 21:58:38 -05:00
return $this->hasMany(Transaction::class, 'symbol', 'symbol');
}
/**
* Grab new split data
*
2025-01-28 17:14:49 -06:00
* @param \DateTimeInterface|null $start_date
2024-08-30 21:58:38 -05:00
* @return void
*/
2025-01-28 17:14:49 -06:00
public static function refreshSplitData(string $symbol)
2024-08-30 21:58:38 -05:00
{
// dates for split data
$splits_meta = self::where(['symbol' => $symbol])
->selectRaw('COUNT(symbol) as total_splits')
->selectRaw('MAX(date) as last_date')
->get()
->first();
// assume need to populate all split data because it didnt exist before
$start_date = new \DateTime('@0');
$end_date = now();
// nope, need to populate newer split data
if ($splits_meta->total_splits) {
2025-01-28 17:14:49 -06:00
2024-08-30 21:58:38 -05:00
$start_date = $splits_meta->last_date->addHours(48);
2025-01-28 17:14:49 -06:00
$end_date = now();
2024-08-30 21:58:38 -05:00
}
// get some data
if ($split_data = collect() && $start_date && $end_date) {
$split_data = app(MarketDataInterface::class)->splits($symbol, $start_date, $end_date);
}
if ($split_data->isNotEmpty()) {
2024-09-09 19:39:38 -05:00
2024-08-30 21:58:38 -05:00
// insert records
(new self)->insertOrIgnore($split_data->map(function ($split) {
2024-09-09 19:39:38 -05:00
return [...$split, ...['id' => Str::uuid()->toString()]];
2025-01-28 17:14:49 -06:00
})->toArray());
2024-08-30 21:58:38 -05:00
}
// sync to transactions
self::syncToTransactions($symbol);
}
2024-08-10 13:30:19 -05:00
/**
* Syncs all transactions of symbol with split data
*
2025-01-28 17:14:49 -06:00
* @param string $symbol
2024-08-10 13:30:19 -05:00
* @return void
*/
2025-01-28 17:14:49 -06:00
public static function syncToTransactions($symbol)
2024-08-10 13:30:19 -05:00
{
2024-09-09 19:39:38 -05:00
// get splits joined with matching holdings
$splits = self::select([
2025-01-28 17:14:49 -06:00
'splits.date',
'splits.symbol',
'splits.split_amount',
'holdings.portfolio_id',
])
->where([
'splits.symbol' => $symbol,
])
2025-03-10 21:17:24 -05:00
->whereDate('splits.date', '>', DB::raw("COALESCE(holdings.splits_synced_at, '1901-01-01')"))
2025-01-28 17:14:49 -06:00
->where('holdings.quantity', '>', 0)
->join('holdings', 'splits.symbol', 'holdings.symbol')
->orderBy('splits.date', 'ASC')
->get();
foreach ($splits as $split) {
2024-08-10 13:30:19 -05:00
2024-09-09 19:39:38 -05:00
// get qty owned when split was issued
2024-08-10 13:30:19 -05:00
$qty_owned = Transaction::where([
2025-01-28 17:14:49 -06:00
'symbol' => $split->symbol,
'portfolio_id' => $split->portfolio_id,
])
2025-04-09 19:25:15 -05:00
->whereDate('transactions.date', '<', $split->date->toDateString())
2025-03-10 21:17:24 -05:00
->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")
2024-09-09 19:39:38 -05:00
->value('qty_owned');
2025-01-28 17:14:49 -06:00
2024-08-10 13:30:19 -05:00
if ($qty_owned > 0) {
Transaction::create([
'symbol' => $split->symbol,
'portfolio_id' => $split->portfolio_id,
'transaction_type' => 'BUY',
'date' => $split->date,
'quantity' => ($qty_owned * $split->split_amount) - $qty_owned,
'cost_basis' => 0,
'split' => true,
'created_at' => now(),
2025-01-28 17:14:49 -06:00
'updated_at' => now(),
2024-08-10 13:30:19 -05:00
]);
2024-09-05 21:21:18 -05:00
Holding::where([
'symbol' => $split->symbol,
2025-01-28 17:14:49 -06:00
'portfolio_id' => $split->portfolio_id,
2024-09-05 21:21:18 -05:00
])->update([
2025-01-28 17:14:49 -06:00
'splits_synced_at' => now(),
2024-09-05 21:21:18 -05:00
]);
2024-08-10 13:30:19 -05:00
}
}
}
}