Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

MacroServiceProvider.php 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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\Models\Accounting\AccountSubtype;
  7. use App\Services\CompanySettingsService;
  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\Actions\Exports\ExportColumn;
  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\Support\Contracts\HasLabel;
  20. use Filament\Support\Enums\IconPosition;
  21. use Filament\Support\RawJs;
  22. use Filament\Tables\Columns\TextColumn;
  23. use Filament\Tables\Contracts\HasTable;
  24. use Illuminate\Contracts\Support\Htmlable;
  25. use Illuminate\Support\Carbon;
  26. use Illuminate\Support\Collection;
  27. use Illuminate\Support\HtmlString;
  28. use Illuminate\Support\ServiceProvider;
  29. class MacroServiceProvider extends ServiceProvider
  30. {
  31. /**
  32. * Register services.
  33. */
  34. public function register(): void
  35. {
  36. //
  37. }
  38. /**
  39. * Bootstrap services.
  40. */
  41. public function boot(): void
  42. {
  43. Collection::macro('whereNot', function (callable | string $key, mixed $value = null): Collection {
  44. return $this->where($key, '!=', $value);
  45. });
  46. TextInput::macro('money', function (string | Closure | null $currency = null, bool $useAffix = true): static {
  47. $currency ??= CurrencyAccessor::getDefaultCurrency();
  48. if ($useAffix) {
  49. $this
  50. ->prefix(static function (TextInput $component) use ($currency) {
  51. $currency = $component->evaluate($currency);
  52. return currency($currency)->getPrefix();
  53. })
  54. ->suffix(static function (TextInput $component) use ($currency) {
  55. $currency = $component->evaluate($currency);
  56. return currency($currency)->getSuffix();
  57. });
  58. }
  59. $this->mask(RawJs::make('$money($input)'))
  60. ->afterStateHydrated(function (TextInput $component, ?int $state) {
  61. if (blank($state)) {
  62. return;
  63. }
  64. $formatted = CurrencyConverter::convertCentsToFormatSimple($state, 'USD');
  65. $component->state($formatted);
  66. })
  67. ->dehydrateStateUsing(function (?string $state): ?int {
  68. if (blank($state)) {
  69. return null;
  70. }
  71. // Remove thousand separators
  72. $cleaned = str_replace(',', '', $state);
  73. // If no decimal point, assume it's whole dollars (add .00)
  74. if (! str_contains($cleaned, '.')) {
  75. $cleaned .= '.00';
  76. }
  77. // Convert to float then to cents (integer)
  78. return (int) round((float) $cleaned * 100);
  79. });
  80. return $this;
  81. });
  82. TextInput::macro('rate', function (string | Closure | null $computation = null, string | Closure | null $currency = null, bool $showAffix = true): static {
  83. return $this
  84. ->when(
  85. $showAffix,
  86. fn (TextInput $component) => $component
  87. ->prefix(function (TextInput $component) use ($computation, $currency) {
  88. $evaluatedComputation = $component->evaluate($computation);
  89. $evaluatedCurrency = $component->evaluate($currency);
  90. return ratePrefix($evaluatedComputation, $evaluatedCurrency);
  91. })
  92. ->suffix(function (TextInput $component) use ($computation, $currency) {
  93. $evaluatedComputation = $component->evaluate($computation);
  94. $evaluatedCurrency = $component->evaluate($currency);
  95. return rateSuffix($evaluatedComputation, $evaluatedCurrency);
  96. })
  97. )
  98. ->mask(static function (TextInput $component) use ($computation, $currency) {
  99. $computation = $component->evaluate($computation);
  100. $currency = $component->evaluate($currency);
  101. $computationEnum = AdjustmentComputation::parse($computation);
  102. if ($computationEnum->isPercentage()) {
  103. return rateMask(computation: $computation);
  104. }
  105. return moneyMask($currency);
  106. })
  107. ->rule(static function (TextInput $component) use ($computation) {
  108. return static function (string $attribute, $value, Closure $fail) use ($computation, $component) {
  109. $computation = $component->evaluate($computation);
  110. $numericValue = (float) $value;
  111. if ($computation instanceof BackedEnum) {
  112. $computation = $computation->value;
  113. }
  114. if ($computation === 'percentage' || $computation === 'compound') {
  115. if ($numericValue < 0 || $numericValue > 100) {
  116. $fail(translate('The rate must be between 0 and 100.'));
  117. }
  118. } elseif ($computation === 'fixed' && $numericValue < 0) {
  119. $fail(translate('The rate must be greater than 0.'));
  120. }
  121. };
  122. });
  123. });
  124. TextColumn::macro('coloredDescription', function (string | Htmlable | Closure | null $description, string $color = 'danger') {
  125. $this->description(static function (TextColumn $column) use ($description, $color): Htmlable {
  126. $description = $column->evaluate($description);
  127. return new HtmlString("<span class='text-{$color}-700 dark:text-{$color}-400'>{$description}</span>");
  128. });
  129. return $this;
  130. });
  131. TextColumn::macro('hideOnTabs', function (array $tabs): static {
  132. $this->toggleable(isToggledHiddenByDefault: function (HasTable $livewire) use ($tabs) {
  133. return in_array($livewire->activeTab, $tabs);
  134. });
  135. return $this;
  136. });
  137. TextColumn::macro('showOnTabs', function (array $tabs): static {
  138. $this->toggleable(isToggledHiddenByDefault: function (HasTable $livewire) use ($tabs) {
  139. return ! in_array($livewire->activeTab, $tabs);
  140. });
  141. return $this;
  142. });
  143. TextColumn::macro('defaultDateFormat', function (): static {
  144. $dateFormat = CompanySettingsService::getDefaultDateFormat();
  145. $this->date($dateFormat);
  146. return $this;
  147. });
  148. DatePicker::macro('defaultDateFormat', function (): static {
  149. $dateFormat = CompanySettingsService::getDefaultDateFormat();
  150. $this->displayFormat($dateFormat);
  151. return $this;
  152. });
  153. TextColumn::macro('currency', function (string | Closure | null $currency = null, ?bool $convert = null): static {
  154. $currency ??= CurrencyAccessor::getDefaultCurrency();
  155. $convert ??= false;
  156. $this->formatStateUsing(static function (TextColumn $column, $state) use ($currency, $convert): ?string {
  157. if (blank($state)) {
  158. return null;
  159. }
  160. $currency = $column->evaluate($currency);
  161. $convert = $column->evaluate($convert);
  162. return money($state, $currency, $convert)->format();
  163. });
  164. return $this;
  165. });
  166. TextEntry::macro('currency', function (string | Closure | null $currency = null, ?bool $convert = null): static {
  167. $currency ??= CurrencyAccessor::getDefaultCurrency();
  168. $convert ??= false;
  169. $this->formatStateUsing(static function (TextEntry $entry, $state) use ($currency, $convert): ?string {
  170. if (blank($state)) {
  171. return null;
  172. }
  173. $currency = $entry->evaluate($currency);
  174. $convert = $entry->evaluate($convert);
  175. return money($state, $currency, $convert)->format();
  176. });
  177. return $this;
  178. });
  179. TextColumn::macro('currencyWithConversion', function (string | Closure | null $currency = null, ?bool $convertFromCents = null): static {
  180. $currency ??= CurrencyAccessor::getDefaultCurrency();
  181. $convertFromCents ??= true;
  182. $this->formatStateUsing(static function (TextColumn $column, $state) use ($currency, $convertFromCents): ?string {
  183. if (blank($state)) {
  184. return null;
  185. }
  186. $currency = $column->evaluate($currency);
  187. $showCurrency = $currency !== CurrencyAccessor::getDefaultCurrency();
  188. if ($convertFromCents) {
  189. $balanceInCents = $state;
  190. } else {
  191. $balanceInCents = CurrencyConverter::convertToCents($state, $currency);
  192. }
  193. if ($balanceInCents < 0) {
  194. return '(' . CurrencyConverter::formatCentsToMoney(abs($balanceInCents), $currency, $showCurrency) . ')';
  195. }
  196. return CurrencyConverter::formatCentsToMoney($balanceInCents, $currency, $showCurrency);
  197. });
  198. $this->description(static function (TextColumn $column, $state) use ($currency, $convertFromCents): ?string {
  199. if (blank($state)) {
  200. return null;
  201. }
  202. $oldCurrency = $column->evaluate($currency);
  203. $newCurrency = CurrencyAccessor::getDefaultCurrency();
  204. if ($oldCurrency === $newCurrency) {
  205. return null;
  206. }
  207. if ($convertFromCents) {
  208. $balanceInCents = $state;
  209. } else {
  210. $balanceInCents = CurrencyConverter::convertToCents($state, $oldCurrency);
  211. }
  212. $convertedBalanceInCents = CurrencyConverter::convertBalance($balanceInCents, $oldCurrency, $newCurrency);
  213. if ($convertedBalanceInCents < 0) {
  214. return '(' . CurrencyConverter::formatCentsToMoney(abs($convertedBalanceInCents), $newCurrency, true) . ')';
  215. }
  216. return CurrencyConverter::formatCentsToMoney($convertedBalanceInCents, $newCurrency, true);
  217. });
  218. return $this;
  219. });
  220. TextEntry::macro('currencyWithConversion', function (string | Closure | null $currency = null): static {
  221. $currency ??= CurrencyAccessor::getDefaultCurrency();
  222. $this->formatStateUsing(static function (TextEntry $entry, $state) use ($currency): ?string {
  223. if (blank($state)) {
  224. return null;
  225. }
  226. $currency = $entry->evaluate($currency);
  227. return CurrencyConverter::formatToMoney($state, $currency);
  228. });
  229. $this->helperText(static function (TextEntry $entry, $state) use ($currency): ?string {
  230. if (blank($state)) {
  231. return null;
  232. }
  233. $oldCurrency = $entry->evaluate($currency);
  234. $newCurrency = CurrencyAccessor::getDefaultCurrency();
  235. if ($oldCurrency === $newCurrency) {
  236. return null;
  237. }
  238. $balanceInCents = CurrencyConverter::convertToCents($state, $oldCurrency);
  239. $convertedBalanceInCents = CurrencyConverter::convertBalance($balanceInCents, $oldCurrency, $newCurrency);
  240. return CurrencyConverter::formatCentsToMoney($convertedBalanceInCents, $newCurrency, true);
  241. });
  242. return $this;
  243. });
  244. Field::macro('validateAccountCode', function (string | Closure | null $subtype = null): static {
  245. $this
  246. ->rules([
  247. fn (Field $component): Closure => static function (string $attribute, $value, Closure $fail) use ($subtype, $component) {
  248. $subtype = $component->evaluate($subtype);
  249. $chartSubtype = AccountSubtype::find($subtype);
  250. $type = $chartSubtype->type;
  251. if (! AccountCode::isValidCode($value, $type)) {
  252. $message = AccountCode::getMessage($type);
  253. $fail($message);
  254. }
  255. },
  256. ]);
  257. return $this;
  258. });
  259. TextColumn::macro('rate', function (string | Closure | null $computation = null): static {
  260. $this->formatStateUsing(static function (TextColumn $column, $state) use ($computation): ?string {
  261. $computation = $column->evaluate($computation);
  262. return rateFormat(state: $state, computation: $computation);
  263. });
  264. return $this;
  265. });
  266. Field::macro('softRequired', function (): static {
  267. $this
  268. ->required()
  269. ->markAsRequired(false);
  270. return $this;
  271. });
  272. // In your macro - simpler logic
  273. TextColumn::macro('asRelativeDay', function (?string $timezone = null): static {
  274. $this->formatStateUsing(function (TextColumn $column, mixed $state) use ($timezone) {
  275. if (blank($state)) {
  276. return null;
  277. }
  278. $timezone ??= $column->getTimezone() ?? CompanySettingsService::getDefaultTimezone();
  279. // Use shiftTimezone to shift UTC calendar date to the specified timezone
  280. // Using setTimezone would convert which is wrong for calendar dates
  281. $date = Carbon::parse($state)->shiftTimezone($timezone);
  282. if ($date->isToday()) {
  283. return 'Today';
  284. } elseif ($date->isTomorrow()) {
  285. return 'Tomorrow';
  286. } elseif ($date->isYesterday()) {
  287. return 'Yesterday';
  288. }
  289. return $date->diffForHumans([
  290. 'options' => CarbonInterface::ONE_DAY_WORDS,
  291. 'skip' => ['month', 'week'], // Skip larger units, force days and years only
  292. 'parts' => 2,
  293. 'join' => ', ',
  294. ]);
  295. });
  296. return $this;
  297. });
  298. TextEntry::macro('asRelativeDay', function (?string $timezone = null): static {
  299. $this->formatStateUsing(function (TextEntry $entry, mixed $state) use ($timezone) {
  300. if (blank($state)) {
  301. return null;
  302. }
  303. $timezone ??= $entry->getTimezone() ?? CompanySettingsService::getDefaultTimezone();
  304. // Use shiftTimezone to shift UTC calendar date to the specified timezone
  305. // Using setTimezone would convert which is wrong for calendar dates
  306. $date = Carbon::parse($state)->shiftTimezone($timezone);
  307. if ($date->isToday()) {
  308. return 'Today';
  309. } elseif ($date->isTomorrow()) {
  310. return 'Tomorrow';
  311. } elseif ($date->isYesterday()) {
  312. return 'Yesterday';
  313. }
  314. return $date->diffForHumans([
  315. 'options' => CarbonInterface::ONE_DAY_WORDS,
  316. 'skip' => ['month', 'week'], // Skip larger units, force days and years only
  317. 'parts' => 2,
  318. 'join' => ', ',
  319. ]);
  320. });
  321. return $this;
  322. });
  323. TextEntry::macro('link', function (bool $condition = true): static {
  324. if ($condition) {
  325. $this
  326. ->limit(50)
  327. ->openUrlInNewTab()
  328. ->icon('heroicon-o-arrow-top-right-on-square')
  329. ->iconColor('primary')
  330. ->iconPosition(IconPosition::After);
  331. }
  332. return $this;
  333. });
  334. Money::macro('swapAmountFor', function ($newCurrency) {
  335. $oldCurrency = $this->currency->getCurrency();
  336. $balanceInSubunits = $this->getAmount();
  337. $oldCurrencySubunit = currency($oldCurrency)->getSubunit();
  338. $newCurrencySubunit = currency($newCurrency)->getSubunit();
  339. $balanceInMajorUnits = $balanceInSubunits / $oldCurrencySubunit;
  340. $oldRate = currency($oldCurrency)->getRate();
  341. $newRate = currency($newCurrency)->getRate();
  342. $ratio = $newRate / $oldRate;
  343. $convertedBalanceInMajorUnits = $balanceInMajorUnits * $ratio;
  344. $roundedConvertedBalanceInMajorUnits = round($convertedBalanceInMajorUnits, currency($newCurrency)->getPrecision());
  345. $convertedBalanceInSubunits = $roundedConvertedBalanceInMajorUnits * $newCurrencySubunit;
  346. return (int) round($convertedBalanceInSubunits);
  347. });
  348. Money::macro('formatWithCode', function (bool $codeBefore = false) {
  349. $formatted = $this->format();
  350. $currencyCode = $this->currency->getCurrency();
  351. if ($codeBefore) {
  352. return $currencyCode . ' ' . $formatted;
  353. }
  354. return $formatted . ' ' . $currencyCode;
  355. });
  356. Currency::macro('getEntity', function () {
  357. $currencyCode = $this->getCurrency();
  358. $entity = config("money.currencies.{$currencyCode}.entity");
  359. return $entity ?? $currencyCode;
  360. });
  361. Currency::macro('getCodePrefix', function () {
  362. if ($this->isSymbolFirst()) {
  363. return '';
  364. }
  365. return ' ' . $this->getCurrency();
  366. });
  367. Currency::macro('getCodeSuffix', function () {
  368. if ($this->isSymbolFirst()) {
  369. return ' ' . $this->getCurrency();
  370. }
  371. return '';
  372. });
  373. Carbon::macro('toDefaultDateFormat', function () {
  374. $dateFormat = CompanySettingsService::getDefaultDateFormat();
  375. return $this->format($dateFormat);
  376. });
  377. Carbon::macro('inCompanyTimezone', function () {
  378. $timezone = CompanySettingsService::getDefaultTimezone();
  379. return $this->setTimezone($timezone);
  380. });
  381. ExportColumn::macro('money', function () {
  382. $this->formatStateUsing(static function ($state) {
  383. if (blank($state) || ! is_int($state)) {
  384. return 0.00;
  385. }
  386. return CurrencyConverter::convertCentsToFloat($state);
  387. });
  388. return $this;
  389. });
  390. ExportColumn::macro('date', function () {
  391. $this->formatStateUsing(static function ($state) {
  392. if (blank($state)) {
  393. return null;
  394. }
  395. try {
  396. return Carbon::parse($state)->toDateString();
  397. } catch (\Exception) {
  398. return null;
  399. }
  400. });
  401. return $this;
  402. });
  403. ExportColumn::macro('dateTime', function () {
  404. $this->formatStateUsing(static function ($state) {
  405. if (blank($state)) {
  406. return null;
  407. }
  408. try {
  409. return Carbon::parse($state)->toDateTimeString();
  410. } catch (\Exception) {
  411. return null;
  412. }
  413. });
  414. return $this;
  415. });
  416. ExportColumn::macro('enum', function () {
  417. $this->formatStateUsing(static function ($state) {
  418. if (blank($state)) {
  419. return null;
  420. }
  421. if (! ($state instanceof BackedEnum)) {
  422. return $state;
  423. }
  424. if ($state instanceof HasLabel) {
  425. return $state->getLabel();
  426. }
  427. return $state->value;
  428. });
  429. return $this;
  430. });
  431. }
  432. }