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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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): static {
  82. $this
  83. ->prefix(static function (TextInput $component) use ($computation) {
  84. $computation = $component->evaluate($computation);
  85. return ratePrefix(computation: $computation);
  86. })
  87. ->suffix(static function (TextInput $component) use ($computation) {
  88. $computation = $component->evaluate($computation);
  89. return rateSuffix(computation: $computation);
  90. })
  91. ->mask(static function (TextInput $component) use ($computation) {
  92. $computation = $component->evaluate($computation);
  93. return rateMask(computation: $computation);
  94. })
  95. ->rule(static function (TextInput $component) use ($computation) {
  96. return static function (string $attribute, $value, Closure $fail) use ($computation, $component) {
  97. $computation = $component->evaluate($computation);
  98. $numericValue = (float) $value;
  99. if ($computation instanceof BackedEnum) {
  100. $computation = $computation->value;
  101. }
  102. if ($computation === 'percentage' || $computation === 'compound') {
  103. if ($numericValue < 0 || $numericValue > 100) {
  104. $fail(translate('The rate must be between 0 and 100.'));
  105. }
  106. } elseif ($computation === 'fixed' && $numericValue < 0) {
  107. $fail(translate('The rate must be greater than 0.'));
  108. }
  109. };
  110. });
  111. return $this;
  112. });
  113. Field::macro('validateAccountCode', function (string | Closure | null $subtype = null): static {
  114. $this
  115. ->rules([
  116. fn (Field $component): Closure => static function (string $attribute, $value, Closure $fail) use ($subtype, $component) {
  117. $subtype = $component->evaluate($subtype);
  118. $chartSubtype = AccountSubtype::find($subtype);
  119. $type = $chartSubtype->type;
  120. if (! AccountCode::isValidCode($value, $type)) {
  121. $message = AccountCode::getMessage($type);
  122. $fail($message);
  123. }
  124. },
  125. ]);
  126. return $this;
  127. });
  128. TextColumn::macro('rate', function (string | Closure | null $computation = null): static {
  129. $this->formatStateUsing(static function (TextColumn $column, $state) use ($computation): ?string {
  130. $computation = $column->evaluate($computation);
  131. return rateFormat(state: $state, computation: $computation);
  132. });
  133. return $this;
  134. });
  135. Field::macro('softRequired', function (): static {
  136. $this
  137. ->required()
  138. ->markAsRequired(false);
  139. return $this;
  140. });
  141. TextColumn::macro('asRelativeDay', function (?string $timezone = null): static {
  142. $this->formatStateUsing(function (TextColumn $column, mixed $state) use ($timezone) {
  143. if (blank($state)) {
  144. return null;
  145. }
  146. $date = Carbon::parse($state)
  147. ->setTimezone($timezone ?? $column->getTimezone());
  148. if ($date->isToday()) {
  149. return 'Today';
  150. }
  151. return $date->diffForHumans([
  152. 'options' => CarbonInterface::ONE_DAY_WORDS,
  153. ]);
  154. });
  155. return $this;
  156. });
  157. TextEntry::macro('asRelativeDay', function (?string $timezone = null): static {
  158. $this->formatStateUsing(function (TextEntry $entry, mixed $state) use ($timezone) {
  159. if (blank($state)) {
  160. return null;
  161. }
  162. $date = Carbon::parse($state)
  163. ->setTimezone($timezone ?? $entry->getTimezone());
  164. if ($date->isToday()) {
  165. return 'Today';
  166. }
  167. return $date->diffForHumans([
  168. 'options' => CarbonInterface::ONE_DAY_WORDS,
  169. ]);
  170. });
  171. return $this;
  172. });
  173. Money::macro('swapAmountFor', function ($newCurrency) {
  174. $oldCurrency = $this->currency->getCurrency();
  175. $balanceInSubunits = $this->getAmount();
  176. $oldCurrencySubunit = currency($oldCurrency)->getSubunit();
  177. $newCurrencySubunit = currency($newCurrency)->getSubunit();
  178. $balanceInMajorUnits = $balanceInSubunits / $oldCurrencySubunit;
  179. $oldRate = currency($oldCurrency)->getRate();
  180. $newRate = currency($newCurrency)->getRate();
  181. $ratio = $newRate / $oldRate;
  182. $convertedBalanceInMajorUnits = $balanceInMajorUnits * $ratio;
  183. $roundedConvertedBalanceInMajorUnits = round($convertedBalanceInMajorUnits, currency($newCurrency)->getPrecision());
  184. $convertedBalanceInSubunits = $roundedConvertedBalanceInMajorUnits * $newCurrencySubunit;
  185. return (int) round($convertedBalanceInSubunits);
  186. });
  187. Money::macro('formatWithCode', function (bool $codeBefore = false) {
  188. $formatted = $this->format();
  189. $currencyCode = $this->currency->getCurrency();
  190. if ($codeBefore) {
  191. return $currencyCode . ' ' . $formatted;
  192. }
  193. return $formatted . ' ' . $currencyCode;
  194. });
  195. Currency::macro('getEntity', function () {
  196. $currencyCode = $this->getCurrency();
  197. $entity = config("money.currencies.{$currencyCode}.entity");
  198. return $entity ?? $currencyCode;
  199. });
  200. Currency::macro('getCodePrefix', function () {
  201. if ($this->isSymbolFirst()) {
  202. return '';
  203. }
  204. return ' ' . $this->getCurrency();
  205. });
  206. Currency::macro('getCodeSuffix', function () {
  207. if ($this->isSymbolFirst()) {
  208. return ' ' . $this->getCurrency();
  209. }
  210. return '';
  211. });
  212. Carbon::macro('toDefaultDateFormat', function () {
  213. $localization = Localization::firstOrFail();
  214. $dateFormat = $localization->date_format->value ?? DateFormat::DEFAULT;
  215. $timezone = $localization->timezone ?? Carbon::now()->timezoneName;
  216. return $this->setTimezone($timezone)->format($dateFormat);
  217. });
  218. }
  219. }