Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

CurrencyConverter.php 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace App\Utilities\Currency;
  3. use App\Facades\Forex;
  4. use Filament\Forms\Set;
  5. class CurrencyConverter
  6. {
  7. public static function convertAndSet($newCurrency, $oldCurrency, $amount): ?string
  8. {
  9. if ($newCurrency === null || $oldCurrency === $newCurrency) {
  10. return null;
  11. }
  12. $old_attr = currency($oldCurrency);
  13. $new_attr = currency($newCurrency);
  14. $temp_balance = str_replace([$old_attr->getThousandsSeparator(), $old_attr->getDecimalMark()], ['', '.'], $amount);
  15. return number_format((float) $temp_balance, $new_attr->getPrecision(), $new_attr->getDecimalMark(), $new_attr->getThousandsSeparator());
  16. }
  17. public static function convertBalance(int $balance, string $oldCurrency, string $newCurrency): int
  18. {
  19. return money($balance, $oldCurrency)->swapAmountFor($newCurrency);
  20. }
  21. public static function prepareForMutator(int $balance, string $currency): string
  22. {
  23. return money($balance, $currency)->formatSimple();
  24. }
  25. public static function prepareForAccessor(string $balance, string $currency): int
  26. {
  27. return money($balance, $currency, true)->getAmount();
  28. }
  29. public static function handleCurrencyChange(Set $set, $state): void
  30. {
  31. $currency = currency($state);
  32. $defaultCurrencyCode = CurrencyAccessor::getDefaultCurrency();
  33. $forexEnabled = Forex::isEnabled();
  34. $exchangeRate = $forexEnabled ? Forex::getCachedExchangeRate($defaultCurrencyCode, $state) : null;
  35. $set('name', $currency->getName() ?? '');
  36. if ($forexEnabled && $exchangeRate !== null) {
  37. $set('rate', $exchangeRate);
  38. }
  39. }
  40. }