Files
nnterp-react-admin/database/seeders/TestEmployeeDataSeeder.php
admin 89506b89bd feat: add TestEmployeeDataSeeder with 10 demo employees + full Skills Matrix & Utilization data
- Created TestEmployeeDataSeeder that seeds 10 test employees, a demo engagement, departments, and designations
- Updated SkillsMatrixDemoSeeder: removed take() limits so all employees get skill assessments
- Updated UtilizationDemoSeeder: removed array_slice limits so all employees get targets, rates, and time entries
- Safe to re-run: uses firstOrCreate/updateOrInsert to avoid duplicates
2026-05-12 11:21:15 +08:00

174 lines
7.1 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Workdo\SkillsMatrix\Database\Seeders\SkillsMatrixDemoSeeder;
use Workdo\Utilization\Database\Seeders\UtilizationDemoSeeder;
class TestEmployeeDataSeeder extends Seeder
{
private int $companyId = 2;
public function run(): void
{
$this->command->info('🚀 Starting Test Employee Data Seeder...');
// Step 1: Create 10 test employees (skip if they already exist)
$employees = $this->createTestEmployees();
// Step 2: Ensure at least one engagement exists for utilization time entries
$this->ensureEngagementExists();
// Step 3: Ensure departments and designations exist for skill requirements
$this->ensureDepartmentsAndDesignations();
// Step 4: Seed Skills Matrix data
$this->command->info('');
$this->command->info('📊 Seeding Skills Matrix data...');
$this->call(SkillsMatrixDemoSeeder::class);
// Step 5: Seed Utilization data
$this->command->info('');
$this->command->info('⏱️ Seeding Utilization data...');
$this->call(UtilizationDemoSeeder::class);
$this->command->info('');
$this->command->info('🎉 All done! Created ' . count($employees) . ' test employees with full Skills Matrix & Utilization data.');
}
private function createTestEmployees(): array
{
$testEmployees = [
['name' => 'Ana Santos', 'email' => 'ana.santos@testdemo.com', 'role' => 'Senior Auditor'],
['name' => 'Carlos Rivera', 'email' => 'carlos.rivera@testdemo.com', 'role' => 'Staff Accountant'],
['name' => 'Maria Dela Cruz', 'email' => 'maria.delacruz@testdemo.com', 'role' => 'Tax Consultant'],
['name' => 'Jose Reyes', 'email' => 'jose.reyes@testdemo.com', 'role' => 'IT Analyst'],
['name' => 'Patricia Bautista', 'email' => 'patricia.bautista@testdemo.com', 'role' => 'Engagement Manager'],
['name' => 'Miguel Torres', 'email' => 'miguel.torres@testdemo.com', 'role' => 'Associate Auditor'],
['name' => 'Sofia Mendoza', 'email' => 'sofia.mendoza@testdemo.com', 'role' => 'Data Analyst'],
['name' => 'Luis Garcia', 'email' => 'luis.garcia@testdemo.com', 'role' => 'Senior Tax Advisor'],
['name' => 'Isabella Cruz', 'email' => 'isabella.cruz@testdemo.com', 'role' => 'Compliance Officer'],
['name' => 'Diego Fernandez', 'email' => 'diego.fernandez@testdemo.com', 'role' => 'Junior Developer'],
];
$created = [];
foreach ($testEmployees as $emp) {
$user = User::firstOrCreate(
['email' => $emp['email']],
[
'name' => $emp['name'],
'password' => Hash::make('password123'),
'type' => 'staff',
'lang' => 'en',
'created_by' => $this->companyId,
'is_disable' => 0,
'is_enable_login' => 1,
'email_verified_at' => now(),
]
);
// Assign 'staff' role if not already assigned
if (!$user->hasRole('staff')) {
$staffRole = \Spatie\Permission\Models\Role::where('name', 'staff')
->where('created_by', $this->companyId)
->first();
if ($staffRole) {
$user->assignRole($staffRole);
}
}
$created[] = $user;
$status = $user->wasRecentlyCreated ? '✅ Created' : '⏩ Already exists';
$this->command->info(" {$status}: {$emp['name']} ({$emp['email']})");
}
return $created;
}
private function ensureEngagementExists(): void
{
$exists = DB::table('engagements')
->where('created_by', $this->companyId)
->where('status', 'active')
->exists();
if (!$exists) {
DB::table('engagements')->insert([
'name' => 'Demo Audit Engagement — Q2 2026',
'client_name' => 'Acme Corporation',
'type' => 'audit',
'status' => 'active',
'priority' => 'high',
'start_date' => now()->startOfMonth()->format('Y-m-d'),
'end_date' => now()->addMonths(3)->format('Y-m-d'),
'budget_hours' => 1200,
'description' => 'Full-scope financial statement audit for FY2025. Includes revenue, receivables, inventory, and IT general controls.',
'created_by' => $this->companyId,
'created_at' => now(),
'updated_at' => now(),
]);
$this->command->info(' ✅ Created demo engagement: "Demo Audit Engagement — Q2 2026"');
} else {
$this->command->info(' ⏩ Active engagement already exists.');
}
}
private function ensureDepartmentsAndDesignations(): void
{
// Departments
$departments = ['Audit & Assurance', 'Tax Advisory', 'IT & Digital'];
foreach ($departments as $dept) {
$exists = DB::table('departments')
->where('department_name', $dept)
->where('created_by', $this->companyId)
->exists();
if (!$exists) {
DB::table('departments')->insert([
'department_name' => $dept,
'created_by' => $this->companyId,
'created_at' => now(),
'updated_at' => now(),
]);
}
}
// Designations
$deptIds = DB::table('departments')
->where('created_by', $this->companyId)
->pluck('id', 'department_name')
->toArray();
$designations = [
['designation_name' => 'Senior Auditor', 'department' => 'Audit & Assurance'],
['designation_name' => 'Tax Consultant', 'department' => 'Tax Advisory'],
['designation_name' => 'Systems Analyst', 'department' => 'IT & Digital'],
];
foreach ($designations as $desig) {
$deptId = $deptIds[$desig['department']] ?? null;
$exists = DB::table('designations')
->where('designation_name', $desig['designation_name'])
->where('created_by', $this->companyId)
->exists();
if (!$exists) {
DB::table('designations')->insert([
'designation_name' => $desig['designation_name'],
'department_id' => $deptId,
'created_by' => $this->companyId,
'created_at' => now(),
'updated_at' => now(),
]);
}
}
$this->command->info(' ✅ Departments and designations ready.');
}
}