選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

format.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. use App\Enums\Setting\NumberFormat;
  3. use App\Models\Setting\Localization;
  4. use Filament\Support\RawJs;
  5. if (! function_exists('generateJsCode')) {
  6. function generateJsCode(string $precision, ?string $currency = null): string
  7. {
  8. $decimal_mark = currency($currency)->getDecimalMark();
  9. $thousands_separator = currency($currency)->getThousandsSeparator();
  10. return "\$money(\$input, '" . $decimal_mark . "', '" . $thousands_separator . "', " . $precision . ');';
  11. }
  12. }
  13. if (! function_exists('generatePercentJsCode')) {
  14. function generatePercentJsCode(string $format, int $precision): string
  15. {
  16. [$decimal_mark, $thousands_separator] = NumberFormat::from($format)->getFormattingParameters();
  17. return "\$money(\$input, '" . $decimal_mark . "', '" . $thousands_separator . "', " . $precision . ');';
  18. }
  19. }
  20. if (! function_exists('moneyMask')) {
  21. function moneyMask(?string $currency = null): RawJs
  22. {
  23. $precision = currency($currency)->getPrecision();
  24. return RawJs::make(generateJsCode($precision, $currency));
  25. }
  26. }
  27. if (! function_exists('percentMask')) {
  28. function percentMask(int $precision = 4): RawJs
  29. {
  30. $format = Localization::firstOrFail()->number_format->value;
  31. return RawJs::make(generatePercentJsCode($format, $precision));
  32. }
  33. }
  34. if (! function_exists('ratePrefix')) {
  35. function ratePrefix($computation, ?string $currency = null): ?string
  36. {
  37. if ($computation instanceof BackedEnum) {
  38. $computation = $computation->value;
  39. }
  40. if ($computation === 'fixed') {
  41. return currency($currency)->getCodePrefix();
  42. }
  43. if ($computation === 'percentage' || $computation === 'compound') {
  44. $percent_first = Localization::firstOrFail()->percent_first;
  45. return $percent_first ? '%' : null;
  46. }
  47. return null;
  48. }
  49. }
  50. if (! function_exists('rateSuffix')) {
  51. function rateSuffix($computation, ?string $currency = null): ?string
  52. {
  53. if ($computation instanceof BackedEnum) {
  54. $computation = $computation->value;
  55. }
  56. if ($computation === 'percentage' || $computation === 'compound') {
  57. $percent_first = Localization::firstOrFail()->percent_first;
  58. return $percent_first ? null : '%';
  59. }
  60. if ($computation === 'fixed') {
  61. return currency($currency)->getCodeSuffix();
  62. }
  63. return null;
  64. }
  65. }
  66. if (! function_exists('rateMask')) {
  67. function rateMask($computation, ?string $currency = null): RawJs
  68. {
  69. if ($computation instanceof BackedEnum) {
  70. $computation = $computation->value;
  71. }
  72. if ($computation === 'percentage' || $computation === 'compound') {
  73. return percentMask(4);
  74. }
  75. $precision = currency($currency)->getPrecision();
  76. return RawJs::make(generateJsCode($precision, $currency));
  77. }
  78. }
  79. if (! function_exists('rateFormat')) {
  80. function rateFormat($state, $computation, ?string $currency = null): ?string
  81. {
  82. if (blank($state)) {
  83. return null;
  84. }
  85. if ($computation instanceof BackedEnum) {
  86. $computation = $computation->value;
  87. }
  88. if ($computation === 'percentage' || $computation === 'compound') {
  89. $percent_first = Localization::firstOrFail()->percent_first;
  90. if ($percent_first) {
  91. return '%' . $state;
  92. }
  93. return $state . '%';
  94. }
  95. if ($computation === 'fixed') {
  96. return money($state, $currency, true)->formatWithCode();
  97. }
  98. return null;
  99. }
  100. }