Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

MacroServiceProvider.php 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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\Services\CompanySettingsService;
  10. use App\Utilities\Accounting\AccountCode;
  11. use App\Utilities\Currency\CurrencyAccessor;
  12. use App\Utilities\Currency\CurrencyConverter;
  13. use BackedEnum;
  14. use Carbon\CarbonInterface;
  15. use Closure;
  16. use Filament\Forms\Components\DatePicker;
  17. use Filament\Forms\Components\Field;
  18. use Filament\Forms\Components\TextInput;
  19. use Filament\Infolists\Components\TextEntry;
  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. $localization = Localization::firstOrFail();
  145. $dateFormat = $localization->date_format->value ?? DateFormat::DEFAULT;
  146. $timezone = $localization->timezone ?? Carbon::now()->timezoneName;
  147. $this->date($dateFormat, $timezone);
  148. return $this;
  149. });
  150. DatePicker::macro('defaultDateFormat', function (): static {
  151. $localization = Localization::firstOrFail();
  152. $dateFormat = $localization->date_format->value ?? DateFormat::DEFAULT;
  153. $timezone = $localization->timezone ?? Carbon::now()->timezoneName;
  154. $this->displayFormat($dateFormat)
  155. ->timezone($timezone);
  156. return $this;
  157. });
  158. TextColumn::macro('currency', function (string | Closure | null $currency = null, ?bool $convert = null): static {
  159. $currency ??= CurrencyAccessor::getDefaultCurrency();
  160. $convert ??= false;
  161. $this->formatStateUsing(static function (TextColumn $column, $state) use ($currency, $convert): ?string {
  162. if (blank($state)) {
  163. return null;
  164. }
  165. $currency = $column->evaluate($currency);
  166. $convert = $column->evaluate($convert);
  167. return money($state, $currency, $convert)->format();
  168. });
  169. return $this;
  170. });
  171. TextEntry::macro('currency', function (string | Closure | null $currency = null, ?bool $convert = null): static {
  172. $currency ??= CurrencyAccessor::getDefaultCurrency();
  173. $convert ??= false;
  174. $this->formatStateUsing(static function (TextEntry $entry, $state) use ($currency, $convert): ?string {
  175. if (blank($state)) {
  176. return null;
  177. }
  178. $currency = $entry->evaluate($currency);
  179. $convert = $entry->evaluate($convert);
  180. return money($state, $currency, $convert)->format();
  181. });
  182. return $this;
  183. });
  184. TextColumn::macro('currencyWithConversion', function (string | Closure | null $currency = null, ?bool $convertFromCents = null): static {
  185. $currency ??= CurrencyAccessor::getDefaultCurrency();
  186. $convertFromCents ??= true;
  187. $this->formatStateUsing(static function (TextColumn $column, $state) use ($currency, $convertFromCents): ?string {
  188. if (blank($state)) {
  189. return null;
  190. }
  191. $currency = $column->evaluate($currency);
  192. $showCurrency = $currency !== CurrencyAccessor::getDefaultCurrency();
  193. if ($convertFromCents) {
  194. $balanceInCents = $state;
  195. } else {
  196. $balanceInCents = CurrencyConverter::convertToCents($state, $currency);
  197. }
  198. if ($balanceInCents < 0) {
  199. return '(' . CurrencyConverter::formatCentsToMoney(abs($balanceInCents), $currency, $showCurrency) . ')';
  200. }
  201. return CurrencyConverter::formatCentsToMoney($balanceInCents, $currency, $showCurrency);
  202. });
  203. $this->description(static function (TextColumn $column, $state) use ($currency, $convertFromCents): ?string {
  204. if (blank($state)) {
  205. return null;
  206. }
  207. $oldCurrency = $column->evaluate($currency);
  208. $newCurrency = CurrencyAccessor::getDefaultCurrency();
  209. if ($oldCurrency === $newCurrency) {
  210. return null;
  211. }
  212. if ($convertFromCents) {
  213. $balanceInCents = $state;
  214. } else {
  215. $balanceInCents = CurrencyConverter::convertToCents($state, $oldCurrency);
  216. }
  217. $convertedBalanceInCents = CurrencyConverter::convertBalance($balanceInCents, $oldCurrency, $newCurrency);
  218. if ($convertedBalanceInCents < 0) {
  219. return '(' . CurrencyConverter::formatCentsToMoney(abs($convertedBalanceInCents), $newCurrency, true) . ')';
  220. }
  221. return CurrencyConverter::formatCentsToMoney($convertedBalanceInCents, $newCurrency, true);
  222. });
  223. return $this;
  224. });
  225. TextEntry::macro('currencyWithConversion', function (string | Closure | null $currency = null): static {
  226. $currency ??= CurrencyAccessor::getDefaultCurrency();
  227. $this->formatStateUsing(static function (TextEntry $entry, $state) use ($currency): ?string {
  228. if (blank($state)) {
  229. return null;
  230. }
  231. $currency = $entry->evaluate($currency);
  232. return CurrencyConverter::formatToMoney($state, $currency);
  233. });
  234. $this->helperText(static function (TextEntry $entry, $state) use ($currency): ?string {
  235. if (blank($state)) {
  236. return null;
  237. }
  238. $oldCurrency = $entry->evaluate($currency);
  239. $newCurrency = CurrencyAccessor::getDefaultCurrency();
  240. if ($oldCurrency === $newCurrency) {
  241. return null;
  242. }
  243. $balanceInCents = CurrencyConverter::convertToCents($state, $oldCurrency);
  244. $convertedBalanceInCents = CurrencyConverter::convertBalance($balanceInCents, $oldCurrency, $newCurrency);
  245. return CurrencyConverter::formatCentsToMoney($convertedBalanceInCents, $newCurrency, true);
  246. });
  247. return $this;
  248. });
  249. Field::macro('validateAccountCode', function (string | Closure | null $subtype = null): static {
  250. $this
  251. ->rules([
  252. fn (Field $component): Closure => static function (string $attribute, $value, Closure $fail) use ($subtype, $component) {
  253. $subtype = $component->evaluate($subtype);
  254. $chartSubtype = AccountSubtype::find($subtype);
  255. $type = $chartSubtype->type;
  256. if (! AccountCode::isValidCode($value, $type)) {
  257. $message = AccountCode::getMessage($type);
  258. $fail($message);
  259. }
  260. },
  261. ]);
  262. return $this;
  263. });
  264. TextColumn::macro('rate', function (string | Closure | null $computation = null): static {
  265. $this->formatStateUsing(static function (TextColumn $column, $state) use ($computation): ?string {
  266. $computation = $column->evaluate($computation);
  267. return rateFormat(state: $state, computation: $computation);
  268. });
  269. return $this;
  270. });
  271. Field::macro('softRequired', function (): static {
  272. $this
  273. ->required()
  274. ->markAsRequired(false);
  275. return $this;
  276. });
  277. TextColumn::macro('asRelativeDay', function (?string $timezone = null): static {
  278. $this->formatStateUsing(function (TextColumn $column, mixed $state) use ($timezone) {
  279. if (blank($state)) {
  280. return null;
  281. }
  282. $date = Carbon::parse($state)
  283. ->setTimezone($timezone ?? $column->getTimezone());
  284. if ($date->isToday()) {
  285. return 'Today';
  286. }
  287. return $date->diffForHumans([
  288. 'options' => CarbonInterface::ONE_DAY_WORDS,
  289. ]);
  290. });
  291. return $this;
  292. });
  293. TextEntry::macro('asRelativeDay', function (?string $timezone = null): static {
  294. $this->formatStateUsing(function (TextEntry $entry, mixed $state) use ($timezone) {
  295. if (blank($state)) {
  296. return null;
  297. }
  298. $date = Carbon::parse($state)
  299. ->setTimezone($timezone ?? $entry->getTimezone());
  300. if ($date->isToday()) {
  301. return 'Today';
  302. }
  303. return $date->diffForHumans([
  304. 'options' => CarbonInterface::ONE_DAY_WORDS,
  305. ]);
  306. });
  307. return $this;
  308. });
  309. TextEntry::macro('link', function (bool $condition = true): static {
  310. if ($condition) {
  311. $this
  312. ->limit(50)
  313. ->openUrlInNewTab()
  314. ->icon('heroicon-o-arrow-top-right-on-square')
  315. ->iconColor('primary')
  316. ->iconPosition(IconPosition::After);
  317. }
  318. return $this;
  319. });
  320. Money::macro('swapAmountFor', function ($newCurrency) {
  321. $oldCurrency = $this->currency->getCurrency();
  322. $balanceInSubunits = $this->getAmount();
  323. $oldCurrencySubunit = currency($oldCurrency)->getSubunit();
  324. $newCurrencySubunit = currency($newCurrency)->getSubunit();
  325. $balanceInMajorUnits = $balanceInSubunits / $oldCurrencySubunit;
  326. $oldRate = currency($oldCurrency)->getRate();
  327. $newRate = currency($newCurrency)->getRate();
  328. $ratio = $newRate / $oldRate;
  329. $convertedBalanceInMajorUnits = $balanceInMajorUnits * $ratio;
  330. $roundedConvertedBalanceInMajorUnits = round($convertedBalanceInMajorUnits, currency($newCurrency)->getPrecision());
  331. $convertedBalanceInSubunits = $roundedConvertedBalanceInMajorUnits * $newCurrencySubunit;
  332. return (int) round($convertedBalanceInSubunits);
  333. });
  334. Money::macro('formatWithCode', function (bool $codeBefore = false) {
  335. $formatted = $this->format();
  336. $currencyCode = $this->currency->getCurrency();
  337. if ($codeBefore) {
  338. return $currencyCode . ' ' . $formatted;
  339. }
  340. return $formatted . ' ' . $currencyCode;
  341. });
  342. Currency::macro('getEntity', function () {
  343. $currencyCode = $this->getCurrency();
  344. $entity = config("money.currencies.{$currencyCode}.entity");
  345. return $entity ?? $currencyCode;
  346. });
  347. Currency::macro('getCodePrefix', function () {
  348. if ($this->isSymbolFirst()) {
  349. return '';
  350. }
  351. return ' ' . $this->getCurrency();
  352. });
  353. Currency::macro('getCodeSuffix', function () {
  354. if ($this->isSymbolFirst()) {
  355. return ' ' . $this->getCurrency();
  356. }
  357. return '';
  358. });
  359. Carbon::macro('toDefaultDateFormat', function () {
  360. $companyId = auth()->user()?->current_company_id;
  361. $dateFormat = CompanySettingsService::getDefaultDateFormat($companyId);
  362. $timezone = CompanySettingsService::getDefaultTimezone($companyId);
  363. return $this->setTimezone($timezone)->format($dateFormat);
  364. });
  365. }
  366. }