Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

CurrencyConverter.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_amount = str_replace([$old_attr->getThousandsSeparator(), $old_attr->getDecimalMark()], ['', '.'], $amount);
  15. return number_format((float) $temp_amount, $new_attr->getPrecision(), $new_attr->getDecimalMark(), $new_attr->getThousandsSeparator());
  16. }
  17. public static function convertBalance(int $amount, string $oldCurrency, string $newCurrency): int
  18. {
  19. return money($amount, $oldCurrency)->swapAmountFor($newCurrency);
  20. }
  21. public static function prepareForMutator(int $amount, string $currency): string
  22. {
  23. return money($amount, $currency)->formatSimple();
  24. }
  25. public static function prepareForAccessor(string $amount, string $currency): int
  26. {
  27. return money($amount, $currency, true)->getAmount();
  28. }
  29. public static function convertCentsToFormatSimple(int $amount, ?string $currency = null): string
  30. {
  31. $currency ??= CurrencyAccessor::getDefaultCurrency();
  32. return money($amount, $currency)->formatSimple();
  33. }
  34. public static function convertToCents(string | float $amount, ?string $currency = null): int
  35. {
  36. $currency ??= CurrencyAccessor::getDefaultCurrency();
  37. return money($amount, $currency, true)->getAmount();
  38. }
  39. public static function formatCentsToMoney(int $amount, ?string $currency = null, bool $withCode = false): string
  40. {
  41. $currency ??= CurrencyAccessor::getDefaultCurrency();
  42. $money = money($amount, $currency);
  43. if ($withCode) {
  44. return $money->formatWithCode();
  45. }
  46. return $money->format();
  47. }
  48. public static function formatToMoney(string | float $amount, ?string $currency = null, bool $withCode = false): string
  49. {
  50. $currency ??= CurrencyAccessor::getDefaultCurrency();
  51. $money = money($amount, $currency, true);
  52. if ($withCode) {
  53. return $money->formatWithCode();
  54. }
  55. return $money->format();
  56. }
  57. public static function convertCentsToFloat(int $amount, ?string $currency = null): float
  58. {
  59. $currency ??= CurrencyAccessor::getDefaultCurrency();
  60. return money($amount, $currency)->getValue();
  61. }
  62. public static function isValidAmount(?string $amount, ?string $currency = null): bool
  63. {
  64. $currency ??= CurrencyAccessor::getDefaultCurrency();
  65. if (blank($amount)) {
  66. return false;
  67. }
  68. try {
  69. money($amount, $currency);
  70. } catch (\Exception $e) {
  71. return false;
  72. }
  73. return true;
  74. }
  75. public static function handleCurrencyChange(Set $set, $state): void
  76. {
  77. $defaultCurrencyCode = CurrencyAccessor::getDefaultCurrency();
  78. $forexEnabled = Forex::isEnabled();
  79. $exchangeRate = $forexEnabled ? Forex::getCachedExchangeRate($defaultCurrencyCode, $state) : null;
  80. if ($forexEnabled && $exchangeRate !== null) {
  81. $set('rate', $exchangeRate);
  82. }
  83. }
  84. }