Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

MacroServiceProvider.php 8.6KB

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