This commit is contained in:
hackerESQ
2024-08-30 21:58:38 -05:00
parent 2a42ce9d12
commit d6cf867c6d
13 changed files with 361 additions and 122 deletions
+65 -25
View File
@@ -3,6 +3,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Interfaces\MarketData\MarketDataInterface;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -26,38 +27,77 @@ class Dividend extends Model
];
/**
* Syncs all holdings of symbol with dividend data
* Grab new dividend data
*
* @param array|self $model
* @param string $symbol
* @return void
*/
public static function syncHoldings(mixed $model)
public static function refreshDividendData(string $symbol)
{
// check if we got an array, if yes then lets create a dummy model
if (is_array($model)) {
$model = (new self)->fill($model);
$dividends_meta = self::where(['symbol' => $symbol])
->selectRaw('COUNT(symbol) as total_dividends')
->selectRaw('MAX(date) as last_date')
->get()
->first();
// assume we need to populate ALL dividend data
$start_date = new \DateTime('@0');
$end_date = now();
// nope, refresh forward looking only
if ( $dividends_meta->total_dividends ) {
$start_date = $dividends_meta->last_date->addHours(48);
$end_date = now();
}
// pull dividend data joined with holdings/transactions
$dividends = self::where(['dividends.symbol' => $model->symbol])
->select(['holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount'])
->selectRaw('@purchased:=(SELECT coalesce(SUM(quantity),0) FROM transactions WHERE transactions.transaction_type = "BUY" AND transactions.symbol = dividends.symbol AND date(transactions.date) <= date(dividends.date) AND holdings.portfolio_id = transactions.portfolio_id ) AS `purchased`')
->selectRaw('@sold:=(SELECT coalesce(SUM(quantity),0) FROM transactions WHERE transactions.transaction_type = "SELL" AND transactions.symbol = dividends.symbol AND date(transactions.date) <= date(dividends.date) AND holdings.portfolio_id = transactions.portfolio_id ) AS `sold`')
->selectRaw('@owned:=(@purchased - @sold) AS `owned`')
->selectRaw('@dividends_received:=(@owned * dividends.dividend_amount) AS `dividends_received`')
->join('transactions', 'transactions.symbol', 'dividends.symbol')
->join('holdings', 'transactions.portfolio_id', 'holdings.portfolio_id')
->groupBy(['holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount'])
->get();
// get some data
if ($dividend_data = collect() && $start_date && $end_date) {
$dividend_data = app(MarketDataInterface::class)->dividends($symbol, $start_date, $end_date);
}
// iterate through holdings and update
Holding::where(['symbol' => $model->symbol])
->get()
->each(function ($holding) use ($dividends) {
$holding->update([
'dividends_earned' => $dividends->where('portfolio_id', $holding->portfolio_id)->sum('dividends_received')
]);
});
// ah, we found some dividends...
if ($dividend_data->isNotEmpty()) {
// create mass insert
foreach ($dividend_data as $index => $dividend){
$dividend_data[$index] = [...$dividend, ...['updated_at' => now(), 'created_at' => now()]];
}
// insert records
(new self)->insert($dividend_data->toArray());
// sync to holdings
$dividends = self::where([
'dividends.symbol' => $dividend_data->last()->symbol,
])->select(['holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount'])
->selectRaw('@purchased:=(SELECT coalesce(SUM(quantity),0) FROM transactions WHERE transactions.transaction_type = "BUY" AND transactions.symbol = dividends.symbol AND date(transactions.date) <= date(dividends.date) AND holdings.portfolio_id = transactions.portfolio_id ) AS `purchased`')
->selectRaw('@sold:=(SELECT coalesce(SUM(quantity),0) FROM transactions WHERE transactions.transaction_type = "SELL" AND transactions.symbol = dividends.symbol AND date(transactions.date) <= date(dividends.date) AND holdings.portfolio_id = transactions.portfolio_id ) AS `sold`')
->selectRaw('@owned:=(@purchased - @sold) AS `owned`')
->selectRaw('@dividends_received:=(@owned * dividends.dividend_amount) AS `dividends_received`')
->join('transactions', 'transactions.symbol', 'dividends.symbol')
->join('holdings', 'transactions.portfolio_id', 'holdings.portfolio_id')
->groupBy(['holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount'])
->get();
// iterate through holdings and update
Holding::where(['symbol' => $symbol])
->get()
->each(function ($holding) use ($dividends) {
$holding->update([
'dividends_earned' => $dividends->where('portfolio_id', $holding->portfolio_id)->sum('dividends_received')
]);
});
// sync most last dividend date in market data
$market_data = MarketData::symbol($symbol)->first();
$dividend_data_latest_date = $dividend_data->sortByDesc('date')->first()['date'];
if ($market_data->dividend_date < $dividend_data_latest_date) {
$market_data->update(['dividend_date' => $dividend_data_latest_date]); // why is this set to latest date?
}
}
return $dividend_data;
}
public function marketData() {
+1 -1
View File
@@ -165,7 +165,7 @@ class Holding extends Model
->join('market_data', 'market_data.symbol', 'holdings.symbol');
}
public function sync()
public function syncTransactionsAndDividends()
{
// pull existing transaction data
$query = Transaction::where([
+6 -16
View File
@@ -34,19 +34,19 @@ class MarketData extends Model
'market_cap' => 0
];
public static function setSplitsHoldingSynced($symbol)
public function holdings()
{
$market_data = self::where('symbol', $symbol)->get()->first();
return $this->hasMany(Holding::class, 'symbol', 'symbol');
}
$market_data->splits_synced_to_holdings_at = now();
$market_data->save();
public function scopeSymbol($query, $symbol)
{
return $query->where('symbol', $symbol);
}
public function refreshMarketData()
{
return static::getMarketData($this->attributes['symbol']);
}
public static function getMarketData($symbol)
@@ -74,14 +74,4 @@ class MarketData extends Model
return $market_data;
}
public function holdings()
{
return $this->hasMany(Holding::class, 'symbol', 'symbol');
}
public function scopeSymbol($query, $symbol)
{
return $query->where('symbol', $symbol);
}
}
+55 -54
View File
@@ -28,6 +28,58 @@ class Split extends Model
'last_date' => 'datetime',
];
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;
}
/**
* Syncs all transactions of symbol with split data
*
@@ -77,60 +129,9 @@ class Split extends Model
}
}
// update market data with latest date
MarketData::setSplitsHoldingSynced($symbol);
// // update market data with latest date
// MarketData::setSplitsHoldingSynced($symbol);
}
/**
* Grab new split data
*
* @param string $symbol
* @param \DateTimeInterface|null $start_date
* @return void
*/
public static function getSplitData(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;
}
public function holdings() {
return $this->hasMany(Holding::class, 'symbol', 'symbol');
}
public function transactions() {
return $this->hasMany(Transaction::class, 'symbol', 'symbol');
}
}
+5 -12
View File
@@ -44,18 +44,16 @@ class Transaction extends Model
static::saved(function ($transaction) {
$transaction->syncHolding();
$transaction->syncToHolding();
$transaction->refreshMarketData();
$transaction->syncDividendsToHolding();
cache()->tags(['metrics', auth()->user()->id])->flush();
});
static::deleted(function ($transaction) {
$transaction->syncHolding();
$transaction->syncToHolding();
cache()->tags(['metrics', auth()->user()->id])->flush();
});
@@ -124,11 +122,6 @@ class Transaction extends Model
{
return MarketData::getMarketData($this->attributes['symbol']);
}
public function syncDividendsToHolding()
{
return Dividend::syncHoldings(['symbol' => $this->attributes['symbol']]);
}
/**
* Writes average cost basis to a sale transaction
@@ -154,7 +147,7 @@ class Transaction extends Model
*
* @return void
*/
public function syncHolding() {
public function syncToHolding() {
// if symbol name changed, sync previous symbol too
if (Arr::has($this->changes, 'symbol')) {
@@ -163,7 +156,7 @@ class Transaction extends Model
$temp->symbol = $this->original['symbol'];
$temp->portfolio_id = $this->portfolio_id;
$temp->syncHolding();
$temp->syncToHolding();
}
// get the holding for a symbol and portfolio (or create one)
@@ -178,6 +171,6 @@ class Transaction extends Model
'total_cost_basis' => $this->quantity * $this->cost_basis,
]);
$holding->sync();
$holding->syncTransactionsAndDividends();
}
}