2024-08-30 21:58:38 -05:00
|
|
|
<?php
|
|
|
|
|
|
2025-01-28 17:33:54 -06:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2024-08-30 21:58:38 -05:00
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
2025-04-09 19:25:15 -05:00
|
|
|
use App\Models\Holding;
|
2024-09-01 21:18:09 -05:00
|
|
|
use App\Models\Portfolio;
|
2024-08-30 21:58:38 -05:00
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
|
|
|
|
class CaptureDailyChange extends Command
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* The name and signature of the console command.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2024-09-06 22:26:54 -05:00
|
|
|
protected $signature = 'capture:daily-change';
|
2024-08-30 21:58:38 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The console command description.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected $description = 'Capture summary of daily change for user\'s holdings';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new command instance.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
parent::__construct();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute the console command.
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
2025-01-28 17:14:49 -06:00
|
|
|
Portfolio::with('holdings.market_data')->get()->each(function ($portfolio) {
|
2024-08-30 21:58:38 -05:00
|
|
|
|
2025-01-28 17:14:49 -06:00
|
|
|
$this->line('Capturing daily change for '.$portfolio->title);
|
2024-08-30 21:58:38 -05:00
|
|
|
|
2025-04-09 19:25:15 -05:00
|
|
|
$metrics = Holding::query()
|
|
|
|
|
->portfolio($portfolio->id)
|
|
|
|
|
->getPortfolioMetrics(config('investbrain.base_currency'));
|
2024-08-30 21:58:38 -05:00
|
|
|
|
2025-04-09 19:25:15 -05:00
|
|
|
$total_cost_basis = $metrics->get('total_cost_basis');
|
|
|
|
|
$total_market_value = $metrics->get('total_market_value');
|
2024-08-30 21:58:38 -05:00
|
|
|
|
2024-09-05 21:21:18 -05:00
|
|
|
$portfolio->daily_change()->create([
|
2024-08-30 21:58:38 -05:00
|
|
|
'date' => now(),
|
|
|
|
|
'total_market_value' => $total_market_value,
|
2024-09-01 21:18:09 -05:00
|
|
|
'total_cost_basis' => $total_cost_basis,
|
|
|
|
|
'total_gain' => $total_market_value - $total_cost_basis,
|
2025-04-09 19:25:15 -05:00
|
|
|
'total_dividends_earned' => $metrics->get('total_dividends_earned'),
|
|
|
|
|
'realized_gains' => $metrics->get('realized_gain_dollars'),
|
2024-08-30 21:58:38 -05:00
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|