您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

MacroServiceProvider.php 8.7KB

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