feat(timesheet): personalize mobile catalog based on employee active simpers and reduce icon clutter

This commit is contained in:
Ray1111-aa
2026-10-27 08:58:16 +07:00
parent 75c830c75d
commit 8ff273fa29
5 changed files with 291 additions and 70 deletions
@@ -24,9 +24,112 @@ class VehicleActivityFormController extends Controller
*/
public function index(Request $request): View
{
$type = $request->input('type', 'all');
$type = $request->input('type');
$query = Vehicle::timesheetEligible();
$user = auth()->user();
$employee = null;
$isPrivilegedUser = false;
$activeSimpers = collect();
$allowedScopes = [];
$simperTypeIds = [];
$hasUniversalScope = false;
$canDriveLv = false;
$heavyScopes = [];
$isRestrictedEmployee = false;
$noActiveSimper = false;
if ($user) {
$employee = $user->getOrCreateEmployee();
// Admin privileged (Super Admin, QMA, Pos Gerbang) dapat memantau seluruh armada
$isPrivilegedUser = $user->role === 'super-admin'
|| $user->hasRole('super-admin')
|| $user->can('qma.vehicles.view')
|| $user->can('qma.logs.view');
// Pengemudi / karyawan biasa dibatasi hanya pada armada sesuai izin SIMPER aktif miliknya
if (! $isPrivilegedUser && $employee) {
$isRestrictedEmployee = true;
$hasAnySimperRecord = $employee->simpers()->exists();
if ($hasAnySimperRecord) {
$activeSimpers = $employee->simpers()->with('simperType')->get()->filter(fn($s) => $s->isValid());
} elseif ($employee->is_operator && $employee->hasValidSimper()) {
// Fallback driver legacy sebelum ada multi-SIMPER
$allowedScopes[] = 'light_vehicle';
$canDriveLv = true;
}
if ($activeSimpers->isNotEmpty()) {
foreach ($activeSimpers as $s) {
if ($s->simperType) {
$simperTypeIds[] = $s->simperType->id;
$scopes = $s->simperType->scopes;
foreach ($scopes as $sc) {
$allowedScopes[] = $sc;
if ($sc === \App\Models\SimperType::SCOPE_ALL || $sc === 'all') {
$hasUniversalScope = true;
}
}
}
}
$allowedScopes = array_unique($allowedScopes);
$canDriveLv = in_array(\App\Models\SimperType::SCOPE_LIGHT_VEHICLE, $allowedScopes, true)
|| in_array('light_vehicle', $allowedScopes, true);
$heavyScopes = array_values(array_diff($allowedScopes, [\App\Models\SimperType::SCOPE_LIGHT_VEHICLE, 'light_vehicle']));
}
if (! $hasUniversalScope) {
if (empty($allowedScopes)) {
// Karyawan belum memiliki SIMPER aktif sama sekali
$noActiveSimper = true;
$query->whereRaw('1 = 0');
} else {
// Sesuaikan default tab type jika karyawan hanya berizin satu jenis
if ($canDriveLv && empty($heavyScopes)) {
if (! $type || $type === 'all' || $type === 'unit_support') {
$type = 'light_vehicle';
}
} elseif (! $canDriveLv && ! empty($heavyScopes)) {
if (! $type || $type === 'all' || $type === 'light_vehicle') {
$type = 'unit_support';
}
}
// Filter armada yang diizinkan sesuai SIMPER
$query->where(function ($subQ) use ($canDriveLv, $heavyScopes, $simperTypeIds) {
if ($canDriveLv) {
$subQ->orWhere('category', 'light_vehicle');
}
if (! empty($heavyScopes)) {
$subQ->orWhere(function ($hq) use ($heavyScopes, $simperTypeIds) {
$hq->where('category', 'unit_support')
->where('is_timesheet_mobile', true)
->where(function ($filterQ) use ($heavyScopes, $simperTypeIds) {
$filterQ->whereIn('unit_category', $heavyScopes)
->orWhereIn('brand_type', $heavyScopes);
if (! empty($simperTypeIds)) {
$filterQ->orWhereIn('required_simper_type_id', $simperTypeIds);
}
if (in_array(\App\Models\SimperType::SCOPE_UNIT_SUPPORT_MOBILE, $heavyScopes, true)
|| in_array('SIMPER-DT', $heavyScopes, true)
|| in_array('unit_support_mobile', $heavyScopes, true)) {
$filterQ->orWhere('is_timesheet_mobile', true);
}
});
});
}
});
}
}
}
}
// Filter tab kategori eksplisit
$type = $type ?? 'all';
if ($type === 'light_vehicle') {
$query->where('category', 'light_vehicle');
} elseif ($type === 'unit_support') {
@@ -44,7 +147,7 @@ class VehicleActivityFormController extends Controller
->orWhere('ownership', 'like', "%{$search}%")
->orWhere('unit_category', 'like', "%{$search}%");
if (!empty($cleanSearch)) {
if (! empty($cleanSearch)) {
$q->orWhereRaw("REPLACE(REPLACE(code, ' ', ''), '-', '') LIKE ?", ["%{$cleanSearch}%"])
->orWhereRaw("REPLACE(REPLACE(license_plate, ' ', ''), '-', '') LIKE ?", ["%{$cleanSearch}%"]);
}
@@ -53,12 +156,17 @@ class VehicleActivityFormController extends Controller
$vehicles = $query->orderBy('code')->paginate(24)->withQueryString();
$employee = null;
if (auth()->check()) {
$employee = auth()->user()->getOrCreateEmployee();
}
return view('mobilisasi.index', compact('vehicles', 'employee', 'type'));
return view('mobilisasi.index', compact(
'vehicles',
'employee',
'type',
'canDriveLv',
'heavyScopes',
'isRestrictedEmployee',
'noActiveSimper',
'activeSimpers',
'hasUniversalScope'
));
}
/**
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -1,6 +1,6 @@
{
"resources/css/app.css": {
"file": "assets/app-DNNr8G8T.css",
"file": "assets/app-Dz2d26Cd.css",
"src": "resources/css/app.css",
"isEntry": true,
"name": "app",
+83 -52
View File
@@ -4,52 +4,75 @@
@section('content')
<div class="w-full space-y-6">
<!-- Header Halaman Simple Kayak Satpam (Seamless, Tanpa Card Wrapper) -->
<!-- Header Halaman Minimalis (Clean Typography, Tanpa Ikon Berlebihan) -->
<div class="flex flex-col sm:flex-row sm:items-center justify-between gap-3 pb-3 border-b border-slate-200/80">
<div class="min-w-0">
<div class="flex items-center flex-wrap gap-2.5">
<div class="flex items-center flex-wrap gap-2">
<h1 class="text-lg sm:text-xl font-black text-slate-900 tracking-tight">
Timesheet Kendaraan Ringan & Armada Mobile
</h1>
<span class="px-2.5 py-1 rounded-lg text-xs font-black bg-emerald-50 text-emerald-800 border border-emerald-200">
Kendaraan Ringan (LV) & Heavy Mobile
<span class="px-2 py-0.5 rounded-md text-[11px] font-bold bg-slate-100 text-slate-700 border border-slate-200">
Katalog Mobilisasi
</span>
</div>
<div class="text-sm sm:text-base text-slate-600 font-medium truncate mt-1 flex items-center flex-wrap gap-1.5">
<div class="text-xs sm:text-sm text-slate-600 font-medium truncate mt-1 flex items-center flex-wrap gap-2">
<span>Pengemudi: <strong class="text-slate-900 font-bold">{{ $employee ? $employee->name : auth()->user()->name }}</strong></span>
@if($employee && $employee->nik)
<span class="text-slate-500 font-mono text-xs sm:text-sm font-semibold">({{ $employee->nik }})</span>
<span class="text-slate-400 font-mono text-xs">({{ $employee->nik }})</span>
@endif
@if($employee && $employee->hasValidSimper())
<span class="inline-flex items-center text-[10px] font-bold text-emerald-700 bg-emerald-100/80 px-2 py-0.5 rounded-md ml-1">
<i class="fa-solid fa-circle-check mr-1 text-emerald-600"></i> SIMPER Aktif
@if(!empty($activeSimpers) && $activeSimpers->isNotEmpty())
<span class="text-slate-300">|</span>
<span class="text-slate-500 text-xs">SIMPER:</span>
@foreach($activeSimpers as $as)
<span class="px-2 py-0.5 rounded text-[10px] font-bold bg-emerald-50 text-emerald-800 border border-emerald-200">
{{ $as->simperType ? $as->simperType->name : 'SIMPER Aktif' }}
</span>
@endforeach
@elseif($employee && $employee->hasValidSimper())
<span class="px-2 py-0.5 rounded text-[10px] font-bold bg-emerald-50 text-emerald-800 border border-emerald-200">
SIMPER Aktif
</span>
@elseif($employee)
<span class="inline-flex items-center text-[10px] font-bold text-rose-700 bg-rose-100/80 px-2 py-0.5 rounded-md ml-1">
<i class="fa-solid fa-circle-xmark mr-1 text-rose-600"></i> SIMPER Exp
<span class="px-2 py-0.5 rounded text-[10px] font-bold bg-amber-50 text-amber-800 border border-amber-200">
Belum Memiliki SIMPER
</span>
@endif
</div>
</div>
</div>
<!-- Segmented Filter Tab: Semua Armada / LV / Heavy Mobile -->
<div class="flex items-center space-x-1 sm:space-x-2 border-b border-slate-200/80 overflow-x-auto pb-px">
<a href="{{ route('mobilisasi.index', array_filter(['search' => request('search'), 'type' => 'all'])) }}"
class="px-4 py-2 text-xs font-bold rounded-t-xl transition whitespace-nowrap {{ ($type ?? 'all') === 'all' ? 'bg-[#1b5e20] text-white shadow-xs' : 'text-slate-600 hover:text-slate-900 hover:bg-slate-100' }}">
<i class="fa-solid fa-layer-group mr-1.5"></i>Semua Armada
</a>
<a href="{{ route('mobilisasi.index', array_filter(['search' => request('search'), 'type' => 'light_vehicle'])) }}"
class="px-4 py-2 text-xs font-bold rounded-t-xl transition whitespace-nowrap {{ ($type ?? '') === 'light_vehicle' ? 'bg-[#1b5e20] text-white shadow-xs' : 'text-slate-600 hover:text-slate-900 hover:bg-slate-100' }}">
<i class="fa-solid fa-car-side mr-1.5"></i>Light Vehicle (LV)
</a>
<a href="{{ route('mobilisasi.index', array_filter(['search' => request('search'), 'type' => 'unit_support'])) }}"
class="px-4 py-2 text-xs font-bold rounded-t-xl transition whitespace-nowrap {{ ($type ?? '') === 'unit_support' ? 'bg-[#1b5e20] text-white shadow-xs' : 'text-slate-600 hover:text-slate-900 hover:bg-slate-100' }}">
<i class="fa-solid fa-truck-moving mr-1.5"></i>Heavy Equipment Mobile
</a>
</div>
@php
$showAllTab = !($isRestrictedEmployee ?? false) || ($hasUniversalScope ?? false) || (($canDriveLv ?? false) && !empty($heavyScopes ?? []));
$showLvTab = !($isRestrictedEmployee ?? false) || ($hasUniversalScope ?? false) || ($canDriveLv ?? false);
$showHeavyTab = !($isRestrictedEmployee ?? false) || ($hasUniversalScope ?? false) || !empty($heavyScopes ?? []);
@endphp
<!-- Filter & Search Bar (Tanpa Card Wrapper, Format Referensi Sesuai Standar) -->
@if($showAllTab || ($showLvTab && $showHeavyTab))
<!-- Segmented Filter Tab: Semua Armada / LV / Heavy Mobile (Minimalis Tanpa Ikon) -->
<div class="flex items-center space-x-1 sm:space-x-2 border-b border-slate-200/80 overflow-x-auto pb-px">
@if($showAllTab)
<a href="{{ route('mobilisasi.index', array_filter(['search' => request('search'), 'type' => 'all'])) }}"
class="px-4 py-2 text-xs font-bold rounded-t-xl transition whitespace-nowrap {{ ($type ?? 'all') === 'all' ? 'bg-[#1b5e20] text-white shadow-xs' : 'text-slate-600 hover:text-slate-900 hover:bg-slate-100' }}">
Semua Armada
</a>
@endif
@if($showLvTab)
<a href="{{ route('mobilisasi.index', array_filter(['search' => request('search'), 'type' => 'light_vehicle'])) }}"
class="px-4 py-2 text-xs font-bold rounded-t-xl transition whitespace-nowrap {{ ($type ?? '') === 'light_vehicle' ? 'bg-[#1b5e20] text-white shadow-xs' : 'text-slate-600 hover:text-slate-900 hover:bg-slate-100' }}">
Light Vehicle (LV)
</a>
@endif
@if($showHeavyTab)
<a href="{{ route('mobilisasi.index', array_filter(['search' => request('search'), 'type' => 'unit_support'])) }}"
class="px-4 py-2 text-xs font-bold rounded-t-xl transition whitespace-nowrap {{ ($type ?? '') === 'unit_support' ? 'bg-[#1b5e20] text-white shadow-xs' : 'text-slate-600 hover:text-slate-900 hover:bg-slate-100' }}">
Heavy Equipment Mobile
</a>
@endif
</div>
@endif
<!-- Filter & Search Bar (Format Bersih Sesuai Standar) -->
<form method="GET" action="{{ route('mobilisasi.index') }}" id="lvFilterForm" class="flex flex-col sm:flex-row items-stretch sm:items-center justify-between gap-2.5 sm:gap-3">
<input type="hidden" name="type" value="{{ $type ?? 'all' }}">
<!-- Pencarian Cepat di Kiri dengan Tombol Cari -->
@@ -89,15 +112,15 @@
@forelse($vehicles as $v)
<div class="lv-card bg-white rounded-2xl border border-slate-200 shadow-sm hover:shadow-md transition p-5 flex flex-col justify-between">
<div>
<!-- Badge Kategori & Status -->
<!-- Badge Kategori & Status (Minimalis) -->
<div class="flex items-center justify-between mb-3">
@if($v->isLightVehicle())
<span class="inline-flex items-center px-2.5 py-1 rounded-lg text-[11px] font-bold bg-emerald-50 text-emerald-800 border border-emerald-200">
<i class="fa-solid fa-car-side mr-1 text-emerald-600"></i> LV
<span class="inline-flex items-center px-2 py-0.5 rounded-md text-[11px] font-bold bg-emerald-50 text-emerald-800 border border-emerald-200">
LV
</span>
@else
<span class="inline-flex items-center px-2.5 py-1 rounded-lg text-[11px] font-bold bg-amber-50 text-amber-800 border border-amber-200">
<i class="fa-solid fa-truck-moving mr-1 text-amber-600"></i> Heavy Mobile
<span class="inline-flex items-center px-2 py-0.5 rounded-md text-[11px] font-bold bg-amber-50 text-amber-800 border border-amber-200">
Heavy Mobile
</span>
@endif
@@ -147,7 +170,6 @@
<div class="mt-5 pt-3 border-t border-slate-100">
@if($v->status === 'Breakdown')
<div class="w-full bg-rose-50 border border-rose-200 text-rose-700 font-bold py-2.5 px-4 rounded-xl text-center text-xs">
<i class="fa-solid fa-triangle-exclamation mr-1.5"></i>
<span>Unit Sedang Breakdown</span>
</div>
@else
@@ -164,26 +186,35 @@
</div>
</div>
@empty
<div id="lvServerEmptyCard" class="col-span-full bg-white rounded-2xl border border-slate-200 p-12 text-center">
<div class="w-16 h-16 rounded-full bg-slate-100 text-slate-400 flex items-center justify-center mx-auto mb-3">
<i class="fa-solid fa-magnifying-glass text-2xl"></i>
@if($noActiveSimper ?? false)
<!-- Kartu Peringatan Bersih Jika Karyawan Belum Punya SIMPER Aktif -->
<div class="col-span-full bg-amber-50/80 border border-amber-200 rounded-2xl p-8 sm:p-10 text-center max-w-lg mx-auto my-6 space-y-2 shadow-xs">
<h3 class="text-base font-bold text-amber-900">Akses Kendaraan Belum Tersedia</h3>
<p class="text-xs text-amber-800 leading-relaxed">
Anda belum memiliki SIMPER aktif yang terdaftar di HSE untuk mengemudikan kendaraan.
</p>
<p class="text-[11px] text-amber-700 font-medium pt-1">
Silakan hubungi bagian HSE untuk pendaftaran atau pembaruan masa berlaku SIMPER Anda.
</p>
</div>
@if(request('search'))
<h3 class="text-base font-bold text-slate-800">
Data "<span class="text-rose-600 font-bold">{{ request('search') }}</span>" tidak ditemukan di tabel
</h3>
<p class="text-xs text-slate-500 mt-1">Pastikan kode unit atau nopol kendaraan yang dicari sudah benar.</p>
<div class="mt-4">
<a href="{{ route('mobilisasi.index') }}" class="inline-flex items-center space-x-1.5 px-3.5 py-2 rounded-xl text-xs font-semibold text-rose-600 bg-rose-50 border border-rose-200 hover:bg-rose-100 transition cursor-pointer">
<i class="fa-solid fa-rotate-left text-[11px]"></i>
<span>Hapus Pencarian</span>
</a>
</div>
@else
<h3 class="text-base font-bold text-slate-800">Kendaraan Ringan Tidak Ditemukan</h3>
<p class="text-xs text-slate-500 mt-1">Belum ada unit kendaraan LV yang terdaftar.</p>
@endif
</div>
@else
<div id="lvServerEmptyCard" class="col-span-full bg-white rounded-2xl border border-slate-200 p-10 text-center shadow-xs">
@if(request('search'))
<h3 class="text-base font-bold text-slate-800">
Data "<span class="text-rose-600 font-bold">{{ request('search') }}</span>" tidak ditemukan di katalog
</h3>
<p class="text-xs text-slate-500 mt-1">Pastikan kode unit atau nopol kendaraan yang dicari sudah benar.</p>
<div class="mt-4">
<a href="{{ route('mobilisasi.index', ['type' => $type ?? 'all']) }}" class="inline-flex items-center px-3.5 py-2 rounded-xl text-xs font-semibold text-rose-600 bg-rose-50 border border-rose-200 hover:bg-rose-100 transition cursor-pointer">
<span>Hapus Pencarian</span>
</a>
</div>
@else
<h3 class="text-base font-bold text-slate-800">Tidak Ada Kendaraan Tersedia</h3>
<p class="text-xs text-slate-500 mt-1">Tidak ada unit kendaraan yang sesuai dengan kategori atau izin mengemudi Anda.</p>
@endif
</div>
@endif
@endforelse
</div>
@@ -107,15 +107,82 @@ class VehicleActivitySimperValidationTest extends TestCase
}
/**
* Unit heavy mobile muncul di katalog /timesheet.
* Driver yang hanya memiliki SIMPER LV hanya melihat armada LV di katalog /timesheet.
*/
public function test_timesheet_catalog_shows_both_lv_and_heavy_mobile(): void
public function test_driver_with_only_lv_simper_sees_only_lv_and_not_dump_truck(): void
{
$response = $this->actingAs($this->driverUser)->get(route('mobilisasi.index'));
$response->assertStatus(200);
$response->assertSee('LV-01');
$response->assertDontSee('DT-01');
}
/**
* Driver yang memiliki SIMPER Dump Truck hanya melihat unit Dump Truck yang sesuai izinnya.
*/
public function test_driver_with_dt_simper_sees_dump_truck(): void
{
$driverDtUser = User::create([
'name' => 'Budi Sukon DT',
'username' => 'driver_dt',
'email' => '[email protected]',
'role' => 'employee',
'password' => bcrypt('password'),
]);
$this->driverDtOnly->update(['user_id' => $driverDtUser->id]);
$response = $this->actingAs($driverDtUser)->get(route('mobilisasi.index'));
$response->assertStatus(200);
$response->assertSee('DT-01');
$response->assertDontSee('Toyota Hilux');
}
/**
* Karyawan yang belum memiliki SIMPER aktif melihat kartu peringatan khusus.
*/
public function test_employee_without_active_simper_sees_warning_card(): void
{
$userNoSimper = User::create([
'name' => 'Employee No Simper',
'username' => 'no_simper_user',
'email' => '[email protected]',
'role' => 'employee',
'password' => bcrypt('password'),
]);
Employee::create([
'user_id' => $userNoSimper->id,
'nik' => 'EMP-NO-SMP',
'name' => 'Employee No Simper',
'is_operator' => false,
]);
$response = $this->actingAs($userNoSimper)->get(route('mobilisasi.index'));
$response->assertStatus(200);
$response->assertSee('Anda belum memiliki SIMPER aktif yang terdaftar di HSE untuk mengemudikan kendaraan.');
$response->assertDontSee('LV-01');
$response->assertDontSee('DT-01');
}
/**
* Super Admin dapat memantau seluruh armada (LV dan Heavy Mobile) tanpa batasan.
*/
public function test_super_admin_sees_both_lv_and_heavy_mobile(): void
{
$this->seed(\Database\Seeders\RolePermissionSeeder::class);
$admin = User::create([
'name' => 'Super Admin Test',
'username' => 'superadmin_test',
'email' => '[email protected]',
'role' => 'super_admin',
'password' => bcrypt('password'),
]);
$admin->assignRole('super_admin');
$response = $this->actingAs($admin)->get(route('mobilisasi.index'));
$response->assertStatus(200);
$response->assertSee('LV-01');
$response->assertSee('DT-01');
$response->assertSee('Heavy Equipment Mobile');
}
/**
@@ -213,18 +280,32 @@ class VehicleActivitySimperValidationTest extends TestCase
$response->assertSee('id="lvSearchBtn"', false);
$response->assertSee('Cari');
// 2. Cari 'DT01' tanpa spasi mencocokkan kode 'DT-01'
$searchResponse1 = $this->actingAs($this->driverUser)->get(route('mobilisasi.index', ['search' => 'DT01']));
// 2. Driver LV cari 'LV01' tanpa tanda hubung mencocokkan kode 'LV-01'
$searchResponse1 = $this->actingAs($this->driverUser)->get(route('mobilisasi.index', ['search' => 'LV01']));
$searchResponse1->assertStatus(200);
$searchResponse1->assertSee('DT-01');
$searchResponse1->assertDontSee('Toyota Hilux');
$searchResponse1->assertSee('LV-01');
// 3. Cari plat nomor tanpa spasi 'BH1234' mencocokkan 'BH 1234 KM'
$searchResponse2 = $this->actingAs($this->driverUser)->get(route('mobilisasi.index', ['search' => 'BH1234']));
$searchResponse2->assertStatus(200);
$searchResponse2->assertSee('Toyota Hilux');
$searchResponse2->assertDontSee('Hino 500');
$searchResponse2->assertSee('LV-01');
$searchResponse2->assertDontSee('DT-01');
// 4. Driver DT cari 'DT01' tanpa tanda hubung mencocokkan 'DT-01'
$driverDtUser = User::firstOrCreate(
['username' => 'driver_dt_search'],
[
'name' => 'Budi Sukon DT Search',
'email' => '[email protected]',
'role' => 'employee',
'password' => bcrypt('password'),
]
);
$this->driverDtOnly->update(['user_id' => $driverDtUser->id]);
$searchResponse3 = $this->actingAs($driverDtUser)->get(route('mobilisasi.index', ['search' => 'DT01']));
$searchResponse3->assertStatus(200);
$searchResponse3->assertSee('DT-01');
$searchResponse3->assertDontSee('Toyota Hilux');
}
}