pelmered / filament-money-field

Money field powered by Money PHP
MIT License
40 stars 19 forks source link

Filament Money Field

Money field powered by Money PHP.

This package gives much better localization support for money fields in Filament than most other packages, and especially the built-in money support on TextColumns and TextEntries. For example when it comes to currency symbols and decimal and thousands separators. Especially for more obscure currencies. This also includes an input field that handles localized formats.

Example of a money field with Swedish localization. This package would give "1 234,56 kr", while most other solutions probably would give you something like "SEK 1234.56" which is not the correct format for Sweden.

Latest Stable Version Total Downloads Monthly Downloads License

Tests Build Status Scrutinizer Code Quality Code Coverage

Tested on PHP 8.2 to 8.3 Tested on OS:es Linux, MacOS, Windows

Requirements

Key features

Installation

composer require pelmered/filament-money-field

Configure your locale

Set the default currency and locale

Set the default options for currency and locale so that you don't have to set them for every field.

Option 1 (Recommended): Put the default options in your .env file.

MONEY_DEFAULT_LOCALE=sv_SE
MONEY_DEFAULT_CURRENCY=SEK

Option 2: Publish the config file and set the default options there.

php artisan vendor:publish --provider="Pelmered\FilamentMoneyField\FilamentMoneyFieldServiceProvider" --tag="config"

Decimals and significant digits

The number of decimals and significant digits can be set in the config file. Defaults to 2.

//with input 123456
MONEY_DECIMAL_DIGITS=0 // Gives 0 decimals, e.g. $1,235
MONEY_DECIMAL_DIGITS=2 // Gives 2 decimals, e.g. $1,234.56

For significant digits, use negative values. For example -2 will give you 2 significant digits.

//with input 12345678
MONEY_DECIMAL_DIGITS=-2 // Gives 2 significant digits, e.g. $120,000
MONEY_DECIMAL_DIGITS=-4 // Gives 4 significant digits, e.g. $123,400

This can also be set on a per-field basis.

MoneyInput::make('price')->decimalDigits(0);
MoneyEntry::make('price')->decimalDigits(2);
MoneyColumn::make('price')->decimalDigits(-2);

Usage

Form

use Filament\Forms\Components\MoneyField;

MoneyInput::make('price'); // Defaults to USD and the current Laravel locale, or what you have set in your .env/config.

MoneyInput::make('price')
    ->currency('USD')
    ->locale('en_US'),

MoneyInput::make('price')
    ->currency('SEK')
    ->locale('sv_SE'),

MoneyInput::make('price')
    ->currency('SEK')
    ->locale('sv_SE')
    ->minValue(0) // Do not allow negative values.
    ->maxValue(10000) // Add min and max value (in minor units, i.e. cents) to the input field. In this case no values over 100
    ->step(100) // Step value for the input field. In this case only multiples of 100 are allowed.
    ->decimals(0)
    ->getSymbolPlacement('after'), // Possible options: 'after', 'before', 'none'. Defaults to 'before'

Table column

use Pelmered\FilamentMoneyField\Tables\Columns\MoneyColumn;

MoneyColumn::make('price'); // Defaults to USD and the current Laravel locale, or what you have set in your .env/config.

MoneyColumn::make('price')
    ->currency('USD')
    ->locale('en_US'),

MoneyColumn::make('price')
    ->currency('SEK')
    ->locale('sv_SE'),

MoneyColumn::make('price')
    ->short(), // Short fromat, e.g. $1.23M instead of $1,234,567.89

InfoList

use Pelmered\FilamentMoneyField\Infolists\Components\MoneyEntry;

MoneyEntry::make('price'); // Defaults to USD and the current Laravel locale, or what you have set in your .env/config.

// The default can be set in the Infolist class with:
public static string $defaultCurrency = 'SEK';

MoneyEntry::make('price')
    ->currency('USD')
    ->locale('en_US'),

MoneyEntry::make('price')
    ->currency('SEK')
    ->locale('sv_SE'),

MoneyEntry::make('price')
    ->short(), // Short fromat, e.g. $1.23M instead of $1,234,567.89

Global Configuration

If you want to use the formatting mask on the MoneyInput component

This will auto format the input field as you type.

This is a bit experimental at the moment and is therefore disabled by default. Hopefully it will be improved in the future and enabled by default in the next major version. Please try it out and provide feedback.

MONEY_USE_INPUT_MASK=true // Defaults to false

Use international currency codes (ISO 4217)

If you want to use international currency codes istead of their symbols or local short variants. For example USD instead of $, EUR instead of € or SEK instead of kr.

MONEY_INTL_CURRENCY_SYMBOL=true // Defaults to false

Placement of currency symbol/code on input fields

Possible options: after, before, none.

MONEY_UNIT_PLACEMENT=after // Defaults to before

Decimals and significant digits

The number of decimals and significant digits can be set in the config file. Defaults to 2.

//with input 123456
MONEY_DECIMAL_DIGITS=0 // Gives 0 decimals, e.g. $1,235
MONEY_DECIMAL_DIGITS=2 // Gives 2 decimals, e.g. $1,234.56

For significant digits, use negative values. For example -2 will give you 2 significant digits.

//with input 12345678
MONEY_DECIMAL_DIGITS=-2 // Gives 2 significant digits, e.g. $120,000
MONEY_DECIMAL_DIGITS=-4 // Gives 4 significant digits, e.g. $123,400

This can also be set on a per-field basis.

MoneyInput::make('price')->decimalDigits(0);
MoneyEntry::make('price')->decimalDigits(2);
MoneyColumn::make('price')->decimalDigits(-2);

// You can also pass a callback to the decimalDigits method.
MoneyInput::make('price')->decimalDigits(function () {
    return 0;
});

Roadmap / Ideas for the future

Contact me or create an issue if you want something of this, or something else. I appreciate if you could tell me a bit about your use case for that feature as well.

Contributing

I'm very happy to receive PRs with fixes or improvements. If it is a new feature, it is probably best to open an issue first, so I can give feedback and see if that is something I think would fit in this package. Especially if it is a larger feature, so you don't waste your time.

When you are submitting a PR, I appreciate if you: