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 8.1KB

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