您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TaxResource.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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\Filament\Company\Resources\Setting\TaxResource\RelationManagers;
  8. use App\Models\Setting\Category;
  9. use App\Models\Setting\Tax;
  10. use Closure;
  11. use Filament\Forms;
  12. use Filament\Forms\Form;
  13. use Filament\Notifications\Notification;
  14. use Filament\Resources\Resource;
  15. use Filament\Tables;
  16. use Filament\Tables\Table;
  17. use Illuminate\Database\Eloquent\Builder;
  18. use Illuminate\Database\Eloquent\Collection;
  19. use Illuminate\Database\Eloquent\SoftDeletingScope;
  20. use Wallo\FilamentSelectify\Components\ToggleButton;
  21. class TaxResource extends Resource
  22. {
  23. protected static ?string $model = Tax::class;
  24. protected static ?string $navigationIcon = 'heroicon-o-receipt-percent';
  25. protected static ?string $navigationGroup = 'Settings';
  26. protected static ?string $slug = 'settings/taxes';
  27. public static function form(Form $form): Form
  28. {
  29. return $form
  30. ->schema([
  31. Forms\Components\Section::make('General')
  32. ->schema([
  33. Forms\Components\TextInput::make('name')
  34. ->label('Name')
  35. ->autofocus()
  36. ->required()
  37. ->maxLength(255)
  38. ->rule(static function (Forms\Get $get, Forms\Components\Component $component): Closure {
  39. return static function (string $attribute, $value, Closure $fail) use ($get, $component) {
  40. $existingCategory = Tax::where('company_id', auth()->user()->currentCompany->id)
  41. ->where('name', $value)
  42. ->where('type', $get('type'))
  43. ->first();
  44. if ($existingCategory && $existingCategory->getKey() !== $component->getRecord()?->getKey()) {
  45. $type = $get('type')->getLabel();
  46. $fail("The {$type} tax \"{$value}\" already exists.");
  47. }
  48. };
  49. }),
  50. Forms\Components\TextInput::make('description')
  51. ->label('Description'),
  52. Forms\Components\Select::make('computation')
  53. ->label('Computation')
  54. ->options(TaxComputation::class)
  55. ->default(TaxComputation::Percentage)
  56. ->live()
  57. ->native(false)
  58. ->required(),
  59. Forms\Components\TextInput::make('rate')
  60. ->label('Rate')
  61. ->numeric()
  62. ->suffix(static function (Forms\Get $get) {
  63. $computation = $get('computation');
  64. if ($computation === TaxComputation::Percentage) {
  65. return '%';
  66. }
  67. return null;
  68. })
  69. ->required(),
  70. Forms\Components\Select::make('type')
  71. ->label('Type')
  72. ->options(TaxType::class)
  73. ->default(TaxType::Sales)
  74. ->native(false)
  75. ->required(),
  76. Forms\Components\Select::make('scope')
  77. ->label('Scope')
  78. ->options(TaxScope::class)
  79. ->native(false),
  80. ToggleButton::make('enabled')
  81. ->label('Enabled'),
  82. ])->columns(),
  83. ]);
  84. }
  85. public static function table(Table $table): Table
  86. {
  87. return $table
  88. ->columns([
  89. Tables\Columns\TextColumn::make('name')
  90. ->label('Name')
  91. ->weight('semibold')
  92. ->icon(static fn (Tax $record) => $record->enabled ? 'heroicon-o-lock-closed' : null)
  93. ->tooltip(static fn (Tax $record) => $record->enabled ? "Default {$record->type->getLabel()} Tax" : null)
  94. ->iconPosition('after')
  95. ->searchable()
  96. ->sortable(),
  97. Tables\Columns\TextColumn::make('computation')
  98. ->label('Computation')
  99. ->searchable()
  100. ->sortable(),
  101. Tables\Columns\TextColumn::make('rate')
  102. ->label('Rate')
  103. ->formatStateUsing(static fn (Tax $record) => $record->rate . ($record->computation === TaxComputation::Percentage ? '%' : null))
  104. ->searchable()
  105. ->sortable(),
  106. Tables\Columns\TextColumn::make('type')
  107. ->label('Type')
  108. ->badge()
  109. ->searchable()
  110. ->sortable(),
  111. ])
  112. ->filters([
  113. //
  114. ])
  115. ->actions([
  116. Tables\Actions\EditAction::make(),
  117. Tables\Actions\DeleteAction::make()
  118. ->before(static function (Tables\Actions\DeleteAction $action, Tax $record) {
  119. if ($record->enabled) {
  120. Notification::make()
  121. ->danger()
  122. ->title('Action Denied')
  123. ->body(__('The :name tax is currently set as your default :type tax and cannot be deleted. Please set a different tax as your default before attempting to delete this one.', ['name' => $record->name, 'type' => $record->type->getLabel()]))
  124. ->persistent()
  125. ->send();
  126. $action->cancel();
  127. }
  128. }),
  129. ])
  130. ->bulkActions([
  131. Tables\Actions\BulkActionGroup::make([
  132. Tables\Actions\DeleteBulkAction::make()
  133. ->before(static function (Collection $records, Tables\Actions\DeleteBulkAction $action) {
  134. $defaultTaxes = $records->filter(static function (Tax $record) {
  135. return $record->enabled;
  136. });
  137. if ($defaultTaxes->isNotEmpty()) {
  138. $defaultTaxNames = $defaultTaxes->pluck('name')->toArray();
  139. Notification::make()
  140. ->danger()
  141. ->title('Action Denied')
  142. ->body(static function () use ($defaultTaxNames) {
  143. $message = __('The following taxes are currently set as your default and cannot be deleted. Please set a different tax as your default before attempting to delete these ones.') . "<br><br>";
  144. $message .= implode("<br>", array_map(static function ($name) {
  145. return "&bull; " . $name;
  146. }, $defaultTaxNames));
  147. return $message;
  148. })
  149. ->persistent()
  150. ->send();
  151. $action->cancel();
  152. }
  153. }),
  154. ]),
  155. ])
  156. ->emptyStateActions([
  157. Tables\Actions\CreateAction::make(),
  158. ]);
  159. }
  160. public static function getRelations(): array
  161. {
  162. return [
  163. //
  164. ];
  165. }
  166. public static function getPages(): array
  167. {
  168. return [
  169. 'index' => Pages\ListTaxes::route('/'),
  170. 'create' => Pages\CreateTax::route('/create'),
  171. 'edit' => Pages\EditTax::route('/{record}/edit'),
  172. ];
  173. }
  174. }