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

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