| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 | <?php
use App\Enums\Setting\NumberFormat;
use App\Models\Setting\Localization;
use Filament\Support\RawJs;
if (! function_exists('generateJsCode')) {
    function generateJsCode(string $precision, ?string $currency = null): string
    {
        $decimal_mark = currency($currency)->getDecimalMark();
        $thousands_separator = currency($currency)->getThousandsSeparator();
        return "\$money(\$input, '" . $decimal_mark . "', '" . $thousands_separator . "', " . $precision . ');';
    }
}
if (! function_exists('generatePercentJsCode')) {
    function generatePercentJsCode(string $format, int $precision): string
    {
        [$decimal_mark, $thousands_separator] = NumberFormat::from($format)->getFormattingParameters();
        return "\$money(\$input, '" . $decimal_mark . "', '" . $thousands_separator . "', " . $precision . ');';
    }
}
if (! function_exists('moneyMask')) {
    function moneyMask(?string $currency = null): RawJs
    {
        $precision = currency($currency)->getPrecision();
        return RawJs::make(generateJsCode($precision, $currency));
    }
}
if (! function_exists('percentMask')) {
    function percentMask(int $precision = 4): RawJs
    {
        $format = Localization::firstOrFail()->number_format->value;
        return RawJs::make(generatePercentJsCode($format, $precision));
    }
}
if (! function_exists('ratePrefix')) {
    function ratePrefix($computation, ?string $currency = null): ?string
    {
        if ($computation instanceof BackedEnum) {
            $computation = $computation->value;
        }
        if ($computation === 'fixed') {
            return currency($currency)->getCodePrefix();
        }
        if ($computation === 'percentage' || $computation === 'compound') {
            $percent_first = Localization::firstOrFail()->percent_first;
            return $percent_first ? '%' : null;
        }
        return null;
    }
}
if (! function_exists('rateSuffix')) {
    function rateSuffix($computation, ?string $currency = null): ?string
    {
        if ($computation instanceof BackedEnum) {
            $computation = $computation->value;
        }
        if ($computation === 'percentage' || $computation === 'compound') {
            $percent_first = Localization::firstOrFail()->percent_first;
            return $percent_first ? null : '%';
        }
        if ($computation === 'fixed') {
            return currency($currency)->getCodeSuffix();
        }
        return null;
    }
}
if (! function_exists('rateMask')) {
    function rateMask($computation, ?string $currency = null): RawJs
    {
        if ($computation instanceof BackedEnum) {
            $computation = $computation->value;
        }
        if ($computation === 'percentage' || $computation === 'compound') {
            return percentMask(4);
        }
        $precision = currency($currency)->getPrecision();
        return RawJs::make(generateJsCode($precision, $currency));
    }
}
if (! function_exists('rateFormat')) {
    function rateFormat($state, $computation, ?string $currency = null): ?string
    {
        if (blank($state)) {
            return null;
        }
        if ($computation instanceof BackedEnum) {
            $computation = $computation->value;
        }
        if ($computation === 'percentage' || $computation === 'compound') {
            $percent_first = Localization::firstOrFail()->percent_first;
            if ($percent_first) {
                return '%' . $state;
            }
            return $state . '%';
        }
        if ($computation === 'fixed') {
            return money($state, $currency, true)->formatWithCode();
        }
        return null;
    }
}
 |