add events to import
This commit is contained in:
@@ -4,18 +4,31 @@ namespace App\Imports;
|
||||
|
||||
use App\Imports\Sheets\SplitsSheet;
|
||||
use App\Imports\Sheets\DividendsSheet;
|
||||
use App\Imports\Sheets\DailyChangesSheet;
|
||||
use App\Imports\Sheets\MarketDataSheet;
|
||||
use App\Imports\Sheets\PortfoliosSheet;
|
||||
use Maatwebsite\Excel\Events\AfterSheet;
|
||||
use App\Imports\Sheets\DailyChangesSheet;
|
||||
use App\Imports\Sheets\TransactionsSheet;
|
||||
use Maatwebsite\Excel\Concerns\Importable;
|
||||
use Maatwebsite\Excel\Concerns\WithEvents;
|
||||
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
|
||||
|
||||
class BackupImport implements WithMultipleSheets
|
||||
class BackupImport implements WithMultipleSheets, WithEvents
|
||||
{
|
||||
|
||||
use Importable;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function registerEvents(): array
|
||||
{
|
||||
return [
|
||||
|
||||
// AfterSheet::class => dd('test')
|
||||
];
|
||||
}
|
||||
|
||||
public function sheets(): array
|
||||
{
|
||||
return [
|
||||
@@ -23,7 +36,5 @@ class BackupImport implements WithMultipleSheets
|
||||
'Transactions' => new TransactionsSheet,
|
||||
'Daily Changes' => new DailyChangesSheet,
|
||||
];
|
||||
|
||||
// Can listen for AfterSheet to run clean up afterwards
|
||||
}
|
||||
}
|
||||
|
||||
+10
-67
@@ -3,7 +3,6 @@
|
||||
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;
|
||||
|
||||
@@ -40,17 +39,16 @@ class Dividend extends Model
|
||||
}
|
||||
|
||||
// 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();
|
||||
$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();
|
||||
|
||||
// iterate through holdings and update
|
||||
Holding::where(['symbol' => $model->symbol])
|
||||
@@ -62,61 +60,6 @@ class Dividend extends Model
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Grab new dividend data
|
||||
*
|
||||
* @param string $symbol
|
||||
* @return void
|
||||
*/
|
||||
public static function getDividendData(string $symbol)
|
||||
{
|
||||
$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();
|
||||
}
|
||||
|
||||
// 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){
|
||||
$dividend_data[$index] = [...$dividend, ...['updated_at' => now(), 'created_at' => now()]];
|
||||
}
|
||||
|
||||
// insert records
|
||||
(new self)->insert($dividend_data->toArray());
|
||||
|
||||
// sync to holdings
|
||||
self::syncHoldings($dividend_data->last());
|
||||
|
||||
// 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() {
|
||||
return $this->belongsTo(MarketData::class, 'symbol', 'symbol');
|
||||
}
|
||||
|
||||
+2
-15
@@ -66,11 +66,7 @@ class Holding extends Model
|
||||
public function dividends()
|
||||
{
|
||||
return $this->hasMany(Dividend::class, 'symbol', 'symbol')
|
||||
->select([
|
||||
'dividends.symbol',
|
||||
'dividends.date',
|
||||
'dividends.dividend_amount',
|
||||
])
|
||||
->select(['dividends.symbol','dividends.date','dividends.dividend_amount'])
|
||||
->selectRaw("SUM(
|
||||
CASE WHEN transaction_type = 'BUY'
|
||||
AND transactions.symbol = dividends.symbol
|
||||
@@ -88,11 +84,7 @@ class Holding extends Model
|
||||
ELSE 0 END
|
||||
) AS sold")
|
||||
->join('transactions', 'transactions.symbol', 'dividends.symbol')
|
||||
->groupBy([
|
||||
'dividends.symbol',
|
||||
'dividends.date',
|
||||
'dividends.dividend_amount',
|
||||
])
|
||||
->groupBy(['dividends.symbol','dividends.date','dividends.dividend_amount'])
|
||||
->orderBy('dividends.date', 'DESC')
|
||||
->where('dividends.date', '>=', function ($query) {
|
||||
$query->selectRaw('min(transactions.date)')
|
||||
@@ -172,11 +164,6 @@ class Holding extends Model
|
||||
// ->selectRaw('COALESCE((@total_gain_dollars / @sum_total_cost_basis) * 100,0) AS total_gain_percent')
|
||||
->join('market_data', 'market_data.symbol', 'holdings.symbol');
|
||||
}
|
||||
|
||||
// public function refreshDividends()
|
||||
// {
|
||||
// return Dividend::getDividendData($this->attributes['symbol']);
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
@@ -121,15 +121,10 @@ class Transaction extends Model
|
||||
return MarketData::getMarketData($this->attributes['symbol']);
|
||||
}
|
||||
|
||||
// public function syncDividendsToHolding()
|
||||
// {
|
||||
// return Dividend::syncHoldings(['symbol' => $this->attributes['symbol']]);
|
||||
// }
|
||||
|
||||
// public function refreshDividends()
|
||||
// {
|
||||
// return Dividend::getDividendData($this->attributes['symbol']);
|
||||
// }
|
||||
public function syncDividendsToHolding()
|
||||
{
|
||||
return Dividend::syncHoldings(['symbol' => $this->attributes['symbol']]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes average cost basis to a sale transaction
|
||||
@@ -157,7 +152,7 @@ class Transaction extends Model
|
||||
*/
|
||||
public function syncHolding() {
|
||||
|
||||
// sync previous symbol too
|
||||
// if symbol name changed, sync previous symbol too
|
||||
if (Arr::has($this->changes, 'symbol')) {
|
||||
|
||||
$temp = new Transaction;
|
||||
@@ -204,10 +199,8 @@ class Transaction extends Model
|
||||
|
||||
$holding->save();
|
||||
|
||||
// load market data while we're here
|
||||
$this->refreshMarketData();
|
||||
|
||||
// // sync dividends to holding
|
||||
// $this->syncDividendsToHolding();
|
||||
$this->syncDividendsToHolding();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user