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.

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