Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CurrencyConverter.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 self::convertToCents($amount, $currency);
  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. $amountInCents = money($amount, $currency, true)->getAmount();
  38. if (is_float($amountInCents)) {
  39. $amountInCents = (int) round($amountInCents);
  40. }
  41. return $amountInCents;
  42. }
  43. public static function formatCentsToMoney(int $amount, ?string $currency = null, bool $withCode = false): string
  44. {
  45. $currency ??= CurrencyAccessor::getDefaultCurrency();
  46. $money = money($amount, $currency);
  47. if ($withCode) {
  48. return $money->formatWithCode();
  49. }
  50. return $money->format();
  51. }
  52. public static function formatToMoney(string | float $amount, ?string $currency = null, bool $withCode = false): string
  53. {
  54. $currency ??= CurrencyAccessor::getDefaultCurrency();
  55. $money = money($amount, $currency, true);
  56. if ($withCode) {
  57. return $money->formatWithCode();
  58. }
  59. return $money->format();
  60. }
  61. public static function convertCentsToFloat(int $amount, ?string $currency = null): float
  62. {
  63. $currency ??= CurrencyAccessor::getDefaultCurrency();
  64. return money($amount, $currency)->getValue();
  65. }
  66. public static function isValidAmount(?string $amount, ?string $currency = null): bool
  67. {
  68. $currency ??= CurrencyAccessor::getDefaultCurrency();
  69. if (blank($amount)) {
  70. return false;
  71. }
  72. try {
  73. money($amount, $currency);
  74. } catch (\Exception $e) {
  75. return false;
  76. }
  77. return true;
  78. }
  79. public static function handleCurrencyChange(Set $set, $state): void
  80. {
  81. $defaultCurrencyCode = CurrencyAccessor::getDefaultCurrency();
  82. $forexEnabled = Forex::isEnabled();
  83. $exchangeRate = $forexEnabled ? Forex::getCachedExchangeRate($defaultCurrencyCode, $state) : null;
  84. if ($forexEnabled && $exchangeRate !== null) {
  85. $set('rate', $exchangeRate);
  86. }
  87. }
  88. }