2026-02-08 15:16:35 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2026-09-23 12:45:02 +07:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2026-02-08 15:16:35 +07:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
2026-09-23 12:45:02 +07:00
|
|
|
use Illuminate\Support\Str;
|
2026-02-08 15:16:35 +07:00
|
|
|
|
|
|
|
|
class Treatment extends Model
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Kolom yang boleh diisi secara mass-assignment.
|
|
|
|
|
*/
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'cat_id',
|
|
|
|
|
'user_id',
|
|
|
|
|
'tanggal',
|
|
|
|
|
'keluhan',
|
|
|
|
|
'diagnosa',
|
|
|
|
|
'nama_obat',
|
|
|
|
|
'dosis',
|
|
|
|
|
'keterangan',
|
|
|
|
|
'foto_bukti',
|
|
|
|
|
'medicine_id',
|
|
|
|
|
'qty_used',
|
2026-09-23 12:45:02 +07:00
|
|
|
'status_perawatan', // Tambahan Baru
|
|
|
|
|
'remark_pasca_sakit'
|
2026-02-08 15:16:35 +07:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-23 12:45:02 +07:00
|
|
|
* Casting data agar formatnya sesuai.
|
2026-02-08 15:16:35 +07:00
|
|
|
*/
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'tanggal' => 'datetime',
|
|
|
|
|
'qty_used' => 'decimal:2',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------- */
|
2026-09-23 12:45:02 +07:00
|
|
|
/* RELASI */
|
2026-02-08 15:16:35 +07:00
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Relasi ke Kucing.
|
|
|
|
|
*/
|
|
|
|
|
public function cat(): BelongsTo
|
|
|
|
|
{
|
2026-09-23 12:45:02 +07:00
|
|
|
return $this->belongsTo(Cat::class)->withTrashed();
|
2026-02-08 15:16:35 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-23 12:45:02 +07:00
|
|
|
* Relasi utama ke User (PIC).
|
2026-02-08 15:16:35 +07:00
|
|
|
*/
|
|
|
|
|
public function user(): BelongsTo
|
|
|
|
|
{
|
2026-09-23 12:45:02 +07:00
|
|
|
return $this->belongsTo(User::class);
|
2026-02-08 15:16:35 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-23 12:45:02 +07:00
|
|
|
* Alias untuk relasi user (Digunakan di Dashboard).
|
2026-02-08 15:16:35 +07:00
|
|
|
*/
|
|
|
|
|
public function pic(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->user();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-23 12:45:02 +07:00
|
|
|
* PENTING: Relasi ke Medicine tunggal (Legacy Support).
|
|
|
|
|
* Ini yang memperbaiki error 'RelationNotFoundException' pada data lama.
|
2026-02-08 15:16:35 +07:00
|
|
|
*/
|
|
|
|
|
public function medicine(): BelongsTo
|
|
|
|
|
{
|
2026-09-23 12:45:02 +07:00
|
|
|
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();
|
2026-02-08 15:16:35 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------- */
|
2026-09-23 12:45:02 +07:00
|
|
|
/* ACCESSORS */
|
2026-02-08 15:16:35 +07:00
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mengambil URL foto bukti.
|
|
|
|
|
*/
|
|
|
|
|
public function getFotoUrlAttribute(): string
|
|
|
|
|
{
|
|
|
|
|
if (!$this->foto_bukti) {
|
|
|
|
|
return 'https://ui-avatars.com/api/?name=No+Image&background=f1f5f9&color=cbd5e1';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Storage::url($this->foto_bukti);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-23 12:45:02 +07:00
|
|
|
/**
|
|
|
|
|
* 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';
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 15:16:35 +07:00
|
|
|
/* -------------------------------------------------------------------------- */
|
2026-09-23 12:45:02 +07:00
|
|
|
/* SCOPES */
|
2026-02-08 15:16:35 +07:00
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Scope untuk mengurutkan data terbaru.
|
|
|
|
|
*/
|
|
|
|
|
public function scopeLatestFirst(Builder $query): void
|
|
|
|
|
{
|
|
|
|
|
$query->orderBy('tanggal', 'desc');
|
|
|
|
|
}
|
2026-09-23 12:45:02 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-02-08 15:16:35 +07:00
|
|
|
}
|