instalasi-sim-rs/app/Models/Employee.php

72 lines
1.6 KiB
PHP

<?php
namespace App\Models;
use App\Traits\InteractsWithUuid;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Employee extends Model
{
use HasFactory, InteractsWithUuid;
protected $table = 'm_employee';
protected $primaryKey = 'id';
public $incrementing = false;
protected $keyType = 'string';
protected $fillable = [
'user_id',
'employee_id',
'gender',
'nik',
'birth_date',
'birth_place',
'address',
'phone_number',
'specialization',
'license_number',
'join_date',
'resign_date',
'is_active',
];
protected $casts = [
'birth_date' => 'date',
'join_date' => 'date',
'resign_date' => 'date',
'is_active' => 'boolean',
];
public function user()
{
return $this->belongsTo(User::class);
}
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
if (empty($model->employee_id)) {
$model->employee_id = $model->generateEmployeeId();
}
});
}
public function generateEmployeeId()
{
$prefix = 'HK-';
$lastEmployee = static::orderBy('created_at', 'desc')->first();
if ($lastEmployee) {
$lastNumber = (int) substr($lastEmployee->employee_id, 3);
$nextNumber = str_pad($lastNumber + 1, 4, '0', STR_PAD_LEFT);
} else {
$nextNumber = '0001';
}
return $prefix . $nextNumber;
}
}