123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
-
- namespace App\Providers;
-
- use Akaunting\Money\Currency;
- use Akaunting\Money\Money;
- use App\Enums\Setting\DateFormat;
- use App\Models\Accounting\AccountSubtype;
- use App\Models\Setting\Localization;
- use App\Utilities\Accounting\AccountCode;
- use App\Utilities\Currency\CurrencyAccessor;
- use BackedEnum;
- use Closure;
- use Filament\Forms\Components\DatePicker;
- use Filament\Forms\Components\Field;
- use Filament\Forms\Components\TextInput;
- use Filament\Tables\Columns\TextColumn;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\ServiceProvider;
- use Illuminate\Support\Str;
-
- class MacroServiceProvider extends ServiceProvider
- {
- /**
- * Register services.
- */
- public function register(): void
- {
- //
- }
-
- /**
- * Bootstrap services.
- */
- public function boot(): void
- {
- TextInput::macro('money', function (string | Closure | null $currency = null): static {
- $currency ??= CurrencyAccessor::getDefaultCurrency();
-
- $this->extraAttributes(['wire:key' => Str::random()])
- ->prefix(static function (TextInput $component) use ($currency) {
- $currency = $component->evaluate($currency);
-
- return currency($currency)->getPrefix();
- })
- ->suffix(static function (TextInput $component) use ($currency) {
- $currency = $component->evaluate($currency);
-
- return currency($currency)->getSuffix();
- })
- ->mask(static function (TextInput $component) use ($currency) {
- $currency = $component->evaluate($currency);
-
- return moneyMask($currency);
- });
-
- return $this;
- });
-
- TextColumn::macro('defaultDateFormat', function (): static {
- $localization = Localization::firstOrFail();
-
- $dateFormat = $localization->date_format->value ?? DateFormat::DEFAULT;
- $timezone = $localization->timezone ?? Carbon::now()->timezoneName;
-
- $this->date($dateFormat, $timezone);
-
- return $this;
- });
-
- DatePicker::macro('defaultDateFormat', function (): static {
- $localization = Localization::firstOrFail();
-
- $dateFormat = $localization->date_format->value ?? DateFormat::DEFAULT;
- $timezone = $localization->timezone ?? Carbon::now()->timezoneName;
-
- $this->displayFormat($dateFormat)
- ->timezone($timezone);
-
- return $this;
- });
-
- TextColumn::macro('currency', function (string | Closure | null $currency = null, ?bool $convert = null): static {
- $currency ??= CurrencyAccessor::getDefaultCurrency();
- $convert ??= true;
-
- $this->formatStateUsing(static function (TextColumn $column, $state) use ($currency, $convert): ?string {
- if (blank($state)) {
- return null;
- }
-
- $currency = $column->evaluate($currency);
- $convert = $column->evaluate($convert);
-
- return money($state, $currency, $convert)->format();
- });
-
- return $this;
- });
-
- TextInput::macro('rate', function (string | Closure | null $computation = null): static {
- $this->extraAttributes(['wire:key' => Str::random()])
- ->prefix(static function (TextInput $component) use ($computation) {
- $computation = $component->evaluate($computation);
-
- return ratePrefix(computation: $computation);
- })
- ->suffix(static function (TextInput $component) use ($computation) {
- $computation = $component->evaluate($computation);
-
- return rateSuffix(computation: $computation);
- })
- ->mask(static function (TextInput $component) use ($computation) {
- $computation = $component->evaluate($computation);
-
- return rateMask(computation: $computation);
- })
- ->rule(static function (TextInput $component) use ($computation) {
- return static function (string $attribute, $value, Closure $fail) use ($computation, $component) {
- $computation = $component->evaluate($computation);
- $numericValue = (float) $value;
-
- if ($computation instanceof BackedEnum) {
- $computation = $computation->value;
- }
-
- if ($computation === 'percentage' || $computation === 'compound') {
- if ($numericValue < 0 || $numericValue > 100) {
- $fail(translate('The rate must be between 0 and 100.'));
- }
- } elseif ($computation === 'fixed' && $numericValue < 0) {
- $fail(translate('The rate must be greater than 0.'));
- }
- };
- });
-
- return $this;
- });
-
- Field::macro('validateAccountCode', function (string | Closure | null $subtype = null): static {
- $this
- ->rules([
- fn (Field $component): Closure => static function (string $attribute, $value, Closure $fail) use ($subtype, $component) {
- $subtype = $component->evaluate($subtype);
- $chartSubtype = AccountSubtype::find($subtype);
- $type = $chartSubtype->type;
-
- if (! AccountCode::isValidCode($value, $type)) {
- $message = AccountCode::getMessage($type);
-
- $fail($message);
- }
- },
- ]);
-
- return $this;
- });
-
- TextColumn::macro('rate', function (string | Closure | null $computation = null): static {
- $this->formatStateUsing(static function (TextColumn $column, $state) use ($computation): ?string {
- $computation = $column->evaluate($computation);
-
- return rateFormat(state: $state, computation: $computation);
- });
-
- return $this;
- });
-
- Field::macro('softRequired', function (): static {
- $this
- ->required()
- ->markAsRequired(false);
-
- return $this;
- });
-
- Money::macro('swapAmountFor', function ($newCurrency) {
- $oldCurrency = $this->currency->getCurrency();
- $balanceInSubunits = $this->getAmount();
-
- $oldCurrencySubunit = currency($oldCurrency)->getSubunit();
- $newCurrencySubunit = currency($newCurrency)->getSubunit();
-
- $balanceInMajorUnits = $balanceInSubunits / $oldCurrencySubunit;
-
- $oldRate = currency($oldCurrency)->getRate();
- $newRate = currency($newCurrency)->getRate();
-
- $ratio = $newRate / $oldRate;
- $convertedBalanceInMajorUnits = $balanceInMajorUnits * $ratio;
-
- $roundedConvertedBalanceInMajorUnits = round($convertedBalanceInMajorUnits, currency($newCurrency)->getPrecision());
-
- $convertedBalanceInSubunits = $roundedConvertedBalanceInMajorUnits * $newCurrencySubunit;
-
- return (int) round($convertedBalanceInSubunits);
- });
-
- Money::macro('formatWithCode', function (bool $codeBefore = false) {
- $formatted = $this->format();
-
- $currencyCode = $this->currency->getCurrency();
-
- if ($currencyCode === CurrencyAccessor::getDefaultCurrency()) {
- return $formatted;
- }
-
- if ($codeBefore) {
- return $currencyCode . ' ' . $formatted;
- }
-
- return $formatted . ' ' . $currencyCode;
- });
-
- Currency::macro('getEntity', function () {
- $currencyCode = $this->getCurrency();
-
- $entity = config("money.currencies.{$currencyCode}.entity");
-
- return $entity ?? $currencyCode;
- });
-
- Currency::macro('getCodePrefix', function () {
- if ($this->isSymbolFirst()) {
- return '';
- }
-
- return ' ' . $this->getCurrency();
- });
-
- Currency::macro('getCodeSuffix', function () {
- if ($this->isSymbolFirst()) {
- return ' ' . $this->getCurrency();
- }
-
- return '';
- });
-
- Carbon::macro('toDefaultDateFormat', function () {
- $localization = Localization::firstOrFail();
-
- $dateFormat = $localization->date_format->value ?? DateFormat::DEFAULT;
- $timezone = $localization->timezone ?? Carbon::now()->timezoneName;
-
- return $this->setTimezone($timezone)->format($dateFormat);
- });
- }
- }
|