fix(employee): synchronize division & position fields across HSE and Admin, auto-populate Private Guard & Anggota for guards

This commit is contained in:
Ray1111-aa
2026-10-28 08:47:02 +07:00
parent 0642caebc1
commit 49359d1ba1
7 changed files with 319 additions and 21 deletions
+35 -13
View File
@@ -52,7 +52,7 @@ class UserController extends Controller
default => $query->latest('created_at'), 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(); $roles = Role::with('permissions')->orderBy('name')->get();
$rolePermissionsMap = $roles->mapWithKeys(function ($r) { $rolePermissionsMap = $roles->mapWithKeys(function ($r) {
return [$r->name => $r->permissions->pluck('name')->toArray()]; return [$r->name => $r->permissions->pluck('name')->toArray()];
@@ -126,23 +126,30 @@ class UserController extends Controller
// Reset permission cache Spatie // Reset permission cache Spatie
app()[PermissionRegistrar::class]->forgetCachedPermissions(); 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'; $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'] !== ''); $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'] !== '') $isOperator = (isset($validated['is_operator']) && $validated['is_operator'] !== '')
? (bool) $validated['is_operator'] ? (bool) $validated['is_operator']
: ($isEmployeeRole ? true : false); : ($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(); $emp = Employee::where('user_id', $user->id)->orWhere('nik', $user->username)->first();
$empData = [ $empData = [
'user_id' => $user->id, 'user_id' => $user->id,
'nik' => $user->username, 'nik' => $user->username,
'name' => $user->name, 'name' => $user->name,
'employee_type' => !empty($validated['employee_type']) ? ucfirst(strtolower($validated['employee_type'])) : ($emp?->employee_type ?? 'Karyawan'), 'employee_type' => !empty($validated['employee_type']) ? ucfirst(strtolower($validated['employee_type'])) : ($emp?->employee_type ?? 'Karyawan'),
'unit_kerja' => $validated['unit_kerja'] ?? ($emp?->unit_kerja ?? 'Umum'), 'unit_kerja' => !empty($validated['unit_kerja']) ? $validated['unit_kerja'] : ($emp?->unit_kerja ?: $defaultUnit),
'division' => $validated['division'] ?? ($emp?->division ?? '-'), 'division' => !empty($validated['division']) ? $validated['division'] : ($emp?->division ?: $defaultDivision),
'position' => $validated['position'] ?? ($emp?->position ?? ($isEmployeeRole ? 'Driver Operasional' : '-')), 'position' => !empty($validated['position']) ? $validated['position'] : ($emp?->position ?: $defaultPosition),
'is_operator' => $isOperator, 'is_operator' => $isOperator,
'photo_path' => $photoPath ?: $emp?->photo_path, 'photo_path' => $photoPath ?: $emp?->photo_path,
]; ];
@@ -248,9 +255,15 @@ class UserController extends Controller
// Sinkronkan data master Employee // Sinkronkan data master Employee
$emp = $user->employee ?: Employee::where('nik', $user->username)->first(); $emp = $user->employee ?: Employee::where('nik', $user->username)->first();
$isEmployeeRole = $user->isEmployee() || $validated['role'] === 'employee'; $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'] !== ''); $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 = [ $empData = [
'name' => $user->name, 'name' => $user->name,
'nik' => $user->username, 'nik' => $user->username,
@@ -259,15 +272,24 @@ class UserController extends Controller
if (array_key_exists('employee_type', $validated) && $validated['employee_type'] !== null) { if (array_key_exists('employee_type', $validated) && $validated['employee_type'] !== null) {
$empData['employee_type'] = ucfirst(strtolower($validated['employee_type'])); $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']; $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']; $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']; $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'] !== '') { if (array_key_exists('is_operator', $validated) && $validated['is_operator'] !== null && $validated['is_operator'] !== '') {
$empData['is_operator'] = (bool) $validated['is_operator']; $empData['is_operator'] = (bool) $validated['is_operator'];
} }
@@ -280,9 +302,9 @@ class UserController extends Controller
} else { } else {
$empData['user_id'] = $user->id; $empData['user_id'] = $user->id;
$empData['employee_type'] = $empData['employee_type'] ?? 'Karyawan'; $empData['employee_type'] = $empData['employee_type'] ?? 'Karyawan';
$empData['unit_kerja'] = $empData['unit_kerja'] ?? 'Umum'; $empData['unit_kerja'] = $empData['unit_kerja'] ?? $defaultUnit;
$empData['division'] = $empData['division'] ?? '-'; $empData['division'] = $empData['division'] ?? $defaultDivision;
$empData['position'] = $empData['position'] ?? ($isEmployeeRole ? 'Driver Operasional' : '-'); $empData['position'] = $empData['position'] ?? $defaultPosition;
$empData['is_operator'] = $empData['is_operator'] ?? ($isEmployeeRole ? true : false); $empData['is_operator'] = $empData['is_operator'] ?? ($isEmployeeRole ? true : false);
Employee::create($empData); Employee::create($empData);
} }
@@ -3,6 +3,7 @@
namespace App\Http\Controllers\Pg; namespace App\Http\Controllers\Pg;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\Employee;
use App\Models\Gate; use App\Models\Gate;
use App\Models\SecurityGuard; use App\Models\SecurityGuard;
use App\Models\User; use App\Models\User;
@@ -255,6 +256,20 @@ class SecurityGuardController extends Controller
'user_id' => $user->id, 'user_id' => $user->id,
'is_active' => true, '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."); 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); $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."); return back()->with('success', "Data petugas Satpam {$guard->name} berhasil diperbarui.");
@@ -333,11 +359,16 @@ class SecurityGuardController extends Controller
DB::transaction(function () use ($guard) { DB::transaction(function () use ($guard) {
$user = $guard->user; $user = $guard->user;
$badgeId = $guard->badge_id;
$userId = $guard->user_id;
$guard->delete(); $guard->delete();
if ($user) { if ($user) {
$user->delete(); $user->delete();
} }
Employee::where('user_id', $userId)->orWhere('nik', $badgeId)->delete();
}); });
return back()->with('success', "Data petugas Satpam {$guardName} ({$badgeId}) berhasil dihapus."); return back()->with('success', "Data petugas Satpam {$guardName} ({$badgeId}) berhasil dihapus.");
@@ -0,0 +1,49 @@
<?php
use App\Models\Employee;
use App\Models\User;
use Illuminate\Database\Migrations\Migration;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
$guardUsers = User::where('role', 'gate_officer')->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
}
};
+38 -8
View File
@@ -348,7 +348,7 @@
<div> <div>
<label class="block text-xs font-bold text-slate-700 mb-1">Role RBAC *</label> <label class="block text-xs font-bold text-slate-700 mb-1">Role RBAC *</label>
<select name="role" required class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white"> <select name="role" id="create_role" onchange="handleUserRoleChange(this.value, 'create')" required class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white">
<option value="">-- Pilih Role --</option> <option value="">-- Pilih Role --</option>
@foreach($roles as $role) @foreach($roles as $role)
<option value="{{ $role->name }}">{{ ucwords(str_replace('_', ' ', $role->name)) }}</option> <option value="{{ $role->name }}">{{ ucwords(str_replace('_', ' ', $role->name)) }}</option>
@@ -369,7 +369,7 @@
<div class="grid grid-cols-2 gap-3"> <div class="grid grid-cols-2 gap-3">
<div> <div>
<label class="block text-[11px] font-semibold text-slate-600 mb-1">Jenis Personil</label> <label class="block text-[11px] font-semibold text-slate-600 mb-1">Jenis Personil</label>
<select name="employee_type" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white text-slate-700"> <select name="employee_type" id="create_employee_type" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white text-slate-700">
<option value="karyawan">Karyawan (Internal KMH)</option> <option value="karyawan">Karyawan (Internal KMH)</option>
<option value="sukon">Sukon (Subkontraktor)</option> <option value="sukon">Sukon (Subkontraktor)</option>
</select> </select>
@@ -378,6 +378,7 @@
<label class="block text-[11px] font-semibold text-slate-600 mb-1">Unit Kerja</label> <label class="block text-[11px] font-semibold text-slate-600 mb-1">Unit Kerja</label>
<input type="text" <input type="text"
name="unit_kerja" name="unit_kerja"
id="create_unit_kerja"
list="adminUnitKerjaSuggestions" list="adminUnitKerjaSuggestions"
class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white text-slate-700" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white text-slate-700"
placeholder="Ketik atau pilih unit kerja..."> placeholder="Ketik atau pilih unit kerja...">
@@ -387,11 +388,11 @@
<div class="grid grid-cols-2 gap-3"> <div class="grid grid-cols-2 gap-3">
<div> <div>
<label class="block text-[11px] font-semibold text-slate-600 mb-1">Divisi</label> <label class="block text-[11px] font-semibold text-slate-600 mb-1">Divisi</label>
<input type="text" name="division" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white text-slate-700" placeholder="Contoh: Operasi / Keuangan"> <input type="text" name="division" id="create_division" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white text-slate-700" placeholder="Contoh: Operasi / Keuangan">
</div> </div>
<div> <div>
<label class="block text-[11px] font-semibold text-slate-600 mb-1">Posisi / Jabatan</label> <label class="block text-[11px] font-semibold text-slate-600 mb-1">Posisi / Jabatan</label>
<input type="text" name="position" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white text-slate-700" placeholder="Contoh: Driver / Staff Finance"> <input type="text" name="position" id="create_position" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white text-slate-700" placeholder="Contoh: Driver / Staff Finance">
</div> </div>
</div> </div>
@@ -479,7 +480,7 @@
<div> <div>
<label class="block text-xs font-bold text-slate-700 mb-1">Role RBAC *</label> <label class="block text-xs font-bold text-slate-700 mb-1">Role RBAC *</label>
<select id="edit_role" name="role" required onchange="updatePermissionCheckboxes()" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white"> <select id="edit_role" name="role" required onchange="handleUserRoleChange(this.value, 'edit')" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white">
@foreach($roles as $role) @foreach($roles as $role)
<option value="{{ $role->name }}">{{ ucwords(str_replace('_', ' ', $role->name)) }}</option> <option value="{{ $role->name }}">{{ ucwords(str_replace('_', ' ', $role->name)) }}</option>
@endforeach @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() { function updatePermissionCheckboxes() {
const roleSelect = document.getElementById('edit_role'); const roleSelect = document.getElementById('edit_role');
const selectedRole = roleSelect ? roleSelect.value : ''; const selectedRole = roleSelect ? roleSelect.value : '';
@@ -780,14 +805,19 @@
if (editGateEl) editGateEl.value = user.gate_id || ''; if (editGateEl) editGateEl.value = user.gate_id || '';
const emp = user.employee || {}; 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'); const editEmployeeType = document.getElementById('edit_employee_type');
if (editEmployeeType) editEmployeeType.value = (emp.employee_type ? emp.employee_type.toLowerCase() : 'karyawan'); if (editEmployeeType) editEmployeeType.value = (emp.employee_type ? emp.employee_type.toLowerCase() : 'karyawan');
const editUnitKerja = document.getElementById('edit_unit_kerja'); 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'); 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'); 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'); const editIsOperator = document.getElementById('edit_is_operator');
if (editIsOperator) editIsOperator.value = (emp.is_operator ? '1' : '0'); if (editIsOperator) editIsOperator.value = (emp.is_operator ? '1' : '0');
@@ -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"> 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">
</div> </div>
<!-- Divisi & Jabatan -->
<div class="grid grid-cols-1 sm:grid-cols-2 gap-3">
<div>
<label class="block text-xs font-bold text-slate-800 mb-1.5">
Divisi <span class="text-rose-500">*</span>
</label>
<input type="text" name="division" required
value="{{ old('division', $employee->division) }}"
placeholder="Contoh: Operasi, Private Guard"
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">
</div>
<div>
<label class="block text-xs font-bold text-slate-800 mb-1.5">
Posisi / Jabatan <span class="text-rose-500">*</span>
</label>
<input type="text" name="position" required
value="{{ old('position', $employee->position) }}"
placeholder="Contoh: Anggota, Driver, Staff"
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">
</div>
</div>
<!-- Status Operator / Driver --> <!-- Status Operator / Driver -->
<div> <div>
<label class="block text-xs font-bold text-slate-800 mb-1.5"> <label class="block text-xs font-bold text-slate-800 mb-1.5">
@@ -789,6 +789,24 @@
class="w-full text-xs px-3 py-2 rounded-xl border border-slate-200 bg-white text-slate-800 font-semibold focus:outline-none focus:border-[#1b5e20] transition"> class="w-full text-xs px-3 py-2 rounded-xl border border-slate-200 bg-white text-slate-800 font-semibold focus:outline-none focus:border-[#1b5e20] transition">
</div> </div>
<!-- Divisi & Jabatan -->
<div class="grid grid-cols-2 gap-2.5">
<div>
<label class="block text-xs font-bold text-slate-800 mb-1">
Divisi <span class="text-rose-500">*</span>
</label>
<input type="text" name="division" id="editDivisionInput" required placeholder="Contoh: Operasi, Private Guard"
class="w-full text-xs px-3 py-2 rounded-xl border border-slate-200 bg-white text-slate-800 font-semibold focus:outline-none focus:border-[#1b5e20] transition">
</div>
<div>
<label class="block text-xs font-bold text-slate-800 mb-1">
Posisi / Jabatan <span class="text-rose-500">*</span>
</label>
<input type="text" name="position" id="editPositionInput" required placeholder="Contoh: Anggota, Driver, Staff"
class="w-full text-xs px-3 py-2 rounded-xl border border-slate-200 bg-white text-slate-800 font-semibold focus:outline-none focus:border-[#1b5e20] transition">
</div>
</div>
<!-- Status Operator --> <!-- Status Operator -->
<div> <div>
<label class="block text-xs font-bold text-slate-800 mb-1">Status Operator / Driver <span class="text-rose-500">*</span></label> <label class="block text-xs font-bold text-slate-800 mb-1">Status Operator / Driver <span class="text-rose-500">*</span></label>
@@ -1188,6 +1206,14 @@
if (editUnitKerjaInput) { if (editUnitKerjaInput) {
editUnitKerjaInput.value = emp.unit_kerja || ''; 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('editDivisionPositionDisplay').textContent = (emp.division || '-') + ' / ' + (emp.position || '-');
document.getElementById('editOperator').value = emp.is_operator ? '1' : '0'; document.getElementById('editOperator').value = emp.is_operator ? '1' : '0';
@@ -312,4 +312,122 @@ class HseEmployeeFlexibleUnitKerjaAndModalTest extends TestCase
$response->assertSessionHas('error'); $response->assertSessionHas('error');
$this->assertDatabaseHas('employees', ['id' => $employee->id]); $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' => '[email protected]',
'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',
]);
}
} }