Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

MacroServiceProvider.php 16KB

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