2026-01-27 00:24:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-01-27 16:23:12 +00:00
|
|
|
namespace Core\Mod\Commerce\Console;
|
2026-01-27 00:24:22 +00:00
|
|
|
|
|
|
|
|
use Illuminate\Console\Command;
|
2026-01-27 16:23:12 +00:00
|
|
|
use Core\Mod\Commerce\Services\CurrencyService;
|
2026-01-27 00:24:22 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Refresh exchange rates from configured provider.
|
|
|
|
|
*
|
|
|
|
|
* Should be scheduled to run periodically (e.g., hourly).
|
|
|
|
|
*/
|
|
|
|
|
class RefreshExchangeRates extends Command
|
|
|
|
|
{
|
|
|
|
|
protected $signature = 'commerce:refresh-exchange-rates
|
|
|
|
|
{--force : Force refresh even if rates are fresh}';
|
|
|
|
|
|
|
|
|
|
protected $description = 'Refresh exchange rates from the configured provider';
|
|
|
|
|
|
|
|
|
|
public function handle(CurrencyService $currencyService): int
|
|
|
|
|
{
|
|
|
|
|
$this->info('Refreshing exchange rates...');
|
|
|
|
|
|
|
|
|
|
$baseCurrency = $currencyService->getBaseCurrency();
|
|
|
|
|
$provider = config('commerce.currencies.exchange_rates.provider', 'ecb');
|
|
|
|
|
|
|
|
|
|
$this->line("Base currency: {$baseCurrency}");
|
|
|
|
|
$this->line("Provider: {$provider}");
|
|
|
|
|
|
|
|
|
|
// Check if rates need refresh
|
2026-01-27 16:23:12 +00:00
|
|
|
if (! $this->option('force') && ! \Core\Mod\Commerce\Models\ExchangeRate::needsRefresh()) {
|
2026-01-27 00:24:22 +00:00
|
|
|
$this->info('Rates are still fresh. Use --force to refresh anyway.');
|
|
|
|
|
|
|
|
|
|
return self::SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$rates = $currencyService->refreshExchangeRates();
|
|
|
|
|
|
|
|
|
|
if (empty($rates)) {
|
|
|
|
|
$this->error('No rates were updated. Check logs for errors.');
|
|
|
|
|
|
|
|
|
|
return self::FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->info('Updated '.count($rates).' exchange rates:');
|
|
|
|
|
|
|
|
|
|
$rows = [];
|
|
|
|
|
foreach ($rates as $currency => $rate) {
|
|
|
|
|
$rows[] = [$baseCurrency, $currency, number_format($rate, 6)];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->table(['From', 'To', 'Rate'], $rows);
|
|
|
|
|
|
|
|
|
|
return self::SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
}
|