2024-08-10 13:30:19 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use App\Models\Transaction;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use App\Interfaces\MarketData\MarketDataInterface;
|
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;
|
|
|
|
|
|
|
|
|
|
class Split extends Model
|
|
|
|
|
{
|
|
|
|
|
use HasFactory;
|
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',
|
|
|
|
|
'first_date' => 'datetime',
|
|
|
|
|
'last_date' => 'datetime',
|
|
|
|
|
];
|
|
|
|
|
|
2024-08-30 21:58:38 -05:00
|
|
|
public function holdings() {
|
|
|
|
|
return $this->hasMany(Holding::class, 'symbol', 'symbol');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function transactions() {
|
|
|
|
|
return $this->hasMany(Transaction::class, 'symbol', 'symbol');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Grab new split data
|
|
|
|
|
*
|
|
|
|
|
* @param string $symbol
|
|
|
|
|
* @param \DateTimeInterface|null $start_date
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public static function refreshSplitData(string $symbol)
|
|
|
|
|
{
|
|
|
|
|
// dates for split data
|
|
|
|
|
$splits_meta = self::where(['symbol' => $symbol])
|
|
|
|
|
->selectRaw('COUNT(symbol) as total_splits')
|
|
|
|
|
->selectRaw('MIN(date) as first_date')
|
|
|
|
|
->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) {
|
|
|
|
|
|
|
|
|
|
$start_date = $splits_meta->last_date->addHours(48);
|
|
|
|
|
$end_date = now();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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()) {
|
|
|
|
|
// insert records
|
|
|
|
|
(new self)->insert($split_data->toArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sync to transactions
|
|
|
|
|
self::syncToTransactions($symbol);
|
|
|
|
|
|
|
|
|
|
return $split_data;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-10 13:30:19 -05:00
|
|
|
/**
|
|
|
|
|
* Syncs all transactions of symbol with split data
|
|
|
|
|
*
|
|
|
|
|
* @param string $symbol
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public static function syncToTransactions($symbol)
|
|
|
|
|
{
|
|
|
|
|
// get relevant split data
|
|
|
|
|
$splits = self::where([
|
|
|
|
|
'splits.symbol' => $symbol,
|
|
|
|
|
])
|
|
|
|
|
->whereDate('transactions.date', '>', DB::raw('IFNULL(market_data.splits_synced_to_holdings_at, "0000-00-00")'))
|
|
|
|
|
->select([
|
|
|
|
|
'splits.date',
|
|
|
|
|
'splits.symbol',
|
|
|
|
|
'splits.split_amount',
|
|
|
|
|
'transactions.portfolio_id'
|
|
|
|
|
])
|
|
|
|
|
->join('transactions', 'transactions.symbol', 'splits.symbol')
|
|
|
|
|
->join('market_data', 'transactions.symbol', 'market_data.symbol')
|
|
|
|
|
->orderBy('splits.date', 'ASC')
|
|
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
foreach($splits as $split) {
|
|
|
|
|
|
|
|
|
|
$qty_owned = Transaction::where([
|
|
|
|
|
'symbol' => $split->symbol,
|
|
|
|
|
'portfolio_id' => $split->portfolio_id
|
|
|
|
|
])
|
|
|
|
|
->whereDate('transactions.date', '<', $split->date->format('Y-m-d'))
|
|
|
|
|
->sum('quantity');
|
|
|
|
|
|
|
|
|
|
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(),
|
|
|
|
|
'updated_at' => now()
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-30 21:58:38 -05:00
|
|
|
// // update market data with latest date
|
|
|
|
|
// MarketData::setSplitsHoldingSynced($symbol);
|
2024-08-10 13:30:19 -05:00
|
|
|
}
|
|
|
|
|
|
2024-08-30 21:58:38 -05:00
|
|
|
|
2024-08-10 13:30:19 -05:00
|
|
|
}
|