選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

MacroServiceProvider.php 15KB

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