initial commit
This commit is contained in:
+62
-19
@@ -4,8 +4,11 @@ namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Treatment extends Model
|
||||
{
|
||||
@@ -24,10 +27,12 @@ class Treatment extends Model
|
||||
'foto_bukti',
|
||||
'medicine_id',
|
||||
'qty_used',
|
||||
'status_perawatan', // Tambahan Baru
|
||||
'remark_pasca_sakit'
|
||||
];
|
||||
|
||||
/**
|
||||
* Casting data agar formatnya sesuai (Tanggal jadi object Carbon).
|
||||
* Casting data agar formatnya sesuai.
|
||||
*/
|
||||
protected $casts = [
|
||||
'tanggal' => 'datetime',
|
||||
@@ -35,7 +40,7 @@ class Treatment extends Model
|
||||
];
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* RELASI (MENGATASI ERROR RELATION NOT FOUND) */
|
||||
/* RELASI */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
@@ -43,25 +48,19 @@ class Treatment extends Model
|
||||
*/
|
||||
public function cat(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Cat::class, 'cat_id')->withDefault([
|
||||
'nama_kucing' => 'Kucing Terhapus'
|
||||
]);
|
||||
return $this->belongsTo(Cat::class)->withTrashed();
|
||||
}
|
||||
|
||||
/**
|
||||
* Relasi utama ke User (PIC).
|
||||
* Nama fungsi 'user' wajib ada karena dipanggil di TreatmentController baris 31.
|
||||
* Relasi utama ke User (PIC).
|
||||
*/
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'user_id')->withDefault([
|
||||
'name' => 'System / Admin'
|
||||
]);
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias untuk relasi user.
|
||||
* Nama fungsi 'pic' wajib ada karena dipanggil di DashboardController.
|
||||
* Alias untuk relasi user (Digunakan di Dashboard).
|
||||
*/
|
||||
public function pic(): BelongsTo
|
||||
{
|
||||
@@ -69,22 +68,38 @@ class Treatment extends Model
|
||||
}
|
||||
|
||||
/**
|
||||
* Relasi ke Inventori Obat.
|
||||
* PENTING: Relasi ke Medicine tunggal (Legacy Support).
|
||||
* Ini yang memperbaiki error 'RelationNotFoundException' pada data lama.
|
||||
*/
|
||||
public function medicine(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Medicine::class, 'medicine_id')->withDefault([
|
||||
'nama_obat' => $this->nama_obat ?? 'Obat Manual'
|
||||
]);
|
||||
return $this->belongsTo(Medicine::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Relasi ke Detail Obat (Sistem Baru Multi-Medicine).
|
||||
*/
|
||||
public function details(): HasMany
|
||||
{
|
||||
return $this->hasMany(TreatmentDetail::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Relasi langsung ke banyak Medicine (Many-to-Many via pivot details).
|
||||
*/
|
||||
public function medicines(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Medicine::class, 'treatment_details')
|
||||
->withPivot('dosis', 'keterangan_pakai')
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* ACCESSORS */
|
||||
/* ACCESSORS */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Mengambil URL foto bukti.
|
||||
* Cara akses di Blade: {{ $treatment->foto_url }}
|
||||
*/
|
||||
public function getFotoUrlAttribute(): string
|
||||
{
|
||||
@@ -95,8 +110,26 @@ class Treatment extends Model
|
||||
return Storage::url($this->foto_bukti);
|
||||
}
|
||||
|
||||
/**
|
||||
* Menghitung Progres Pengobatan secara otomatis.
|
||||
* Cara pakai di Blade: {{ $treatment->status_progress }}
|
||||
*/
|
||||
public function getStatusProgressAttribute(): string
|
||||
{
|
||||
$text = strtolower($this->diagnosa . ' ' . $this->keterangan);
|
||||
|
||||
// Keyword indikator membaik
|
||||
$improvingKeywords = ['baik', 'makan', 'aktif', 'sembuh', 'lincah', 'normal', 'turun panas'];
|
||||
|
||||
if (Str::contains($text, $improvingKeywords)) {
|
||||
return 'MEMBAIK';
|
||||
}
|
||||
|
||||
return 'STUCK';
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* SCOPES */
|
||||
/* SCOPES */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
@@ -106,4 +139,14 @@ class Treatment extends Model
|
||||
{
|
||||
$query->orderBy('tanggal', 'desc');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope khusus untuk mengambil rekam medis yang sedang aktif (Under Treatment).
|
||||
*/
|
||||
public function scopeActiveTreatment(Builder $query): Builder
|
||||
{
|
||||
return $query->whereHas('cat', function($q) {
|
||||
$q->where('treatment_status', 1);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user