Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ConfigureCurrencies.php 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Utilities\Currency;
  3. use Akaunting\Money\Currency as CurrencyBase;
  4. use App\Models\Setting\Currency as CurrencyModel;
  5. use Illuminate\Database\Eloquent\Collection;
  6. use Symfony\Component\Intl\Currencies;
  7. use Symfony\Component\Intl\Exception\MissingResourceException;
  8. class ConfigureCurrencies
  9. {
  10. public static function syncCurrencies(): void
  11. {
  12. $currencies = static::fetchCurrencies();
  13. if ($currencies->isEmpty()) {
  14. return;
  15. }
  16. $customCurrencies = static::formatCurrencies($currencies);
  17. static::mergeAndSetCurrencies($customCurrencies);
  18. $defaultCurrency = CurrencyAccessor::getDefaultCurrency();
  19. if ($defaultCurrency) {
  20. config(['money.defaults.currency' => $defaultCurrency]);
  21. }
  22. }
  23. protected static function fetchCurrencies(): Collection
  24. {
  25. return CurrencyModel::all();
  26. }
  27. protected static function formatCurrencies(Collection $currencies): array
  28. {
  29. $customCurrencies = [];
  30. foreach ($currencies as $currency) {
  31. $customCurrencies[$currency->code] = [
  32. 'name' => $currency->name,
  33. 'rate' => $currency->rate,
  34. 'precision' => $currency->precision,
  35. 'symbol' => $currency->symbol,
  36. 'symbol_first' => $currency->symbol_first,
  37. 'decimal_mark' => $currency->decimal_mark,
  38. 'thousands_separator' => $currency->thousands_separator,
  39. ];
  40. }
  41. return $customCurrencies;
  42. }
  43. protected static function mergeAndSetCurrencies(array $customCurrencies): void
  44. {
  45. $existingCurrencies = CurrencyBase::getCurrencies();
  46. foreach ($existingCurrencies as $code => $currency) {
  47. try {
  48. $name = Currencies::getName($code, app()->getLocale());
  49. $existingCurrencies[$code]['name'] = ucwords($name);
  50. } catch (MissingResourceException $e) {
  51. $existingCurrencies[$code]['name'] = $currency['name'];
  52. }
  53. }
  54. $mergedCurrencies = array_replace_recursive($existingCurrencies, $customCurrencies);
  55. CurrencyBase::setCurrencies($mergedCurrencies);
  56. }
  57. }