From 49359d1ba100fbb818d8d561abe6bf37cb9212ce Mon Sep 17 00:00:00 2001 From: Ray1111-aa Date: Wed, 28 Oct 2026 08:47:02 +0700 Subject: [PATCH] fix(employee): synchronize division & position fields across HSE and Admin, auto-populate Private Guard & Anggota for guards --- app/Http/Controllers/Admin/UserController.php | 48 +++++-- .../Pg/SecurityGuardController.php | 31 +++++ ...ync_security_guards_to_employees_table.php | 49 ++++++++ resources/views/admin/users/index.blade.php | 46 +++++-- resources/views/hse/employees_edit.blade.php | 22 ++++ resources/views/hse/employees_index.blade.php | 26 ++++ ...eEmployeeFlexibleUnitKerjaAndModalTest.php | 118 ++++++++++++++++++ 7 files changed, 319 insertions(+), 21 deletions(-) create mode 100644 database/migrations/2026_10_28_083500_sync_security_guards_to_employees_table.php diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index d12e4a1..d9ee4fe 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -52,7 +52,7 @@ class UserController extends Controller default => $query->latest('created_at'), }; - $users = $query->with('permissions')->paginate(15)->withQueryString(); + $users = $query->with(['permissions', 'roles', 'employee'])->paginate(15)->withQueryString(); $roles = Role::with('permissions')->orderBy('name')->get(); $rolePermissionsMap = $roles->mapWithKeys(function ($r) { return [$r->name => $r->permissions->pluck('name')->toArray()]; @@ -126,23 +126,30 @@ class UserController extends Controller // Reset permission cache Spatie app()[PermissionRegistrar::class]->forgetCachedPermissions(); - // Sinkronisasi profil master Employee jika role karyawan atau ada isian data karyawan + // Sinkronisasi profil master Employee jika role karyawan, gate officer, pg admin, atau ada isian data karyawan $isEmployeeRole = $user->isEmployee() || $validated['role'] === 'employee'; + $isGateOfficer = $user->isGateOfficer() || $validated['role'] === 'gate_officer'; + $isPgAdmin = $user->isPgAdmin() || $validated['role'] === 'pg_admin'; $hasEmployeeFields = !empty($validated['unit_kerja']) || !empty($validated['division']) || !empty($validated['position']) || (isset($validated['is_operator']) && $validated['is_operator'] !== ''); - if ($isEmployeeRole || $hasEmployeeFields) { + if ($isEmployeeRole || $isGateOfficer || $isPgAdmin || $hasEmployeeFields) { $isOperator = (isset($validated['is_operator']) && $validated['is_operator'] !== '') ? (bool) $validated['is_operator'] : ($isEmployeeRole ? true : false); + + $defaultUnit = ($isGateOfficer || $isPgAdmin) ? 'Private Guard' : 'Umum'; + $defaultDivision = ($isGateOfficer || $isPgAdmin) ? 'Private Guard' : '-'; + $defaultPosition = $isGateOfficer ? 'Anggota' : ($isPgAdmin ? 'Admin PG' : ($isEmployeeRole ? 'Driver Operasional' : '-')); + $emp = Employee::where('user_id', $user->id)->orWhere('nik', $user->username)->first(); $empData = [ 'user_id' => $user->id, 'nik' => $user->username, 'name' => $user->name, 'employee_type' => !empty($validated['employee_type']) ? ucfirst(strtolower($validated['employee_type'])) : ($emp?->employee_type ?? 'Karyawan'), - 'unit_kerja' => $validated['unit_kerja'] ?? ($emp?->unit_kerja ?? 'Umum'), - 'division' => $validated['division'] ?? ($emp?->division ?? '-'), - 'position' => $validated['position'] ?? ($emp?->position ?? ($isEmployeeRole ? 'Driver Operasional' : '-')), + 'unit_kerja' => !empty($validated['unit_kerja']) ? $validated['unit_kerja'] : ($emp?->unit_kerja ?: $defaultUnit), + 'division' => !empty($validated['division']) ? $validated['division'] : ($emp?->division ?: $defaultDivision), + 'position' => !empty($validated['position']) ? $validated['position'] : ($emp?->position ?: $defaultPosition), 'is_operator' => $isOperator, 'photo_path' => $photoPath ?: $emp?->photo_path, ]; @@ -248,9 +255,15 @@ class UserController extends Controller // Sinkronkan data master Employee $emp = $user->employee ?: Employee::where('nik', $user->username)->first(); $isEmployeeRole = $user->isEmployee() || $validated['role'] === 'employee'; + $isGateOfficer = $user->isGateOfficer() || $validated['role'] === 'gate_officer'; + $isPgAdmin = $user->isPgAdmin() || $validated['role'] === 'pg_admin'; $hasEmployeeFields = !empty($validated['unit_kerja']) || !empty($validated['division']) || !empty($validated['position']) || (isset($validated['is_operator']) && $validated['is_operator'] !== ''); - if ($emp || $isEmployeeRole || $hasEmployeeFields) { + if ($emp || $isEmployeeRole || $isGateOfficer || $isPgAdmin || $hasEmployeeFields) { + $defaultUnit = ($isGateOfficer || $isPgAdmin) ? 'Private Guard' : 'Umum'; + $defaultDivision = ($isGateOfficer || $isPgAdmin) ? 'Private Guard' : '-'; + $defaultPosition = $isGateOfficer ? 'Anggota' : ($isPgAdmin ? 'Admin PG' : ($isEmployeeRole ? 'Driver Operasional' : '-')); + $empData = [ 'name' => $user->name, 'nik' => $user->username, @@ -259,15 +272,24 @@ class UserController extends Controller if (array_key_exists('employee_type', $validated) && $validated['employee_type'] !== null) { $empData['employee_type'] = ucfirst(strtolower($validated['employee_type'])); } - if (array_key_exists('unit_kerja', $validated) && $validated['unit_kerja'] !== null) { + if (!empty($validated['unit_kerja'])) { $empData['unit_kerja'] = $validated['unit_kerja']; + } elseif ($isGateOfficer || $isPgAdmin) { + $empData['unit_kerja'] = ($emp && !empty($emp->unit_kerja)) ? $emp->unit_kerja : $defaultUnit; } - if (array_key_exists('division', $validated) && $validated['division'] !== null) { + + if (!empty($validated['division'])) { $empData['division'] = $validated['division']; + } elseif ($isGateOfficer || $isPgAdmin) { + $empData['division'] = ($emp && !empty($emp->division) && $emp->division !== '-') ? $emp->division : $defaultDivision; } - if (array_key_exists('position', $validated) && $validated['position'] !== null) { + + if (!empty($validated['position'])) { $empData['position'] = $validated['position']; + } elseif ($isGateOfficer || $isPgAdmin) { + $empData['position'] = ($emp && !empty($emp->position) && $emp->position !== '-') ? $emp->position : $defaultPosition; } + if (array_key_exists('is_operator', $validated) && $validated['is_operator'] !== null && $validated['is_operator'] !== '') { $empData['is_operator'] = (bool) $validated['is_operator']; } @@ -280,9 +302,9 @@ class UserController extends Controller } else { $empData['user_id'] = $user->id; $empData['employee_type'] = $empData['employee_type'] ?? 'Karyawan'; - $empData['unit_kerja'] = $empData['unit_kerja'] ?? 'Umum'; - $empData['division'] = $empData['division'] ?? '-'; - $empData['position'] = $empData['position'] ?? ($isEmployeeRole ? 'Driver Operasional' : '-'); + $empData['unit_kerja'] = $empData['unit_kerja'] ?? $defaultUnit; + $empData['division'] = $empData['division'] ?? $defaultDivision; + $empData['position'] = $empData['position'] ?? $defaultPosition; $empData['is_operator'] = $empData['is_operator'] ?? ($isEmployeeRole ? true : false); Employee::create($empData); } diff --git a/app/Http/Controllers/Pg/SecurityGuardController.php b/app/Http/Controllers/Pg/SecurityGuardController.php index e3fa12e..549e183 100644 --- a/app/Http/Controllers/Pg/SecurityGuardController.php +++ b/app/Http/Controllers/Pg/SecurityGuardController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Pg; use App\Http\Controllers\Controller; +use App\Models\Employee; use App\Models\Gate; use App\Models\SecurityGuard; use App\Models\User; @@ -255,6 +256,20 @@ class SecurityGuardController extends Controller 'user_id' => $user->id, 'is_active' => true, ]); + + // 3. Otomatis buat / sinkronkan profil Employee untuk Satpam (Divisi: Private Guard, Jabatan: Anggota) + Employee::firstOrCreate( + ['nik' => $badgeId], + [ + 'user_id' => $user->id, + 'name' => $request->input('name'), + 'employee_type' => 'Karyawan', + 'unit_kerja' => 'Private Guard', + 'division' => 'Private Guard', + 'position' => 'Anggota', + 'is_operator' => false, + ] + ); }); return back()->with('success', "Petugas Satpam {$request->input('name')} (ID: {$request->input('badge_id')}) berhasil didaftarkan."); @@ -299,6 +314,17 @@ class SecurityGuardController extends Controller $guard->user->update($userData); } + + // Sinkronkan nama dan profil Employee Satpam + $emp = Employee::where('user_id', $guard->user_id)->orWhere('nik', $guard->badge_id)->first(); + if ($emp) { + $emp->update([ + 'name' => $guard->name, + 'unit_kerja' => $emp->unit_kerja ?: 'Private Guard', + 'division' => ($emp->division && $emp->division !== '-') ? $emp->division : 'Private Guard', + 'position' => ($emp->position && $emp->position !== '-') ? $emp->position : 'Anggota', + ]); + } }); return back()->with('success', "Data petugas Satpam {$guard->name} berhasil diperbarui."); @@ -333,11 +359,16 @@ class SecurityGuardController extends Controller DB::transaction(function () use ($guard) { $user = $guard->user; + $badgeId = $guard->badge_id; + $userId = $guard->user_id; + $guard->delete(); if ($user) { $user->delete(); } + + Employee::where('user_id', $userId)->orWhere('nik', $badgeId)->delete(); }); return back()->with('success', "Data petugas Satpam {$guardName} ({$badgeId}) berhasil dihapus."); diff --git a/database/migrations/2026_10_28_083500_sync_security_guards_to_employees_table.php b/database/migrations/2026_10_28_083500_sync_security_guards_to_employees_table.php new file mode 100644 index 0000000..9446f23 --- /dev/null +++ b/database/migrations/2026_10_28_083500_sync_security_guards_to_employees_table.php @@ -0,0 +1,49 @@ +get(); + foreach ($guardUsers as $user) { + $emp = Employee::where('user_id', $user->id)->orWhere('nik', $user->username)->first(); + if ($emp) { + $emp->update([ + 'user_id' => $user->id, + 'name' => $user->name, + 'unit_kerja' => $emp->unit_kerja ?: 'Private Guard', + 'division' => ($emp->division && $emp->division !== '-') ? $emp->division : 'Private Guard', + 'position' => ($emp->position && $emp->position !== '-') ? $emp->position : 'Anggota', + 'employee_type' => $emp->employee_type ?: 'Karyawan', + ]); + } else { + Employee::create([ + 'user_id' => $user->id, + 'nik' => $user->username, + 'name' => $user->name, + 'photo_path' => $user->photo_path, + 'employee_type' => 'Karyawan', + 'unit_kerja' => 'Private Guard', + 'division' => 'Private Guard', + 'position' => 'Anggota', + 'is_operator' => false, + ]); + } + } + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + // No destructive rollback needed + } +}; diff --git a/resources/views/admin/users/index.blade.php b/resources/views/admin/users/index.blade.php index 822f793..19c1785 100644 --- a/resources/views/admin/users/index.blade.php +++ b/resources/views/admin/users/index.blade.php @@ -348,7 +348,7 @@
- @foreach($roles as $role) @@ -369,7 +369,7 @@
- @@ -378,6 +378,7 @@ @@ -387,11 +388,11 @@
- +
- +
@@ -479,7 +480,7 @@
- @foreach($roles as $role) @endforeach @@ -685,6 +686,30 @@ } } + function handleUserRoleChange(role, mode) { + if (mode === 'edit') { + updatePermissionCheckboxes(); + } + const isGuard = (role === 'gate_officer' || role === 'pg_admin'); + const unitEl = document.getElementById(mode + '_unit_kerja'); + const divEl = document.getElementById(mode + '_division'); + const posEl = document.getElementById(mode + '_position'); + const empTypeEl = document.getElementById(mode + '_employee_type'); + + if (isGuard) { + if (empTypeEl) empTypeEl.value = 'karyawan'; + if (unitEl && (!unitEl.value || unitEl.value === 'Umum')) { + unitEl.value = 'Private Guard'; + } + if (divEl && (!divEl.value || divEl.value === '-')) { + divEl.value = 'Private Guard'; + } + if (posEl && (!posEl.value || posEl.value === '-')) { + posEl.value = (role === 'pg_admin') ? 'Admin PG' : 'Anggota'; + } + } + } + function updatePermissionCheckboxes() { const roleSelect = document.getElementById('edit_role'); const selectedRole = roleSelect ? roleSelect.value : ''; @@ -780,14 +805,19 @@ if (editGateEl) editGateEl.value = user.gate_id || ''; const emp = user.employee || {}; + const isGuard = (primaryRole === 'gate_officer' || primaryRole === 'pg_admin'); + const defaultUnit = isGuard ? 'Private Guard' : ''; + const defaultDivision = isGuard ? 'Private Guard' : ''; + const defaultPosition = isGuard ? (primaryRole === 'pg_admin' ? 'Admin PG' : 'Anggota') : ''; + const editEmployeeType = document.getElementById('edit_employee_type'); if (editEmployeeType) editEmployeeType.value = (emp.employee_type ? emp.employee_type.toLowerCase() : 'karyawan'); const editUnitKerja = document.getElementById('edit_unit_kerja'); - if (editUnitKerja) editUnitKerja.value = emp.unit_kerja || ''; + if (editUnitKerja) editUnitKerja.value = emp.unit_kerja || defaultUnit; const editDivision = document.getElementById('edit_division'); - if (editDivision) editDivision.value = emp.division || ''; + if (editDivision) editDivision.value = (emp.division && emp.division !== '-') ? emp.division : defaultDivision; const editPosition = document.getElementById('edit_position'); - if (editPosition) editPosition.value = emp.position || ''; + if (editPosition) editPosition.value = (emp.position && emp.position !== '-') ? emp.position : defaultPosition; const editIsOperator = document.getElementById('edit_is_operator'); if (editIsOperator) editIsOperator.value = (emp.is_operator ? '1' : '0'); diff --git a/resources/views/hse/employees_edit.blade.php b/resources/views/hse/employees_edit.blade.php index 8a450c0..0b4e9a4 100644 --- a/resources/views/hse/employees_edit.blade.php +++ b/resources/views/hse/employees_edit.blade.php @@ -138,6 +138,28 @@ class="w-full text-xs px-3.5 py-2.5 rounded-xl border border-slate-200 bg-white text-slate-800 font-semibold focus:outline-none focus:border-[#1b5e20] focus:ring-2 focus:ring-[#1b5e20]/10 transition">
+ +
+
+ + +
+
+ + +
+
+
+ +
+
+ + +
+
+ + +
+
+
@@ -1188,6 +1206,14 @@ if (editUnitKerjaInput) { editUnitKerjaInput.value = emp.unit_kerja || ''; } + const editDivisionInput = document.getElementById('editDivisionInput'); + if (editDivisionInput) { + editDivisionInput.value = emp.division || ''; + } + const editPositionInput = document.getElementById('editPositionInput'); + if (editPositionInput) { + editPositionInput.value = emp.position || ''; + } document.getElementById('editDivisionPositionDisplay').textContent = (emp.division || '-') + ' / ' + (emp.position || '-'); document.getElementById('editOperator').value = emp.is_operator ? '1' : '0'; diff --git a/tests/Feature/HseEmployeeFlexibleUnitKerjaAndModalTest.php b/tests/Feature/HseEmployeeFlexibleUnitKerjaAndModalTest.php index 2b056c3..d5c0076 100644 --- a/tests/Feature/HseEmployeeFlexibleUnitKerjaAndModalTest.php +++ b/tests/Feature/HseEmployeeFlexibleUnitKerjaAndModalTest.php @@ -312,4 +312,122 @@ class HseEmployeeFlexibleUnitKerjaAndModalTest extends TestCase $response->assertSessionHas('error'); $this->assertDatabaseHas('employees', ['id' => $employee->id]); } + + public function test_hse_edit_modal_and_edit_page_render_division_and_position_inputs(): void + { + $employee = Employee::create([ + 'nik' => 'KMH-DIV-POS-01', + 'name' => 'Hendro Siswanto', + 'employee_type' => 'Karyawan', + 'unit_kerja' => 'Operasi', + 'division' => 'Pembangkit Listrik', + 'position' => 'Supervisor Turbin', + 'is_operator' => false, + ]); + + // Verifikasi pada index editModal + $responseIndex = $this->actingAs($this->hseAdmin)->get(route('hse.employees.index')); + $responseIndex->assertStatus(200); + $contentIndex = $responseIndex->getContent(); + + $this->assertStringContainsString('name="division" id="editDivisionInput"', $contentIndex); + $this->assertStringContainsString('name="position" id="editPositionInput"', $contentIndex); + + // Verifikasi pada full page edit + $responseEdit = $this->actingAs($this->hseAdmin)->get(route('hse.employees.edit', $employee->id)); + $responseEdit->assertStatus(200); + $contentEdit = $responseEdit->getContent(); + + $this->assertStringContainsString('name="division"', $contentEdit); + $this->assertStringContainsString('name="position"', $contentEdit); + $this->assertStringContainsString('value="Pembangkit Listrik"', $contentEdit); + $this->assertStringContainsString('value="Supervisor Turbin"', $contentEdit); + } + + public function test_hse_can_update_employee_division_and_position(): void + { + $employee = Employee::create([ + 'nik' => 'KMH-DIV-POS-02', + 'name' => 'Siti Nurhaliza', + 'employee_type' => 'Karyawan', + 'unit_kerja' => 'Umum', + 'division' => 'Operasi', + 'position' => 'Staff', + 'is_operator' => false, + ]); + + $response = $this->actingAs($this->hseAdmin) + ->from(route('hse.employees.index')) + ->post(route('hse.employees.update', $employee->id), [ + 'unit_kerja' => 'K3 & Lingkungan', + 'division' => 'HSE', + 'position' => 'Officer K3', + 'is_operator' => '0', + ]); + + $response->assertSessionHasNoErrors(); + $employee->refresh(); + + $this->assertEquals('K3 & Lingkungan', $employee->unit_kerja); + $this->assertEquals('HSE', $employee->division); + $this->assertEquals('Officer K3', $employee->position); + } + + public function test_security_guard_and_gate_officer_auto_populates_private_guard_and_anggota(): void + { + // 1. Pembuatan via SecurityGuardController + $adminPg = User::factory()->create([ + 'username' => 'admin_pg_test', + 'role' => 'pg_admin', + ]); + $pgRole = Role::firstOrCreate(['name' => 'pg_admin']); + $adminPg->assignRole($pgRole); + + $responseGuard = $this->actingAs($adminPg) + ->post(route('pg.guards.store'), [ + 'badge_id' => 'SEC-999', + 'name' => 'Satpam Bambang', + 'team' => 1, + 'password' => 'secret123', + ]); + + $responseGuard->assertSessionHasNoErrors(); + + $this->assertDatabaseHas('employees', [ + 'nik' => 'SEC-999', + 'name' => 'Satpam Bambang', + 'unit_kerja' => 'Private Guard', + 'division' => 'Private Guard', + 'position' => 'Anggota', + ]); + + // 2. Pembuatan via Super Admin UserController + $superAdmin = User::factory()->create([ + 'username' => 'superadmin_pg_test', + 'role' => 'super_admin', + ]); + $superRole = Role::firstOrCreate(['name' => 'super_admin']); + $superAdmin->assignRole($superRole); + + Role::firstOrCreate(['name' => 'gate_officer']); + + $responseUser = $this->actingAs($superAdmin) + ->post(route('admin.users.store'), [ + 'name' => 'Satpam Wawan', + 'username' => 'SEC-888', + 'email' => 'sec888@kmh.co.id', + 'password' => 'password123', + 'role' => 'gate_officer', + ]); + + $responseUser->assertSessionHasNoErrors(); + + $this->assertDatabaseHas('employees', [ + 'nik' => 'SEC-888', + 'name' => 'Satpam Wawan', + 'unit_kerja' => 'Private Guard', + 'division' => 'Private Guard', + 'position' => 'Anggota', + ]); + } }