Skip to content

Commit

Permalink
Merge pull request #23 from abishekrsrikaanth/add-summarizer-macro
Browse files Browse the repository at this point in the history
Adding new macro's to Filament Table Summarizer
  • Loading branch information
ariaieboy authored May 14, 2024
2 parents 87f2362 + dda37ed commit c6e51f5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 15 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Filament V3 unlike V2 that uses [laravel-money](https://github.com/akaunting/lar

This package will add a new `currency(string | Closure $currency = null, bool $shouldConvert = false)` method to the `TextColumn` that uses the Filament V2 money formatter.

Additionally, you can use the `currency(string | Closure $currency = null, bool $shouldConvert = false)` method when adding Summarizers to the table. Currently this method only exists for the `Sum` and `Average` Summarizer.

By using this package you can configure the formatter using [laravel-money config](https://github.com/akaunting/laravel-money/blob/master/config/money.php).

For example, you can customize the `symbol`, `symbol_first`, `decimal_mark`, and `thousands_separator` for each currency. Or if you want you can add your custom currency to the config and use it in the `currency()` method instead of standard money.
Expand All @@ -33,6 +35,14 @@ php artisan vendor:publish --tag=money
\Filament\Tables\Columns\TextColumn::make('money')
->currency('USD');

\Filament\Tables\Columns\TextColumn::make('money')
->currency('USD')
->summarize(\Filament\Tables\Columns\Summarizers\Sum::make()->currency());

\Filament\Tables\Columns\TextColumn::make('money')
->currency('USD')
->summarize(\Filament\Tables\Columns\Summarizers\Average::make()->currency());

\Filament\Forms\Components\TextInput::make('money')
->currencyMask(thousandSeparator: ',',decimalSeparator: '.',precision: 2)
```
Expand Down
19 changes: 19 additions & 0 deletions ide-helper.stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,22 @@ public function currencyMask($thousandSeparator = ',', $decimalSeparator = '.',
}
}
}
namespace Filament\Tables\Columns\Summarizers {

use Closure;

class Average
{
public function currency(string | Closure | null $currency = null, bool $shouldConvert = false): self
{
return $this;
}
}
class Sum
{
public function currency(string | Closure | null $currency = null, bool $shouldConvert = false): self
{
return $this;
}
}
}
58 changes: 43 additions & 15 deletions src/FilamentCurrencyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Filament\Tables\Columns\TextColumn;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Filament\Tables\Columns\Summarizers;

class FilamentCurrencyServiceProvider extends PackageServiceProvider
{
Expand All @@ -25,24 +26,29 @@ public function configurePackage(Package $package): void

public function bootingPackage(): void
{
TextColumn::macro('currency', function (string | Closure | null $currency = null, bool $shouldConvert = false): TextColumn {
$formatter = static function($state, $evaluator, $currency, $shouldConvert) {
if (blank($state)) {
return null;
}

if (blank($currency)) {
$currency = config('filament-currency.default_currency');
}

return (new Money\Money(
$state,
(new Money\Currency(strtoupper($evaluator->evaluate($currency)))),
$shouldConvert,
))->format();
};
TextColumn::macro('currency', function (string | Closure | null $currency = null, bool $shouldConvert = false) use($formatter): TextColumn {
/**
* @var TextColumn $this
*/
$this->formatStateUsing(static function (Column $column, $state) use ($currency, $shouldConvert): ?string {
if (blank($state)) {
return null;
}

if (blank($currency)) {
$currency = config('filament-currency.default_currency');
}

return (new Money\Money(
$state,
(new Money\Currency(strtoupper($column->evaluate($currency)))),
$shouldConvert,
))->format();
$this->formatStateUsing(static function (Column $column, $state) use ($currency, $shouldConvert, $formatter): ?string {

return $formatter($state, $column, $currency, $shouldConvert);

});

return $this;
Expand All @@ -53,5 +59,27 @@ public function bootingPackage(): void

return $this;
});

Summarizers\Sum::macro('currency', function (string | Closure | null $currency = null, bool $shouldConvert = false) use($formatter): Summarizers\Sum {

$this->formatStateUsing(static function (Summarizers\Summarizer $summarizer, $state) use ($currency, $shouldConvert, $formatter): ?string {

return $formatter($state, $summarizer, $currency, $shouldConvert);

});

return $this;
});

Summarizers\Average::macro('currency', function (string | Closure | null $currency = null, bool $shouldConvert = false) use($formatter): Summarizers\Average {

$this->formatStateUsing(static function (Summarizers\Summarizer $summarizer, $state) use ($currency, $shouldConvert, $formatter): ?string {

return $formatter($state, $summarizer, $currency, $shouldConvert);

});

return $this;
});
}
}

0 comments on commit c6e51f5

Please sign in to comment.