Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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