You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MacroServiceProvider.php 8.7KB

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