Files
autocrm/app/Filament/Central/Resources/SuperAdminResource.php
T
Vasyka 10426d0c91 Central panel SaaS upgrade — Plans/Subscriptions/SuperAdmins/Detail page
Models & migrations:
- subscriptions table (company, plan, period, amount, status, dates, invoice)
- super_admins: role enum (owner/admin/support/sales/finance) + phone + notes
- Subscription model with STATUSES/PERIODS/PAYMENT_METHODS + invoice number
  generator + extends company.active_until on mark_paid
- Company model: subscriptions() + latestSubscription() relations
- SuperAdmin model: role helpers (isOwner, canManageBilling, canManageTenants)

Filament Central panel:
- PlanResource (CRUD, features checklist, limits per plan, abonati count badge)
- SubscriptionResource (CRUD, mark_paid action, navigation badge for overdue)
- SuperAdminResource (CRUD, reset password, toggle 2FA, can't self-delete)
- ViewCompany page with live stats (users/clients/vehicles/WO/parts/revenue/
  storage/last_login + days_until_expiry), subscriptions history table,
  config snapshot, action buttons (open/issue invoice/upload logo/suspend)
- CompanyResource: row click → view, openUrlInNewTab action, recordTitleAttribute,
  empty state, view route registered
- PlatformStats widget upgraded: 6 cards (incl. MRR realized this month, overdue
  invoices count, click-through to filtered tables)
- RevenueChart: 12-month MRR line chart
- RecentTenants: latest 8 tenants with click-through
- PendingPayments: pending+overdue invoices table
- Database notifications enabled + Cmd+K global search
- HEAD_END render hook: PWA manifest + theme color + emoji favicon
- /admin-manifest.json route

Seeder:
- Plans aligned with new FEATURE_OPTIONS (kanban/pdf/reports/ai/api/reverb/etc)
- 4 plans: Free / Basic / Pro / Enterprise (with proper limits)
- SuperAdmin gets role='owner'
- Demo subscription for psauto on Pro plan, marked paid this month
2026-05-07 22:02:44 +00:00

151 lines
6.9 KiB
PHP

<?php
namespace App\Filament\Central\Resources;
use App\Filament\Central\Resources\SuperAdminResource\Pages;
use App\Models\Central\SuperAdmin;
use Filament\Actions;
use Filament\Forms;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Schemas;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Hash;
class SuperAdminResource extends Resource
{
protected static ?string $model = SuperAdmin::class;
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-shield-check';
protected static ?string $navigationLabel = 'Super-admini';
protected static ?string $modelLabel = 'super-admin';
protected static ?string $pluralModelLabel = 'super-admini';
protected static string|\UnitEnum|null $navigationGroup = 'Acces';
protected static ?int $navigationSort = 50;
public static function form(Schema $schema): Schema
{
return $schema->components([
Schemas\Components\Section::make('Identificare')
->columns(2)
->schema([
Forms\Components\TextInput::make('name')->required()->maxLength(120),
Forms\Components\TextInput::make('email')->email()->required()->unique(ignoreRecord: true)->maxLength(120),
Forms\Components\TextInput::make('phone')->tel()->maxLength(40),
Forms\Components\Toggle::make('is_active')->label('Activ')->default(true),
]),
Schemas\Components\Section::make('Permisiuni')
->columns(1)
->schema([
Forms\Components\Select::make('role')
->options(SuperAdmin::ROLES)
->default('support')
->required()
->helperText('Owner = drepturi totale. Admin = aproape la fel. Sales = doar tenanți + planuri. Finance = facturi. Support = read-only.'),
]),
Schemas\Components\Section::make('Parolă')
->columns(1)
->schema([
Forms\Components\TextInput::make('password')
->password()
->revealable()
->dehydrated(fn ($state) => filled($state))
->dehydrateStateUsing(fn ($state) => Hash::make($state))
->required(fn (string $operation) => $operation === 'create')
->minLength(8)
->helperText(fn (string $operation) => $operation === 'edit' ? 'Lasă gol ca să o păstrezi.' : 'Min 8 caractere.'),
]),
Forms\Components\Textarea::make('notes')->label('Note interne')->rows(2)->columnSpanFull(),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')->searchable()->sortable()->weight('bold'),
Tables\Columns\TextColumn::make('email')->searchable()->copyable(),
Tables\Columns\TextColumn::make('role')
->badge()
->formatStateUsing(fn ($s) => array_keys(SuperAdmin::ROLES, SuperAdmin::ROLES[$s] ?? $s)[0] ?? $s)
->color(fn ($s) => match ($s) {
'owner' => 'danger',
'admin' => 'warning',
'finance' => 'success',
'sales' => 'info',
default => 'gray',
}),
Tables\Columns\IconColumn::make('is_active')->boolean()->label('Activ'),
Tables\Columns\IconColumn::make('app_authentication_secret')
->label('2FA')
->boolean()
->getStateUsing(fn ($r) => $r->app_authentication_secret !== null)
->trueIcon('heroicon-o-lock-closed')
->falseIcon('heroicon-o-lock-open')
->trueColor('success')
->falseColor('warning'),
Tables\Columns\TextColumn::make('last_login_at')->label('Ultim login')->dateTime()->placeholder('—'),
Tables\Columns\TextColumn::make('created_at')->date(),
])
->filters([
Tables\Filters\SelectFilter::make('role')->options(SuperAdmin::ROLES),
Tables\Filters\TernaryFilter::make('is_active')->label('Activ'),
])
->actions([
Actions\Action::make('reset_password')
->label('Reset parolă')
->icon('heroicon-m-key')
->color('warning')
->schema([
Forms\Components\TextInput::make('new_password')
->password()->required()->minLength(8)->revealable(),
])
->action(function (SuperAdmin $r, array $data) {
$r->update(['password' => Hash::make($data['new_password'])]);
Notification::make()->title('Parolă resetată.')->success()->send();
}),
Actions\Action::make('toggle_2fa')
->label(fn ($r) => $r->app_authentication_secret ? 'Dezactivează 2FA' : 'Forțează re-setare 2FA')
->icon('heroicon-m-lock-open')
->color('danger')
->visible(fn ($r) => $r->app_authentication_secret !== null)
->requiresConfirmation()
->action(function (SuperAdmin $r) {
$r->forceFill([
'app_authentication_secret' => null,
'app_authentication_recovery_codes' => null,
'email_authentication_at' => null,
])->saveQuietly();
Notification::make()->title('2FA dezactivat.')->success()->send();
}),
Actions\EditAction::make(),
Actions\DeleteAction::make()
->before(function (SuperAdmin $r) {
if (auth('central')->id() === $r->id) {
Notification::make()->title('Nu te poți șterge pe tine!')->danger()->send();
return false;
}
}),
])
->emptyStateHeading('Doar tu ești aici')
->emptyStateDescription('Adaugă echipa ta — colegi de la suport, sales, finance — fiecare cu rolul lui.')
->defaultSort('created_at', 'desc');
}
public static function getPages(): array
{
return [
'index' => Pages\ListSuperAdmins::route('/'),
'create' => Pages\CreateSuperAdmin::route('/create'),
'edit' => Pages\EditSuperAdmin::route('/{record}/edit'),
];
}
}