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.

AccountResource.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace App\Filament\Company\Resources\Banking;
  3. use App\Enums\Accounting\AccountCategory;
  4. use App\Enums\Banking\BankAccountType;
  5. use App\Filament\Company\Resources\Banking\AccountResource\Pages;
  6. use App\Filament\Forms\Components\CreateCurrencySelect;
  7. use App\Models\Accounting\AccountSubtype;
  8. use App\Models\Banking\BankAccount;
  9. use Filament\Forms;
  10. use Filament\Forms\Form;
  11. use Filament\Resources\Resource;
  12. use Filament\Support\Enums\Alignment;
  13. use Filament\Support\Enums\FontWeight;
  14. use Filament\Tables;
  15. use Filament\Tables\Table;
  16. use Illuminate\Support\Collection;
  17. use Illuminate\Support\Facades\Auth;
  18. use Illuminate\Validation\Rules\Unique;
  19. use Wallo\FilamentSelectify\Components\ToggleButton;
  20. class AccountResource extends Resource
  21. {
  22. protected static ?string $model = BankAccount::class;
  23. protected static ?string $modelLabel = 'Account';
  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('Account Information')
  34. ->schema([
  35. Forms\Components\Select::make('type')
  36. ->options(BankAccountType::class)
  37. ->localizeLabel()
  38. ->searchable()
  39. ->columnSpan(1)
  40. ->default(BankAccountType::DEFAULT)
  41. ->live()
  42. ->afterStateUpdated(static function (Forms\Set $set, $state, ?BankAccount $bankAccount, string $operation) {
  43. if ($operation === 'create') {
  44. $set('account.subtype_id', null);
  45. } elseif ($operation === 'edit' && $bankAccount !== null) {
  46. if ($state !== $bankAccount->type->value) {
  47. $set('account.subtype_id', null);
  48. } else {
  49. $set('account.subtype_id', $bankAccount->account->subtype_id);
  50. }
  51. }
  52. })
  53. ->required(),
  54. Forms\Components\Group::make()
  55. ->columnStart(2)
  56. ->relationship('account')
  57. ->schema([
  58. Forms\Components\Select::make('subtype_id')
  59. ->options(static fn (Forms\Get $get) => static::groupSubtypesBySubtypeType(BankAccountType::parse($get('data.type', true))))
  60. ->localizeLabel()
  61. ->searchable()
  62. ->live()
  63. ->required(),
  64. ]),
  65. Forms\Components\Group::make()
  66. ->relationship('account')
  67. ->columns()
  68. ->columnSpanFull()
  69. ->schema([
  70. Forms\Components\TextInput::make('name')
  71. ->maxLength(100)
  72. ->localizeLabel()
  73. ->required(),
  74. CreateCurrencySelect::make('currency_code')
  75. ->relationship('currency', 'name'),
  76. ]),
  77. Forms\Components\Group::make()
  78. ->columns()
  79. ->columnSpanFull()
  80. ->schema([
  81. Forms\Components\TextInput::make('number')
  82. ->localizeLabel('Account Number')
  83. ->unique(ignoreRecord: true, modifyRuleUsing: static function (Unique $rule, $state) {
  84. $companyId = Auth::user()->currentCompany->id;
  85. return $rule->where('company_id', $companyId)->where('number', $state);
  86. })
  87. ->maxLength(20)
  88. ->validationAttribute('account number'),
  89. ToggleButton::make('enabled')
  90. ->localizeLabel('Default'),
  91. ]),
  92. ])->columns(),
  93. ]);
  94. }
  95. public static function table(Table $table): Table
  96. {
  97. return $table
  98. ->columns([
  99. Tables\Columns\TextColumn::make('account.name')
  100. ->localizeLabel('Account')
  101. ->searchable()
  102. ->weight(FontWeight::Medium)
  103. ->icon(static fn (BankAccount $record) => $record->isEnabled() ? 'heroicon-o-lock-closed' : null)
  104. ->tooltip(static fn (BankAccount $record) => $record->isEnabled() ? 'Default Account' : null)
  105. ->iconPosition('after')
  106. ->description(static fn (BankAccount $record) => $record->mask ?? null),
  107. Tables\Columns\TextColumn::make('account.subtype.name')
  108. ->localizeLabel('Subtype')
  109. ->sortable()
  110. ->toggleable(),
  111. Tables\Columns\TextColumn::make('account.ending_balance')
  112. ->localizeLabel('Ending Balance')
  113. ->state(static fn (BankAccount $record) => $record->account->ending_balance->convert()->formatWithCode())
  114. ->toggleable()
  115. ->alignment(Alignment::End),
  116. ])
  117. ->filters([
  118. //
  119. ])
  120. ->actions([
  121. Tables\Actions\EditAction::make(),
  122. ])
  123. ->bulkActions([
  124. Tables\Actions\BulkActionGroup::make([
  125. Tables\Actions\DeleteBulkAction::make()
  126. ->requiresConfirmation()
  127. ->modalDescription('Are you sure you want to delete the selected accounts? All transactions associated with the accounts will be deleted as well.'),
  128. ]),
  129. ])
  130. ->checkIfRecordIsSelectableUsing(static function (BankAccount $record) {
  131. return $record->isDisabled();
  132. })
  133. ->emptyStateActions([
  134. Tables\Actions\CreateAction::make(),
  135. ]);
  136. }
  137. public static function getPages(): array
  138. {
  139. return [
  140. 'index' => Pages\ListAccounts::route('/'),
  141. 'create' => Pages\CreateAccount::route('/create'),
  142. 'edit' => Pages\EditAccount::route('/{record}/edit'),
  143. ];
  144. }
  145. public static function groupSubtypesBySubtypeType(BankAccountType $bankAccountType): array
  146. {
  147. $category = match ($bankAccountType) {
  148. BankAccountType::Depository, BankAccountType::Investment => AccountCategory::Asset,
  149. BankAccountType::Credit, BankAccountType::Loan => AccountCategory::Liability,
  150. default => null,
  151. };
  152. if ($category === null) {
  153. return [];
  154. }
  155. $subtypes = AccountSubtype::where('category', $category)->get();
  156. return $subtypes->groupBy(fn (AccountSubtype $subtype) => $subtype->type->getLabel())
  157. ->map(fn (Collection $subtypes, string $type) => $subtypes->mapWithKeys(static fn (AccountSubtype $subtype) => [$subtype->id => $subtype->name]))
  158. ->toArray();
  159. }
  160. }