Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

CurrencyResource.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Filament\Company\Clusters\Settings\Resources;
  3. use App\Facades\Forex;
  4. use App\Filament\Company\Clusters\Settings;
  5. use App\Filament\Company\Clusters\Settings\Resources\CurrencyResource\Pages;
  6. use App\Models\Setting\Currency as CurrencyModel;
  7. use App\Utilities\Currency\CurrencyAccessor;
  8. use Filament\Forms;
  9. use Filament\Forms\Form;
  10. use Filament\Resources\Resource;
  11. use Filament\Support\Enums\FontWeight;
  12. use Filament\Tables;
  13. use Filament\Tables\Table;
  14. class CurrencyResource extends Resource
  15. {
  16. protected static ?string $model = CurrencyModel::class;
  17. protected static ?string $modelLabel = 'currency';
  18. protected static ?string $cluster = Settings::class;
  19. public static function getModelLabel(): string
  20. {
  21. $modelLabel = static::$modelLabel;
  22. return translate($modelLabel);
  23. }
  24. public static function form(Form $form): Form
  25. {
  26. return $form
  27. ->schema([
  28. Forms\Components\Section::make('General')
  29. ->schema([
  30. Forms\Components\Select::make('code')
  31. ->options(CurrencyAccessor::getAvailableCurrencies())
  32. ->searchable()
  33. ->live()
  34. ->required()
  35. ->localizeLabel()
  36. ->disabledOn('edit')
  37. ->afterStateUpdated(static function (Forms\Set $set, $state) {
  38. if (! $state) {
  39. return;
  40. }
  41. $defaultCurrencyCode = CurrencyAccessor::getDefaultCurrency();
  42. $exchangeRate = Forex::getCachedExchangeRate($defaultCurrencyCode, $state);
  43. if ($exchangeRate !== null) {
  44. $set('rate', $exchangeRate);
  45. }
  46. }),
  47. Forms\Components\TextInput::make('rate')
  48. ->numeric()
  49. ->rule('gt:0')
  50. ->live()
  51. ->localizeLabel()
  52. ->required(),
  53. ])->columns(),
  54. ]);
  55. }
  56. public static function table(Table $table): Table
  57. {
  58. return $table
  59. ->columns([
  60. Tables\Columns\TextColumn::make('name')
  61. ->localizeLabel()
  62. ->weight(FontWeight::Medium)
  63. ->icon(static fn (CurrencyModel $record) => $record->isEnabled() ? 'heroicon-o-lock-closed' : null)
  64. ->tooltip(static function (CurrencyModel $record) {
  65. $tooltipMessage = translate('Default :record', [
  66. 'record' => static::getModelLabel(),
  67. ]);
  68. return $record->isEnabled() ? $tooltipMessage : null;
  69. })
  70. ->iconPosition('after')
  71. ->searchable()
  72. ->sortable(),
  73. Tables\Columns\TextColumn::make('code')
  74. ->localizeLabel()
  75. ->searchable()
  76. ->sortable(),
  77. Tables\Columns\TextColumn::make('symbol')
  78. ->localizeLabel()
  79. ->searchable()
  80. ->sortable(),
  81. Tables\Columns\TextColumn::make('rate')
  82. ->localizeLabel()
  83. ->searchable()
  84. ->sortable(),
  85. ])
  86. ->filters([
  87. //
  88. ])
  89. ->actions([
  90. Tables\Actions\EditAction::make(),
  91. ])
  92. ->bulkActions([
  93. //
  94. ]);
  95. }
  96. public static function getPages(): array
  97. {
  98. return [
  99. 'index' => Pages\ListCurrencies::route('/'),
  100. 'create' => Pages\CreateCurrency::route('/create'),
  101. 'edit' => Pages\EditCurrency::route('/{record}/edit'),
  102. ];
  103. }
  104. }