Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

TaxResource.php 6.4KB

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