fix(employee): strictly filter heavy equipment history tabs by active SIMPER scope (hide heavy tabs for LV-only drivers)

This commit is contained in:
Ray1111-aa
2026-10-28 15:07:27 +07:00
parent 2c959ca392
commit 48678df82b
3 changed files with 158 additions and 4 deletions
@@ -221,10 +221,26 @@ class DashboardController extends Controller
$employee = $user->getOrCreateEmployee(); $employee = $user->getOrCreateEmployee();
// 1. Periksa ketersediaan modul/tab berdasarkan wewenang (permission) & riwayat aktual karyawan // 1. Cek riwayat aktual yang pernah dikerjakan oleh karyawan
$canP2h = $user->can('employee.p2h') || ($employee && P2hRecord::where('employee_id', $employee->id)->exists()); $hasP2hRecords = $employee ? P2hRecord::where('employee_id', $employee->id)->exists() : false;
$canTimesheet = ($employee && $employee->is_operator) || ($employee && UnitSupportTimesheet::where('operator_id', $employee->id)->exists()) || $user->can('qma.logs.view'); $hasTimesheetRecords = $employee ? UnitSupportTimesheet::where('operator_id', $employee->id)->exists() : false;
$canTrips = $user->can('employee.timesheet') || ($employee && VehicleActivity::where('driver_id', $employee->id)->exists()); $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 // Hitung total catatan riwayat per kategori untuk badge tab
$p2hCount = $employee ? P2hRecord::where('employee_id', $employee->id)->count() : 0; $p2hCount = $employee ? P2hRecord::where('employee_id', $employee->id)->count() : 0;
+78
View File
@@ -206,6 +206,84 @@ class Employee extends Model
return $this->hasValidSimper(); 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 public function getPhotoUrlAttribute(): ?string
{ {
$path = $this->photo_path; $path = $this->photo_path;
+60
View File
@@ -92,6 +92,30 @@ class EmployeeHistoryTest extends TestCase
'is_active' => true, '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 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 = $this->actingAs($this->userA)->get(route('employee.history.p2h_pdf', $recordB->id));
$resPdf->assertStatus(403); $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' => '[email protected]',
'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');
}
} }