您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

MacroServiceProvider.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. $showCurrency = $currency !== CurrencyAccessor::getDefaultCurrency();
  169. if ($convertFromCents) {
  170. $balanceInCents = $state;
  171. } else {
  172. $balanceInCents = CurrencyConverter::convertToCents($state, $currency);
  173. }
  174. if ($balanceInCents < 0) {
  175. return '(' . CurrencyConverter::formatCentsToMoney(abs($balanceInCents), $currency, $showCurrency) . ')';
  176. }
  177. return CurrencyConverter::formatCentsToMoney($balanceInCents, $currency, $showCurrency);
  178. });
  179. $this->description(static function (TextColumn $column, $state) use ($currency, $convertFromCents): ?string {
  180. if (blank($state)) {
  181. return null;
  182. }
  183. $oldCurrency = $column->evaluate($currency);
  184. $newCurrency = CurrencyAccessor::getDefaultCurrency();
  185. if ($oldCurrency === $newCurrency) {
  186. return null;
  187. }
  188. if ($convertFromCents) {
  189. $balanceInCents = $state;
  190. } else {
  191. $balanceInCents = CurrencyConverter::convertToCents($state, $oldCurrency);
  192. }
  193. $convertedBalanceInCents = CurrencyConverter::convertBalance($balanceInCents, $oldCurrency, $newCurrency);
  194. if ($convertedBalanceInCents < 0) {
  195. return '(' . CurrencyConverter::formatCentsToMoney(abs($convertedBalanceInCents), $newCurrency, true) . ')';
  196. }
  197. return CurrencyConverter::formatCentsToMoney($convertedBalanceInCents, $newCurrency, true);
  198. });
  199. return $this;
  200. });
  201. TextEntry::macro('currencyWithConversion', function (string | Closure | null $currency = null): static {
  202. $currency ??= CurrencyAccessor::getDefaultCurrency();
  203. $this->formatStateUsing(static function (TextEntry $entry, $state) use ($currency): ?string {
  204. if (blank($state)) {
  205. return null;
  206. }
  207. $currency = $entry->evaluate($currency);
  208. return CurrencyConverter::formatToMoney($state, $currency);
  209. });
  210. $this->helperText(static function (TextEntry $entry, $state) use ($currency): ?string {
  211. if (blank($state)) {
  212. return null;
  213. }
  214. $oldCurrency = $entry->evaluate($currency);
  215. $newCurrency = CurrencyAccessor::getDefaultCurrency();
  216. if ($oldCurrency === $newCurrency) {
  217. return null;
  218. }
  219. $balanceInCents = CurrencyConverter::convertToCents($state, $oldCurrency);
  220. $convertedBalanceInCents = CurrencyConverter::convertBalance($balanceInCents, $oldCurrency, $newCurrency);
  221. return CurrencyConverter::formatCentsToMoney($convertedBalanceInCents, $newCurrency, true);
  222. });
  223. return $this;
  224. });
  225. Field::macro('validateAccountCode', function (string | Closure | null $subtype = null): static {
  226. $this
  227. ->rules([
  228. fn (Field $component): Closure => static function (string $attribute, $value, Closure $fail) use ($subtype, $component) {
  229. $subtype = $component->evaluate($subtype);
  230. $chartSubtype = AccountSubtype::find($subtype);
  231. $type = $chartSubtype->type;
  232. if (! AccountCode::isValidCode($value, $type)) {
  233. $message = AccountCode::getMessage($type);
  234. $fail($message);
  235. }
  236. },
  237. ]);
  238. return $this;
  239. });
  240. TextColumn::macro('rate', function (string | Closure | null $computation = null): static {
  241. $this->formatStateUsing(static function (TextColumn $column, $state) use ($computation): ?string {
  242. $computation = $column->evaluate($computation);
  243. return rateFormat(state: $state, computation: $computation);
  244. });
  245. return $this;
  246. });
  247. Field::macro('softRequired', function (): static {
  248. $this
  249. ->required()
  250. ->markAsRequired(false);
  251. return $this;
  252. });
  253. TextColumn::macro('asRelativeDay', function (?string $timezone = null): static {
  254. $this->formatStateUsing(function (TextColumn $column, mixed $state) use ($timezone) {
  255. if (blank($state)) {
  256. return null;
  257. }
  258. $date = Carbon::parse($state)
  259. ->setTimezone($timezone ?? $column->getTimezone());
  260. if ($date->isToday()) {
  261. return 'Today';
  262. }
  263. return $date->diffForHumans([
  264. 'options' => CarbonInterface::ONE_DAY_WORDS,
  265. ]);
  266. });
  267. return $this;
  268. });
  269. TextEntry::macro('asRelativeDay', function (?string $timezone = null): static {
  270. $this->formatStateUsing(function (TextEntry $entry, mixed $state) use ($timezone) {
  271. if (blank($state)) {
  272. return null;
  273. }
  274. $date = Carbon::parse($state)
  275. ->setTimezone($timezone ?? $entry->getTimezone());
  276. if ($date->isToday()) {
  277. return 'Today';
  278. }
  279. return $date->diffForHumans([
  280. 'options' => CarbonInterface::ONE_DAY_WORDS,
  281. ]);
  282. });
  283. return $this;
  284. });
  285. Money::macro('swapAmountFor', function ($newCurrency) {
  286. $oldCurrency = $this->currency->getCurrency();
  287. $balanceInSubunits = $this->getAmount();
  288. $oldCurrencySubunit = currency($oldCurrency)->getSubunit();
  289. $newCurrencySubunit = currency($newCurrency)->getSubunit();
  290. $balanceInMajorUnits = $balanceInSubunits / $oldCurrencySubunit;
  291. $oldRate = currency($oldCurrency)->getRate();
  292. $newRate = currency($newCurrency)->getRate();
  293. $ratio = $newRate / $oldRate;
  294. $convertedBalanceInMajorUnits = $balanceInMajorUnits * $ratio;
  295. $roundedConvertedBalanceInMajorUnits = round($convertedBalanceInMajorUnits, currency($newCurrency)->getPrecision());
  296. $convertedBalanceInSubunits = $roundedConvertedBalanceInMajorUnits * $newCurrencySubunit;
  297. return (int) round($convertedBalanceInSubunits);
  298. });
  299. Money::macro('formatWithCode', function (bool $codeBefore = false) {
  300. $formatted = $this->format();
  301. $currencyCode = $this->currency->getCurrency();
  302. if ($codeBefore) {
  303. return $currencyCode . ' ' . $formatted;
  304. }
  305. return $formatted . ' ' . $currencyCode;
  306. });
  307. Currency::macro('getEntity', function () {
  308. $currencyCode = $this->getCurrency();
  309. $entity = config("money.currencies.{$currencyCode}.entity");
  310. return $entity ?? $currencyCode;
  311. });
  312. Currency::macro('getCodePrefix', function () {
  313. if ($this->isSymbolFirst()) {
  314. return '';
  315. }
  316. return ' ' . $this->getCurrency();
  317. });
  318. Currency::macro('getCodeSuffix', function () {
  319. if ($this->isSymbolFirst()) {
  320. return ' ' . $this->getCurrency();
  321. }
  322. return '';
  323. });
  324. Carbon::macro('toDefaultDateFormat', function () {
  325. $localization = Localization::firstOrFail();
  326. $dateFormat = $localization->date_format->value ?? DateFormat::DEFAULT;
  327. $timezone = $localization->timezone ?? Carbon::now()->timezoneName;
  328. return $this->setTimezone($timezone)->format($dateFormat);
  329. });
  330. }
  331. }