| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 | <?php
namespace App\Filament\Resources;
use App\Actions\OptionAction\CreateCurrency;
use App\Filament\Resources\AccountResource\Pages;
use App\Filament\Resources\AccountResource\RelationManagers;
use App\Models\Banking\Account;
use App\Models\Setting\Currency;
use App\Services\CurrencyService;
use Closure;
use Exception;
use Filament\Forms;
use Filament\Forms\Components\TextInput\Mask;
use Filament\Notifications\Notification;
use Filament\Resources\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rules\Unique;
use Wallo\FilamentSelectify\Components\ToggleButton;
class AccountResource extends Resource
{
    protected static ?string $model = Account::class;
    protected static ?string $navigationIcon = 'heroicon-o-credit-card';
    protected static ?string $navigationGroup = 'Banking';
    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Group::make()
                    ->schema([
                        Forms\Components\Section::make('Account Information')
                            ->schema([
                                Forms\Components\Select::make('type')
                                    ->label('Type')
                                    ->options(Account::getAccountTypes())
                                    ->searchable()
                                    ->default('checking')
                                    ->reactive()
                                    ->disablePlaceholderSelection()
                                    ->required(),
                                Forms\Components\TextInput::make('name')
                                    ->label('Name')
                                    ->maxLength(100)
                                    ->required(),
                                Forms\Components\TextInput::make('number')
                                    ->label('Account Number')
                                    ->unique(callback: static function (Unique $rule, $state) {
                                        $companyId = Auth::user()->currentCompany->id;
                                        return $rule->where('company_id', $companyId)->where('number', $state);
                                    }, ignoreRecord: true)
                                    ->maxLength(20)
                                    ->validationAttribute('account number')
                                    ->required(),
                                ToggleButton::make('enabled')
                                    ->label('Default Account')
                                    ->hidden(static fn (Closure $get) => $get('type') === 'credit_card')
                                    ->offColor('danger')
                                    ->onColor('primary'),
                            ])->columns(),
                        Forms\Components\Section::make('Currency & Balance')
                            ->schema([
                                Forms\Components\Select::make('currency_code')
                                    ->label('Currency')
                                    ->relationship('currency', 'name')
                                    ->preload()
                                    ->default(Currency::getDefaultCurrency())
                                    ->searchable()
                                    ->reactive()
                                    ->required()
                                    ->createOptionForm([
                                        Forms\Components\Select::make('currency.code')
                                            ->label('Code')
                                            ->searchable()
                                            ->options(Currency::getCurrencyCodes())
                                            ->reactive()
                                            ->afterStateUpdated(static function (callable $set, $state) {
                                                if ($state === null) {
                                                    return;
                                                }
                                                $code = $state;
                                                $currencyConfig = config("money.{$code}", []);
                                                $currencyService = app(CurrencyService::class);
                                                $defaultCurrency = Currency::getDefaultCurrency();
                                                $rate = 1;
                                                if ($defaultCurrency !== null) {
                                                    $rate = $currencyService->getCachedExchangeRate($defaultCurrency, $code);
                                                }
                                                $set('currency.name', $currencyConfig['name'] ?? '');
                                                $set('currency.rate', $rate);
                                            })
                                            ->required(),
                                        Forms\Components\TextInput::make('currency.name')
                                            ->label('Name')
                                            ->maxLength(100)
                                            ->required(),
                                        Forms\Components\TextInput::make('currency.rate')
                                            ->label('Rate')
                                            ->numeric()
                                            ->required(),
                                    ])->createOptionAction(static function (Forms\Components\Actions\Action $action) {
                                        return $action
                                            ->label('Add Currency')
                                            ->modalHeading('Add Currency')
                                            ->modalButton('Add')
                                            ->action(static function (array $data) {
                                                return DB::transaction(static function () use ($data) {
                                                    $code = $data['currency']['code'];
                                                    $name = $data['currency']['name'];
                                                    $rate = $data['currency']['rate'];
                                                    return (new CreateCurrency())->create($code, $name, $rate);
                                                });
                                            });
                                    }),
                                Forms\Components\TextInput::make('opening_balance')
                                    ->label('Opening Balance')
                                    ->required()
                                    ->default('0')
                                    ->numeric()
                                    ->mask(static fn (Forms\Components\TextInput\Mask $mask, Closure $get) => $mask
                                        ->patternBlocks([
                                            'money' => static fn (Mask $mask) => $mask
                                                ->numeric()
                                                ->decimalPlaces(config('money.' . $get('currency_code') . '.precision'))
                                                ->decimalSeparator(config('money.' . $get('currency_code') . '.decimal_mark'))
                                                ->thousandsSeparator(config('money.' . $get('currency_code') . '.thousands_separator'))
                                                ->signed()
                                                ->padFractionalZeros()
                                                ->normalizeZeros(),
                                    ])
                                    ->pattern(config('money.' . $get('currency_code') . '.symbol_first') ? config('money.' . $get('currency_code') . '.symbol') . 'money' : 'money' . config('money.' . $get('currency_code') . '.symbol'))
                                    ->lazyPlaceholder(false)),
                            ])->columns(),
                        Forms\Components\Tabs::make('Account Specifications')
                            ->tabs([
                                Forms\Components\Tabs\Tab::make('Bank Information')
                                    ->icon('heroicon-o-credit-card')
                                    ->schema([
                                        Forms\Components\TextInput::make('bank_name')
                                            ->label('Bank Name')
                                            ->maxLength(100),
                                        Forms\Components\TextInput::make('bank_phone')
                                            ->label('Bank Phone')
                                            ->tel()
                                            ->maxLength(20),
                                        Forms\Components\Textarea::make('bank_address')
                                            ->label('Bank Address')
                                            ->columnSpanFull(),
                                    ])->columns(),
                                Forms\Components\Tabs\Tab::make('Additional Information')
                                    ->icon('heroicon-o-information-circle')
                                    ->schema([
                                        Forms\Components\TextInput::make('description')
                                            ->label('Description')
                                            ->maxLength(100),
                                        Forms\Components\SpatieTagsInput::make('tags')
                                            ->label('Tags')
                                            ->placeholder('Enter tags...')
                                            ->type('statuses')
                                            ->suggestions([
                                                'Business',
                                                'Personal',
                                                'College Fund',
                                            ]),
                                        Forms\Components\MarkdownEditor::make('notes')
                                            ->label('Notes')
                                            ->columnSpanFull(),
                                    ])->columns(),
                            ]),
                    ])->columnSpan(['lg' => 2]),
                Forms\Components\Group::make()
                    ->schema([
                        Forms\Components\Section::make('Routing Information')
                            ->schema([
                                Forms\Components\TextInput::make('aba_routing_number')
                                    ->label('ABA Number')
                                    ->integer()
                                    ->length(9),
                                Forms\Components\TextInput::make('ach_routing_number')
                                    ->label('ACH Number')
                                    ->integer()
                                    ->length(9),
                            ]),
                        Forms\Components\Section::make('International Bank Information')
                            ->schema([
                                Forms\Components\TextInput::make('bic_swift_code')
                                    ->label('BIC/SWIFT Code')
                                    ->maxLength(11),
                                Forms\Components\TextInput::make('iban')
                                    ->label('IBAN')
                                    ->maxLength(34),
                            ]),
                    ])->columnSpan(['lg' => 1]),
            ])->columns(3);
    }
    /**
     * @throws Exception
     */
    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('name')
                    ->label('Account')
                    ->searchable()
                    ->weight('semibold')
                    ->icon(static fn (Account $record) => $record->enabled ? 'heroicon-o-lock-closed' : null)
                    ->tooltip(static fn (Account $record) => $record->enabled ? 'Default Account' : null)
                    ->iconPosition('after')
                    ->description(static fn (Account $record) => $record->number ?: 'N/A')
                    ->sortable(),
                Tables\Columns\TextColumn::make('bank_name')
                    ->label('Bank')
                    ->placeholder('N/A')
                    ->description(static fn (Account $record) => $record->bank_phone ?: 'N/A')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\BadgeColumn::make('status')
                    ->label('Status')
                    ->colors([
                        'primary' => 'open',
                        'success' => 'active',
                        'secondary' => 'dormant',
                        'warning' => 'restricted',
                        'danger' => 'closed',
                    ])
                    ->icons([
                        'heroicon-o-cash' => 'open',
                        'heroicon-o-clock' => 'active',
                        'heroicon-o-status-offline' => 'dormant',
                        'heroicon-o-exclamation' => 'restricted',
                        'heroicon-o-x-circle' => 'closed',
                    ])
                    ->sortable(),
                Tables\Columns\TextColumn::make('opening_balance')
                    ->label('Current Balance')
                    ->sortable()
                    ->money(static fn ($record) => $record->currency_code, true),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make()
                    ->before(static function (Tables\Actions\DeleteAction $action, Account $record) {
                        if ($record->enabled) {
                            Notification::make()
                                ->danger()
                                ->title('Action Denied')
                                ->body(__('The :name account is currently set as your default account and cannot be deleted. Please set a different account as your default before attempting to delete this one.', ['name' => $record->name]))
                                ->persistent()
                                ->send();
                            $action->cancel();
                        }
                    }),
            ])
            ->bulkActions([
                Tables\Actions\DeleteBulkAction::make()
                    ->before(static function (Tables\Actions\DeleteBulkAction $action, Collection $records) {
                        foreach ($records as $record) {
                            if ($record->enabled) {
                                Notification::make()
                                    ->danger()
                                    ->title('Action Denied')
                                    ->body(__('The :name account is currently set as your default account and cannot be deleted. Please set a different account as your default before attempting to delete this one.', ['name' => $record->name]))
                                    ->persistent()
                                    ->send();
                                $action->cancel();
                            }
                        }
                    }),
            ]);
    }
    public static function getRelations(): array
    {
        return [
            //
        ];
    }
    public static function getPages(): array
    {
        return [
            'index' => Pages\ListAccounts::route('/'),
            'create' => Pages\CreateAccount::route('/create'),
            'edit' => Pages\EditAccount::route('/{record}/edit'),
        ];
    }
}
 |