71 lines
1.7 KiB
PHP

<?php
namespace App\Models;
use App\Traits\InteractsWithUuid;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Patient extends Model
{
use HasFactory, InteractsWithUuid;
protected $primaryKey = 'id';
public $incrementing = false;
protected $keyType = 'string';
protected $table = 'm_patient';
protected $fillable = [
'id',
'medical_record_number',
'nik',
'name',
'gender',
'birth_date',
'birth_place',
'address',
'phone_number',
'email',
'blood_type',
'religion',
'marital_status',
'emergency_contact_name',
'emergency_contact_phone',
'emergency_contact_relation',
];
protected $casts = [
'birth_date' => 'date',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
protected $attributes = [
'blood_type' => 'unknown',
];
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
if (empty($model->medical_record_number)) {
$model->medical_record_number = self::generateMedicalRecordNumber();
}
});
}
public static function generateMedicalRecordNumber()
{
$prefix = 'MRN';
$year = Carbon::now()->format('Y');
$month = Carbon::now()->format('m');
$sequence = self::whereYear('created_at', $year)
->whereMonth('created_at', $month)
->count() + 1;
return sprintf('%s-%s%s-%04d', $prefix, $year, $month, $sequence);
}
}