46 lines
1.4 KiB
PHP
46 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Treatment;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class MedicalDashboardController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$monthlyTrends = Treatment::select(
|
|
DB::raw('MONTHNAME(tanggal) as month'),
|
|
DB::raw('COUNT(*) as total')
|
|
)
|
|
->where('tanggal', '>=', now()->subMonths(6))
|
|
->groupBy('month', DB::raw('MONTH(tanggal)'))
|
|
->orderBy(DB::raw('MONTH(tanggal)'))
|
|
->get();
|
|
|
|
// treatments memakai medicine_id. Nama obat diambil dari tabel medicines.
|
|
$topObats = Treatment::query()
|
|
->join('medicines', 'treatments.medicine_id', '=', 'medicines.id')
|
|
->select(
|
|
'medicines.id',
|
|
'medicines.nama_obat',
|
|
DB::raw('SUM(treatments.qty_used) as total_used')
|
|
)
|
|
->whereNotNull('treatments.medicine_id')
|
|
->groupBy('medicines.id', 'medicines.nama_obat')
|
|
->orderByDesc('total_used')
|
|
->limit(5)
|
|
->get();
|
|
|
|
$topComplaints = Treatment::select('keluhan', DB::raw('COUNT(*) as total'))
|
|
->whereNotNull('keluhan')
|
|
->where('keluhan', '!=', '')
|
|
->groupBy('keluhan')
|
|
->orderByDesc('total')
|
|
->limit(5)
|
|
->get();
|
|
|
|
return view('pages.dashboard.medical', compact('monthlyTrends', 'topObats', 'topComplaints'));
|
|
}
|
|
}
|