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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. namespace App\Providers;
  3. use Akaunting\Money\Currency;
  4. use Akaunting\Money\Money;
  5. use App\Enums\Accounting\AdjustmentComputation;
  6. use App\Enums\Setting\DateFormat;
  7. use App\Models\Accounting\AccountSubtype;
  8. use App\Models\Setting\Localization;
  9. use App\Utilities\Accounting\AccountCode;
  10. use App\Utilities\Currency\CurrencyAccessor;
  11. use App\Utilities\Currency\CurrencyConverter;
  12. use BackedEnum;
  13. use Carbon\CarbonInterface;
  14. use Closure;
  15. use Filament\Forms\Components\DatePicker;
  16. use Filament\Forms\Components\Field;
  17. use Filament\Forms\Components\TextInput;
  18. use Filament\Infolists\Components\TextEntry;
  19. use Filament\Tables\Columns\TextColumn;
  20. use Filament\Tables\Contracts\HasTable;
  21. use Illuminate\Contracts\Support\Htmlable;
  22. use Illuminate\Support\Carbon;
  23. use Illuminate\Support\HtmlString;
  24. use Illuminate\Support\ServiceProvider;
  25. class MacroServiceProvider extends ServiceProvider
  26. {
  27. /**
  28. * Register services.
  29. */
  30. public function register(): void
  31. {
  32. //
  33. }
  34. /**
  35. * Bootstrap services.
  36. */
  37. public function boot(): void
  38. {
  39. TextInput::macro('money', function (string | Closure | null $currency = null, bool $useAffix = true): static {
  40. $currency ??= CurrencyAccessor::getDefaultCurrency();
  41. if ($useAffix) {
  42. $this
  43. ->prefix(static function (TextInput $component) use ($currency) {
  44. $currency = $component->evaluate($currency);
  45. return currency($currency)->getPrefix();
  46. })
  47. ->suffix(static function (TextInput $component) use ($currency) {
  48. $currency = $component->evaluate($currency);
  49. return currency($currency)->getSuffix();
  50. });
  51. }
  52. $this->mask(static function (TextInput $component) use ($currency) {
  53. $currency = $component->evaluate($currency);
  54. return moneyMask($currency);
  55. });
  56. return $this;
  57. });
  58. TextInput::macro('rate', function (string | Closure | null $computation = null, string | Closure | null $currency = null, bool $showAffix = true): static {
  59. return $this
  60. ->when(
  61. $showAffix,
  62. fn (TextInput $component) => $component
  63. ->prefix(function (TextInput $component) use ($computation, $currency) {
  64. $evaluatedComputation = $component->evaluate($computation);
  65. $evaluatedCurrency = $component->evaluate($currency);
  66. return ratePrefix($evaluatedComputation, $evaluatedCurrency);
  67. })
  68. ->suffix(function (TextInput $component) use ($computation, $currency) {
  69. $evaluatedComputation = $component->evaluate($computation);
  70. $evaluatedCurrency = $component->evaluate($currency);
  71. return rateSuffix($evaluatedComputation, $evaluatedCurrency);
  72. })
  73. )
  74. ->mask(static function (TextInput $component) use ($computation, $currency) {
  75. $computation = $component->evaluate($computation);
  76. $currency = $component->evaluate($currency);
  77. $computationEnum = AdjustmentComputation::parse($computation);
  78. if ($computationEnum->isPercentage()) {
  79. return rateMask(computation: $computation);
  80. }
  81. return moneyMask($currency);
  82. })
  83. ->rule(static function (TextInput $component) use ($computation) {
  84. return static function (string $attribute, $value, Closure $fail) use ($computation, $component) {
  85. $computation = $component->evaluate($computation);
  86. $numericValue = (float) $value;
  87. if ($computation instanceof BackedEnum) {
  88. $computation = $computation->value;
  89. }
  90. if ($computation === 'percentage' || $computation === 'compound') {
  91. if ($numericValue < 0 || $numericValue > 100) {
  92. $fail(translate('The rate must be between 0 and 100.'));
  93. }
  94. } elseif ($computation === 'fixed' && $numericValue < 0) {
  95. $fail(translate('The rate must be greater than 0.'));
  96. }
  97. };
  98. });
  99. });
  100. TextColumn::macro('coloredDescription', function (string | Htmlable | Closure | null $description, string $color = 'danger') {
  101. $this->description(static function (TextColumn $column) use ($description, $color): Htmlable {
  102. $description = $column->evaluate($description);
  103. return new HtmlString("<span class='text-{$color}-700 dark:text-{$color}-400'>{$description}</span>");
  104. });
  105. return $this;
  106. });
  107. TextColumn::macro('hideOnTabs', function (array $tabs): static {
  108. $this->toggleable(isToggledHiddenByDefault: function (HasTable $livewire) use ($tabs) {
  109. return in_array($livewire->activeTab, $tabs);
  110. });
  111. return $this;
  112. });
  113. TextColumn::macro('showOnTabs', function (array $tabs): static {
  114. $this->toggleable(isToggledHiddenByDefault: function (HasTable $livewire) use ($tabs) {
  115. return ! in_array($livewire->activeTab, $tabs);
  116. });
  117. return $this;
  118. });
  119. TextColumn::macro('defaultDateFormat', function (): static {
  120. $localization = Localization::firstOrFail();
  121. $dateFormat = $localization->date_format->value ?? DateFormat::DEFAULT;
  122. $timezone = $localization->timezone ?? Carbon::now()->timezoneName;
  123. $this->date($dateFormat, $timezone);
  124. return $this;
  125. });
  126. DatePicker::macro('defaultDateFormat', function (): static {
  127. $localization = Localization::firstOrFail();
  128. $dateFormat = $localization->date_format->value ?? DateFormat::DEFAULT;
  129. $timezone = $localization->timezone ?? Carbon::now()->timezoneName;
  130. $this->displayFormat($dateFormat)
  131. ->timezone($timezone);
  132. return $this;
  133. });
  134. TextColumn::macro('currency', function (string | Closure | null $currency = null, ?bool $convert = null): static {
  135. $currency ??= CurrencyAccessor::getDefaultCurrency();
  136. $convert ??= true;
  137. $this->formatStateUsing(static function (TextColumn $column, $state) use ($currency, $convert): ?string {
  138. if (blank($state)) {
  139. return null;
  140. }
  141. $currency = $column->evaluate($currency);
  142. $convert = $column->evaluate($convert);
  143. return money($state, $currency, $convert)->format();
  144. });
  145. return $this;
  146. });
  147. TextEntry::macro('currency', function (string | Closure | null $currency = null, ?bool $convert = null): static {
  148. $currency ??= CurrencyAccessor::getDefaultCurrency();
  149. $convert ??= true;
  150. $this->formatStateUsing(static function (TextEntry $entry, $state) use ($currency, $convert): ?string {
  151. if (blank($state)) {
  152. return null;
  153. }
  154. $currency = $entry->evaluate($currency);
  155. $convert = $entry->evaluate($convert);
  156. return money($state, $currency, $convert)->format();
  157. });
  158. return $this;
  159. });
  160. TextColumn::macro('currencyWithConversion', function (string | Closure | null $currency = null, ?bool $convertFromCents = null): static {
  161. $currency ??= CurrencyAccessor::getDefaultCurrency();
  162. $convertFromCents ??= false;
  163. $this->formatStateUsing(static function (TextColumn $column, $state) use ($currency, $convertFromCents): ?string {
  164. if (blank($state)) {
  165. return null;
  166. }
  167. $currency = $column->evaluate($currency);
  168. if ($convertFromCents) {
  169. return CurrencyConverter::formatCentsToMoney($state, $currency);
  170. }
  171. return CurrencyConverter::formatToMoney($state, $currency);
  172. });
  173. $this->description(static function (TextColumn $column, $state) use ($currency, $convertFromCents): ?string {
  174. if (blank($state)) {
  175. return null;
  176. }
  177. $oldCurrency = $column->evaluate($currency);
  178. $newCurrency = CurrencyAccessor::getDefaultCurrency();
  179. if ($oldCurrency === $newCurrency) {
  180. return null;
  181. }
  182. if ($convertFromCents) {
  183. $balanceInCents = $state;
  184. } else {
  185. $balanceInCents = CurrencyConverter::convertToCents($state, $oldCurrency);
  186. }
  187. $convertedBalanceInCents = CurrencyConverter::convertBalance($balanceInCents, $oldCurrency, $newCurrency);
  188. return CurrencyConverter::formatCentsToMoney($convertedBalanceInCents, $newCurrency, true);
  189. });
  190. return $this;
  191. });
  192. TextEntry::macro('currencyWithConversion', function (string | Closure | null $currency = null): static {
  193. $currency ??= CurrencyAccessor::getDefaultCurrency();
  194. $this->formatStateUsing(static function (TextEntry $entry, $state) use ($currency): ?string {
  195. if (blank($state)) {
  196. return null;
  197. }
  198. $currency = $entry->evaluate($currency);
  199. return CurrencyConverter::formatToMoney($state, $currency);
  200. });
  201. $this->helperText(static function (TextEntry $entry, $state) use ($currency): ?string {
  202. if (blank($state)) {
  203. return null;
  204. }
  205. $oldCurrency = $entry->evaluate($currency);
  206. $newCurrency = CurrencyAccessor::getDefaultCurrency();
  207. if ($oldCurrency === $newCurrency) {
  208. return null;
  209. }
  210. $balanceInCents = CurrencyConverter::convertToCents($state, $oldCurrency);
  211. $convertedBalanceInCents = CurrencyConverter::convertBalance($balanceInCents, $oldCurrency, $newCurrency);
  212. return CurrencyConverter::formatCentsToMoney($convertedBalanceInCents, $newCurrency, true);
  213. });
  214. return $this;
  215. });
  216. Field::macro('validateAccountCode', function (string | Closure | null $subtype = null): static {
  217. $this
  218. ->rules([
  219. fn (Field $component): Closure => static function (string $attribute, $value, Closure $fail) use ($subtype, $component) {
  220. $subtype = $component->evaluate($subtype);
  221. $chartSubtype = AccountSubtype::find($subtype);
  222. $type = $chartSubtype->type;
  223. if (! AccountCode::isValidCode($value, $type)) {
  224. $message = AccountCode::getMessage($type);
  225. $fail($message);
  226. }
  227. },
  228. ]);
  229. return $this;
  230. });
  231. TextColumn::macro('rate', function (string | Closure | null $computation = null): static {
  232. $this->formatStateUsing(static function (TextColumn $column, $state) use ($computation): ?string {
  233. $computation = $column->evaluate($computation);
  234. return rateFormat(state: $state, computation: $computation);
  235. });
  236. return $this;
  237. });
  238. Field::macro('softRequired', function (): static {
  239. $this
  240. ->required()
  241. ->markAsRequired(false);
  242. return $this;
  243. });
  244. TextColumn::macro('asRelativeDay', function (?string $timezone = null): static {
  245. $this->formatStateUsing(function (TextColumn $column, mixed $state) use ($timezone) {
  246. if (blank($state)) {
  247. return null;
  248. }
  249. $date = Carbon::parse($state)
  250. ->setTimezone($timezone ?? $column->getTimezone());
  251. if ($date->isToday()) {
  252. return 'Today';
  253. }
  254. return $date->diffForHumans([
  255. 'options' => CarbonInterface::ONE_DAY_WORDS,
  256. ]);
  257. });
  258. return $this;
  259. });
  260. TextEntry::macro('asRelativeDay', function (?string $timezone = null): static {
  261. $this->formatStateUsing(function (TextEntry $entry, mixed $state) use ($timezone) {
  262. if (blank($state)) {
  263. return null;
  264. }
  265. $date = Carbon::parse($state)
  266. ->setTimezone($timezone ?? $entry->getTimezone());
  267. if ($date->isToday()) {
  268. return 'Today';
  269. }
  270. return $date->diffForHumans([
  271. 'options' => CarbonInterface::ONE_DAY_WORDS,
  272. ]);
  273. });
  274. return $this;
  275. });
  276. Money::macro('swapAmountFor', function ($newCurrency) {
  277. $oldCurrency = $this->currency->getCurrency();
  278. $balanceInSubunits = $this->getAmount();
  279. $oldCurrencySubunit = currency($oldCurrency)->getSubunit();
  280. $newCurrencySubunit = currency($newCurrency)->getSubunit();
  281. $balanceInMajorUnits = $balanceInSubunits / $oldCurrencySubunit;
  282. $oldRate = currency($oldCurrency)->getRate();
  283. $newRate = currency($newCurrency)->getRate();
  284. $ratio = $newRate / $oldRate;
  285. $convertedBalanceInMajorUnits = $balanceInMajorUnits * $ratio;
  286. $roundedConvertedBalanceInMajorUnits = round($convertedBalanceInMajorUnits, currency($newCurrency)->getPrecision());
  287. $convertedBalanceInSubunits = $roundedConvertedBalanceInMajorUnits * $newCurrencySubunit;
  288. return (int) round($convertedBalanceInSubunits);
  289. });
  290. Money::macro('formatWithCode', function (bool $codeBefore = false) {
  291. $formatted = $this->format();
  292. $currencyCode = $this->currency->getCurrency();
  293. if ($codeBefore) {
  294. return $currencyCode . ' ' . $formatted;
  295. }
  296. return $formatted . ' ' . $currencyCode;
  297. });
  298. Currency::macro('getEntity', function () {
  299. $currencyCode = $this->getCurrency();
  300. $entity = config("money.currencies.{$currencyCode}.entity");
  301. return $entity ?? $currencyCode;
  302. });
  303. Currency::macro('getCodePrefix', function () {
  304. if ($this->isSymbolFirst()) {
  305. return '';
  306. }
  307. return ' ' . $this->getCurrency();
  308. });
  309. Currency::macro('getCodeSuffix', function () {
  310. if ($this->isSymbolFirst()) {
  311. return ' ' . $this->getCurrency();
  312. }
  313. return '';
  314. });
  315. Carbon::macro('toDefaultDateFormat', function () {
  316. $localization = Localization::firstOrFail();
  317. $dateFormat = $localization->date_format->value ?? DateFormat::DEFAULT;
  318. $timezone = $localization->timezone ?? Carbon::now()->timezoneName;
  319. return $this->setTimezone($timezone)->format($dateFormat);
  320. });
  321. }
  322. }