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

AccountResource.php 8.0KB

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