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.2KB

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