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 6.8KB

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