You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TaxResource.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace App\Filament\Company\Clusters\Settings\Resources;
  3. use App\Concerns\NotifiesOnDelete;
  4. use App\Enums\Setting\TaxComputation;
  5. use App\Enums\Setting\TaxScope;
  6. use App\Enums\Setting\TaxType;
  7. use App\Filament\Company\Clusters\Settings;
  8. use App\Filament\Company\Clusters\Settings\Resources\TaxResource\Pages;
  9. use App\Models\Setting\Tax;
  10. use Closure;
  11. use Filament\Forms;
  12. use Filament\Forms\Form;
  13. use Filament\Resources\Resource;
  14. use Filament\Support\Enums\FontWeight;
  15. use Filament\Tables;
  16. use Filament\Tables\Table;
  17. use Wallo\FilamentSelectify\Components\ToggleButton;
  18. class TaxResource extends Resource
  19. {
  20. use NotifiesOnDelete;
  21. protected static ?string $model = Tax::class;
  22. protected static ?string $modelLabel = 'Tax';
  23. protected static ?string $cluster = Settings::class;
  24. public static function getModelLabel(): string
  25. {
  26. $modelLabel = static::$modelLabel;
  27. return translate($modelLabel);
  28. }
  29. public static function form(Form $form): Form
  30. {
  31. return $form
  32. ->schema([
  33. Forms\Components\Section::make('General')
  34. ->schema([
  35. Forms\Components\TextInput::make('name')
  36. ->autofocus()
  37. ->required()
  38. ->localizeLabel()
  39. ->maxLength(255)
  40. ->rule(static function (Forms\Get $get, Forms\Components\Component $component): Closure {
  41. return static function (string $attribute, $value, Closure $fail) use ($component, $get) {
  42. $existingTax = Tax::where('name', $value)
  43. ->whereKeyNot($component->getRecord()?->getKey())
  44. ->where('type', $get('type'))
  45. ->first();
  46. if ($existingTax) {
  47. $message = translate('The :Type :record ":name" already exists.', [
  48. 'Type' => $existingTax->type->getLabel(),
  49. 'record' => strtolower(static::getModelLabel()),
  50. 'name' => $value,
  51. ]);
  52. $fail($message);
  53. }
  54. };
  55. }),
  56. Forms\Components\TextInput::make('description'),
  57. Forms\Components\Select::make('computation')
  58. ->localizeLabel()
  59. ->options(TaxComputation::class)
  60. ->default(TaxComputation::Percentage)
  61. ->live()
  62. ->required(),
  63. Forms\Components\TextInput::make('rate')
  64. ->localizeLabel()
  65. ->rate(static fn (Forms\Get $get) => $get('computation'))
  66. ->required(),
  67. Forms\Components\Select::make('type')
  68. ->localizeLabel()
  69. ->options(TaxType::class)
  70. ->default(TaxType::Sales)
  71. ->required(),
  72. Forms\Components\Select::make('scope')
  73. ->localizeLabel()
  74. ->options(TaxScope::class),
  75. ToggleButton::make('enabled')
  76. ->localizeLabel('Default')
  77. ->onLabel(Tax::enabledLabel())
  78. ->offLabel(Tax::disabledLabel()),
  79. ])->columns(),
  80. ]);
  81. }
  82. public static function table(Table $table): Table
  83. {
  84. return $table
  85. ->columns([
  86. Tables\Columns\TextColumn::make('name')
  87. ->localizeLabel()
  88. ->weight(FontWeight::Medium)
  89. ->icon(static fn (Tax $record) => $record->isEnabled() ? 'heroicon-o-lock-closed' : null)
  90. ->tooltip(static function (Tax $record) {
  91. if ($record->isDisabled()) {
  92. return null;
  93. }
  94. return translate('Default :Type :Record', [
  95. 'Type' => $record->type->getLabel(),
  96. 'Record' => static::getModelLabel(),
  97. ]);
  98. })
  99. ->iconPosition('after')
  100. ->searchable()
  101. ->sortable(),
  102. Tables\Columns\TextColumn::make('computation')
  103. ->localizeLabel()
  104. ->searchable()
  105. ->sortable(),
  106. Tables\Columns\TextColumn::make('rate')
  107. ->localizeLabel()
  108. ->rate(static fn (Tax $record) => $record->computation->value)
  109. ->searchable()
  110. ->sortable(),
  111. Tables\Columns\TextColumn::make('type')
  112. ->localizeLabel()
  113. ->badge()
  114. ->searchable()
  115. ->sortable(),
  116. ])
  117. ->filters([
  118. //
  119. ])
  120. ->actions([
  121. Tables\Actions\EditAction::make(),
  122. Tables\Actions\DeleteAction::make(),
  123. ])
  124. ->bulkActions([
  125. Tables\Actions\BulkActionGroup::make([
  126. Tables\Actions\DeleteBulkAction::make(),
  127. ]),
  128. ])
  129. ->checkIfRecordIsSelectableUsing(static function (Tax $record) {
  130. return $record->isDisabled();
  131. })
  132. ->emptyStateActions([
  133. Tables\Actions\CreateAction::make(),
  134. ]);
  135. }
  136. public static function getPages(): array
  137. {
  138. return [
  139. 'index' => Pages\ListTaxes::route('/'),
  140. 'create' => Pages\CreateTax::route('/create'),
  141. 'edit' => Pages\EditTax::route('/{record}/edit'),
  142. ];
  143. }
  144. }