You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CurrencyConverter.php 3.1KB

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