2026-10-12 23:47:41 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2026-10-15 13:53:52 +07:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2026-10-12 23:47:41 +07:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
|
|
|
|
|
|
class Employee extends Model
|
|
|
|
|
{
|
|
|
|
|
use HasFactory;
|
|
|
|
|
|
|
|
|
|
protected $fillable = [
|
2026-10-15 13:53:52 +07:00
|
|
|
'user_id',
|
2026-10-12 23:47:41 +07:00
|
|
|
'nik',
|
|
|
|
|
'name',
|
|
|
|
|
'unit_kerja',
|
|
|
|
|
'division',
|
|
|
|
|
'position',
|
|
|
|
|
'is_operator',
|
|
|
|
|
'simper_expired_at',
|
|
|
|
|
'photo_path',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'is_operator' => 'boolean',
|
|
|
|
|
'simper_expired_at' => 'date',
|
|
|
|
|
];
|
|
|
|
|
|
2026-10-20 16:00:29 +07:00
|
|
|
protected $appends = [
|
|
|
|
|
'photo_url',
|
|
|
|
|
];
|
|
|
|
|
|
2026-10-12 23:47:41 +07:00
|
|
|
/**
|
|
|
|
|
* Cek apakah SIMPER masih aktif hari ini.
|
|
|
|
|
*/
|
|
|
|
|
public function hasValidSimper(): bool
|
|
|
|
|
{
|
|
|
|
|
if (!$this->simper_expired_at) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->simper_expired_at->isFuture() || $this->simper_expired_at->isToday();
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-20 16:00:29 +07:00
|
|
|
public function getPhotoUrlAttribute(): ?string
|
|
|
|
|
{
|
|
|
|
|
if (!$this->photo_path) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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, '/'));
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-12 23:47:41 +07:00
|
|
|
/**
|
|
|
|
|
* Scope untuk mengambil hanya karyawan yang berstatus operator/driver.
|
|
|
|
|
*/
|
|
|
|
|
public function scopeOperators(Builder $query): Builder
|
|
|
|
|
{
|
|
|
|
|
return $query->where('is_operator', true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function activities(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(VehicleActivity::class, 'driver_id');
|
|
|
|
|
}
|
2026-10-15 13:53:52 +07:00
|
|
|
|
|
|
|
|
public function p2hRecords(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(P2hRecord::class, 'employee_id')->orderByDesc('inspected_at');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function user(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
|
}
|
2026-10-12 23:47:41 +07:00
|
|
|
}
|