Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

MacroServiceProvider.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace App\Providers;
  3. use Akaunting\Money\Currency;
  4. use Akaunting\Money\Money;
  5. use BackedEnum;
  6. use Closure;
  7. use Filament\Forms\Components\Field;
  8. use Filament\Forms\Components\TextInput;
  9. use Filament\Tables\Columns\TextColumn;
  10. use Illuminate\Support\ServiceProvider;
  11. use Illuminate\Support\Str;
  12. class MacroServiceProvider extends ServiceProvider
  13. {
  14. /**
  15. * Register services.
  16. */
  17. public function register(): void
  18. {
  19. //
  20. }
  21. /**
  22. * Bootstrap services.
  23. */
  24. public function boot(): void
  25. {
  26. TextInput::macro('money', function (string | Closure | null $currency = null): static {
  27. $this->extraAttributes(['wire:key' => Str::random()])
  28. ->prefix(static function (TextInput $component) use ($currency) {
  29. $currency = $component->evaluate($currency);
  30. return currency($currency)->getPrefix();
  31. })
  32. ->suffix(static function (TextInput $component) use ($currency) {
  33. $currency = $component->evaluate($currency);
  34. return currency($currency)->getSuffix();
  35. })
  36. ->mask(static function (TextInput $component) use ($currency) {
  37. $currency = $component->evaluate($currency);
  38. return moneyMask($currency);
  39. });
  40. return $this;
  41. });
  42. TextColumn::macro('currency', function (string | Closure | null $currency = null, ?bool $convert = null): static {
  43. $this->formatStateUsing(static function (TextColumn $column, $state) use ($currency, $convert): ?string {
  44. if (blank($state)) {
  45. return null;
  46. }
  47. $currency = $column->evaluate($currency);
  48. $convert = $column->evaluate($convert);
  49. return money($state, $currency, $convert)->formatWithCode();
  50. });
  51. return $this;
  52. });
  53. TextInput::macro('rate', function (string | Closure | null $computation = null): static {
  54. $this->extraAttributes(['wire:key' => Str::random()])
  55. ->prefix(static function (TextInput $component) use ($computation) {
  56. $computation = $component->evaluate($computation);
  57. return ratePrefix(computation: $computation);
  58. })
  59. ->suffix(static function (TextInput $component) use ($computation) {
  60. $computation = $component->evaluate($computation);
  61. return rateSuffix(computation: $computation);
  62. })
  63. ->mask(static function (TextInput $component) use ($computation) {
  64. $computation = $component->evaluate($computation);
  65. return rateMask(computation: $computation);
  66. })
  67. ->rule(static function (TextInput $component) use ($computation) {
  68. return static function (string $attribute, $value, Closure $fail) use ($computation, $component) {
  69. $computation = $component->evaluate($computation);
  70. $numericValue = (float) $value;
  71. if ($computation instanceof BackedEnum) {
  72. $computation = $computation->value;
  73. }
  74. if ($computation === 'percentage' || $computation === 'compound') {
  75. if ($numericValue < 0 || $numericValue > 100) {
  76. $fail(translate('The rate must be between 0 and 100.'));
  77. }
  78. } elseif ($computation === 'fixed' && $numericValue < 0) {
  79. $fail(translate('The rate must be greater than 0.'));
  80. }
  81. };
  82. });
  83. return $this;
  84. });
  85. TextColumn::macro('rate', function (string | Closure | null $computation = null): static {
  86. $this->formatStateUsing(static function (TextColumn $column, $state) use ($computation): ?string {
  87. $computation = $column->evaluate($computation);
  88. return rateFormat(state: $state, computation: $computation);
  89. });
  90. return $this;
  91. });
  92. Field::macro('softRequired', function (): static {
  93. $this
  94. ->required()
  95. ->markAsRequired(false);
  96. return $this;
  97. });
  98. Money::macro('swapAmountFor', function ($newCurrency) {
  99. $oldCurrency = $this->currency->getCurrency();
  100. $balance = $this->getAmount();
  101. $oldRate = currency($oldCurrency)->getRate();
  102. $newRate = currency($newCurrency)->getRate();
  103. $ratio = $newRate / $oldRate;
  104. $convertedBalance = money($balance, $oldCurrency)->multiply($ratio)->getAmount();
  105. return (int) filter_var($convertedBalance, FILTER_SANITIZE_NUMBER_INT);
  106. });
  107. Money::macro('formatWithCode', function () {
  108. $formatted = $this->formatSimple();
  109. $isSymbolFirst = $this->currency->isSymbolFirst();
  110. $currencyCode = $this->currency->getCurrency();
  111. if ($isSymbolFirst) {
  112. return $formatted . ' ' . $currencyCode;
  113. }
  114. return $currencyCode . ' ' . $formatted;
  115. });
  116. Currency::macro('getEntity', function () {
  117. $currencyCode = $this->getCurrency();
  118. $entity = config("money.currencies.{$currencyCode}.entity");
  119. return $entity ?? $currencyCode;
  120. });
  121. Currency::macro('getCodePrefix', function () {
  122. if ($this->isSymbolFirst()) {
  123. return '';
  124. }
  125. return ' ' . $this->getCurrency();
  126. });
  127. Currency::macro('getCodeSuffix', function () {
  128. if ($this->isSymbolFirst()) {
  129. return ' ' . $this->getCurrency();
  130. }
  131. return '';
  132. });
  133. }
  134. }