您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

InitializeCurrencies.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Console\Commands;
  3. use Akaunting\Money\Currency;
  4. use App\Contracts\CurrencyHandler;
  5. use App\Facades\Forex;
  6. use App\Models\Service\CurrencyList;
  7. use Illuminate\Console\Command;
  8. class InitializeCurrencies extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'currency:init';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Initialize currencies from the API';
  22. public function __construct(private readonly CurrencyHandler $currencyService)
  23. {
  24. parent::__construct();
  25. }
  26. /**
  27. * Execute the console command.
  28. */
  29. public function handle(): void
  30. {
  31. $this->info('Fetching supported currencies from the API...');
  32. $apiSupportedCurrencies = $this->currencyService->getSupportedCurrencies();
  33. if (Forex::isDisabled()) {
  34. $this->error('The Currency Exchange Rate feature is disabled.');
  35. return;
  36. }
  37. if (empty($apiSupportedCurrencies)) {
  38. $this->error('Failed to fetch supported currencies from the API.');
  39. return;
  40. }
  41. $appSupportedCurrencies = array_keys(Currency::getCurrencies());
  42. foreach ($appSupportedCurrencies as $appSupportedCurrency) {
  43. $isAvailable = in_array($appSupportedCurrency, $apiSupportedCurrencies, true);
  44. $currencyAttributes = [
  45. 'code' => $appSupportedCurrency,
  46. 'name' => currency($appSupportedCurrency)->getName(),
  47. 'entity' => currency($appSupportedCurrency)->getEntity(),
  48. 'available' => $isAvailable,
  49. ];
  50. CurrencyList::updateOrCreate(
  51. ['code' => $appSupportedCurrency],
  52. $currencyAttributes
  53. );
  54. }
  55. $this->info('Successfully initialized currencies.');
  56. }
  57. }