option('force') && ! $this->confirm('Are you sure you want to migrate employees data? This will add new employees to your database.')) { $this->info('Migration cancelled.'); return; } $this->info('Starting employees migration...'); try { $oldBiographical = DB::connection('old_database')->table('biographical')->whereNull('deleted_at')->get(); $this->info("Found {$oldBiographical->count()} employees to migrate."); $bar = $this->output->createProgressBar($oldBiographical->count()); $migrated = 0; $skipped = 0; foreach ($oldBiographical as $oldBio) { $user = User::find($oldBio->user_id); if (! $user) { $this->warn("User ID {$oldBio->user_id} not found, skipping employee {$oldBio->name}..."); $skipped++; $bar->advance(); continue; } $existingEmployee = Employee::where('user_id', $oldBio->user_id)->first(); if ($existingEmployee) { $this->warn("Employee for user ID {$oldBio->user_id} already exists, skipping..."); $skipped++; $bar->advance(); continue; } // Get user data for additional info $oldUser = DB::connection('old_database')->table('users')->where('id', $oldBio->user_id)->first(); Employee::create([ 'user_id' => $oldBio->user_id, 'full_name' => $oldBio->name, 'code' => $this->generateEmployeeCode($oldBio->user_id), 'phone_number' => $oldBio->phone_number, 'base_salary' => $oldUser ? $oldUser->salary : 0, 'gender' => $oldBio->gender == '1' ? Gender::MALE : Gender::FEMALE, 'address' => $oldBio->pob, // Using place of birth as address 'birthdate' => $oldBio->dob, 'hire_date' => $oldUser ? $oldUser->entry_date : now()->toDateString(), 'resign_date' => $oldUser && $oldUser->out_date ? $oldUser->out_date : null, 'status' => EmploymentStatus::PERMANENT, 'created_at' => $oldBio->created_at, 'updated_at' => $oldBio->updated_at, ]); $migrated++; $bar->advance(); } $bar->finish(); $this->newLine(2); $this->info('✅ Employees migration completed!'); $this->info("📊 Migrated: {$migrated} employees"); $this->info("⏭️ Skipped: {$skipped} employees (user not found or already exist)"); } catch (\Exception $e) { $this->error("❌ Migration failed: {$e->getMessage()}"); return 1; } return 0; } private function generateEmployeeCode(int $userId): string { return 'EMP'.str_pad($userId, 4, '0', STR_PAD_LEFT); } }