選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

TaxResource.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 ($get, $component) {
  42. $existingTax = Tax::where('company_id', auth()->user()->currentCompany->id)
  43. ->where('name', $value)
  44. ->where('type', $get('type'))
  45. ->first();
  46. if ($existingTax && $existingTax->getKey() !== $component->getRecord()?->getKey()) {
  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. $tooltipMessage = translate('Default :Type :Record', [
  92. 'Type' => $record->type->getLabel(),
  93. 'Record' => static::getModelLabel(),
  94. ]);
  95. return $record->isEnabled() ? $tooltipMessage : null;
  96. })
  97. ->iconPosition('after')
  98. ->searchable()
  99. ->sortable(),
  100. Tables\Columns\TextColumn::make('computation')
  101. ->localizeLabel()
  102. ->searchable()
  103. ->sortable(),
  104. Tables\Columns\TextColumn::make('rate')
  105. ->localizeLabel()
  106. ->rate(static fn (Tax $record) => $record->computation->value)
  107. ->searchable()
  108. ->sortable(),
  109. Tables\Columns\TextColumn::make('type')
  110. ->localizeLabel()
  111. ->badge()
  112. ->searchable()
  113. ->sortable(),
  114. ])
  115. ->filters([
  116. //
  117. ])
  118. ->actions([
  119. Tables\Actions\EditAction::make(),
  120. Tables\Actions\DeleteAction::make(),
  121. ])
  122. ->bulkActions([
  123. Tables\Actions\BulkActionGroup::make([
  124. Tables\Actions\DeleteBulkAction::make(),
  125. ]),
  126. ])
  127. ->checkIfRecordIsSelectableUsing(static function (Tax $record) {
  128. return $record->isDisabled();
  129. })
  130. ->emptyStateActions([
  131. Tables\Actions\CreateAction::make(),
  132. ]);
  133. }
  134. public static function getPages(): array
  135. {
  136. return [
  137. 'index' => Pages\ListTaxes::route('/'),
  138. 'create' => Pages\CreateTax::route('/create'),
  139. 'edit' => Pages\EditTax::route('/{record}/edit'),
  140. ];
  141. }
  142. }