Files
autocrm/app/Filament/Central/Resources/PlanResource.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

126 lines
5.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Filament\Central\Resources;
use App\Filament\Central\Resources\PlanResource\Pages;
use App\Models\Central\Plan;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Schemas;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class PlanResource extends Resource
{
protected static ?string $model = Plan::class;
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-rectangle-stack';
protected static ?string $navigationLabel = 'Planuri';
protected static ?string $modelLabel = 'plan';
protected static ?string $pluralModelLabel = 'planuri';
protected static ?int $navigationSort = 20;
public const FEATURE_OPTIONS = [
'kanban' => 'Kanban WO',
'reports' => 'Rapoarte avansate',
'ai' => 'Asistent AI',
'pdf' => 'Generare PDF',
'reverb' => 'WebSocket real-time',
'api' => 'REST API + tokens',
'multi_user' => 'Multi-user (>1 cont)',
'white_label' => 'White-label complet',
'priority_support' => 'Suport prioritar',
'custom_domain' => 'Domeniu propriu',
];
public static function form(Schema $schema): Schema
{
return $schema->components([
Schemas\Components\Section::make('Identificare')
->columns(2)
->schema([
Forms\Components\TextInput::make('slug')
->required()->alphaDash()->unique(ignoreRecord: true)
->dehydrateStateUsing(fn ($s) => strtolower((string) $s))
->extraInputAttributes(['style' => 'text-transform:lowercase']),
Forms\Components\TextInput::make('name')->required()->maxLength(60),
Forms\Components\Toggle::make('is_active')->label('Activ')->default(true),
Forms\Components\Toggle::make('is_public')->label('Public (afișat la signup)')->default(true),
]),
Schemas\Components\Section::make('Preț')
->columns(3)
->schema([
Forms\Components\TextInput::make('price_monthly')->label('Preț lunar')->numeric()->required()->suffix(fn (Forms\Get $get) => $get('currency') ?? 'MDL'),
Forms\Components\TextInput::make('price_yearly')->label('Preț anual')->numeric()->helperText('De obicei 10× preț lunar (2 luni gratis).')->suffix(fn (Forms\Get $get) => $get('currency') ?? 'MDL'),
Forms\Components\Select::make('currency')->options(['MDL' => 'MDL', 'EUR' => 'EUR', 'USD' => 'USD'])->default('MDL'),
]),
Schemas\Components\Section::make('Funcționalități incluse')
->columns(2)
->schema([
Forms\Components\CheckboxList::make('features')
->options(self::FEATURE_OPTIONS)
->columns(2)
->columnSpanFull(),
]),
Schemas\Components\Section::make('Limite')
->columns(3)
->schema([
Forms\Components\TextInput::make('limits.max_users')->label('Max useri')->numeric()->placeholder('∞'),
Forms\Components\TextInput::make('limits.max_clients')->label('Max clienți')->numeric()->placeholder('∞'),
Forms\Components\TextInput::make('limits.max_vehicles')->label('Max mașini')->numeric()->placeholder('∞'),
Forms\Components\TextInput::make('limits.max_work_orders_month')->label('Max fișe/lună')->numeric()->placeholder('∞'),
Forms\Components\TextInput::make('limits.storage_mb')->label('Storage (MB)')->numeric()->placeholder('∞'),
Forms\Components\TextInput::make('limits.ai_messages_month')->label('Mesaje AI/lună')->numeric()->placeholder('∞'),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')->searchable()->sortable(),
Tables\Columns\TextColumn::make('price_monthly')
->money(fn ($r) => $r->currency)
->label('Lunar')
->sortable(),
Tables\Columns\TextColumn::make('price_yearly')
->money(fn ($r) => $r->currency)
->label('Anual'),
Tables\Columns\TextColumn::make('companies_count')
->counts('companies')
->label('Abonați')
->badge()
->color('primary'),
Tables\Columns\TextColumn::make('features')
->label('Funcționalități')
->formatStateUsing(fn ($s) => is_array($s) ? count($s) . ' incluse' : '—')
->badge(),
Tables\Columns\IconColumn::make('is_active')->boolean()->label('Activ'),
Tables\Columns\IconColumn::make('is_public')->boolean()->label('Public'),
])
->actions([
Actions\EditAction::make(),
Actions\DeleteAction::make(),
])
->emptyStateHeading('Niciun plan definit')
->emptyStateDescription('Creează planuri (ex: Free, Basic, Pro) și atribuie-le companiilor.')
->defaultSort('price_monthly');
}
public static function getPages(): array
{
return [
'index' => Pages\ListPlans::route('/'),
'create' => Pages\CreatePlan::route('/create'),
'edit' => Pages\EditPlan::route('/{record}/edit'),
];
}
}