2026-10-12 23:47:41 +07:00
<? php
namespace App\Models ;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Database\Factories\UserFactory ;
use Illuminate\Database\Eloquent\Factories\HasFactory ;
use Illuminate\Database\Eloquent\Relations\BelongsTo ;
2026-10-22 09:28:19 +07:00
use Illuminate\Database\Eloquent\Relations\HasMany ;
2026-10-13 13:46:35 +07:00
use Illuminate\Database\Eloquent\Relations\HasOne ;
2026-10-12 23:47:41 +07:00
use Illuminate\Foundation\Auth\User as Authenticatable ;
use Illuminate\Notifications\Notifiable ;
2026-10-28 09:47:16 +07:00
use Illuminate\Database\Eloquent\SoftDeletes ;
2026-10-15 15:03:24 +07:00
use Spatie\Permission\Traits\HasRoles ;
2026-10-12 23:47:41 +07:00
class User extends Authenticatable
{
/** @use HasFactory<UserFactory> */
2026-10-28 09:47:16 +07:00
use HasFactory , Notifiable , HasRoles , SoftDeletes ;
2026-10-15 15:03:24 +07:00
/**
* The "booted" method of the model.
*/
protected static function booted () : void
{
static :: created ( function ( User $user ) {
if ( $user -> role && \Illuminate\Support\Facades\Schema :: hasTable ( 'roles' )) {
$role = \Spatie\Permission\Models\Role :: where ( 'name' , $user -> role ) -> first ();
if ( $role && ! $user -> hasRole ( $user -> role )) {
$user -> assignRole ( $role );
}
}
});
2026-10-15 20:58:21 +07:00
static :: deleting ( function ( User $user ) {
2026-11-03 09:14:53 +07:00
$employee = $user -> employee ?: Employee :: where ( 'user_id' , $user -> id ) -> orWhere ( 'nik' , $user -> username ) -> first ();
$guard = $user -> securityGuard ?: SecurityGuard :: where ( 'user_id' , $user -> id ) -> first ();
if ( $user -> isForceDeleting ()) {
$employeeTrashed = $employee ?: Employee :: withTrashed () -> where ( 'user_id' , $user -> id ) -> orWhere ( 'nik' , $user -> username ) -> first ();
if ( $employeeTrashed && ( $employeeTrashed -> activities () -> exists () || $employeeTrashed -> p2hRecords () -> exists () || $employeeTrashed -> supportTimesheets () -> exists ())) {
throw new \RuntimeException ( "Tidak dapat menghapus permanen user: Karyawan ' { $user -> name } ' memiliki data audit riwayat operasional kendaraan atau inspeksi P2H." );
2026-10-15 20:58:21 +07:00
}
2026-11-03 09:14:53 +07:00
$guardTrashed = $guard ?: SecurityGuard :: withTrashed () -> where ( 'user_id' , $user -> id ) -> first ();
if ( $guardTrashed && $guardTrashed -> activities () -> exists ()) {
throw new \RuntimeException ( "Tidak dapat menghapus permanen user: Petugas Satpam ' { $user -> name } ' memiliki riwayat data validasi pos gerbang." );
}
} else {
// Soft delete: cukup cascade soft delete data employee & securityGuard jika ada dan belum terhapus
if ( $employee && ! $employee -> trashed ()) {
$employee -> delete ();
}
if ( $guard && ! $guard -> trashed ()) {
$guard -> delete ();
2026-10-15 20:58:21 +07:00
}
}
});
2026-10-15 15:03:24 +07:00
}
2026-10-12 23:47:41 +07:00
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'name' ,
'username' ,
'email' ,
'password' ,
'role' ,
'gate_id' ,
2026-10-20 19:22:28 +07:00
'photo_path' ,
];
/**
* The accessors to append to the model's array form.
*
* @var list<string>
*/
protected $appends = [
'photo_url' ,
2026-10-12 23:47:41 +07:00
];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
*/
protected $hidden = [
'password' ,
'remember_token' ,
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts () : array
{
return [
'email_verified_at' => 'datetime' ,
'password' => 'hashed' ,
];
}
public function gate () : BelongsTo
{
return $this -> belongsTo ( Gate :: class );
}
2026-10-13 13:46:35 +07:00
public function securityGuard () : HasOne
{
return $this -> hasOne ( SecurityGuard :: class );
}
2026-10-15 13:53:52 +07:00
public function employee () : HasOne
{
return $this -> hasOne ( Employee :: class );
}
2026-10-20 19:22:28 +07:00
/**
* Dapatkan URL foto profil pengguna (mendukung delegasi otomatis ke foto master Employee).
*/
public function getPhotoUrlAttribute () : ? string
{
if ( ! empty ( $this -> photo_path )) {
if ( str_starts_with ( $this -> photo_path , 'http://' ) || str_starts_with ( $this -> photo_path , 'https://' )) {
return $this -> photo_path ;
}
return asset ( 'storage/' . ltrim ( $this -> photo_path , '/' ));
}
// Delegasi ke profil employee tertaut
if ( $this -> relationLoaded ( 'employee' ) && $this -> employee ) {
return $this -> employee -> photo_url ;
}
$employee = $this -> employee () -> first ();
if ( $employee && $employee -> photo_url ) {
return $employee -> photo_url ;
}
// Fallback cari employee dengan NIK sama dengan username
if ( ! empty ( $this -> username )) {
$employeeByNik = Employee :: where ( 'nik' , $this -> username ) -> first ();
if ( $employeeByNik ) {
if ( empty ( $employeeByNik -> user_id )) {
$employeeByNik -> update ([ 'user_id' => $this -> id ]);
}
return $employeeByNik -> photo_url ;
}
}
return null ;
}
2026-10-19 23:21:55 +07:00
/**
* Dapatkan profil Employee yang tertaut, atau cari berdasarkan NIK (username),
* atau buat profil Employee default jika akun berstatus employee / super admin penguji.
*/
public function getOrCreateEmployee () : ? Employee
{
if ( $this -> relationLoaded ( 'employee' ) && $this -> employee ) {
return $this -> employee ;
}
$emp = $this -> employee () -> first ();
if ( $emp ) {
return $emp ;
}
// Cek jika sudah ada master Employee dengan NIK sama dengan username
2026-10-24 15:30:08 +07:00
if ( ! empty ( $this -> username )) {
$emp = Employee :: where ( 'nik' , $this -> username ) -> first ();
if ( $emp ) {
$emp -> update ([ 'user_id' => $this -> id ]);
return $emp ;
}
2026-10-19 23:21:55 +07:00
}
2026-10-24 15:30:08 +07:00
$defaultNik = ! empty ( $this -> username ) ? $this -> username : 'EMP-' . str_pad ( $this -> id , 4 , '0' , STR_PAD_LEFT );
if ( Employee :: where ( 'nik' , $defaultNik ) -> exists ()) {
$defaultNik = 'EMP-' . $this -> id . '-' . rand ( 100 , 999 );
2026-10-19 23:21:55 +07:00
}
2026-10-24 15:30:08 +07:00
return Employee :: create ([
'user_id' => $this -> id ,
'nik' => $defaultNik ,
'name' => $this -> name ,
'unit_kerja' => $this -> isQmaAdmin () ? 'QMA' : ( $this -> isPgAdmin () ? 'Gate' : 'Umum' ),
'division' => 'Operasional' ,
'position' => $this -> isQmaAdmin () ? 'Admin QMA' : ( $this -> isPgAdmin () ? 'Admin Gate' : 'Driver / Operator' ),
'is_operator' => true ,
'simper_expired_at' => now () -> addYears ( 2 ),
]);
2026-10-19 23:21:55 +07:00
}
2026-10-20 20:15:24 +07:00
public function locationHistories () : HasMany
{
return $this -> hasMany ( VehicleLocationHistory :: class );
}
2026-10-15 13:53:52 +07:00
public function isEmployee () : bool
{
2026-10-20 14:51:59 +07:00
if ( $this -> isSuperAdmin ()) {
return false ;
}
2026-10-20 14:41:57 +07:00
return $this -> role === 'employee'
|| $this -> hasRole ( 'employee' )
|| $this -> can ( 'employee.dashboard' )
|| $this -> can ( 'employee.timesheet' )
|| $this -> can ( 'employee.p2h' );
2026-10-15 13:53:52 +07:00
}
2026-10-12 23:47:41 +07:00
public function isGateOfficer () : bool
{
2026-10-15 15:03:24 +07:00
return $this -> role === 'gate_officer' || $this -> hasRole ( 'gate_officer' );
2026-10-12 23:47:41 +07:00
}
public function isHseAdmin () : bool
{
2026-10-15 15:03:24 +07:00
return $this -> isSuperAdmin () || $this -> role === 'hse_admin' || $this -> hasRole ( 'hse_admin' );
2026-10-12 23:47:41 +07:00
}
public function isQmaAdmin () : bool
{
2026-10-15 15:03:24 +07:00
return $this -> isSuperAdmin () || $this -> role === 'qma_admin' || $this -> hasRole ( 'qma_admin' );
2026-10-12 23:47:41 +07:00
}
public function isPgAdmin () : bool
{
2026-10-15 15:03:24 +07:00
return $this -> isSuperAdmin () || $this -> role === 'pg_admin' || $this -> hasRole ( 'pg_admin' );
2026-10-12 23:47:41 +07:00
}
2026-10-15 13:53:52 +07:00
public function isSuperAdmin () : bool
{
2026-10-15 20:58:21 +07:00
return in_array ( $this -> role , [ 'super_admin' , 'superadmin' ]) || $this -> hasRole ( 'super_admin' );
2026-10-15 13:53:52 +07:00
}
2026-10-12 23:47:41 +07:00
public function isAdmin () : bool
{
2026-10-15 20:58:21 +07:00
return $this -> isSuperAdmin () || in_array ( $this -> role , [ 'admin' , 'super_admin' , 'superadmin' ]) || $this -> hasRole ( 'admin' );
2026-10-12 23:47:41 +07:00
}
}