feat(employee): add personal activity history view with dynamic permission-based tabs, minimal icon design, and p2h detail modal

This commit is contained in:
Ray1111-aa
2026-10-28 14:58:54 +07:00
parent 315830c924
commit 2c959ca392
6 changed files with 1268 additions and 4 deletions
@@ -3,11 +3,16 @@
namespace App\Http\Controllers\Employee;
use App\Http\Controllers\Controller;
use App\Models\P2hRecord;
use App\Models\UnitSupportTimesheet;
use App\Models\Vehicle;
use App\Models\VehicleActivity;
use App\Services\P2hInspectionPdfExporter;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\View\View;
class DashboardController extends Controller
@@ -201,4 +206,260 @@ class DashboardController extends Controller
'validated_at' => $activity->validated_at ? $activity->validated_at->format('H:i') . ' WIB' : null,
]);
}
/**
* Tampilkan halaman Riwayat Aktivitas Pribadi Karyawan.
* Mengisolasi data hanya milik karyawan yang sedang login (P2H, Timesheet Alat Berat, dan Perjalanan LV).
* Tab yang tampil dinamis menyesuaikan hak akses dan catatan riwayat yang dimiliki karyawan.
*/
public function history(Request $request): View
{
$user = auth()->user();
if (!$user->isSuperAdmin() && !$user->can('employee.dashboard') && !$user->can('employee.timesheet') && !$user->can('employee.p2h') && !$user->isEmployee()) {
abort(403, 'Akses Ditolak: Anda tidak memiliki wewenang untuk membuka halaman riwayat aktivitas karyawan.');
}
$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());
// Hitung total catatan riwayat per kategori untuk badge tab
$p2hCount = $employee ? P2hRecord::where('employee_id', $employee->id)->count() : 0;
$timesheetCount = $employee ? UnitSupportTimesheet::where('operator_id', $employee->id)->count() : 0;
$tripCount = $employee ? VehicleActivity::where('driver_id', $employee->id)->count() : 0;
// Tentukan tab aktif
$availableTabs = [];
if ($canP2h) {
$availableTabs[] = 'p2h';
}
if ($canTimesheet) {
$availableTabs[] = 'timesheet';
}
if ($canTrips) {
$availableTabs[] = 'trips';
}
$requestedTab = $request->query('tab');
if ($requestedTab && in_array($requestedTab, $availableTabs, true)) {
$activeTab = $requestedTab;
} else {
$activeTab = $availableTabs[0] ?? 'none';
}
// Filter parameter
$search = trim((string) $request->query('search', ''));
$dateFrom = $request->query('date_from');
$dateTo = $request->query('date_to');
$status = $request->query('status');
$p2hRecords = collect();
$timesheetRecords = collect();
$tripRecords = collect();
// Query data khusus tab aktif
if ($employee && $activeTab === 'p2h') {
$query = P2hRecord::with(['vehicle', 'template', 'validator'])
->where('employee_id', $employee->id);
if ($search !== '') {
$query->where(function ($q) use ($search) {
$q->where('transaction_no', 'like', "%{$search}%")
->orWhereHas('vehicle', function ($vq) use ($search) {
$vq->where('code', 'like', "%{$search}%")
->orWhere('brand_type', 'like', "%{$search}%");
});
});
}
if ($dateFrom) {
$query->whereDate('inspected_at', '>=', $dateFrom);
}
if ($dateTo) {
$query->whereDate('inspected_at', '<=', $dateTo);
}
if ($status) {
if (in_array($status, ['pending', 'approved', 'rejected'], true)) {
$query->where('validation_status', $status);
} elseif (in_array($status, ['good', 'has_defect'], true)) {
$query->where('summary_condition', $status);
}
}
$p2hRecords = $query->latest('inspected_at')
->latest('id')
->paginate(15)
->withQueryString();
} elseif ($employee && $activeTab === 'timesheet') {
$query = UnitSupportTimesheet::with(['vehicle'])
->where('operator_id', $employee->id);
if ($search !== '') {
$query->where(function ($q) use ($search) {
$q->where('timesheet_no', 'like', "%{$search}%")
->orWhere('location', 'like', "%{$search}%")
->orWhere('task_description', 'like', "%{$search}%")
->orWhereHas('vehicle', function ($vq) use ($search) {
$vq->where('code', 'like', "%{$search}%")
->orWhere('brand_type', 'like', "%{$search}%");
});
});
}
if ($dateFrom) {
$query->whereDate('date', '>=', $dateFrom);
}
if ($dateTo) {
$query->whereDate('date', '<=', $dateTo);
}
if ($status) {
$query->where('status', $status);
}
$timesheetRecords = $query->latest('date')
->latest('id')
->paginate(15)
->withQueryString();
} elseif ($employee && $activeTab === 'trips') {
$query = VehicleActivity::with(['vehicle', 'gate', 'securityGuard'])
->where('driver_id', $employee->id);
if ($search !== '') {
$query->where(function ($q) use ($search) {
$q->where('transaction_no', 'like', "%{$search}%")
->orWhere('purpose', 'like', "%{$search}%")
->orWhereHas('vehicle', function ($vq) use ($search) {
$vq->where('code', 'like', "%{$search}%")
->orWhere('brand_type', 'like', "%{$search}%")
->orWhere('license_plate', 'like', "%{$search}%");
})
->orWhereHas('gate', function ($gq) use ($search) {
$gq->where('name', 'like', "%{$search}%");
});
});
}
if ($dateFrom) {
$query->whereDate('recorded_at', '>=', $dateFrom);
}
if ($dateTo) {
$query->whereDate('recorded_at', '<=', $dateTo);
}
if ($status) {
$query->where('validation_status', $status);
}
$tripRecords = $query->latest('recorded_at')
->latest('id')
->paginate(15)
->withQueryString();
}
// Ringkasan KPI pribadi karyawan
$stats = [
'p2h_total' => $p2hCount,
'p2h_good' => $employee ? P2hRecord::where('employee_id', $employee->id)->where('summary_condition', 'good')->count() : 0,
'p2h_defects' => $employee ? P2hRecord::where('employee_id', $employee->id)->where('summary_condition', 'has_defect')->count() : 0,
'timesheet_total' => $timesheetCount,
'timesheet_hours' => $employee ? (float) UnitSupportTimesheet::where('operator_id', $employee->id)->sum('operating_hours') : 0.0,
'trip_total' => $tripCount,
'trip_distance' => $employee ? (float) VehicleActivity::where('driver_id', $employee->id)->sum('distance') : 0.0,
];
return view('employee.history', compact(
'employee',
'canP2h',
'canTimesheet',
'canTrips',
'p2hCount',
'timesheetCount',
'tripCount',
'activeTab',
'p2hRecords',
'timesheetRecords',
'tripRecords',
'stats',
'search',
'dateFrom',
'dateTo',
'status'
));
}
/**
* Detail checklist P2H pribadi karyawan (Format JSON untuk modal inspeksi).
*/
public function p2hDetail(int $id): JsonResponse
{
$user = auth()->user();
$employee = $user->getOrCreateEmployee();
$record = P2hRecord::with(['vehicle', 'employee', 'template', 'validator', 'items'])
->findOrFail($id);
if (!$user->isSuperAdmin() && (!$employee || $record->employee_id !== $employee->id)) {
abort(403, 'Aksi Ditolak: Anda hanya dapat melihat lembar pemeriksaan P2H milik Anda sendiri.');
}
return response()->json([
'id' => $record->id,
'transaction_no' => $record->transaction_no,
'vehicle_code' => $record->vehicle?->code ?? '-',
'vehicle_name' => $record->vehicle?->brand_type ?? '-',
'template_name' => $record->template?->name ?? 'P2H Checksheet',
'shift' => $record->shift,
'start_hm' => $record->start_hm,
'start_km' => $record->start_km,
'summary_condition' => $record->summary_condition,
'defect_count' => $record->defect_count,
'general_notes' => $record->general_notes,
'validation_status' => $record->validation_status,
'validator_name' => $record->validator?->name,
'validation_notes' => $record->validation_notes,
'validated_at' => $record->validated_at ? $record->validated_at->format('d/m/Y H:i') : null,
'inspected_at' => $record->inspected_at ? $record->inspected_at->format('d/m/Y H:i') : null,
'items' => $record->items->map(function ($item) {
return [
'id' => $item->id,
'item_name' => $item->title,
'section' => $item->section,
'condition' => $item->condition_status,
'notes' => $item->notes,
];
}),
]);
}
/**
* Download / Cetak PDF lembar pemeriksaan P2H pribadi milik karyawan.
*/
public function p2hPdf(int $id): Response
{
$user = auth()->user();
$employee = $user->getOrCreateEmployee();
$record = P2hRecord::with(['vehicle', 'employee', 'template', 'validator', 'items'])
->findOrFail($id);
if (!$user->isSuperAdmin() && (!$employee || $record->employee_id !== $employee->id)) {
abort(403, 'Aksi Ditolak: Anda hanya dapat mengunduh lembar pemeriksaan P2H milik Anda sendiri.');
}
$pdfBinary = P2hInspectionPdfExporter::generate($record);
$fileName = 'Lembar_P2H_' . ($record->vehicle->code ?? 'Unit') . '_' . $record->transaction_no . '.pdf';
return response($pdfBinary, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="' . $fileName . '"',
'Content-Length' => strlen($pdfBinary),
'Cache-Control' => 'private, max-age=0, must-revalidate',
'Pragma' => 'public',
]);
}
}
+13 -1
View File
@@ -312,9 +312,16 @@
<p class="text-xs text-slate-500">Catatan mobilisasi kendaraan yang pernah Anda isi di sistem.</p>
</div>
</div>
<span class="text-xs font-semibold text-slate-400 self-start sm:self-center">
<div class="flex items-center gap-3 self-start sm:self-center">
<span class="text-xs font-semibold text-slate-400">
{{ $myRecentActivities->count() }} Perjalanan Terbaru
</span>
<a href="{{ route('employee.history') }}"
class="inline-flex items-center text-xs font-bold text-[#1b5e20] hover:underline">
<span>Lihat Seluruh Riwayat</span>
<i class="fa-solid fa-arrow-right text-[10px] ml-1"></i>
</a>
</div>
</div>
<div class="overflow-x-auto -mx-5 sm:mx-0">
@@ -411,6 +418,11 @@
<p class="text-[11px] text-slate-400 mt-1 max-w-sm mx-auto">
Catatan mobilisasi unit kendaraan ringan (LV) yang Anda kendarai akan otomatis muncul di sini setelah Anda mengisi formulir perjalanan.
</p>
<div class="mt-3">
<a href="{{ route('employee.history') }}" class="text-xs font-bold text-[#1b5e20] hover:underline">
Buka Halaman Riwayat Saya &rarr;
</a>
</div>
</div>
@endif
</div>
+637
View File
@@ -0,0 +1,637 @@
@extends('layouts.app')
@section('title', 'Riwayat Aktivitas - Sistem RKM | PT Kerinci Merangin Hidro')
@section('content')
<div class="w-full space-y-6">
<!-- Header Halaman Bersih & Minimalis -->
<div class="flex flex-col sm:flex-row sm:items-center justify-between gap-4 pb-4 border-b border-slate-200">
<div>
<div class="flex items-center space-x-2 text-xs font-semibold text-slate-500 uppercase tracking-wider mb-1">
<span>Portal Karyawan</span>
<span>&bull;</span>
<span class="text-[#1b5e20] font-bold">Catatan Operasional</span>
</div>
<h1 class="text-2xl sm:text-3xl font-extrabold text-slate-900 tracking-tight">
Riwayat Aktivitas Saya
</h1>
<p class="text-xs sm:text-sm text-slate-500 mt-1">
Rekap seluruh aktivitas pemeriksaan kelaikan P2H, timesheet alat berat, dan mobilisasi kendaraan Anda.
</p>
</div>
<div class="flex items-center gap-2 self-start sm:self-center">
<a href="{{ route('employee.dashboard') }}"
class="px-3.5 py-2 text-xs font-bold text-slate-700 bg-white border border-slate-300 rounded-xl hover:bg-slate-50 transition shadow-2xs">
&larr; Kembali ke Dashboard
</a>
</div>
</div>
<!-- Ringkasan KPI Pribadi Karyawan (Tanpa Ikon Berlebih, Tipografi Bersih) -->
<div class="grid grid-cols-1 {{ ($canP2h && $canTimesheet && $canTrips) ? 'sm:grid-cols-3' : (($canP2h || $canTimesheet) && ($canTrips || ($canP2h && $canTimesheet)) ? 'sm:grid-cols-2' : 'sm:grid-cols-1') }} gap-4">
@if($canP2h)
<div class="bg-white border border-slate-200 rounded-xl p-4 shadow-2xs">
<p class="text-[11px] font-bold uppercase tracking-wider text-slate-400">P2H Heavy Equipment</p>
<div class="mt-2 flex items-baseline justify-between">
<span class="text-2xl font-black text-slate-900">{{ number_format($stats['p2h_total']) }}</span>
<span class="text-xs font-semibold text-emerald-700 bg-emerald-50 px-2 py-0.5 rounded border border-emerald-200">
{{ $stats['p2h_good'] }} Layak
</span>
</div>
<p class="text-xs text-slate-500 mt-2">
{{ $stats['p2h_defects'] }} catatan memiliki temuan kerusakan
</p>
</div>
@endif
@if($canTimesheet)
<div class="bg-white border border-slate-200 rounded-xl p-4 shadow-2xs">
<p class="text-[11px] font-bold uppercase tracking-wider text-slate-400">Timesheet Alat Berat</p>
<div class="mt-2 flex items-baseline justify-between">
<span class="text-2xl font-black text-slate-900">{{ number_format($stats['timesheet_hours'], 1, ',', '.') }} Jam</span>
<span class="text-xs font-semibold text-slate-700 bg-slate-100 px-2 py-0.5 rounded border border-slate-200">
{{ number_format($stats['timesheet_total']) }} Sesi
</span>
</div>
<p class="text-xs text-slate-500 mt-2">
Total akumulasi jam operasi alat berat
</p>
</div>
@endif
@if($canTrips)
<div class="bg-white border border-slate-200 rounded-xl p-4 shadow-2xs">
<p class="text-[11px] font-bold uppercase tracking-wider text-slate-400">Perjalanan Gerbang (LV)</p>
<div class="mt-2 flex items-baseline justify-between">
<span class="text-2xl font-black text-slate-900">{{ number_format($stats['trip_distance'], 0, ',', '.') }} KM</span>
<span class="text-xs font-semibold text-blue-700 bg-blue-50 px-2 py-0.5 rounded border border-blue-200">
{{ number_format($stats['trip_total']) }} Trip
</span>
</div>
<p class="text-xs text-slate-500 mt-2">
Jarak kumulatif mobilisasi kendaraan dinas
</p>
</div>
@endif
</div>
<!-- Tab Navigasi Dinamis Berdasarkan Hak Akses & Riwayat -->
<div class="bg-white border border-slate-200 rounded-2xl shadow-2xs overflow-hidden">
<!-- Header Tab -->
<div class="border-b border-slate-200 px-5 pt-3 bg-slate-50/50">
<div class="flex space-x-6 overflow-x-auto no-scrollbar">
@if($canP2h)
<a href="{{ route('employee.history', array_merge(request()->except(['page', 'p2h_page', 'ts_page', 'trip_page']), ['tab' => 'p2h'])) }}"
class="pb-3 text-xs sm:text-sm font-bold border-b-2 whitespace-nowrap transition flex items-center gap-2 {{ $activeTab === 'p2h' ? 'border-[#1b5e20] text-[#1b5e20]' : 'border-transparent text-slate-500 hover:text-slate-800' }}">
<span>P2H Heavy Equipment</span>
<span class="px-2 py-0.5 text-[11px] rounded-full {{ $activeTab === 'p2h' ? 'bg-emerald-100 text-emerald-800' : 'bg-slate-200/80 text-slate-600' }}">
{{ $p2hCount }}
</span>
</a>
@endif
@if($canTimesheet)
<a href="{{ route('employee.history', array_merge(request()->except(['page', 'p2h_page', 'ts_page', 'trip_page']), ['tab' => 'timesheet'])) }}"
class="pb-3 text-xs sm:text-sm font-bold border-b-2 whitespace-nowrap transition flex items-center gap-2 {{ $activeTab === 'timesheet' ? 'border-[#1b5e20] text-[#1b5e20]' : 'border-transparent text-slate-500 hover:text-slate-800' }}">
<span>Timesheet Heavy Equipment</span>
<span class="px-2 py-0.5 text-[11px] rounded-full {{ $activeTab === 'timesheet' ? 'bg-emerald-100 text-emerald-800' : 'bg-slate-200/80 text-slate-600' }}">
{{ $timesheetCount }}
</span>
</a>
@endif
@if($canTrips)
<a href="{{ route('employee.history', array_merge(request()->except(['page', 'p2h_page', 'ts_page', 'trip_page']), ['tab' => 'trips'])) }}"
class="pb-3 text-xs sm:text-sm font-bold border-b-2 whitespace-nowrap transition flex items-center gap-2 {{ $activeTab === 'trips' ? 'border-[#1b5e20] text-[#1b5e20]' : 'border-transparent text-slate-500 hover:text-slate-800' }}">
<span>Mobilisasi & Pos Gerbang (LV)</span>
<span class="px-2 py-0.5 text-[11px] rounded-full {{ $activeTab === 'trips' ? 'bg-emerald-100 text-emerald-800' : 'bg-slate-200/80 text-slate-600' }}">
{{ $tripCount }}
</span>
</a>
@endif
</div>
</div>
<!-- Filter Bar Bersih -->
<div class="p-5 border-b border-slate-100 bg-white">
<form method="GET" action="{{ route('employee.history') }}" class="flex flex-col lg:flex-row items-stretch lg:items-center justify-between gap-3">
<input type="hidden" name="tab" value="{{ $activeTab }}">
<div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-4 gap-2.5 flex-1">
<!-- Kata Kunci Pencarian -->
<div class="sm:col-span-2 md:col-span-1">
<input type="text"
name="search"
value="{{ $search }}"
placeholder="Cari transaksi, kode unit..."
class="w-full text-xs px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl focus:bg-white focus:outline-none focus:ring-2 focus:ring-[#1b5e20]/20 focus:border-[#1b5e20] transition">
</div>
<!-- Dari Tanggal -->
<div>
<input type="date"
name="date_from"
value="{{ $dateFrom }}"
title="Dari Tanggal"
class="w-full text-xs px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl focus:bg-white focus:outline-none focus:ring-2 focus:ring-[#1b5e20]/20 focus:border-[#1b5e20] transition text-slate-600">
</div>
<!-- Sampai Tanggal -->
<div>
<input type="date"
name="date_to"
value="{{ $dateTo }}"
title="Sampai Tanggal"
class="w-full text-xs px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl focus:bg-white focus:outline-none focus:ring-2 focus:ring-[#1b5e20]/20 focus:border-[#1b5e20] transition text-slate-600">
</div>
<!-- Filter Status Sesuai Tab Aktif -->
<div>
<select name="status" class="w-full text-xs px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl focus:bg-white focus:outline-none focus:ring-2 focus:ring-[#1b5e20]/20 focus:border-[#1b5e20] transition text-slate-600">
<option value="">Semua Status</option>
@if($activeTab === 'p2h')
<option value="pending" {{ $status === 'pending' ? 'selected' : '' }}>Menunggu Approval QMA</option>
<option value="approved" {{ $status === 'approved' ? 'selected' : '' }}>Disetujui</option>
<option value="rejected" {{ $status === 'rejected' ? 'selected' : '' }}>Ditolak</option>
<option value="good" {{ $status === 'good' ? 'selected' : '' }}>Kondisi: Layak Jalan</option>
<option value="has_defect" {{ $status === 'has_defect' ? 'selected' : '' }}>Kondisi: Ada Temuan</option>
@elseif($activeTab === 'timesheet')
<option value="completed" {{ $status === 'completed' ? 'selected' : '' }}>Selesai</option>
<option value="running" {{ $status === 'running' ? 'selected' : '' }}>Sedang Berjalan</option>
@elseif($activeTab === 'trips')
<option value="approved" {{ $status === 'approved' ? 'selected' : '' }}>Disetujui Satpam</option>
<option value="pending" {{ $status === 'pending' ? 'selected' : '' }}>Menunggu Validasi</option>
<option value="rejected" {{ $status === 'rejected' ? 'selected' : '' }}>Ditolak Satpam</option>
<option value="cancelled" {{ $status === 'cancelled' ? 'selected' : '' }}>Dibatalkan</option>
@endif
</select>
</div>
</div>
<!-- Tombol Filter & Reset -->
<div class="flex items-center gap-2 flex-shrink-0">
<button type="submit"
class="px-4 py-2 bg-[#1b5e20] hover:bg-[#144918] text-white font-bold text-xs rounded-xl shadow-2xs transition">
Terapkan Filter
</button>
@if($search || $dateFrom || $dateTo || $status)
<a href="{{ route('employee.history', ['tab' => $activeTab]) }}"
class="px-3 py-2 bg-slate-100 hover:bg-slate-200 text-slate-600 text-xs font-semibold rounded-xl transition">
Reset
</a>
@endif
</div>
</form>
</div>
<!-- ========================================================== -->
<!-- TAB 1: RIWAYAT P2H HEAVY EQUIPMENT -->
<!-- ========================================================== -->
@if($activeTab === 'p2h')
@if(isset($p2hRecords) && $p2hRecords->isNotEmpty())
<div class="overflow-x-auto">
<table class="w-full text-xs text-left min-w-[760px]">
<thead>
<tr class="bg-slate-50 text-slate-500 font-bold uppercase tracking-wider text-[10px] border-b border-slate-200">
<th class="py-3 px-4">Waktu Inspeksi</th>
<th class="py-3 px-4">No. Transaksi</th>
<th class="py-3 px-4">Unit Alat Berat</th>
<th class="py-3 px-4">Shift & Odometer</th>
<th class="py-3 px-4 text-center">Kelaikan Operasi</th>
<th class="py-3 px-4 text-center">Status QMA</th>
<th class="py-3 px-4 text-right">Aksi</th>
</tr>
</thead>
<tbody class="divide-y divide-slate-100 text-slate-700">
@foreach($p2hRecords as $rec)
<tr class="hover:bg-slate-50/60 transition">
<td class="py-3 px-4 whitespace-nowrap">
<span class="font-bold text-slate-900 block">{{ $rec->inspected_at ? $rec->inspected_at->format('d/m/Y') : '-' }}</span>
<span class="text-[11px] text-slate-400 block">{{ $rec->inspected_at ? $rec->inspected_at->format('H:i') . ' WIB' : '-' }}</span>
</td>
<td class="py-3 px-4 whitespace-nowrap font-mono font-bold text-slate-800">
{{ $rec->transaction_no }}
</td>
<td class="py-3 px-4 whitespace-nowrap">
<span class="font-bold text-[#1b5e20] font-mono text-xs block">{{ $rec->vehicle->code ?? '-' }}</span>
<span class="text-[11px] text-slate-500 block truncate max-w-[160px]">{{ $rec->vehicle->brand_type ?? '-' }}</span>
</td>
<td class="py-3 px-4 whitespace-nowrap">
<span class="font-medium text-slate-800 block">Shift {{ $rec->shift }}</span>
<span class="text-[11px] font-mono text-slate-500 block">HM: {{ number_format($rec->start_hm, 1) }}</span>
</td>
<td class="py-3 px-4 text-center whitespace-nowrap">
@if($rec->summary_condition === 'good')
<span class="inline-block px-2.5 py-0.5 rounded text-[11px] font-bold bg-emerald-50 text-emerald-700 border border-emerald-200">
Layak Jalan
</span>
@else
<span class="inline-block px-2.5 py-0.5 rounded text-[11px] font-bold bg-amber-50 text-amber-800 border border-amber-200" title="{{ $rec->defect_count }} temuan kerusakan">
Ada Temuan ({{ $rec->defect_count }})
</span>
@endif
</td>
<td class="py-3 px-4 text-center whitespace-nowrap">
@if($rec->validation_status === 'approved')
<span class="inline-block px-2 py-0.5 rounded text-[11px] font-bold bg-emerald-50 text-emerald-700 border border-emerald-200" title="Disetujui oleh: {{ $rec->validator->name ?? 'Admin QMA' }}">
Disetujui
</span>
@elseif($rec->validation_status === 'rejected')
<span class="inline-block px-2 py-0.5 rounded text-[11px] font-bold bg-rose-50 text-rose-700 border border-rose-200" title="Alasan: {{ $rec->validation_notes }}">
Ditolak
</span>
@else
<span class="inline-block px-2 py-0.5 rounded text-[11px] font-bold bg-amber-50 text-amber-800 border border-amber-200">
Menunggu QMA
</span>
@endif
</td>
<td class="py-3 px-4 text-right whitespace-nowrap space-x-1">
<button type="button"
onclick="viewP2hDetail({{ $rec->id }})"
class="px-2.5 py-1 text-[11px] font-semibold text-slate-700 bg-white border border-slate-200 rounded-lg hover:bg-slate-50 transition cursor-pointer">
Detail
</button>
<a href="{{ route('employee.history.p2h_pdf', $rec->id) }}"
target="_blank"
class="px-2.5 py-1 text-[11px] font-semibold text-rose-700 bg-rose-50 border border-rose-200 rounded-lg hover:bg-rose-100 transition">
PDF
</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<!-- Pagination Links -->
<div class="p-4 border-t border-slate-100 bg-white">
{{ $p2hRecords->links() }}
</div>
@else
<div class="py-12 px-4 text-center">
<p class="text-sm font-bold text-slate-700">Belum Ada Riwayat P2H Heavy Equipment</p>
<p class="text-xs text-slate-400 mt-1 max-w-sm mx-auto">
Data pemeriksaan kelaikan unit yang pernah Anda kerjakan akan otomatis tampil di tabel ini.
</p>
@can('employee.p2h')
<div class="mt-4">
<a href="{{ route('p2h.index') }}" class="px-4 py-2 bg-[#1b5e20] text-white text-xs font-bold rounded-xl hover:bg-[#144918] transition inline-block">
Mulai Pemeriksaan P2H Sekarang
</a>
</div>
@endcan
</div>
@endif
<!-- ========================================================== -->
<!-- TAB 2: RIWAYAT TIMESHEET ALAT BERAT -->
<!-- ========================================================== -->
@elseif($activeTab === 'timesheet')
@if(isset($timesheetRecords) && $timesheetRecords->isNotEmpty())
<div class="overflow-x-auto">
<table class="w-full text-xs text-left min-w-[760px]">
<thead>
<tr class="bg-slate-50 text-slate-500 font-bold uppercase tracking-wider text-[10px] border-b border-slate-200">
<th class="py-3 px-4">Tanggal & Shift</th>
<th class="py-3 px-4">No. Timesheet</th>
<th class="py-3 px-4">Unit Alat Berat</th>
<th class="py-3 px-4 text-right">Odometer (HM / KM)</th>
<th class="py-3 px-4 text-right">Jam Operasi</th>
<th class="py-3 px-4">Lokasi & Aktivitas</th>
<th class="py-3 px-4 text-center">Status</th>
</tr>
</thead>
<tbody class="divide-y divide-slate-100 text-slate-700">
@foreach($timesheetRecords as $ts)
<tr class="hover:bg-slate-50/60 transition">
<td class="py-3 px-4 whitespace-nowrap">
<span class="font-bold text-slate-900 block">{{ $ts->date ? $ts->date->format('d/m/Y') : '-' }}</span>
<span class="text-[11px] text-slate-500 block font-medium">Shift {{ $ts->shift }}</span>
</td>
<td class="py-3 px-4 whitespace-nowrap font-mono font-bold text-slate-800">
{{ $ts->timesheet_no }}
</td>
<td class="py-3 px-4 whitespace-nowrap">
<span class="font-bold text-[#1b5e20] font-mono text-xs block">{{ $ts->vehicle->code ?? '-' }}</span>
<span class="text-[11px] text-slate-500 block truncate max-w-[150px]">{{ $ts->vehicle->brand_type ?? '-' }}</span>
</td>
<td class="py-3 px-4 text-right whitespace-nowrap font-mono">
<span class="text-slate-900 font-semibold block">{{ number_format($ts->start_meter, 1) }} &rarr; {{ number_format($ts->end_meter, 1) }}</span>
<span class="text-[10px] text-slate-400 block">{{ strtoupper($ts->meter_type ?? 'HM') }}</span>
</td>
<td class="py-3 px-4 text-right whitespace-nowrap font-mono">
<span class="font-bold text-slate-900 block">{{ number_format($ts->operating_hours, 1) }} Jam</span>
@if($ts->standby_hours > 0 || $ts->breakdown_hours > 0)
<span class="text-[10px] text-slate-400 block">
Stb: {{ number_format($ts->standby_hours, 1) }} | BD: {{ number_format($ts->breakdown_hours, 1) }}
</span>
@endif
</td>
<td class="py-3 px-4">
<span class="font-bold text-slate-800 block truncate max-w-[180px]">{{ $ts->location ?? '-' }}</span>
<span class="text-[11px] text-slate-500 block truncate max-w-[200px]">{{ $ts->task_description ?? '-' }}</span>
</td>
<td class="py-3 px-4 text-center whitespace-nowrap">
@if($ts->status === 'completed')
<span class="inline-block px-2 py-0.5 rounded text-[11px] font-bold bg-emerald-50 text-emerald-700 border border-emerald-200">
Selesai
</span>
@else
<span class="inline-block px-2 py-0.5 rounded text-[11px] font-bold bg-blue-50 text-blue-700 border border-blue-200">
Berjalan
</span>
@endif
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<!-- Pagination Links -->
<div class="p-4 border-t border-slate-100 bg-white">
{{ $timesheetRecords->links() }}
</div>
@else
<div class="py-12 px-4 text-center">
<p class="text-sm font-bold text-slate-700">Belum Ada Riwayat Timesheet Alat Berat</p>
<p class="text-xs text-slate-400 mt-1 max-w-sm mx-auto">
Data jam kerja dan catatan operasional operator heavy equipment akan tampil di sini setelah dicatat.
</p>
</div>
@endif
<!-- ========================================================== -->
<!-- TAB 3: RIWAYAT MOBILISASI & POS GERBANG (LV) -->
<!-- ========================================================== -->
@elseif($activeTab === 'trips')
@if(isset($tripRecords) && $tripRecords->isNotEmpty())
<div class="overflow-x-auto">
<table class="w-full text-xs text-left min-w-[760px]">
<thead>
<tr class="bg-slate-50 text-slate-500 font-bold uppercase tracking-wider text-[10px] border-b border-slate-200">
<th class="py-3 px-4">Waktu</th>
<th class="py-3 px-4">No. Transaksi</th>
<th class="py-3 px-4">Unit Kendaraan</th>
<th class="py-3 px-4">Pos Gerbang & Arah</th>
<th class="py-3 px-4 text-right">Odometer & Jarak</th>
<th class="py-3 px-4">Keperluan</th>
<th class="py-3 px-4 text-center">Status Satpam</th>
</tr>
</thead>
<tbody class="divide-y divide-slate-100 text-slate-700">
@foreach($tripRecords as $act)
<tr class="hover:bg-slate-50/60 transition">
<td class="py-3 px-4 whitespace-nowrap">
<span class="font-bold text-slate-900 block">{{ $act->recorded_at ? $act->recorded_at->format('d/m/Y') : '-' }}</span>
<span class="text-[11px] text-slate-400 block">{{ $act->recorded_at ? $act->recorded_at->format('H:i') . ' WIB' : '-' }}</span>
</td>
<td class="py-3 px-4 whitespace-nowrap font-mono font-bold text-slate-800">
{{ $act->transaction_no }}
</td>
<td class="py-3 px-4 whitespace-nowrap">
<span class="font-bold text-[#1b5e20] font-mono text-xs block">{{ $act->vehicle->code ?? '-' }}</span>
<span class="text-[11px] text-slate-500 block truncate max-w-[160px]">
{{ $act->vehicle->brand_type ?? '-' }} &bull; {{ $act->vehicle->license_plate ?? 'Tanpa Plat' }}
</span>
</td>
<td class="py-3 px-4 whitespace-nowrap">
<span class="font-bold text-slate-800 block">{{ $act->gate->name ?? 'Pos Gerbang' }}</span>
@if($act->direction === 'in')
<span class="inline-block mt-0.5 px-1.5 py-0.2 rounded text-[10px] font-bold bg-emerald-50 text-emerald-700 border border-emerald-200">
MASUK
</span>
@else
<span class="inline-block mt-0.5 px-1.5 py-0.2 rounded text-[10px] font-bold bg-blue-50 text-blue-700 border border-blue-200">
KELUAR
</span>
@endif
</td>
<td class="py-3 px-4 text-right whitespace-nowrap font-mono">
<span class="font-bold text-slate-900 block">{{ number_format($act->current_km, 0, ',', '.') }} KM</span>
<span class="text-[11px] text-emerald-700 font-semibold block">+{{ number_format($act->distance, 0, ',', '.') }} KM</span>
</td>
<td class="py-3 px-4">
<span class="text-slate-700 block truncate max-w-[180px]" title="{{ $act->purpose }}">
{{ $act->purpose ?: '-' }}
</span>
</td>
<td class="py-3 px-4 text-center whitespace-nowrap">
@if($act->validation_status === 'approved')
<span class="inline-block px-2 py-0.5 rounded text-[11px] font-bold bg-emerald-50 text-emerald-700 border border-emerald-200" title="Divalidasi oleh: {{ $act->securityGuard->name ?? 'Satpam' }}">
Disetujui
</span>
@elseif($act->validation_status === 'cancelled')
<span class="inline-block px-2 py-0.5 rounded text-[11px] font-bold bg-slate-100 text-slate-700 border border-slate-200" title="{{ $act->rejection_note }}">
Dibatalkan
</span>
@elseif($act->validation_status === 'rejected')
<span class="inline-block px-2 py-0.5 rounded text-[11px] font-bold bg-rose-50 text-rose-700 border border-rose-200" title="Alasan: {{ $act->rejection_note }}">
Ditolak
</span>
@else
<span class="inline-block px-2 py-0.5 rounded text-[11px] font-bold bg-amber-50 text-amber-800 border border-amber-200">
Menunggu Satpam
</span>
@endif
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<!-- Pagination Links -->
<div class="p-4 border-t border-slate-100 bg-white">
{{ $tripRecords->links() }}
</div>
@else
<div class="py-12 px-4 text-center">
<p class="text-sm font-bold text-slate-700">Belum Ada Riwayat Perjalanan Pos Gerbang</p>
<p class="text-xs text-slate-400 mt-1 max-w-sm mx-auto">
Catatan mobilisasi kendaraan ringan (LV) Anda yang melewati pos penjagaan satpam akan dicatat di sini.
</p>
</div>
@endif
@else
<div class="py-12 px-4 text-center">
<p class="text-sm font-bold text-slate-700">Tidak Ada Modul Riwayat yang Tersedia</p>
<p class="text-xs text-slate-400 mt-1 max-w-sm mx-auto">
Akun Anda saat ini belum memiliki hak akses operasional yang aktif.
</p>
</div>
@endif
</div>
</div>
<!-- ========================================================== -->
<!-- MODAL DETAIL INSPEKSI P2H (DESAIN BERSIH TANPA IKON BERLEBIH) -->
<!-- ========================================================== -->
<div id="p2hDetailModal" class="fixed inset-0 z-50 hidden overflow-y-auto bg-slate-900/40 backdrop-blur-xs flex items-center justify-center p-4">
<div class="bg-white rounded-2xl shadow-xl border border-slate-200 w-full max-w-2xl overflow-hidden flex flex-col max-h-[90vh]">
<!-- Modal Header -->
<div class="px-6 py-4 border-b border-slate-100 flex items-center justify-between bg-slate-50/50">
<div>
<h3 class="text-base font-bold text-slate-900" id="modalTrxNo">Detail Lembar P2H</h3>
<p class="text-xs text-slate-500 mt-0.5" id="modalSubTitle">Hasil pemeriksaan kelaikan unit alat berat</p>
</div>
<button type="button"
onclick="closeP2hDetailModal()"
class="text-slate-400 hover:text-slate-700 text-lg font-bold p-1 cursor-pointer">
&times;
</button>
</div>
<!-- Modal Body Scrollable -->
<div class="p-6 overflow-y-auto space-y-4 text-xs">
<!-- Metadata Grid -->
<div class="grid grid-cols-2 sm:grid-cols-4 gap-3 p-3 bg-slate-50 rounded-xl border border-slate-100">
<div>
<span class="text-[10px] text-slate-400 uppercase font-bold block">Unit Alat Berat</span>
<span class="font-bold text-[#1b5e20] text-xs font-mono" id="modalUnitCode">-</span>
<span class="text-[11px] text-slate-600 block truncate" id="modalUnitName">-</span>
</div>
<div>
<span class="text-[10px] text-slate-400 uppercase font-bold block">Shift & Odometer</span>
<span class="font-bold text-slate-800 text-xs block" id="modalShift">-</span>
<span class="text-[11px] font-mono text-slate-600 block" id="modalHm">-</span>
</div>
<div>
<span class="text-[10px] text-slate-400 uppercase font-bold block">Waktu Inspeksi</span>
<span class="font-semibold text-slate-800 text-xs block" id="modalTime">-</span>
</div>
<div>
<span class="text-[10px] text-slate-400 uppercase font-bold block">Status Approval</span>
<span class="inline-block mt-0.5 px-2 py-0.5 text-[10px] font-bold rounded" id="modalStatusBadge">-</span>
</div>
</div>
<!-- Catatan Umum -->
<div id="modalGeneralNotesContainer" class="p-3 bg-amber-50/60 rounded-xl border border-amber-200/60 hidden">
<span class="text-[11px] font-bold text-amber-900 block mb-0.5">Catatan Operator:</span>
<p class="text-xs text-amber-800" id="modalGeneralNotes"></p>
</div>
<!-- Tabel Checklist Items -->
<div>
<p class="font-bold text-slate-800 mb-2 uppercase text-[10px] tracking-wider text-slate-400">Daftar Pemeriksaan Komponen:</p>
<div class="border border-slate-200 rounded-xl overflow-hidden">
<table class="w-full text-left text-xs">
<thead>
<tr class="bg-slate-50 text-slate-600 font-bold uppercase tracking-wider text-[10px] border-b border-slate-200">
<th class="py-2.5 px-3">Bagian</th>
<th class="py-2.5 px-3">Komponen / Item</th>
<th class="py-2.5 px-3 text-center">Kondisi</th>
<th class="py-2.5 px-3">Catatan Temuan</th>
</tr>
</thead>
<tbody id="modalItemsTableBody" class="divide-y divide-slate-100">
<!-- Injected via JavaScript -->
</tbody>
</table>
</div>
</div>
</div>
<!-- Modal Footer -->
<div class="px-6 py-3 border-t border-slate-100 bg-slate-50/50 flex items-center justify-between">
<a id="modalDownloadPdfBtn" href="#" target="_blank" class="px-3.5 py-1.5 bg-rose-600 hover:bg-rose-700 text-white font-bold text-xs rounded-xl transition shadow-2xs">
Cetak Lembar PDF
</a>
<button type="button"
onclick="closeP2hDetailModal()"
class="px-4 py-1.5 bg-slate-200 hover:bg-slate-300 text-slate-700 text-xs font-semibold rounded-xl transition cursor-pointer">
Tutup
</button>
</div>
</div>
</div>
<script>
function viewP2hDetail(recordId) {
const modal = document.getElementById('p2hDetailModal');
const pdfBtn = document.getElementById('modalDownloadPdfBtn');
const tableBody = document.getElementById('modalItemsTableBody');
pdfBtn.href = "{{ url('/karyawan/riwayat/p2h') }}/" + recordId + "/pdf";
tableBody.innerHTML = '<tr><td colspan="4" class="py-4 text-center text-slate-400">Memuat data...</td></tr>';
modal.classList.remove('hidden');
fetch("{{ url('/karyawan/riwayat/p2h') }}/" + recordId)
.then(res => {
if (!res.ok) throw new Error('Gagal memuat detail data.');
return res.json();
})
.then(data => {
document.getElementById('modalTrxNo').textContent = 'P2H: ' + data.transaction_no;
document.getElementById('modalSubTitle').textContent = data.template_name;
document.getElementById('modalUnitCode').textContent = data.vehicle_code;
document.getElementById('modalUnitName').textContent = data.vehicle_name;
document.getElementById('modalShift').textContent = 'Shift ' + data.shift;
document.getElementById('modalHm').textContent = 'HM: ' + (data.start_hm || '0');
document.getElementById('modalTime').textContent = data.inspected_at || '-';
const badge = document.getElementById('modalStatusBadge');
if (data.validation_status === 'approved') {
badge.className = 'inline-block mt-0.5 px-2 py-0.5 text-[10px] font-bold rounded bg-emerald-50 text-emerald-700 border border-emerald-200';
badge.textContent = 'Disetujui';
} else if (data.validation_status === 'rejected') {
badge.className = 'inline-block mt-0.5 px-2 py-0.5 text-[10px] font-bold rounded bg-rose-50 text-rose-700 border border-rose-200';
badge.textContent = 'Ditolak';
} else {
badge.className = 'inline-block mt-0.5 px-2 py-0.5 text-[10px] font-bold rounded bg-amber-50 text-amber-800 border border-amber-200';
badge.textContent = 'Menunggu QMA';
}
const notesContainer = document.getElementById('modalGeneralNotesContainer');
if (data.general_notes && data.general_notes.trim() !== '') {
notesContainer.classList.remove('hidden');
document.getElementById('modalGeneralNotes').textContent = data.general_notes;
} else {
notesContainer.classList.add('hidden');
}
if (data.items && data.items.length > 0) {
tableBody.innerHTML = data.items.map(item => {
const isGood = item.condition === 'baik' || item.condition === 'good' || item.condition === 'ok';
const condBadge = isGood
? '<span class="px-2 py-0.5 rounded text-[10px] font-bold bg-emerald-50 text-emerald-700 border border-emerald-200">Baik (OK)</span>'
: '<span class="px-2 py-0.5 rounded text-[10px] font-bold bg-rose-50 text-rose-700 border border-rose-200">Rusak (Defect)</span>';
return `
<tr class="hover:bg-slate-50/50">
<td class="py-2.5 px-3 text-slate-500 font-medium">${item.section || '-'}</td>
<td class="py-2.5 px-3 font-semibold text-slate-800">${item.item_name || '-'}</td>
<td class="py-2.5 px-3 text-center">${condBadge}</td>
<td class="py-2.5 px-3 text-slate-600">${item.notes || '-'}</td>
</tr>
`;
}).join('');
} else {
tableBody.innerHTML = '<tr><td colspan="4" class="py-3 text-center text-slate-400">Tidak ada komponen checklist tercatat.</td></tr>';
}
})
.catch(err => {
tableBody.innerHTML = '<tr><td colspan="4" class="py-4 text-center text-rose-500">' + err.message + '</td></tr>';
});
}
function closeP2hDetailModal() {
document.getElementById('p2hDetailModal').classList.add('hidden');
}
// Tutup modal jika klik di luar box
document.getElementById('p2hDetailModal').addEventListener('click', function(e) {
if (e.target === this) {
closeP2hDetailModal();
}
});
</script>
@endsection
+13
View File
@@ -501,6 +501,12 @@
<span>Timesheet</span>
</a>
@endcan
<a href="{{ route('employee.history') }}"
class="flex items-center space-x-3 px-3.5 py-2.5 text-sm {{ request()->routeIs('employee.history*') ? 'sidebar-item-active' : 'sidebar-item-inactive' }}">
<i class="fa-solid fa-clock-rotate-left text-base w-5 text-center"></i>
<span>Riwayat Saya</span>
</a>
</nav>
</div>
@endif
@@ -881,6 +887,11 @@
<span>Timesheet</span>
</a>
@endcan
<a href="{{ route('employee.history') }}" class="flex items-center space-x-3 px-3 py-2.5 rounded-xl text-sm {{ request()->routeIs('employee.history*') ? 'sidebar-item-active' : 'sidebar-item-inactive' }}">
<i class="fa-solid fa-clock-rotate-left w-5 text-center"></i>
<span>Riwayat Saya</span>
</a>
</nav>
</div>
@endif
@@ -987,6 +998,8 @@
Log Verifikasi Gerbang
@elseif(request()->routeIs('employee.dashboard'))
Dashboard Karyawan
@elseif(request()->routeIs('employee.history*'))
Riwayat Aktivitas
@elseif(request()->routeIs('p2h.*'))
Pemeriksaan P2H
@elseif(request()->routeIs('timesheet.*') || request()->routeIs('mobilisasi.*'))
+4 -1
View File
@@ -188,8 +188,11 @@ Route::middleware('auth')->group(function () {
Route::post('/{code}', [P2hFormController::class, 'store'])->middleware('throttle:30,1')->name('store');
});
// 6. Modul Karyawan (Dashboard, P2H, Timesheet)
// 6. Modul Karyawan (Dashboard, Riwayat Aktivitas, P2H, Timesheet)
Route::get('/karyawan/dashboard', [\App\Http\Controllers\Employee\DashboardController::class, 'index'])->name('employee.dashboard');
Route::get('/karyawan/riwayat', [\App\Http\Controllers\Employee\DashboardController::class, 'history'])->name('employee.history');
Route::get('/karyawan/riwayat/p2h/{id}', [\App\Http\Controllers\Employee\DashboardController::class, 'p2hDetail'])->name('employee.history.p2h_detail');
Route::get('/karyawan/riwayat/p2h/{id}/pdf', [\App\Http\Controllers\Employee\DashboardController::class, 'p2hPdf'])->name('employee.history.p2h_pdf');
Route::get('/karyawan/aktivitas/status-polling', [\App\Http\Controllers\Employee\DashboardController::class, 'activeTripStatus'])->name('employee.activity.status_polling');
Route::post('/karyawan/aktivitas/{id}/update-km', [\App\Http\Controllers\Employee\DashboardController::class, 'updateKm'])->name('employee.activity.update_km');
Route::post('/karyawan/aktivitas/{id}/cancel', [\App\Http\Controllers\Employee\DashboardController::class, 'cancelActivity'])->name('employee.activity.cancel');
+338
View File
@@ -0,0 +1,338 @@
<?php
namespace Tests\Feature;
use App\Models\Employee;
use App\Models\Gate;
use App\Models\P2hRecord;
use App\Models\P2hRecordItem;
use App\Models\P2hTemplate;
use App\Models\UnitSupportTimesheet;
use App\Models\User;
use App\Models\Vehicle;
use App\Models\VehicleActivity;
use Carbon\Carbon;
use Database\Seeders\RolePermissionSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;
class EmployeeHistoryTest extends TestCase
{
use RefreshDatabase;
protected User $userA;
protected Employee $empA;
protected User $userB;
protected Employee $empB;
protected Vehicle $heavyEquipment;
protected Vehicle $lightVehicle;
protected Gate $gate;
protected P2hTemplate $template;
protected function setUp(): void
{
parent::setUp();
$this->seed(RolePermissionSeeder::class);
// Karyawan / Operator A
$this->userA = User::create([
'name' => 'Operator Alex Test',
'username' => 'operator_alex',
'email' => '[email protected]',
'password' => Hash::make('password123'),
'role' => 'employee',
]);
$this->userA->givePermissionTo(['employee.dashboard', 'employee.p2h', 'employee.timesheet']);
$this->empA = $this->userA->getOrCreateEmployee();
$this->empA->update(['is_operator' => true]);
// Karyawan / Operator B
$this->userB = User::create([
'name' => 'Operator Budi Test',
'username' => 'operator_budi',
'email' => '[email protected]',
'password' => Hash::make('password123'),
'role' => 'employee',
]);
$this->userB->givePermissionTo(['employee.dashboard', 'employee.p2h', 'employee.timesheet']);
$this->empB = $this->userB->getOrCreateEmployee();
$this->empB->update(['is_operator' => true]);
// Unit Heavy Equipment
$this->heavyEquipment = Vehicle::create([
'code' => 'EXC-HIST-01',
'brand_type' => 'Komatsu PC200 History Test',
'category' => 'unit_support',
'unit_category' => 'Excavator',
'status' => 'Operation',
]);
// Unit Light Vehicle
$this->lightVehicle = Vehicle::create([
'code' => 'LV-HIST-01',
'brand_type' => 'Toyota Hilux 4x4',
'category' => 'light_vehicle',
'license_plate' => 'BH 1234 XY',
'status' => 'Operation',
]);
// Pos Gerbang
$this->gate = Gate::create([
'name' => 'Pos Gerbang Utama PLTA',
'code' => 'PG-01',
'status' => 'active',
]);
// Template P2H
$this->template = P2hTemplate::firstOrCreate(
['code' => 'EXD4RP'],
[
'name' => 'Excavator Checksheet Test',
'is_active' => true,
]
);
}
public function test_guest_is_redirected_to_login(): void
{
$response = $this->get(route('employee.history'));
$response->assertRedirect(route('login'));
}
public function test_employee_can_access_history_page(): void
{
$response = $this->actingAs($this->userA)->get(route('employee.history'));
$response->assertStatus(200);
$response->assertSee('Riwayat Aktivitas Saya');
$response->assertSee('P2H Heavy Equipment');
}
public function test_employee_only_sees_their_own_p2h_records(): void
{
// P2H milik A
$recordA = P2hRecord::create([
'transaction_no' => 'P2H-ALEX-001',
'vehicle_id' => $this->heavyEquipment->id,
'employee_id' => $this->empA->id,
'template_id' => $this->template->id,
'shift' => '1',
'start_hm' => 1250.5,
'summary_condition' => 'good',
'defect_count' => 0,
'validation_status' => 'approved',
'inspected_at' => Carbon::now(),
]);
// P2H milik B
$recordB = P2hRecord::create([
'transaction_no' => 'P2H-BUDI-999',
'vehicle_id' => $this->heavyEquipment->id,
'employee_id' => $this->empB->id,
'template_id' => $this->template->id,
'shift' => '2',
'start_hm' => 2400.0,
'summary_condition' => 'has_defect',
'defect_count' => 1,
'validation_status' => 'pending',
'inspected_at' => Carbon::now(),
]);
// Acting as User A
$responseA = $this->actingAs($this->userA)->get(route('employee.history', ['tab' => 'p2h']));
$responseA->assertStatus(200);
$responseA->assertSee('P2H-ALEX-001');
$responseA->assertDontSee('P2H-BUDI-999');
// Acting as User B
$responseB = $this->actingAs($this->userB)->get(route('employee.history', ['tab' => 'p2h']));
$responseB->assertStatus(200);
$responseB->assertSee('P2H-BUDI-999');
$responseB->assertDontSee('P2H-ALEX-001');
}
public function test_employee_only_sees_their_own_timesheets(): void
{
// Timesheet milik Operator A
UnitSupportTimesheet::create([
'timesheet_no' => 'TS-ALEX-001',
'vehicle_id' => $this->heavyEquipment->id,
'operator_id' => $this->empA->id,
'date' => Carbon::today(),
'shift' => '1',
'meter_type' => 'hm',
'start_meter' => 100,
'end_meter' => 108,
'operating_hours' => 8.0,
'activity_type' => 'Operation',
'location' => 'Incline Shaft Section 1',
'task_description' => 'Mucking and clearing rocks',
'status' => 'completed',
]);
// Timesheet milik Operator B
UnitSupportTimesheet::create([
'timesheet_no' => 'TS-BUDI-999',
'vehicle_id' => $this->heavyEquipment->id,
'operator_id' => $this->empB->id,
'date' => Carbon::today(),
'shift' => '2',
'meter_type' => 'hm',
'start_meter' => 200,
'end_meter' => 206,
'operating_hours' => 6.0,
'activity_type' => 'Operation',
'location' => 'Powerhouse Cavern',
'task_description' => 'Excavation trench',
'status' => 'completed',
]);
// Acting as User A
$responseA = $this->actingAs($this->userA)->get(route('employee.history', ['tab' => 'timesheet']));
$responseA->assertStatus(200);
$responseA->assertSee('TS-ALEX-001');
$responseA->assertSee('Incline Shaft Section 1');
$responseA->assertDontSee('TS-BUDI-999');
$responseA->assertDontSee('Powerhouse Cavern');
// Acting as User B
$responseB = $this->actingAs($this->userB)->get(route('employee.history', ['tab' => 'timesheet']));
$responseB->assertStatus(200);
$responseB->assertSee('TS-BUDI-999');
$responseB->assertSee('Powerhouse Cavern');
$responseB->assertDontSee('TS-ALEX-001');
}
public function test_employee_only_sees_their_own_vehicle_activity_trips(): void
{
// Trip milik Driver A
VehicleActivity::create([
'transaction_no' => 'TRIP-ALEX-001',
'vehicle_id' => $this->lightVehicle->id,
'driver_id' => $this->empA->id,
'gate_id' => $this->gate->id,
'direction' => 'out',
'previous_km' => 10000,
'current_km' => 10035,
'distance' => 35,
'purpose' => 'Antar Logistik ke Camp Merangin Alex',
'recorded_at' => Carbon::now(),
'validation_status' => 'approved',
]);
// Trip milik Driver B
VehicleActivity::create([
'transaction_no' => 'TRIP-BUDI-999',
'vehicle_id' => $this->lightVehicle->id,
'driver_id' => $this->empB->id,
'gate_id' => $this->gate->id,
'direction' => 'in',
'previous_km' => 20000,
'current_km' => 20050,
'distance' => 50,
'purpose' => 'Patroli Jalur Pipa Budi',
'recorded_at' => Carbon::now(),
'validation_status' => 'approved',
]);
// Acting as User A
$responseA = $this->actingAs($this->userA)->get(route('employee.history', ['tab' => 'trips']));
$responseA->assertStatus(200);
$responseA->assertSee('TRIP-ALEX-001');
$responseA->assertSee('Antar Logistik ke Camp Merangin Alex');
$responseA->assertDontSee('TRIP-BUDI-999');
$responseA->assertDontSee('Patroli Jalur Pipa Budi');
// Acting as User B
$responseB = $this->actingAs($this->userB)->get(route('employee.history', ['tab' => 'trips']));
$responseB->assertStatus(200);
$responseB->assertSee('TRIP-BUDI-999');
$responseB->assertSee('Patroli Jalur Pipa Budi');
$responseB->assertDontSee('TRIP-ALEX-001');
}
public function test_tabs_adapt_according_to_employee_permissions_and_records(): void
{
// User C: Hanya punya akses Timesheet LV, tidak punya P2H dan bukan operator
$userC = User::create([
'name' => 'Driver Cici Test',
'username' => 'driver_cici',
'email' => '[email protected]',
'password' => Hash::make('password123'),
'role' => 'employee',
]);
$userC->syncRoles([]);
$userC->givePermissionTo(['employee.dashboard', 'employee.timesheet']);
$empC = $userC->getOrCreateEmployee();
$empC->update(['is_operator' => false]);
$response = $this->actingAs($userC)->get(route('employee.history'));
$response->assertStatus(200);
$response->assertSee('Mobilisasi');
$response->assertSee('Pos Gerbang');
$response->assertDontSee('P2H Heavy Equipment');
$response->assertDontSee('Timesheet Heavy Equipment');
}
public function test_employee_can_view_own_p2h_detail_and_pdf(): void
{
$recordA = P2hRecord::create([
'transaction_no' => 'P2H-DETAIL-001',
'vehicle_id' => $this->heavyEquipment->id,
'employee_id' => $this->empA->id,
'template_id' => $this->template->id,
'shift' => '1',
'start_hm' => 1500,
'summary_condition' => 'good',
'defect_count' => 0,
'validation_status' => 'approved',
'inspected_at' => Carbon::now(),
]);
P2hRecordItem::create([
'p2h_record_id' => $recordA->id,
'section' => 'Cabin Inspection',
'title' => 'Kaca Spion & Wiper',
'question_type' => 'choice',
'condition_status' => 'baik',
]);
// Detail JSON
$resJson = $this->actingAs($this->userA)->get(route('employee.history.p2h_detail', $recordA->id));
$resJson->assertStatus(200);
$resJson->assertJsonFragment([
'transaction_no' => 'P2H-DETAIL-001',
'item_name' => 'Kaca Spion & Wiper',
]);
// PDF stream
$resPdf = $this->actingAs($this->userA)->get(route('employee.history.p2h_pdf', $recordA->id));
$resPdf->assertStatus(200);
$resPdf->assertHeader('Content-Type', 'application/pdf');
}
public function test_employee_cannot_view_or_download_pdf_of_other_employee_p2h(): void
{
// Record milik B
$recordB = P2hRecord::create([
'transaction_no' => 'P2H-PRIVATE-BUDI',
'vehicle_id' => $this->heavyEquipment->id,
'employee_id' => $this->empB->id,
'template_id' => $this->template->id,
'shift' => '1',
'start_hm' => 2000,
'summary_condition' => 'good',
'defect_count' => 0,
'validation_status' => 'approved',
'inspected_at' => Carbon::now(),
]);
// User A mencoba melihat detail B -> 403 Forbidden
$resDetail = $this->actingAs($this->userA)->get(route('employee.history.p2h_detail', $recordB->id));
$resDetail->assertStatus(403);
// User A mencoba mengunduh PDF B -> 403 Forbidden
$resPdf = $this->actingAs($this->userA)->get(route('employee.history.p2h_pdf', $recordB->id));
$resPdf->assertStatus(403);
}
}