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 10KB

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