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

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