Files
autocrm/database/seeders/DatabaseSeeder.php
T
Vasyka 4b1635d045 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
2026-05-05 21:29:52 +00:00

140 lines
5.5 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\Central\Company;
use App\Models\Central\Plan;
use App\Models\Central\SuperAdmin;
use App\Models\Tenant\Client;
use App\Models\Tenant\User;
use App\Models\Tenant\Vehicle;
use App\Tenancy\TenantManager;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
class DatabaseSeeder extends Seeder
{
public function run(): void
{
// ─── Plans ────────────────────────────────────────────────
$free = Plan::firstOrCreate(['slug' => 'free'], [
'name' => 'Free',
'price_monthly' => 0,
'currency' => 'MDL',
'features' => ['core'],
'limits' => ['max_users' => 2, 'max_vehicles' => 50],
]);
Plan::firstOrCreate(['slug' => 'basic'], [
'name' => 'Basic',
'price_monthly' => 299,
'currency' => 'MDL',
'features' => ['core', 'workorders', 'kanban'],
'limits' => ['max_users' => 5, 'max_vehicles' => 500],
]);
Plan::firstOrCreate(['slug' => 'pro'], [
'name' => 'Pro',
'price_monthly' => 599,
'currency' => 'MDL',
'features' => ['core', 'workorders', 'kanban', 'reports', 'ai', 'api'],
'limits' => ['max_users' => -1, 'max_vehicles' => -1],
]);
// ─── Super-admin (operator platformă) ─────────────────────
SuperAdmin::firstOrCreate(['email' => 'vasyka.moraru@gmail.com'], [
'name' => 'Vasyka',
'password' => Hash::make('admin123'),
'is_active' => true,
]);
// ─── PSauto demo company ──────────────────────────────────
$psauto = Company::firstOrCreate(['slug' => 'psauto'], [
'name' => 'PSauto SRL',
'display_name' => 'PSauto',
'city' => 'Chișinău',
'phone' => '+373 22 123 456',
'email' => 'contact@psauto.md',
'contact_name' => 'Manager PSauto',
'status' => 'active',
'plan_id' => $free->id,
'active_until' => now()->addYear(),
'settings' => [
'currency' => 'MDL',
'language' => 'ro',
'theme_color' => '#3B82F6',
'labor_rate' => 400,
'posts' => ['Post 1', 'Post 2', 'Post 3'],
],
]);
// Activate tenant context for the seeded data so global scopes auto-fill company_id.
app(TenantManager::class)->setCurrent($psauto);
// ─── Admin user pentru PSauto ─────────────────────────────
User::firstOrCreate(
['company_id' => $psauto->id, 'email' => 'admin@psauto.md'],
[
'name' => 'Administrator PSauto',
'password' => Hash::make('admin123'),
'role' => 'admin',
'status' => 'active',
'phone' => '+373 22 123 456',
'locale' => 'ro',
'email_verified_at' => now(),
]
);
// ─── Clienți demo (din AutoCRM.html) ──────────────────────
$c1 = Client::firstOrCreate(
['company_id' => $psauto->id, 'phone' => '+373 69 100001'],
[
'type' => 'individual',
'name' => 'Ion Popescu',
'email' => 'ion@mail.com',
'status' => 'vip',
'source' => 'recommend',
]
);
$c2 = Client::firstOrCreate(
['company_id' => $psauto->id, 'phone' => '+373 79 200002'],
[
'type' => 'individual',
'name' => 'Maria Dumitru',
'status' => 'active',
'discount_pct' => 5,
'source' => 'site',
]
);
$c3 = Client::firstOrCreate(
['company_id' => $psauto->id, 'phone' => '+373 60 300003'],
[
'type' => 'individual',
'name' => 'Andrei Lupu',
'status' => 'active',
'discount_pct' => 10,
'notes' => 'Doar piese originale',
'source' => 'instagram',
]
);
// ─── Mașini demo ──────────────────────────────────────────
Vehicle::firstOrCreate(
['company_id' => $psauto->id, 'client_id' => $c1->id, 'make' => 'BMW', 'model' => 'X5'],
['year' => 2020, 'plate' => 'CIU 001', 'engine' => '3.0i', 'gearbox' => 'Automat 8AT', 'fuel' => 'Benzină', 'mileage' => 85000, 'color' => 'Alb']
);
Vehicle::firstOrCreate(
['company_id' => $psauto->id, 'client_id' => $c2->id, 'make' => 'Audi', 'model' => 'A4'],
['year' => 2019, 'plate' => 'CIU 002', 'engine' => '2.0 TDI', 'gearbox' => 'DSG7', 'fuel' => 'Diesel', 'mileage' => 45000, 'color' => 'Negru']
);
Vehicle::firstOrCreate(
['company_id' => $psauto->id, 'client_id' => $c3->id, 'make' => 'Porsche', 'model' => 'Cayenne'],
['year' => 2021, 'plate' => 'CIU 003', 'engine' => '3.0 TDI', 'gearbox' => 'Tiptronic', 'fuel' => 'Diesel', 'mileage' => 22000, 'color' => 'Gri']
);
app(TenantManager::class)->clear();
}
}