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-25 22:34:11 +07:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2026-10-12 23:47:41 +07:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
|
|
|
|
|
|
class Employee extends Model
|
|
|
|
|
{
|
|
|
|
|
use HasFactory;
|
|
|
|
|
|
2026-10-25 21:26:11 +07:00
|
|
|
public const TYPE_KARYAWAN = 'Karyawan';
|
|
|
|
|
public const TYPE_SUKON = 'Sukon';
|
|
|
|
|
|
2026-10-12 23:47:41 +07:00
|
|
|
protected $fillable = [
|
2026-10-15 13:53:52 +07:00
|
|
|
'user_id',
|
2026-10-12 23:47:41 +07:00
|
|
|
'nik',
|
|
|
|
|
'name',
|
2026-10-25 21:26:11 +07:00
|
|
|
'employee_type',
|
2026-10-12 23:47:41 +07:00
|
|
|
'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-25 21:26:11 +07:00
|
|
|
public function isSukon(): bool
|
|
|
|
|
{
|
|
|
|
|
return strcasecmp($this->employee_type ?? '', self::TYPE_SUKON) === 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isKaryawan(): bool
|
|
|
|
|
{
|
|
|
|
|
return empty($this->employee_type) || strcasecmp($this->employee_type, self::TYPE_KARYAWAN) === 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function scopeSukon(Builder $query): Builder
|
|
|
|
|
{
|
|
|
|
|
return $query->where('employee_type', self::TYPE_SUKON);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function scopeKaryawan(Builder $query): Builder
|
|
|
|
|
{
|
|
|
|
|
return $query->where(function ($q) {
|
|
|
|
|
$q->where('employee_type', self::TYPE_KARYAWAN)
|
|
|
|
|
->orWhereNull('employee_type');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
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-25 22:34:11 +07:00
|
|
|
public function simpers(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(EmployeeSimper::class)->with('simperType');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function simperTypes(): BelongsToMany
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(SimperType::class, 'employee_simpers')
|
|
|
|
|
->withPivot(['simper_number', 'expired_at', 'status', 'notes'])
|
|
|
|
|
->withTimestamps();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cek apakah personil memiliki SIMPER aktif untuk kode jenis SIMPER tertentu.
|
|
|
|
|
*/
|
|
|
|
|
public function hasValidSimperForType(string $code): bool
|
|
|
|
|
{
|
|
|
|
|
$hasAnySimper = $this->relationLoaded('simpers') ? $this->simpers->isNotEmpty() : $this->simpers()->exists();
|
|
|
|
|
|
|
|
|
|
// Jika belum ada record di employee_simpers, gunakan fallback kolom simper_expired_at
|
|
|
|
|
if (! $hasAnySimper) {
|
|
|
|
|
return $this->hasValidSimper();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$simpers = $this->relationLoaded('simpers') ? $this->simpers : $this->simpers()->with('simperType')->get();
|
|
|
|
|
|
|
|
|
|
foreach ($simpers as $s) {
|
|
|
|
|
if ($s->simperType && $s->simperType->code === $code && $s->isValid()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cek apakah personil memiliki SIMPER dengan cakupan scope tertentu.
|
|
|
|
|
*/
|
|
|
|
|
public function hasValidSimperForScope(string $scope): bool
|
|
|
|
|
{
|
|
|
|
|
$simpers = $this->relationLoaded('simpers') ? $this->simpers : $this->simpers()->with('simperType')->get();
|
|
|
|
|
foreach ($simpers as $s) {
|
|
|
|
|
if ($s->simperType && $s->simperType->category_scope === $scope && $s->isValid()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cek apakah personil memiliki izin SIMPER yang sah untuk kendaraan tertentu.
|
|
|
|
|
*/
|
|
|
|
|
public function hasValidSimperForVehicle(Vehicle $vehicle): bool
|
|
|
|
|
{
|
|
|
|
|
if (! $this->is_operator) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$hasAnySimper = $this->relationLoaded('simpers') ? $this->simpers->isNotEmpty() : $this->simpers()->exists();
|
|
|
|
|
|
|
|
|
|
// Fallback jika belum dimigrasikan ke employee_simpers
|
|
|
|
|
if (! $hasAnySimper) {
|
|
|
|
|
return $this->hasValidSimper();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 1. Jika kendaraan mensyaratkan spesifik required_simper_type_id
|
|
|
|
|
if ($vehicle->required_simper_type_id) {
|
|
|
|
|
$simpers = $this->relationLoaded('simpers') ? $this->simpers : $this->simpers()->get();
|
|
|
|
|
foreach ($simpers as $s) {
|
|
|
|
|
if ($s->simper_type_id === $vehicle->required_simper_type_id && $s->isValid()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. Berdasarkan kategori kendaraan
|
|
|
|
|
if ($vehicle->isLightVehicle()) {
|
|
|
|
|
return $this->hasValidSimperForType('SIMPER-LV') || $this->hasValidSimperForScope(SimperType::SCOPE_ALL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($vehicle->isUnitSupport() && $vehicle->is_timesheet_mobile) {
|
|
|
|
|
return $this->hasValidSimperForType('SIMPER-DT')
|
|
|
|
|
|| $this->hasValidSimperForType('SIMPER-HEAVY')
|
|
|
|
|
|| $this->hasValidSimperForScope(SimperType::SCOPE_ALL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($vehicle->isUnitSupport()) {
|
|
|
|
|
return $this->hasValidSimperForType('SIMPER-HEAVY')
|
|
|
|
|
|| $this->hasValidSimperForType('SIMPER-DT')
|
|
|
|
|
|| $this->hasValidSimperForScope(SimperType::SCOPE_ALL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->hasValidSimper();
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-20 16:00:29 +07:00
|
|
|
public function getPhotoUrlAttribute(): ?string
|
|
|
|
|
{
|
2026-10-20 19:22:28 +07:00
|
|
|
$path = $this->photo_path;
|
|
|
|
|
if (empty($path) && $this->user_id) {
|
|
|
|
|
$path = $this->user?->photo_path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$path) {
|
2026-10-20 16:00:29 +07:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-20 19:22:28 +07:00
|
|
|
if (str_starts_with($path, 'http://') || str_starts_with($path, 'https://')) {
|
|
|
|
|
return $path;
|
2026-10-20 16:00:29 +07:00
|
|
|
}
|
|
|
|
|
|
2026-10-20 19:22:28 +07:00
|
|
|
return asset('storage/' . ltrim($path, '/'));
|
2026-10-20 16:00:29 +07:00
|
|
|
}
|
|
|
|
|
|
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');
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-20 20:46:48 +07:00
|
|
|
public function supportTimesheets(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(UnitSupportTimesheet::class, 'operator_id')->orderByDesc('date');
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-15 13:53:52 +07:00
|
|
|
public function user(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
|
}
|
2026-10-12 23:47:41 +07:00
|
|
|
}
|