Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

MacroServiceProvider.php 8.7KB

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