Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

MacroServiceProvider.php 6.9KB

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