Faza 2: multi-tenancy + Filament dual panels + seed PSauto

Schema centrală:
- companies (slug unique, status, plan_id, settings JSON, trial/active dates)
- super_admins (operator platform)
- plans (free/basic/pro)

Schema tenant (toate cu company_id NOT NULL):
- users (UNIQUE company_id+email)
- clients
- vehicles

Tenancy core:
- App\Tenancy\TenantManager singleton
- App\Models\Concerns\BelongsToTenant trait + TenantScope
- ResolveTenant middleware (slug → Company, 404 pentru rezervate/missing)
- CheckTenantStatus middleware (suspended/expired/archived)
- Fail-safe: TenantScope returns 0 rows când tenant nu e rezolvat

Auth guards:
- 'central' guard cu super_admins provider (panou platform)
- 'web' guard cu users provider (per-tenant)

Filament panels:
- CentralPanelProvider la service.mir.md/admin
- TenantPanelProvider la <slug>.service.mir.md/app
- CompanyResource (central): CRUD companii cu status badge + filtre
- ClientResource (tenant): CRUD clienți cu status, sursă, sold
- VehicleResource (tenant): CRUD mașini cu marcă/model/VIN

Seed:
- 3 plans (free/basic/pro)
- super-admin: vasyka.moraru@gmail.com / admin123
- demo company 'psauto' cu admin user admin@psauto.md / admin123
- 3 clienți + 3 mașini preluate din AutoCRM.html

Bootstrap:
- TrustProxies (Cloudflare→Traefik HTTPS detection)
- forceScheme/forceRootUrl când APP_URL e HTTPS
- Helper global tenant() în app/helpers.php (autoload via composer)
- RUN_SEED env var în entrypoint pentru db:seed condiționat
This commit is contained in:
2026-05-05 21:29:52 +00:00
parent 125566cb81
commit 4b1635d045
48 changed files with 1510 additions and 386 deletions
@@ -0,0 +1,118 @@
<?php
namespace App\Filament\Central\Resources;
use App\Filament\Central\Resources\CompanyResource\Pages;
use App\Models\Central\Company;
use App\Models\Central\Plan;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class CompanyResource extends Resource
{
protected static ?string $model = Company::class;
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-building-office-2';
protected static ?string $navigationLabel = 'Companii';
protected static ?string $modelLabel = 'companie';
protected static ?string $pluralModelLabel = 'companii';
public static function form(Schema $schema): Schema
{
return $schema->components([
Forms\Components\Section::make('Identificare')
->columns(2)
->schema([
Forms\Components\TextInput::make('slug')
->required()
->alphaDash()
->lowercase()
->maxLength(30)
->unique(ignoreRecord: true)
->helperText('Subdomeniul: <slug>.service.mir.md'),
Forms\Components\TextInput::make('name')->required()->maxLength(120),
Forms\Components\TextInput::make('display_name')->maxLength(120),
Forms\Components\TextInput::make('city')->maxLength(60),
]),
Forms\Components\Section::make('Contact')
->columns(2)
->schema([
Forms\Components\TextInput::make('contact_name')->maxLength(120),
Forms\Components\TextInput::make('phone')->tel()->maxLength(40),
Forms\Components\TextInput::make('email')->email()->maxLength(120),
]),
Forms\Components\Section::make('Abonament')
->columns(2)
->schema([
Forms\Components\Select::make('status')
->options([
'trial' => 'Trial',
'active' => 'Activ',
'expired' => 'Expirat',
'suspended' => 'Suspendat',
'archived' => 'Arhivat',
])
->default('trial')
->required(),
Forms\Components\Select::make('plan_id')
->label('Plan')
->options(fn () => Plan::pluck('name', 'id'))
->searchable(),
Forms\Components\DateTimePicker::make('trial_ends_at')->label('Trial expiră la'),
Forms\Components\DateTimePicker::make('active_until')->label('Abonament până la'),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('id')->sortable(),
Tables\Columns\TextColumn::make('slug')
->searchable()
->copyable()
->url(fn (Company $r) => $r->url('/app'))
->openUrlInNewTab(),
Tables\Columns\TextColumn::make('name')->searchable()->sortable(),
Tables\Columns\TextColumn::make('status')
->badge()
->colors([
'success' => ['active'],
'warning' => ['trial'],
'danger' => ['suspended', 'expired'],
'gray' => ['archived'],
]),
Tables\Columns\TextColumn::make('plan.name')->label('Plan')->placeholder('—'),
Tables\Columns\TextColumn::make('city')->toggleable(),
Tables\Columns\TextColumn::make('users_count')->counts('users')->label('Useri'),
Tables\Columns\TextColumn::make('created_at')->date()->sortable(),
])
->filters([
Tables\Filters\SelectFilter::make('status')->options([
'trial' => 'Trial', 'active' => 'Activ', 'expired' => 'Expirat',
'suspended' => 'Suspendat', 'archived' => 'Arhivat',
]),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->defaultSort('created_at', 'desc');
}
public static function getPages(): array
{
return [
'index' => Pages\ListCompanies::route('/'),
'create' => Pages\CreateCompany::route('/create'),
'edit' => Pages\EditCompany::route('/{record}/edit'),
];
}
}
@@ -0,0 +1,11 @@
<?php
namespace App\Filament\Central\Resources\CompanyResource\Pages;
use App\Filament\Central\Resources\CompanyResource;
use Filament\Resources\Pages\CreateRecord;
class CreateCompany extends CreateRecord
{
protected static string $resource = CompanyResource::class;
}
@@ -0,0 +1,17 @@
<?php
namespace App\Filament\Central\Resources\CompanyResource\Pages;
use App\Filament\Central\Resources\CompanyResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditCompany extends EditRecord
{
protected static string $resource = CompanyResource::class;
protected function getHeaderActions(): array
{
return [Actions\DeleteAction::make()];
}
}
@@ -0,0 +1,17 @@
<?php
namespace App\Filament\Central\Resources\CompanyResource\Pages;
use App\Filament\Central\Resources\CompanyResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListCompanies extends ListRecords
{
protected static string $resource = CompanyResource::class;
protected function getHeaderActions(): array
{
return [Actions\CreateAction::make()];
}
}