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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 App\Utilities\Currency\CurrencyConverter;
  11. use BackedEnum;
  12. use Carbon\CarbonInterface;
  13. use Closure;
  14. use Filament\Forms\Components\DatePicker;
  15. use Filament\Forms\Components\Field;
  16. use Filament\Forms\Components\TextInput;
  17. use Filament\Infolists\Components\TextEntry;
  18. use Filament\Tables\Columns\TextColumn;
  19. use Illuminate\Support\Carbon;
  20. use Illuminate\Support\ServiceProvider;
  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. TextEntry::macro('currency', function (string | Closure | null $currency = null, ?bool $convert = null): static {
  83. $currency ??= CurrencyAccessor::getDefaultCurrency();
  84. $convert ??= true;
  85. $this->formatStateUsing(static function (TextEntry $entry, $state) use ($currency, $convert): ?string {
  86. if (blank($state)) {
  87. return null;
  88. }
  89. $currency = $entry->evaluate($currency);
  90. $convert = $entry->evaluate($convert);
  91. return money($state, $currency, $convert)->format();
  92. });
  93. return $this;
  94. });
  95. TextColumn::macro('currencyWithConversion', function (string | Closure | null $currency = null): static {
  96. $currency ??= CurrencyAccessor::getDefaultCurrency();
  97. $this->formatStateUsing(static function (TextColumn $column, $state) use ($currency): ?string {
  98. if (blank($state)) {
  99. return null;
  100. }
  101. $currency = $column->evaluate($currency);
  102. return CurrencyConverter::formatToMoney($state, $currency);
  103. });
  104. $this->description(static function (TextColumn $column, $state) use ($currency): ?string {
  105. if (blank($state)) {
  106. return null;
  107. }
  108. $oldCurrency = $column->evaluate($currency);
  109. $newCurrency = CurrencyAccessor::getDefaultCurrency();
  110. if ($oldCurrency === $newCurrency) {
  111. return null;
  112. }
  113. $balanceInCents = CurrencyConverter::convertToCents($state, $oldCurrency);
  114. $convertedBalanceInCents = CurrencyConverter::convertBalance($balanceInCents, $oldCurrency, $newCurrency);
  115. return CurrencyConverter::formatCentsToMoney($convertedBalanceInCents, $newCurrency, true);
  116. });
  117. return $this;
  118. });
  119. TextEntry::macro('currencyWithConversion', function (string | Closure | null $currency = null): static {
  120. $currency ??= CurrencyAccessor::getDefaultCurrency();
  121. $this->formatStateUsing(static function (TextEntry $entry, $state) use ($currency): ?string {
  122. if (blank($state)) {
  123. return null;
  124. }
  125. $currency = $entry->evaluate($currency);
  126. return CurrencyConverter::formatToMoney($state, $currency);
  127. });
  128. $this->helperText(static function (TextEntry $entry, $state) use ($currency): ?string {
  129. if (blank($state)) {
  130. return null;
  131. }
  132. $oldCurrency = $entry->evaluate($currency);
  133. $newCurrency = CurrencyAccessor::getDefaultCurrency();
  134. if ($oldCurrency === $newCurrency) {
  135. return null;
  136. }
  137. $balanceInCents = CurrencyConverter::convertToCents($state, $oldCurrency);
  138. $convertedBalanceInCents = CurrencyConverter::convertBalance($balanceInCents, $oldCurrency, $newCurrency);
  139. return CurrencyConverter::formatCentsToMoney($convertedBalanceInCents, $newCurrency, true);
  140. });
  141. return $this;
  142. });
  143. TextInput::macro('rate', function (string | Closure | null $computation = null, bool $showAffix = true): static {
  144. $this
  145. ->when(
  146. $showAffix,
  147. fn (TextInput $component) => $component
  148. ->prefix(static function (TextInput $component) use ($computation) {
  149. $computation = $component->evaluate($computation);
  150. return ratePrefix(computation: $computation);
  151. })
  152. ->suffix(static function (TextInput $component) use ($computation) {
  153. $computation = $component->evaluate($computation);
  154. return rateSuffix(computation: $computation);
  155. })
  156. )
  157. ->mask(static function (TextInput $component) use ($computation) {
  158. $computation = $component->evaluate($computation);
  159. return rateMask(computation: $computation);
  160. })
  161. ->rule(static function (TextInput $component) use ($computation) {
  162. return static function (string $attribute, $value, Closure $fail) use ($computation, $component) {
  163. $computation = $component->evaluate($computation);
  164. $numericValue = (float) $value;
  165. if ($computation instanceof BackedEnum) {
  166. $computation = $computation->value;
  167. }
  168. if ($computation === 'percentage' || $computation === 'compound') {
  169. if ($numericValue < 0 || $numericValue > 100) {
  170. $fail(translate('The rate must be between 0 and 100.'));
  171. }
  172. } elseif ($computation === 'fixed' && $numericValue < 0) {
  173. $fail(translate('The rate must be greater than 0.'));
  174. }
  175. };
  176. });
  177. return $this;
  178. });
  179. Field::macro('validateAccountCode', function (string | Closure | null $subtype = null): static {
  180. $this
  181. ->rules([
  182. fn (Field $component): Closure => static function (string $attribute, $value, Closure $fail) use ($subtype, $component) {
  183. $subtype = $component->evaluate($subtype);
  184. $chartSubtype = AccountSubtype::find($subtype);
  185. $type = $chartSubtype->type;
  186. if (! AccountCode::isValidCode($value, $type)) {
  187. $message = AccountCode::getMessage($type);
  188. $fail($message);
  189. }
  190. },
  191. ]);
  192. return $this;
  193. });
  194. TextColumn::macro('rate', function (string | Closure | null $computation = null): static {
  195. $this->formatStateUsing(static function (TextColumn $column, $state) use ($computation): ?string {
  196. $computation = $column->evaluate($computation);
  197. return rateFormat(state: $state, computation: $computation);
  198. });
  199. return $this;
  200. });
  201. Field::macro('softRequired', function (): static {
  202. $this
  203. ->required()
  204. ->markAsRequired(false);
  205. return $this;
  206. });
  207. TextColumn::macro('asRelativeDay', function (?string $timezone = null): static {
  208. $this->formatStateUsing(function (TextColumn $column, mixed $state) use ($timezone) {
  209. if (blank($state)) {
  210. return null;
  211. }
  212. $date = Carbon::parse($state)
  213. ->setTimezone($timezone ?? $column->getTimezone());
  214. if ($date->isToday()) {
  215. return 'Today';
  216. }
  217. return $date->diffForHumans([
  218. 'options' => CarbonInterface::ONE_DAY_WORDS,
  219. ]);
  220. });
  221. return $this;
  222. });
  223. TextEntry::macro('asRelativeDay', function (?string $timezone = null): static {
  224. $this->formatStateUsing(function (TextEntry $entry, mixed $state) use ($timezone) {
  225. if (blank($state)) {
  226. return null;
  227. }
  228. $date = Carbon::parse($state)
  229. ->setTimezone($timezone ?? $entry->getTimezone());
  230. if ($date->isToday()) {
  231. return 'Today';
  232. }
  233. return $date->diffForHumans([
  234. 'options' => CarbonInterface::ONE_DAY_WORDS,
  235. ]);
  236. });
  237. return $this;
  238. });
  239. Money::macro('swapAmountFor', function ($newCurrency) {
  240. $oldCurrency = $this->currency->getCurrency();
  241. $balanceInSubunits = $this->getAmount();
  242. $oldCurrencySubunit = currency($oldCurrency)->getSubunit();
  243. $newCurrencySubunit = currency($newCurrency)->getSubunit();
  244. $balanceInMajorUnits = $balanceInSubunits / $oldCurrencySubunit;
  245. $oldRate = currency($oldCurrency)->getRate();
  246. $newRate = currency($newCurrency)->getRate();
  247. $ratio = $newRate / $oldRate;
  248. $convertedBalanceInMajorUnits = $balanceInMajorUnits * $ratio;
  249. $roundedConvertedBalanceInMajorUnits = round($convertedBalanceInMajorUnits, currency($newCurrency)->getPrecision());
  250. $convertedBalanceInSubunits = $roundedConvertedBalanceInMajorUnits * $newCurrencySubunit;
  251. return (int) round($convertedBalanceInSubunits);
  252. });
  253. Money::macro('formatWithCode', function (bool $codeBefore = false) {
  254. $formatted = $this->format();
  255. $currencyCode = $this->currency->getCurrency();
  256. if ($codeBefore) {
  257. return $currencyCode . ' ' . $formatted;
  258. }
  259. return $formatted . ' ' . $currencyCode;
  260. });
  261. Currency::macro('getEntity', function () {
  262. $currencyCode = $this->getCurrency();
  263. $entity = config("money.currencies.{$currencyCode}.entity");
  264. return $entity ?? $currencyCode;
  265. });
  266. Currency::macro('getCodePrefix', function () {
  267. if ($this->isSymbolFirst()) {
  268. return '';
  269. }
  270. return ' ' . $this->getCurrency();
  271. });
  272. Currency::macro('getCodeSuffix', function () {
  273. if ($this->isSymbolFirst()) {
  274. return ' ' . $this->getCurrency();
  275. }
  276. return '';
  277. });
  278. Carbon::macro('toDefaultDateFormat', function () {
  279. $localization = Localization::firstOrFail();
  280. $dateFormat = $localization->date_format->value ?? DateFormat::DEFAULT;
  281. $timezone = $localization->timezone ?? Carbon::now()->timezoneName;
  282. return $this->setTimezone($timezone)->format($dateFormat);
  283. });
  284. }
  285. }