diff --git a/app/Console/Commands/ExpirePendingActivitiesCommand.php b/app/Console/Commands/ExpirePendingActivitiesCommand.php new file mode 100644 index 0000000..1ba3432 --- /dev/null +++ b/app/Console/Commands/ExpirePendingActivitiesCommand.php @@ -0,0 +1,42 @@ +option('hours'); + if ($hours <= 0) { + $hours = VehicleActivity::PENDING_TIMEOUT_HOURS; + } + + $this->info("Memeriksa tiket perjalanan pending yang berusia lebih dari {$hours} jam..."); + + $affected = VehicleActivity::expireOverduePendingActivities($hours); + + $this->info("Selesai: Sebanyak {$affected} tiket perjalanan berhasil dibatalkan otomatis."); + + return Command::SUCCESS; + } +} diff --git a/app/Http/Controllers/Employee/DashboardController.php b/app/Http/Controllers/Employee/DashboardController.php index feee870..10ec240 100644 --- a/app/Http/Controllers/Employee/DashboardController.php +++ b/app/Http/Controllers/Employee/DashboardController.php @@ -29,6 +29,9 @@ class DashboardController extends Controller $myRecentActivities = collect(); if ($employee) { + // Otomatis batalkan perjalanan pending yang sudah melewati batas waktu 5 jam + VehicleActivity::expireOverduePendingActivities(); + $pendingActivity = VehicleActivity::with(['vehicle', 'gate']) ->where('driver_id', $employee->id) ->where('validation_status', 'pending') diff --git a/app/Http/Controllers/Pg/GateInspectionController.php b/app/Http/Controllers/Pg/GateInspectionController.php index 9b3767b..3e21c5f 100644 --- a/app/Http/Controllers/Pg/GateInspectionController.php +++ b/app/Http/Controllers/Pg/GateInspectionController.php @@ -46,6 +46,9 @@ class GateInspectionController extends Controller $needsGateSelection = true; } + // Otomatis bersihkan antrean pending yang sudah lewat batas 5 jam + VehicleActivity::expireOverduePendingActivities(); + $pendingActivities = collect(); if ($gate) { $pendingActivities = VehicleActivity::where('gate_id', $gate->id) @@ -112,6 +115,9 @@ class GateInspectionController extends Controller ?: ($user ? $user->gate_id : null); $gate = $gateId ? Gate::find($gateId) : Gate::first(); + // Otomatis bersihkan antrean pending yang sudah lewat batas 5 jam + VehicleActivity::expireOverduePendingActivities(); + $pendingActivities = collect(); if ($gate) { $pendingActivities = VehicleActivity::where('gate_id', $gate->id) diff --git a/app/Http/Controllers/Pg/VerificationLogController.php b/app/Http/Controllers/Pg/VerificationLogController.php index b4fc650..84850d8 100644 --- a/app/Http/Controllers/Pg/VerificationLogController.php +++ b/app/Http/Controllers/Pg/VerificationLogController.php @@ -3,13 +3,16 @@ namespace App\Http\Controllers\Pg; use App\Http\Controllers\Controller; +use App\Models\AdminActivityLog; use App\Models\Gate; use App\Models\SecurityGuard; use App\Models\VehicleActivity; use App\Services\PgTablePdfExporter; use App\Services\SimpleXlsxExporter; use Carbon\Carbon; +use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; use Illuminate\View\View; use Symfony\Component\HttpFoundation\Response; @@ -20,6 +23,9 @@ class VerificationLogController extends Controller */ protected function getVerificationLogsQuery(Request $request) { + // Otomatis batalkan antrean pending yang melewati batas 5 jam + VehicleActivity::expireOverduePendingActivities(); + $query = VehicleActivity::with(['vehicle', 'driver', 'gate', 'securityGuard']) ->orderByRaw('COALESCE(validated_at, recorded_at) DESC'); @@ -314,4 +320,60 @@ class VerificationLogController extends Controller 'Content-Disposition' => 'inline; filename="' . $fileName . '"', ]); } + + /** + * Hapus riwayat log verifikasi gerbang oleh Admin yang berwenang. + */ + public function destroy(int $id): RedirectResponse + { + $activity = VehicleActivity::with(['vehicle', 'driver', 'gate', 'securityGuard'])->findOrFail($id); + + $vehicleCode = $activity->vehicle?->code ?? '-'; + $trxNo = $activity->transaction_no; + $driverName = $activity->driver?->name ?? 'Pengemudi'; + $gateName = $activity->gate?->name ?? 'Gerbang'; + + DB::transaction(function () use ($activity, $vehicleCode, $trxNo, $driverName, $gateName) { + // Jika aktivitas yang dihapus adalah aktivitas approved terakhir, kembalikan posisi & km master jika perlu + if ($activity->validation_status === 'approved' && $activity->vehicle) { + $prevApproved = VehicleActivity::where('vehicle_id', $activity->vehicle_id) + ->where('id', '!=', $activity->id) + ->where('validation_status', 'approved') + ->latest('recorded_at') + ->first(); + + if ($prevApproved) { + $activity->vehicle->update([ + 'current_km' => $prevApproved->current_km, + ]); + } + } + + // Catat log audit ke AdminActivityLog + AdminActivityLog::record( + actionType: 'delete', + module: 'Keamanan & Pos Satpam (PG)', + description: "Menghapus riwayat verifikasi perlintasan gerbang {$trxNo} pada unit {$vehicleCode} di pos {$gateName} (Driver: {$driverName}).", + targetType: 'VehicleActivity', + targetId: $activity->id, + targetName: "{$vehicleCode} ({$trxNo})", + oldValues: [ + 'transaction_no' => $trxNo, + 'current_km' => (float)$activity->current_km, + 'direction' => $activity->direction, + 'gate' => $gateName, + 'validation_status' => $activity->validation_status, + ], + newValues: null + ); + + // Hapus relasi riwayat lokasi (jika ada) + $activity->locationHistories()->delete(); + + // Hapus aktivitas + $activity->delete(); + }); + + return back()->with('success', "Riwayat log verifikasi gerbang {$trxNo} ({$vehicleCode}) berhasil dihapus."); + } } diff --git a/app/Http/Controllers/Public/VehicleActivityFormController.php b/app/Http/Controllers/Public/VehicleActivityFormController.php index 623903d..9c22b67 100644 --- a/app/Http/Controllers/Public/VehicleActivityFormController.php +++ b/app/Http/Controllers/Public/VehicleActivityFormController.php @@ -66,6 +66,9 @@ class VehicleActivityFormController extends Controller ->orderBy('name') ->get(); + // Bersihkan data aktivitas pending yang telah kedaluwarsa (> 5 jam) + VehicleActivity::expireOverduePendingActivities(); + // Tentukan arah yang diperbolehkan berdasarkan riwayat terakhir $allowedDirection = $vehicle->determineNextAllowedDirection(); diff --git a/app/Http/Controllers/Qma/ActivityLogController.php b/app/Http/Controllers/Qma/ActivityLogController.php index b64716c..59296cd 100644 --- a/app/Http/Controllers/Qma/ActivityLogController.php +++ b/app/Http/Controllers/Qma/ActivityLogController.php @@ -23,6 +23,9 @@ class ActivityLogController extends Controller */ private function getFilteredActivityQuery(Request $request): Builder { + // Otomatis update status perjalanan pending yang kedaluwarsa (> 5 jam) + VehicleActivity::expireOverduePendingActivities(); + $query = VehicleActivity::with(['vehicle', 'driver', 'gate', 'securityGuard']) ->latest('recorded_at'); @@ -741,4 +744,59 @@ class ActivityLogController extends Controller return back()->with('success', "Odometer transaksi {$activity->transaction_no} ({$activity->vehicle->code}) berhasil dikoreksi menjadi " . number_format($newCurrentKm, 0, ',', '.') . " KM."); } + + /** + * Hapus catatan aktivitas perjalanan kendaraan (Timesheet LV) dari sistem. + */ + public function destroy(int $id): RedirectResponse + { + $activity = VehicleActivity::with(['vehicle', 'driver'])->findOrFail($id); + + $vehicleCode = $activity->vehicle?->code ?? '-'; + $trxNo = $activity->transaction_no; + $driverName = $activity->driver?->name ?? 'Pengemudi'; + + DB::transaction(function () use ($activity, $vehicleCode, $trxNo, $driverName) { + // Jika aktivitas yang dihapus adalah aktivitas approved terakhir, sinkronkan master km ke aktivitas sebelumnya + if ($activity->validation_status === 'approved' && $activity->vehicle) { + $prevApproved = VehicleActivity::where('vehicle_id', $activity->vehicle_id) + ->where('id', '!=', $activity->id) + ->where('validation_status', 'approved') + ->latest('recorded_at') + ->first(); + + if ($prevApproved) { + $activity->vehicle->update([ + 'current_km' => $prevApproved->current_km, + ]); + } + } + + // Catat log audit ke AdminActivityLog + AdminActivityLog::record( + actionType: 'delete', + module: 'Operasional Kendaraan (QMA)', + description: "Menghapus catatan perjalanan {$trxNo} pada unit {$vehicleCode} (Driver: {$driverName}).", + targetType: 'VehicleActivity', + targetId: $activity->id, + targetName: "{$vehicleCode} ({$trxNo})", + oldValues: [ + 'transaction_no' => $trxNo, + 'current_km' => (float)$activity->current_km, + 'distance' => (float)$activity->distance, + 'direction' => $activity->direction, + 'validation_status' => $activity->validation_status, + ], + newValues: null + ); + + // Hapus relasi riwayat lokasi (jika ada) + $activity->locationHistories()->delete(); + + // Hapus aktivitas + $activity->delete(); + }); + + return back()->with('success', "Catatan perjalanan {$trxNo} ({$vehicleCode}) berhasil dihapus dari sistem."); + } } diff --git a/app/Models/VehicleActivity.php b/app/Models/VehicleActivity.php index f02d684..ac5716a 100644 --- a/app/Models/VehicleActivity.php +++ b/app/Models/VehicleActivity.php @@ -2,6 +2,7 @@ namespace App\Models; +use Carbon\Carbon; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -12,6 +13,8 @@ class VehicleActivity extends Model { use HasFactory; + public const PENDING_TIMEOUT_HOURS = 5; + protected $fillable = [ 'transaction_no', 'vehicle_id', @@ -95,6 +98,54 @@ class VehicleActivity extends Model return $this->validation_status === 'rejected'; } + /** + * Cek apakah tiket perjalanan pending sudah melebihi batas waktu 5 jam. + */ + public function isOverdue(int $hours = self::PENDING_TIMEOUT_HOURS): bool + { + if ($this->validation_status !== 'pending' || !$this->recorded_at) { + return false; + } + + return $this->recorded_at->copy()->addHours($hours)->isPast(); + } + + /** + * Waktu tepat tiket kedaluwarsa (recorded_at + 5 jam). + */ + public function getExpiresAtAttribute(): ?Carbon + { + return $this->recorded_at ? $this->recorded_at->copy()->addHours(self::PENDING_TIMEOUT_HOURS) : null; + } + + /** + * Hitung sisa detik sebelum tiket kedaluwarsa. + */ + public function getRemainingSecondsAttribute(): int + { + if ($this->validation_status !== 'pending' || !$this->recorded_at) { + return 0; + } + + $expireTime = $this->expires_at; + return max(0, (int) Carbon::now()->diffInSeconds($expireTime, false)); + } + + /** + * Batalkan otomatis seluruh tiket pending yang sudah melebihi batas waktu (default 5 jam). + */ + public static function expireOverduePendingActivities(int $hours = self::PENDING_TIMEOUT_HOURS): int + { + $cutoff = Carbon::now()->subHours($hours); + + return static::where('validation_status', 'pending') + ->where('recorded_at', '<=', $cutoff) + ->update([ + 'validation_status' => 'cancelled', + 'rejection_note' => "Dibatalkan sistem: Melebihi batas waktu verifikasi ({$hours} jam)", + ]); + } + public function scopePending(Builder $query): Builder { return $query->where('validation_status', 'pending'); diff --git a/config/rbac_permissions.php b/config/rbac_permissions.php index c89d21a..de9076e 100644 --- a/config/rbac_permissions.php +++ b/config/rbac_permissions.php @@ -100,6 +100,10 @@ return [ 'label' => 'Koreksi Odometer & Kelola Timesheet Support', 'description' => 'Mengoreksi angka kilometer perjalanan LV serta menambah, mengubah, dan menghapus Timesheet Unit Support.', ], + 'qma.logs.delete' => [ + 'label' => 'Menghapus Catatan Perjalanan (Timesheet LV)', + 'description' => 'Menghapus riwayat data perjalanan mobilisasi kendaraan operasional dari sistem.', + ], 'qma.summary.view' => [ 'label' => 'Melihat Rekapitulasi & Laporan Armada', 'description' => 'Melihat total kilometer, jumlah perjalanan, dan ringkasan penggunaan armada per periode.', @@ -146,6 +150,10 @@ return [ 'label' => 'Melihat Riwayat Keluar-Masuk Gerbang', 'description' => 'Melihat catatan kendaraan yang diperiksa, disetujui, atau ditolak oleh satpam di pos gerbang.', ], + 'pg.logs.delete' => [ + 'label' => 'Menghapus Riwayat Verifikasi Gerbang', + 'description' => 'Menghapus catatan log verifikasi perlintasan kendaraan pos gerbang dari sistem.', + ], ], ], 'gate' => [ diff --git a/resources/views/employee/dashboard.blade.php b/resources/views/employee/dashboard.blade.php index 56b6f6e..6b39e34 100644 --- a/resources/views/employee/dashboard.blade.php +++ b/resources/views/employee/dashboard.blade.php @@ -83,7 +83,15 @@ -
+ {{ $item->rejection_note }} +
+ @endif +