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

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