diff --git a/app/Http/Controllers/Employee/DashboardController.php b/app/Http/Controllers/Employee/DashboardController.php index 10ec240..60875f4 100644 --- a/app/Http/Controllers/Employee/DashboardController.php +++ b/app/Http/Controllers/Employee/DashboardController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Employee; use App\Http\Controllers\Controller; use App\Models\Vehicle; use App\Models\VehicleActivity; +use Illuminate\Http\JsonResponse; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\View\View; @@ -132,4 +133,72 @@ class DashboardController extends Controller return redirect()->route('employee.dashboard') ->with('success', "Perjalanan {$activity->transaction_no} ({$activity->vehicle->code}) berhasil dibatalkan."); } + + /** + * Endpoint micro-polling super ringan (JSON) untuk memantau status validasi tiket perjalanan driver. + * Menghemat bandwidth & CPU server dibanding me-refresh halaman penuh secara berulang. + */ + public function activeTripStatus(Request $request): JsonResponse + { + $user = auth()->user(); + if (!$user) { + return response()->json(['status' => 'unauthenticated'], 401); + } + + $employee = $user->getOrCreateEmployee(); + if (!$employee) { + return response()->json(['status' => 'no_employee', 'has_pending' => false]); + } + + $activityId = $request->query('activity_id'); + + $activity = null; + if ($activityId) { + $activity = VehicleActivity::with(['vehicle', 'gate', 'securityGuard']) + ->where('driver_id', $employee->id) + ->find($activityId); + } + + if (!$activity) { + // Ambil tiket pending terbaru yang masih aktif + $activity = VehicleActivity::with(['vehicle', 'gate', 'securityGuard']) + ->where('driver_id', $employee->id) + ->where('validation_status', 'pending') + ->latest('recorded_at') + ->first(); + } + + // Jika masih null, ambil tiket paling akhir dari driver ini + if (!$activity) { + $activity = VehicleActivity::with(['vehicle', 'gate', 'securityGuard']) + ->where('driver_id', $employee->id) + ->latest('recorded_at') + ->first(); + } + + if (!$activity) { + return response()->json([ + 'has_pending' => false, + 'status' => 'none', + ]); + } + + return response()->json([ + 'id' => $activity->id, + 'transaction_no' => $activity->transaction_no, + 'vehicle_code' => $activity->vehicle?->code ?? '-', + 'vehicle_name' => $activity->vehicle?->brand_type ?? '-', + 'status' => $activity->validation_status, + 'has_pending' => $activity->validation_status === 'pending', + 'is_approved' => $activity->validation_status === 'approved', + 'is_rejected' => $activity->validation_status === 'rejected', + 'is_cancelled' => $activity->validation_status === 'cancelled', + 'cancellation_source' => $activity->cancellation_source_label, + 'gate_name' => $activity->gate?->name ?? 'Pos Gerbang', + 'guard_name' => $activity->securityGuard?->name ?? 'Petugas Satpam', + 'rejection_note' => $activity->rejection_note, + 'recorded_at' => $activity->recorded_at ? $activity->recorded_at->format('H:i') . ' WIB' : null, + 'validated_at' => $activity->validated_at ? $activity->validated_at->format('H:i') . ' WIB' : null, + ]); + } } diff --git a/app/Http/Controllers/Pg/VerificationLogController.php b/app/Http/Controllers/Pg/VerificationLogController.php index 84850d8..396b705 100644 --- a/app/Http/Controllers/Pg/VerificationLogController.php +++ b/app/Http/Controllers/Pg/VerificationLogController.php @@ -67,9 +67,28 @@ class VerificationLogController extends Controller $query->where('direction', $request->input('direction')); } - // Filter Status Verifikasi (approved, rejected, pending) + // Filter Status Verifikasi (approved, rejected, pending, cancelled, cancelled_system, cancelled_driver) if ($request->filled('status')) { - $query->where('validation_status', $request->input('status')); + $status = $request->input('status'); + if ($status === 'cancelled_system') { + $query->where('validation_status', 'cancelled') + ->where(function ($q) { + $q->where('rejection_note', 'like', '%sistem%') + ->orWhere('rejection_note', 'like', '%timeout%') + ->orWhere('rejection_note', 'like', '%melebihi batas%'); + }); + } elseif ($status === 'cancelled_driver') { + $query->where('validation_status', 'cancelled') + ->where(function ($q) { + $q->where(function ($sub) { + $sub->where('rejection_note', 'not like', '%sistem%') + ->where('rejection_note', 'not like', '%timeout%') + ->where('rejection_note', 'not like', '%melebihi batas%'); + })->orWhereNull('rejection_note'); + }); + } else { + $query->where('validation_status', $status); + } } // Filter Kata Kunci Pencarian (No Transaksi, Petugas Satpam, Badge, Driver, NIK, Plat, Kode LV) @@ -188,6 +207,7 @@ class VerificationLogController extends Controller $status = match($item->validation_status) { 'approved' => 'Disetujui', 'rejected' => 'Ditolak', + 'cancelled' => $item->cancellation_source_label, default => 'Menunggu', }; @@ -285,6 +305,7 @@ class VerificationLogController extends Controller $status = match($item->validation_status) { 'approved' => 'Disetujui', 'rejected' => 'Ditolak' . ($item->rejection_note ? ' (' . $item->rejection_note . ')' : ''), + 'cancelled' => $item->cancellation_source_label, default => 'Menunggu', }; $odometer = number_format($item->current_km ?? 0, 0, ',', '.') . ' KM'; diff --git a/app/Http/Controllers/Public/VehicleActivityFormController.php b/app/Http/Controllers/Public/VehicleActivityFormController.php index 9c22b67..ae681e4 100644 --- a/app/Http/Controllers/Public/VehicleActivityFormController.php +++ b/app/Http/Controllers/Public/VehicleActivityFormController.php @@ -261,7 +261,7 @@ class VehicleActivityFormController extends Controller }); return redirect() - ->route('vehicle.activity.form', ['code' => $code]) - ->with('success', 'Aktivitas kendaraan berhasil dicatat! Silakan lapor ke petugas Satpam di pos gerbang untuk verifikasi fisik.'); + ->route('employee.dashboard') + ->with('success', 'Form perjalanan berhasil dikirim! Silakan lapor ke petugas Satpam di pos gerbang untuk verifikasi fisik.'); } } diff --git a/app/Http/Controllers/Qma/ActivityLogController.php b/app/Http/Controllers/Qma/ActivityLogController.php index 59296cd..a938961 100644 --- a/app/Http/Controllers/Qma/ActivityLogController.php +++ b/app/Http/Controllers/Qma/ActivityLogController.php @@ -69,9 +69,28 @@ class ActivityLogController extends Controller }); } - // Filter Status Validasi (pending, approved, rejected) + // Filter Status Validasi (pending, approved, rejected, cancelled, cancelled_system, cancelled_driver) if ($request->filled('status')) { - $query->where('validation_status', $request->input('status')); + $status = $request->input('status'); + if ($status === 'cancelled_system') { + $query->where('validation_status', 'cancelled') + ->where(function (Builder $q) { + $q->where('rejection_note', 'like', '%sistem%') + ->orWhere('rejection_note', 'like', '%timeout%') + ->orWhere('rejection_note', 'like', '%melebihi batas%'); + }); + } elseif ($status === 'cancelled_driver') { + $query->where('validation_status', 'cancelled') + ->where(function (Builder $q) { + $q->where(function (Builder $sub) { + $sub->where('rejection_note', 'not like', '%sistem%') + ->where('rejection_note', 'not like', '%timeout%') + ->where('rejection_note', 'not like', '%melebihi batas%'); + })->orWhereNull('rejection_note'); + }); + } else { + $query->where('validation_status', $status); + } } return $query; @@ -151,7 +170,12 @@ class ActivityLogController extends Controller $currKm = $act->current_km !== null ? number_format($act->current_km, 0, ',', '.') : '-'; $diffKm = $act->distance !== null ? number_format($act->distance, 0, ',', '.') : '-'; $purpose = $act->purpose ?: '-'; - $statusPg = $act->validation_status === 'approved' ? 'Disetujui' : ($act->validation_status === 'rejected' ? 'Ditolak' : ($act->validation_status === 'cancelled' ? 'Dibatalkan' : 'Menunggu PG')); + $statusPg = match($act->validation_status) { + 'approved' => 'Disetujui', + 'rejected' => 'Ditolak' . ($act->rejection_note ? ' (' . $act->rejection_note . ')' : ''), + 'cancelled' => $act->cancellation_source_label, + default => 'Menunggu PG', + }; if ($act->is_corrected) { $statusPg .= ' (Terkoreksi)'; } @@ -255,7 +279,12 @@ class ActivityLogController extends Controller $currKm = $act->current_km !== null ? number_format($act->current_km, 0, ',', '.') : '-'; $diffKm = $act->distance !== null ? number_format($act->distance, 0, ',', '.') . ' KM' : '-'; $purpose = $act->purpose ?: '-'; - $statusPg = $act->validation_status === 'approved' ? 'Disetujui' : ($act->validation_status === 'rejected' ? 'Ditolak' : ($act->validation_status === 'cancelled' ? 'Dibatalkan' : 'Menunggu PG')); + $statusPg = match($act->validation_status) { + 'approved' => 'Disetujui', + 'rejected' => 'Ditolak' . ($act->rejection_note ? ' (' . $act->rejection_note . ')' : ''), + 'cancelled' => $act->cancellation_source_label, + default => 'Menunggu PG', + }; if ($act->is_corrected) { $statusPg .= ' (Terkoreksi)'; } diff --git a/app/Models/VehicleActivity.php b/app/Models/VehicleActivity.php index ac5716a..bf4ce0f 100644 --- a/app/Models/VehicleActivity.php +++ b/app/Models/VehicleActivity.php @@ -93,6 +93,43 @@ class VehicleActivity extends Model return $this->validation_status === 'cancelled'; } + /** + * Cek apakah pembatalan dilakukan otomatis oleh sistem (timeout > 5 jam). + */ + public function isCancelledBySystem(): bool + { + if ($this->validation_status !== 'cancelled') { + return false; + } + + $note = strtolower((string) $this->rejection_note); + return str_contains($note, 'sistem') || str_contains($note, 'timeout') || str_contains($note, 'melebihi batas'); + } + + /** + * Cek apakah pembatalan dilakukan mandiri oleh pengguna / driver. + */ + public function isCancelledByUser(): bool + { + return $this->validation_status === 'cancelled' && !$this->isCancelledBySystem(); + } + + /** + * Label sumber pembatalan yang deskriptif dan ramah pengguna. + */ + public function getCancellationSourceLabelAttribute(): string + { + if ($this->isCancelledBySystem()) { + return 'Batal Sistem (Timeout)'; + } + + if ($this->isCancelledByUser()) { + return 'Dibatalkan Pengemudi'; + } + + return 'Dibatalkan'; + } + public function isRejected(): bool { return $this->validation_status === 'rejected'; diff --git a/resources/views/employee/dashboard.blade.php b/resources/views/employee/dashboard.blade.php index 6b39e34..b4a3cd5 100644 --- a/resources/views/employee/dashboard.blade.php +++ b/resources/views/employee/dashboard.blade.php @@ -59,7 +59,9 @@ {{-- CARD PERJALANAN AKTIF (MENUNGGU PEMERIKSAAN SATPAM) --}} @if(isset($pendingActivity) && $pendingActivity) -
+
{{-- Dekorasi Latar Belakang --}}
@@ -97,6 +99,11 @@ Menunggu Pemeriksaan Satpam + + + + Auto-Check Aktif +
@@ -366,9 +373,23 @@ Disetujui @elseif($act->validation_status === 'cancelled') - - Dibatalkan - + @if($act->isCancelledBySystem()) +
+ + Batal Sistem + + Timeout > 5 Jam +
+ @else +
+ + Batal Pengemudi + + @if($act->rejection_note && $act->rejection_note !== 'Dibatalkan mandiri oleh pengemudi') + {{ $act->rejection_note }} + @endif +
+ @endif @elseif($act->validation_status === 'rejected') Ditolak @@ -590,6 +611,54 @@
+{{-- MODAL 4: NOTIFIKASI HASIL VALIDASI POS GERBANG (POLLING REAL-TIME) --}} + +