clean up scheduled tasks
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Console\Commands;
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Models\Portfolio;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
|
|
||||||
@@ -38,37 +39,27 @@ class CaptureDailyChange extends Command
|
|||||||
*/
|
*/
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
User::all()->each(function($user){
|
Portfolio::with('holdings.market_data')->get()->each(function($portfolio){
|
||||||
|
|
||||||
$this->line('Capturing daily change for ' . $user->name);
|
$this->line('Capturing daily change for ' . $portfolio->title);
|
||||||
|
|
||||||
$portfolios = $user->portfolios()->withoutWishlists()->with(['holdings.market_data'])->get();
|
$total_cost_basis = $portfolio->holdings->sum('total_cost_basis');
|
||||||
|
|
||||||
$total_cost_basis = $portfolios->reduce(function ($carry, $portfolio) {
|
$total_dividends = $portfolio->holdings->sum('dividends_earned');
|
||||||
return $carry + $portfolio->holdings->sum('total_cost_basis');
|
|
||||||
|
$realized_gains = $portfolio->holdings->sum('realized_gain_dollars');
|
||||||
|
|
||||||
|
$total_market_value = $portfolio->holdings->sum(function($holding) {
|
||||||
|
return $holding->market_data->market_value * $holding->quantity;
|
||||||
});
|
});
|
||||||
|
|
||||||
$total_dividends = $portfolios->reduce(function ($carry, $portfolio) {
|
$portfolio->daily_changes()->create([
|
||||||
return $carry + $portfolio->holdings->sum('dividends_earned');
|
|
||||||
});
|
|
||||||
|
|
||||||
$realized_gains = $portfolios->reduce(function ($carry, $portfolio) {
|
|
||||||
return $carry + $portfolio->holdings->sum('realized_gain_loss_dollars');
|
|
||||||
});
|
|
||||||
|
|
||||||
$total_market_value = $portfolios->reduce(function ($carry, $portfolio) {
|
|
||||||
return $carry + $portfolio->holdings->sum(function($holding) {
|
|
||||||
return $holding->market_data->market_value * $holding->quantity;
|
|
||||||
}) ;
|
|
||||||
});
|
|
||||||
|
|
||||||
$user->daily_changes()->create([
|
|
||||||
'date' => now(),
|
'date' => now(),
|
||||||
'total_cost_basis' => $total_cost_basis,
|
|
||||||
'total_market_value' => $total_market_value,
|
'total_market_value' => $total_market_value,
|
||||||
'total_dividends' => $total_dividends,
|
'total_cost_basis' => $total_cost_basis,
|
||||||
'realized_gains' => $realized_gains,
|
'total_gain' => $total_market_value - $total_cost_basis,
|
||||||
'total_gain_loss' => $total_market_value - $total_cost_basis
|
'total_dividends_earned' => $total_dividends,
|
||||||
|
'realized_gains' => $realized_gains
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ class RefreshDividendData extends Command
|
|||||||
|
|
||||||
foreach ($holdings as $holding) {
|
foreach ($holdings as $holding) {
|
||||||
$this->line('Refreshing ' . $holding->symbol);
|
$this->line('Refreshing ' . $holding->symbol);
|
||||||
|
|
||||||
Dividend::refreshDividendData($holding->symbol);
|
Dividend::refreshDividendData($holding->symbol);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Console\Commands;
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Models\Holding;
|
||||||
use App\Models\MarketData;
|
use App\Models\MarketData;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
|
|
||||||
@@ -40,11 +41,16 @@ class RefreshMarketData extends Command
|
|||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
// get all symbols from market data
|
// get all symbols from market data
|
||||||
$symbols = MarketData::get(['symbol']);
|
$symbols = Holding::where('quantity', '>', 0)
|
||||||
|
->select(['symbol'])
|
||||||
|
->distinct()
|
||||||
|
->get()
|
||||||
|
->pluck('symbol');
|
||||||
|
|
||||||
foreach ($symbols as $symbol) {
|
foreach ($symbols as $symbol) {
|
||||||
$this->line('Refreshing ' . $symbol->symbol);
|
$this->line('Refreshing ' . $symbol);
|
||||||
$symbol->refreshMarketData();
|
|
||||||
|
MarketData::getMarketData($symbol);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ class RefreshSplitData extends Command
|
|||||||
|
|
||||||
foreach ($holdings as $holding) {
|
foreach ($holdings as $holding) {
|
||||||
$this->line('Refreshing ' . $holding->symbol);
|
$this->line('Refreshing ' . $holding->symbol);
|
||||||
|
|
||||||
Split::refreshSplitData($holding->symbol);
|
Split::refreshSplitData($holding->symbol);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,11 +50,6 @@ class MarketData extends Model
|
|||||||
return $query->where('symbol', $symbol);
|
return $query->where('symbol', $symbol);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function refreshMarketData()
|
|
||||||
{
|
|
||||||
return static::getMarketData($this->attributes['symbol']);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function getMarketData($symbol)
|
public static function getMarketData($symbol)
|
||||||
{
|
{
|
||||||
$market_data = self::firstOrNew([
|
$market_data = self::firstOrNew([
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user