Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

MacroServiceProvider.php 14KB

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