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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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([
  58. 'default' => 1,
  59. 'lg' => 2,
  60. ])
  61. ->relationship('account')
  62. ->schema([
  63. Forms\Components\Select::make('subtype_id')
  64. ->options(static fn (Forms\Get $get) => static::groupSubtypesBySubtypeType(BankAccountType::parse($get('data.type', true))))
  65. ->disabledOn('edit')
  66. ->localizeLabel()
  67. ->searchable()
  68. ->live()
  69. ->required(),
  70. ]),
  71. Forms\Components\Group::make()
  72. ->relationship('account')
  73. ->columns()
  74. ->columnSpanFull()
  75. ->schema([
  76. Forms\Components\TextInput::make('name')
  77. ->maxLength(100)
  78. ->localizeLabel()
  79. ->required(),
  80. CreateCurrencySelect::make('currency_code')
  81. ->disabledOn('edit'),
  82. ]),
  83. Forms\Components\Group::make()
  84. ->columns()
  85. ->columnSpanFull()
  86. ->schema([
  87. Forms\Components\TextInput::make('number')
  88. ->localizeLabel('Account number')
  89. ->unique(ignoreRecord: true, modifyRuleUsing: static function (Unique $rule, $state) {
  90. $companyId = Auth::user()->currentCompany->id;
  91. return $rule->where('company_id', $companyId)->where('number', $state);
  92. })
  93. ->maxLength(20)
  94. ->validationAttribute('account number'),
  95. ToggleButton::make('enabled')
  96. ->localizeLabel('Default'),
  97. ]),
  98. ])->columns(),
  99. ]);
  100. }
  101. public static function table(Table $table): Table
  102. {
  103. return $table
  104. ->modifyQueryUsing(function (Builder $query) {
  105. $query->with([
  106. 'account',
  107. 'account.subtype',
  108. ]);
  109. })
  110. ->columns([
  111. Tables\Columns\TextColumn::make('account.name')
  112. ->localizeLabel('Account')
  113. ->searchable()
  114. ->weight(FontWeight::Medium)
  115. ->icon(static fn (BankAccount $record) => $record->isEnabled() ? 'heroicon-o-lock-closed' : null)
  116. ->tooltip(static fn (BankAccount $record) => $record->isEnabled() ? 'Default Account' : null)
  117. ->iconPosition('after')
  118. ->description(static fn (BankAccount $record) => $record->mask ?? null),
  119. Tables\Columns\TextColumn::make('account.subtype.name')
  120. ->localizeLabel('Subtype')
  121. ->sortable()
  122. ->toggleable(),
  123. Tables\Columns\TextColumn::make('account.ending_balance')
  124. ->localizeLabel('Ending balance')
  125. ->state(static fn (BankAccount $record) => $record->account->ending_balance->convert()->formatWithCode())
  126. ->toggleable()
  127. ->alignment(Alignment::End),
  128. ])
  129. ->filters([
  130. //
  131. ])
  132. ->actions([
  133. Tables\Actions\EditAction::make(),
  134. ])
  135. ->bulkActions([
  136. Tables\Actions\BulkActionGroup::make([
  137. Tables\Actions\DeleteBulkAction::make()
  138. ->requiresConfirmation()
  139. ->modalDescription('Are you sure you want to delete the selected accounts? All transactions associated with the accounts will be deleted as well.')
  140. ->hidden(function (Table $table) {
  141. return $table->getAllSelectableRecordsCount() === 0;
  142. }),
  143. ]),
  144. ])
  145. ->checkIfRecordIsSelectableUsing(static function (BankAccount $record) {
  146. return $record->isDisabled();
  147. });
  148. }
  149. public static function getPages(): array
  150. {
  151. return [
  152. 'index' => Pages\ListAccounts::route('/'),
  153. 'create' => Pages\CreateAccount::route('/create'),
  154. 'edit' => Pages\EditAccount::route('/{record}/edit'),
  155. ];
  156. }
  157. public static function groupSubtypesBySubtypeType(BankAccountType $bankAccountType): array
  158. {
  159. $category = match ($bankAccountType) {
  160. BankAccountType::Depository, BankAccountType::Investment => AccountCategory::Asset,
  161. BankAccountType::Credit, BankAccountType::Loan => AccountCategory::Liability,
  162. default => null,
  163. };
  164. if ($category === null) {
  165. return [];
  166. }
  167. $subtypes = AccountSubtype::where('category', $category)->get();
  168. return $subtypes->groupBy(fn (AccountSubtype $subtype) => $subtype->type->getLabel())
  169. ->map(fn (Collection $subtypes, string $type) => $subtypes->mapWithKeys(static fn (AccountSubtype $subtype) => [$subtype->id => $subtype->name]))
  170. ->toArray();
  171. }
  172. }