89 lines
2.0 KiB
PHP
89 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
//use Illuminate\Database\Eloquent\SoftDeletes; // Tambahkan ini jika pakai softDeletes di migrasi
|
|
|
|
class Appointment extends Model
|
|
{
|
|
// use HasFactory, SoftDeletes;
|
|
use HasFactory;
|
|
|
|
/**
|
|
* Kolom yang boleh diisi secara massal.
|
|
*/
|
|
protected $fillable = [
|
|
'patient_id',
|
|
'doctor_id',
|
|
'service_category_id',
|
|
'appointment_date',
|
|
'queue_number',
|
|
'status',
|
|
'notes',
|
|
'created_by',
|
|
];
|
|
|
|
/**
|
|
* Casting tipe data agar Carbon (Date) otomatis terformat.
|
|
*/
|
|
protected $casts = [
|
|
'appointment_date' => 'date',
|
|
];
|
|
|
|
/**
|
|
* Relasi ke User (sebagai Pasien).
|
|
*/
|
|
public function patient()
|
|
{
|
|
return $this->belongsTo(User::class, 'patient_id');
|
|
}
|
|
|
|
/**
|
|
* Relasi ke Dokter (Tabel Doctors).
|
|
*/
|
|
public function doctor()
|
|
{
|
|
// Diarahkan ke model Doctor, bukan User
|
|
return $this->belongsTo(Doctor::class, 'doctor_id');
|
|
}
|
|
|
|
/**
|
|
* Relasi ke Kategori Layanan (Poli).
|
|
*/
|
|
public function serviceCategory()
|
|
{
|
|
return $this->belongsTo(ServiceCategory::class, 'service_category_id');
|
|
}
|
|
|
|
/**
|
|
* Relasi ke MedicalRecord (jika sudah ada rekam medis untuk appointment ini).
|
|
*/
|
|
public function medicalRecord()
|
|
{
|
|
return $this->hasOne(MedicalRecord::class, 'appointment_id');
|
|
}
|
|
|
|
/**
|
|
* Relasi ke User (siapa yang membuat data ini, Admin atau Pasien sendiri).
|
|
*/
|
|
public function creator()
|
|
{
|
|
return $this->belongsTo(User::class, 'created_by');
|
|
}
|
|
|
|
/**
|
|
* Scope untuk mempermudah filter antrean hari ini.
|
|
*/
|
|
public function scopeToday($query)
|
|
{
|
|
return $query->whereDate('appointment_date', today());
|
|
}
|
|
|
|
public static function generateNextQueue($date)
|
|
{
|
|
return self::whereDate('appointment_date', $date)->count() + 1;
|
|
}
|
|
}
|