Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CurrencyConverter.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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): string
  49. {
  50. $currency ??= CurrencyAccessor::getDefaultCurrency();
  51. return money($amount, $currency, true)->format();
  52. }
  53. public static function convertCentsToFloat(int $amount, ?string $currency = null): float
  54. {
  55. $currency ??= CurrencyAccessor::getDefaultCurrency();
  56. return money($amount, $currency)->getValue();
  57. }
  58. public static function isValidAmount(?string $amount, ?string $currency = null): bool
  59. {
  60. $currency ??= CurrencyAccessor::getDefaultCurrency();
  61. if (blank($amount)) {
  62. return false;
  63. }
  64. try {
  65. money($amount, $currency);
  66. } catch (\Exception $e) {
  67. return false;
  68. }
  69. return true;
  70. }
  71. public static function handleCurrencyChange(Set $set, $state): void
  72. {
  73. $currency = currency($state);
  74. $defaultCurrencyCode = CurrencyAccessor::getDefaultCurrency();
  75. $forexEnabled = Forex::isEnabled();
  76. $exchangeRate = $forexEnabled ? Forex::getCachedExchangeRate($defaultCurrencyCode, $state) : null;
  77. $set('name', $currency->getName() ?? '');
  78. if ($forexEnabled && $exchangeRate !== null) {
  79. $set('rate', $exchangeRate);
  80. }
  81. }
  82. }