diff --git a/app/Http/Controllers/Employee/DashboardController.php b/app/Http/Controllers/Employee/DashboardController.php index c9a5295..bdf3661 100644 --- a/app/Http/Controllers/Employee/DashboardController.php +++ b/app/Http/Controllers/Employee/DashboardController.php @@ -221,10 +221,26 @@ class DashboardController extends Controller $employee = $user->getOrCreateEmployee(); - // 1. Periksa ketersediaan modul/tab berdasarkan wewenang (permission) & riwayat aktual karyawan - $canP2h = $user->can('employee.p2h') || ($employee && P2hRecord::where('employee_id', $employee->id)->exists()); - $canTimesheet = ($employee && $employee->is_operator) || ($employee && UnitSupportTimesheet::where('operator_id', $employee->id)->exists()) || $user->can('qma.logs.view'); - $canTrips = $user->can('employee.timesheet') || ($employee && VehicleActivity::where('driver_id', $employee->id)->exists()); + // 1. Cek riwayat aktual yang pernah dikerjakan oleh karyawan + $hasP2hRecords = $employee ? P2hRecord::where('employee_id', $employee->id)->exists() : false; + $hasTimesheetRecords = $employee ? UnitSupportTimesheet::where('operator_id', $employee->id)->exists() : false; + $hasTripRecords = $employee ? VehicleActivity::where('driver_id', $employee->id)->exists() : false; + + // 2. Cek kualifikasi SIMPER karyawan terhadap jenis armada (Heavy Equipment vs Light Vehicle) + $canHeavy = $user->isSuperAdmin() || ($employee && $employee->canOperateHeavyEquipment()); + $canLv = $user->isSuperAdmin() || ($employee && $employee->canDriveLightVehicle()); + + // 3. Modul P2H Heavy Equipment: + // Hanya tampil jika karyawan memiliki riwayat P2H ATAU (memiliki hak akses employee.p2h DAN izin SIMPER alat berat) + $canP2h = $hasP2hRecords || ($user->can('employee.p2h') && $canHeavy); + + // 4. Modul Timesheet Heavy Equipment: + // Hanya tampil jika karyawan memiliki riwayat timesheet ATAU (memiliki izin SIMPER alat berat dan status operator) + $canTimesheet = $hasTimesheetRecords || ($canHeavy && ($employee?->is_operator || $user->can('qma.logs.view'))); + + // 5. Modul Perjalanan Pos Gerbang (LV): + // Tampil jika karyawan memiliki riwayat perjalanan ATAU (memiliki hak akses employee.timesheet dan SIMPER LV) + $canTrips = $hasTripRecords || ($user->can('employee.timesheet') && ($canLv || !$canHeavy)); // Hitung total catatan riwayat per kategori untuk badge tab $p2hCount = $employee ? P2hRecord::where('employee_id', $employee->id)->count() : 0; diff --git a/app/Models/Employee.php b/app/Models/Employee.php index d68eaa9..fe0c6d4 100644 --- a/app/Models/Employee.php +++ b/app/Models/Employee.php @@ -206,6 +206,84 @@ class Employee extends Model return $this->hasValidSimper(); } + /** + * Cek apakah personil memiliki izin SIMPER aktif yang mencakup Heavy Equipment / Unit Support (Alat Berat atau Dump Truck). + */ + public function canOperateHeavyEquipment(): bool + { + $hasAnySimper = $this->relationLoaded('simpers') ? $this->simpers->isNotEmpty() : $this->simpers()->exists(); + + if ($hasAnySimper) { + $simpers = $this->relationLoaded('simpers') + ? $this->simpers + : $this->simpers()->with('simperType')->get(); + + foreach ($simpers as $s) { + if ($s->isValid() && $s->simperType) { + $type = $s->simperType; + // Cakupan Universal (Semua Armada) + if ($type->coversScope(SimperType::SCOPE_ALL)) { + return true; + } + // Cakupan Heavy Equipment atau Mobile Unit Support (Dump Truck) + if ($type->coversScope(SimperType::SCOPE_UNIT_SUPPORT_HEAVY) + || $type->coversScope(SimperType::SCOPE_UNIT_SUPPORT_MOBILE) + || $type->code === 'SIMPER-HEAVY' + || $type->code === 'SIMPER-DT') { + return true; + } + // Cakupan kategori spesifik (Excavator, Bulldozer, Crane, dll.) + foreach ($type->scopes as $scope) { + if ($scope !== SimperType::SCOPE_LIGHT_VEHICLE && $scope !== 'light_vehicle') { + return true; + } + } + } + } + + return false; + } + + // Fallback untuk driver/operator legacy: jika belum multi-simper, hanya jika ditandai is_operator + // dan posisinya mengandung kata 'operator', 'heavy', 'excavator', 'dt', dsb. + if ($this->is_operator && $this->hasValidSimper()) { + $pos = strtolower($this->position ?? ''); + if (str_contains($pos, 'operator') || str_contains($pos, 'alat berat') || str_contains($pos, 'excavator') || str_contains($pos, 'dt') || str_contains($pos, 'dump truck')) { + return true; + } + } + + return false; + } + + /** + * Cek apakah personil memiliki izin SIMPER aktif untuk kendaraan ringan (Light Vehicle). + */ + public function canDriveLightVehicle(): bool + { + $hasAnySimper = $this->relationLoaded('simpers') ? $this->simpers->isNotEmpty() : $this->simpers()->exists(); + + if ($hasAnySimper) { + $simpers = $this->relationLoaded('simpers') + ? $this->simpers + : $this->simpers()->with('simperType')->get(); + + foreach ($simpers as $s) { + if ($s->isValid() && $s->simperType) { + if ($s->simperType->coversScope(SimperType::SCOPE_ALL) + || $s->simperType->coversScope(SimperType::SCOPE_LIGHT_VEHICLE) + || $s->simperType->code === 'SIMPER-LV') { + return true; + } + } + } + + return false; + } + + return $this->hasValidSimper(); + } + public function getPhotoUrlAttribute(): ?string { $path = $this->photo_path; diff --git a/tests/Feature/EmployeeHistoryTest.php b/tests/Feature/EmployeeHistoryTest.php index 3811ad5..c5a6194 100644 --- a/tests/Feature/EmployeeHistoryTest.php +++ b/tests/Feature/EmployeeHistoryTest.php @@ -92,6 +92,30 @@ class EmployeeHistoryTest extends TestCase 'is_active' => true, ] ); + + // Berikan SIMPER Heavy Equipment untuk Operator A & B + $simperHeavy = \App\Models\SimperType::firstOrCreate( + ['code' => 'SIMPER-HEAVY'], + [ + 'name' => 'Heavy Equipment', + 'scopes' => ['unit_support_heavy', 'Excavator'], + 'is_active' => true, + ] + ); + + \App\Models\EmployeeSimper::create([ + 'employee_id' => $this->empA->id, + 'simper_type_id' => $simperHeavy->id, + 'status' => 'active', + 'expired_at' => Carbon::now()->addMonths(6), + ]); + + \App\Models\EmployeeSimper::create([ + 'employee_id' => $this->empB->id, + 'simper_type_id' => $simperHeavy->id, + 'status' => 'active', + 'expired_at' => Carbon::now()->addMonths(6), + ]); } public function test_guest_is_redirected_to_login(): void @@ -335,4 +359,40 @@ class EmployeeHistoryTest extends TestCase $resPdf = $this->actingAs($this->userA)->get(route('employee.history.p2h_pdf', $recordB->id)); $resPdf->assertStatus(403); } + + public function test_employee_with_only_lv_simper_does_not_see_heavy_equipment(): void + { + $simperLv = \App\Models\SimperType::firstOrCreate( + ['code' => 'SIMPER-LV'], + [ + 'name' => 'Light Vehicle Only', + 'scopes' => ['light_vehicle'], + 'is_active' => true, + ] + ); + + $userDriver = User::create([ + 'name' => 'Driver Doni LV Only', + 'username' => 'driver_doni', + 'email' => 'doni@kerinci.local', + 'password' => Hash::make('password123'), + 'role' => 'employee', + ]); + $empDriver = $userDriver->getOrCreateEmployee(); + $empDriver->update(['is_operator' => true]); + + \App\Models\EmployeeSimper::create([ + 'employee_id' => $empDriver->id, + 'simper_type_id' => $simperLv->id, + 'status' => 'active', + 'expired_at' => Carbon::now()->addMonths(6), + ]); + + $response = $this->actingAs($userDriver)->get(route('employee.history')); + $response->assertStatus(200); + $response->assertSee('Mobilisasi'); + $response->assertSee('Pos Gerbang'); + $response->assertDontSee('P2H Heavy Equipment'); + $response->assertDontSee('Timesheet Heavy Equipment'); + } }