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

MacroServiceProvider.php 15KB

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