You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CategoryResource.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace App\Filament\Company\Resources\Setting;
  3. use App\Enums\Accounting\AccountCategory;
  4. use App\Enums\CategoryType;
  5. use App\Filament\Company\Resources\Setting\CategoryResource\Pages;
  6. use App\Models\Accounting\Account;
  7. use App\Models\Setting\Category;
  8. use BackedEnum;
  9. use Closure;
  10. use Exception;
  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 CategoryResource extends Resource
  20. {
  21. protected static ?string $model = Category::class;
  22. protected static ?string $modelLabel = 'Category';
  23. protected static ?string $navigationIcon = 'heroicon-o-folder';
  24. protected static ?string $navigationGroup = 'Settings';
  25. protected static ?string $slug = 'settings/categories';
  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. ->columns(1)
  42. ->schema([
  43. Forms\Components\TextInput::make('name')
  44. ->localizeLabel()
  45. ->required()
  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. $existingCategory = Category::where('company_id', auth()->user()->currentCompany->id)
  50. ->where('name', $value)
  51. ->where('type', $get('type'))
  52. ->first();
  53. if ($existingCategory && $existingCategory->getKey() !== $component->getRecord()?->getKey()) {
  54. $message = translate('The :Type :record ":name" already exists.', [
  55. 'Type' => $existingCategory->type->getLabel(),
  56. 'record' => strtolower(static::getModelLabel()),
  57. 'name' => $value,
  58. ]);
  59. $fail($message);
  60. }
  61. };
  62. }),
  63. Forms\Components\Select::make('type')
  64. ->localizeLabel()
  65. ->options(CategoryType::class)
  66. ->live()
  67. ->required()
  68. ->afterStateUpdated(static function (Forms\Set $set, $state, ?Category $record, string $operation) {
  69. $accountOptions = static::getAccountOptions($state);
  70. if ($operation === 'create') {
  71. $set('account_id', null);
  72. } elseif ($operation === 'edit' && $record !== null) {
  73. if (! array_key_exists($record->account_id, $accountOptions)) {
  74. $set('account_id', null);
  75. } else {
  76. $set('account_id', $record->account_id);
  77. }
  78. }
  79. }),
  80. Forms\Components\Select::make('account_id')
  81. ->label('Account')
  82. ->searchable()
  83. ->preload()
  84. ->options(static function (Forms\Get $get) {
  85. return static::getAccountOptions($get('type'));
  86. }),
  87. Forms\Components\ColorPicker::make('color')
  88. ->localizeLabel()
  89. ->required(),
  90. ToggleButton::make('enabled')
  91. ->localizeLabel('Default')
  92. ->onLabel(Category::enabledLabel())
  93. ->offLabel(Category::disabledLabel()),
  94. ]);
  95. }
  96. /**
  97. * @throws Exception
  98. */
  99. public static function table(Table $table): Table
  100. {
  101. return $table
  102. ->columns([
  103. Tables\Columns\TextColumn::make('name')
  104. ->localizeLabel()
  105. ->weight(FontWeight::Medium)
  106. ->icon(static fn (Category $record) => $record->isEnabled() ? 'heroicon-o-lock-closed' : null)
  107. ->tooltip(static function (Category $record) {
  108. $tooltipMessage = translate('Default :Type :Record', [
  109. 'Type' => $record->type->getLabel(),
  110. 'Record' => static::getModelLabel(),
  111. ]);
  112. return $record->isEnabled() ? $tooltipMessage : null;
  113. })
  114. ->iconPosition('after')
  115. ->searchable()
  116. ->sortable(),
  117. Tables\Columns\TextColumn::make('type')
  118. ->localizeLabel()
  119. ->sortable()
  120. ->searchable(),
  121. Tables\Columns\ColorColumn::make('color')
  122. ->localizeLabel()
  123. ->copyable(),
  124. ])
  125. ->filters([
  126. Tables\Filters\SelectFilter::make('type')
  127. ->label('Type')
  128. ->multiple()
  129. ->searchable()
  130. ->options(CategoryType::class),
  131. ])
  132. ->actions([
  133. Tables\Actions\EditAction::make(),
  134. Tables\Actions\DeleteAction::make(),
  135. ])
  136. ->bulkActions([
  137. Tables\Actions\BulkActionGroup::make([
  138. Tables\Actions\DeleteBulkAction::make(),
  139. ]),
  140. ])
  141. ->checkIfRecordIsSelectableUsing(static function (Category $record) {
  142. return $record->isDisabled();
  143. });
  144. }
  145. public static function getPages(): array
  146. {
  147. return [
  148. 'index' => Pages\ManageCategory::route('/'),
  149. ];
  150. }
  151. public static function getAccountOptions($typeValue): array
  152. {
  153. $typeString = $typeValue instanceof BackedEnum ? $typeValue->value : $typeValue;
  154. $accountCategory = match ($typeString) {
  155. CategoryType::Income->value => AccountCategory::Revenue,
  156. CategoryType::Expense->value => AccountCategory::Expense,
  157. default => null,
  158. };
  159. if ($accountCategory) {
  160. $accounts = Account::where('category', $accountCategory)->get();
  161. } else {
  162. $accounts = Account::all();
  163. }
  164. return $accounts->pluck('name', 'id')->toArray();
  165. }
  166. }