2024-08-10 13:30:19 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2024-09-06 19:50:28 -05:00
|
|
|
use App\Models\Holding;
|
|
|
|
|
use App\Models\MarketData;
|
|
|
|
|
use App\Models\Transaction;
|
2024-09-11 21:58:48 -05:00
|
|
|
use Illuminate\Support\Str;
|
2024-10-18 20:46:22 -05:00
|
|
|
use Illuminate\Support\Carbon;
|
2024-08-10 13:30:19 -05:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2024-08-30 21:58:38 -05:00
|
|
|
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 Dividend 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',
|
|
|
|
|
'dividend_amount',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $hidden = [];
|
|
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'date' => 'datetime',
|
|
|
|
|
'last_date' => 'datetime',
|
|
|
|
|
];
|
|
|
|
|
|
2024-09-01 15:34:55 -05:00
|
|
|
public function marketData() {
|
|
|
|
|
return $this->belongsTo(MarketData::class, 'symbol', 'symbol');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function holdings() {
|
|
|
|
|
return $this->hasMany(Holding::class, 'symbol', 'symbol');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function transactions() {
|
|
|
|
|
return $this->hasMany(Transaction::class, 'symbol', 'symbol');
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 21:58:48 -05:00
|
|
|
public function scopeSymbol($query, $symbol)
|
|
|
|
|
{
|
|
|
|
|
return $query->where('dividends.symbol', $symbol);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-10 13:30:19 -05:00
|
|
|
/**
|
2024-08-30 21:58:38 -05:00
|
|
|
* Grab new dividend data
|
2024-08-10 13:30:19 -05:00
|
|
|
*
|
2024-08-30 21:58:38 -05:00
|
|
|
* @param string $symbol
|
2024-08-10 13:30:19 -05:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2024-08-30 21:58:38 -05:00
|
|
|
public static function refreshDividendData(string $symbol)
|
2024-08-10 13:30:19 -05:00
|
|
|
{
|
2024-08-30 21:58:38 -05:00
|
|
|
$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);
|
2024-08-10 13:30:19 -05:00
|
|
|
}
|
|
|
|
|
|
2024-08-30 21:58:38 -05:00
|
|
|
// get some data
|
|
|
|
|
if ($dividend_data = collect() && $start_date && $end_date) {
|
|
|
|
|
$dividend_data = app(MarketDataInterface::class)->dividends($symbol, $start_date, $end_date);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ah, we found some dividends...
|
|
|
|
|
if ($dividend_data->isNotEmpty()) {
|
|
|
|
|
// create mass insert
|
|
|
|
|
foreach ($dividend_data as $index => $dividend){
|
2024-09-11 21:58:48 -05:00
|
|
|
$dividend_data[$index] = [...$dividend, ...['id' => Str::uuid()->toString(), 'updated_at' => now(), 'created_at' => now()]];
|
2024-08-30 21:58:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// insert records
|
|
|
|
|
(new self)->insert($dividend_data->toArray());
|
|
|
|
|
|
|
|
|
|
// sync to holdings
|
2024-10-18 20:46:22 -05:00
|
|
|
self::syncHoldings($symbol);
|
2024-08-30 21:58:38 -05:00
|
|
|
|
2024-10-18 20:46:22 -05:00
|
|
|
// get market data
|
2024-09-15 20:39:14 -05:00
|
|
|
$market_data = MarketData::firstOrNew(['symbol' => $symbol]);
|
2024-10-18 20:46:22 -05:00
|
|
|
|
|
|
|
|
// re-invest dividends
|
|
|
|
|
self::reinvestDividends($dividend_data, $market_data);
|
|
|
|
|
|
|
|
|
|
// sync last dividend amount to market data table
|
2024-09-11 21:58:48 -05:00
|
|
|
$market_data->last_dividend_amount = $dividend_data->sortByDesc('date')->first()['dividend_amount'];
|
2024-09-01 15:34:55 -05:00
|
|
|
$market_data->save();
|
2024-08-30 21:58:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $dividend_data;
|
2024-08-10 13:30:19 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-18 20:46:22 -05:00
|
|
|
public static function syncHoldings(string $symbol): void
|
2024-09-01 15:34:55 -05:00
|
|
|
{
|
|
|
|
|
// group by holdings
|
2024-09-15 23:42:11 -05:00
|
|
|
$dividends = self::select(['holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount'])
|
2024-09-16 00:18:19 -05:00
|
|
|
->selectRaw('
|
|
|
|
|
(COALESCE(CASE WHEN transactions.transaction_type = "BUY"
|
|
|
|
|
AND date(transactions.date) <= date(dividends.date)
|
|
|
|
|
THEN transactions.quantity ELSE 0 END, 0)
|
|
|
|
|
- COALESCE(CASE WHEN transactions.transaction_type = "SELL"
|
|
|
|
|
AND date(transactions.date) <= date(dividends.date)
|
|
|
|
|
THEN transactions.quantity ELSE 0 END, 0))
|
|
|
|
|
* dividends.dividend_amount
|
2024-09-16 00:24:01 -05:00
|
|
|
AS total_received
|
2024-09-15 23:42:11 -05:00
|
|
|
')
|
|
|
|
|
->join('transactions', 'transactions.symbol', '=', 'dividends.symbol')
|
|
|
|
|
->join('holdings', 'transactions.portfolio_id', '=', 'holdings.portfolio_id')
|
2024-10-18 20:46:22 -05:00
|
|
|
->where('dividends.symbol', $symbol)
|
2024-09-16 00:24:01 -05:00
|
|
|
->groupBy('holdings.portfolio_id', 'dividends.date', 'dividends.symbol', 'dividends.dividend_amount', 'total_received')
|
|
|
|
|
->havingRaw('total_received > 0')
|
2024-09-15 23:42:11 -05:00
|
|
|
->get();
|
2024-09-01 15:34:55 -05:00
|
|
|
|
|
|
|
|
// 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)
|
2024-09-16 00:24:01 -05:00
|
|
|
->sum('total_received')
|
2024-09-01 15:34:55 -05:00
|
|
|
]);
|
|
|
|
|
});
|
2024-08-10 13:30:19 -05:00
|
|
|
}
|
2024-10-18 20:46:22 -05:00
|
|
|
|
|
|
|
|
public static function reinvestDividends(iterable $dividend_data, MarketData $market_data): void
|
|
|
|
|
{
|
|
|
|
|
// re-invest dividends
|
|
|
|
|
Holding::where([
|
|
|
|
|
'symbol' => $market_data->symbol,
|
|
|
|
|
'reinvest_dividends' => true,
|
|
|
|
|
])
|
|
|
|
|
->get()
|
|
|
|
|
->each(function($holding) use ($dividend_data, $market_data) {
|
|
|
|
|
|
|
|
|
|
foreach($dividend_data as $dividend) {
|
|
|
|
|
|
|
|
|
|
Transaction::create([
|
|
|
|
|
'date' => $dividend['date'],
|
|
|
|
|
'portfolio_id' => $holding->portfolio_id,
|
|
|
|
|
'symbol' => $holding->symbol,
|
|
|
|
|
'transaction_type' => "BUY",
|
|
|
|
|
'reinvested_dividend' => true,
|
2024-10-18 22:08:52 -05:00
|
|
|
'cost_basis' => 0,
|
2024-10-18 20:46:22 -05:00
|
|
|
'quantity' => ($dividend['dividend_amount'] * $holding->qtyOwned(Carbon::parse($dividend['date']))) / $market_data->market_value,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-08-10 13:30:19 -05:00
|
|
|
}
|