Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AccountResource.php 7.4KB

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