2026-10-15 13:53:52 +07:00
<? php
namespace App\Http\Controllers\Employee ;
use App\Http\Controllers\Controller ;
use App\Models\Vehicle ;
2026-10-19 23:21:55 +07:00
use App\Models\VehicleActivity ;
use Illuminate\Http\RedirectResponse ;
2026-10-15 13:53:52 +07:00
use Illuminate\Http\Request ;
use Illuminate\View\View ;
class DashboardController extends Controller
{
/**
* Tampilkan halaman dashboard utama karyawan / operator.
*/
public function index () : View
{
2026-10-19 23:21:55 +07:00
$user = auth () -> user ();
if ( ! $user -> isSuperAdmin () && ! $user -> can ( 'employee.dashboard' ) && ! $user -> can ( 'employee.timesheet' ) && ! $user -> can ( 'employee.p2h' )) {
abort ( 403 , 'Akses Ditolak: Anda tidak memiliki wewenang untuk membuka halaman operasional karyawan.' );
}
$employee = $user -> getOrCreateEmployee ();
2026-10-15 13:53:52 +07:00
$totalSupport = Vehicle :: unitSupports () -> count ();
$totalLv = Vehicle :: lightVehicles () -> count ();
2026-10-19 23:21:55 +07:00
$pendingActivity = null ;
$myRecentActivities = collect ();
if ( $employee ) {
2026-10-22 12:23:13 +07:00
// Otomatis batalkan perjalanan pending yang sudah melewati batas waktu 5 jam
VehicleActivity :: expireOverduePendingActivities ();
2026-10-19 23:21:55 +07:00
$pendingActivity = VehicleActivity :: with ([ 'vehicle' , 'gate' ])
-> where ( 'driver_id' , $employee -> id )
-> where ( 'validation_status' , 'pending' )
-> latest ( 'recorded_at' )
-> first ();
$myRecentActivities = VehicleActivity :: with ([ 'vehicle' , 'gate' , 'securityGuard' ])
-> where ( 'driver_id' , $employee -> id )
-> latest ( 'recorded_at' )
-> take ( 6 )
-> get ();
}
return view ( 'employee.dashboard' , compact (
'employee' ,
'totalSupport' ,
'totalLv' ,
'pendingActivity' ,
'myRecentActivities'
));
}
/**
2026-10-20 19:36:54 +07:00
* Koreksi angka KM pada perjalanan yang masih berstatus pending oleh pengemudi.
2026-10-19 23:21:55 +07:00
*/
public function updateKm ( Request $request , int $id ) : RedirectResponse
{
$employee = auth () -> user () -> getOrCreateEmployee ();
if ( ! $employee ) {
return redirect () -> route ( 'employee.dashboard' ) -> with ( 'error' , 'Profil karyawan tidak ditemukan.' );
}
$activity = VehicleActivity :: with ( 'vehicle' ) -> findOrFail ( $id );
if ( $activity -> driver_id !== $employee -> id ) {
2026-10-20 19:36:54 +07:00
abort ( 403 , 'Aksi Ditolak: Anda tidak memiliki akses untuk mengubah perjalanan ini.' );
2026-10-19 23:21:55 +07:00
}
if ( $activity -> validation_status !== 'pending' ) {
return redirect () -> route ( 'employee.dashboard' )
2026-10-20 19:36:54 +07:00
-> with ( 'error' , 'Gagal: Perjalanan ini sudah diproses/disetujui oleh petugas Satpam di pos gerbang dan terkunci permanen.' );
2026-10-19 23:21:55 +07:00
}
$validated = $request -> validate ([
'current_km' => [ 'required' , 'numeric' , 'min:0' ],
], [
'current_km.required' => 'Nilai odometer KM saat ini wajib diisi.' ,
'current_km.numeric' => 'Nilai odometer harus berupa angka.' ,
'current_km.min' => 'Nilai odometer tidak boleh negatif.' ,
]);
$previousKm = ( float ) $activity -> previous_km ;
$newCurrentKm = ( float ) $validated [ 'current_km' ];
if ( $newCurrentKm < $previousKm ) {
return back () -> withInput () -> with ( 'error' , "Nilai kilometer input ( { $newCurrentKm } KM) tidak boleh lebih kecil dari kilometer awal ( { $previousKm } KM)." );
}
$newDistance = max ( 0 , $newCurrentKm - $previousKm );
$activity -> update ([
'current_km' => $newCurrentKm ,
'distance' => $newDistance ,
]);
return redirect () -> route ( 'employee.dashboard' )
2026-10-20 19:36:54 +07:00
-> with ( 'success' , "Odometer perjalanan { $activity -> transaction_no } ( { $activity -> vehicle -> code } ) berhasil diperbaiki menjadi " . number_format ( $newCurrentKm , 0 , ',' , '.' ) . " KM." );
2026-10-19 23:21:55 +07:00
}
/**
2026-10-20 19:36:54 +07:00
* Batalkan perjalanan yang masih berstatus pending oleh pengemudi.
2026-10-19 23:21:55 +07:00
*/
public function cancelActivity ( Request $request , int $id ) : RedirectResponse
{
$employee = auth () -> user () -> getOrCreateEmployee ();
if ( ! $employee ) {
return redirect () -> route ( 'employee.dashboard' ) -> with ( 'error' , 'Profil karyawan tidak ditemukan.' );
}
$activity = VehicleActivity :: with ( 'vehicle' ) -> findOrFail ( $id );
if ( $activity -> driver_id !== $employee -> id ) {
2026-10-20 19:36:54 +07:00
abort ( 403 , 'Aksi Ditolak: Anda tidak memiliki akses untuk membatalkan perjalanan ini.' );
2026-10-19 23:21:55 +07:00
}
if ( $activity -> validation_status !== 'pending' ) {
return redirect () -> route ( 'employee.dashboard' )
2026-10-20 19:36:54 +07:00
-> with ( 'error' , 'Gagal: Perjalanan ini sudah diproses/disetujui oleh petugas Satpam di pos gerbang dan tidak dapat dibatalkan.' );
2026-10-19 23:21:55 +07:00
}
$reason = trim ( $request -> input ( 'cancel_reason' , 'Dibatalkan mandiri oleh pengemudi' ));
$activity -> update ([
'validation_status' => 'cancelled' ,
'rejection_note' => $reason ,
]);
return redirect () -> route ( 'employee.dashboard' )
2026-10-20 19:36:54 +07:00
-> with ( 'success' , "Perjalanan { $activity -> transaction_no } ( { $activity -> vehicle -> code } ) berhasil dibatalkan." );
2026-10-15 13:53:52 +07:00
}
}