update
This commit is contained in:
@@ -309,65 +309,108 @@ class ReportController extends Controller
|
||||
}
|
||||
echo json_encode($lists);
|
||||
} else {
|
||||
$query = Periksa::select('*');
|
||||
$query = Periksa::select('id', 'nofoto', 'noregister', 'nmpasien', 'tlppasien', 'tgllahirpasien', 'usia', 'alamatpasien', 'ktp', 'bpjs', 'asuransi', 'urgensi', 'asalpasien', 'klinisi', 'klinis', 'nmanalis', 'nmppdssenior', 'kd_spesimen', 'nm_spesimen', 'dlp', 'diagnosa2', 'mulai', 'verifikasi', 'status')->whereYear('mulai', $tahun);
|
||||
|
||||
if ($bulan !== 'ALL') {
|
||||
$query->whereMonth('mulai', $bulan);
|
||||
}
|
||||
$periksa = $query
|
||||
->whereYear('mulai', $tahun)
|
||||
->orderBy('mulai', 'ASC')
|
||||
->get();
|
||||
|
||||
if ($periksa->count() == 0) {
|
||||
/**
|
||||
* CURSOR → Streaming data, tidak boros memori
|
||||
*/
|
||||
$periksaCursor = $query->orderBy('mulai', 'ASC')->cursor();
|
||||
|
||||
$hasil = [];
|
||||
|
||||
/**
|
||||
* Karena cursor tidak bisa pluck di awal,
|
||||
* kita kumpulkan accnumber & poli_id sambil streaming
|
||||
*/
|
||||
$acc = [];
|
||||
$poli_ids = [];
|
||||
|
||||
foreach ($periksaCursor as $r) {
|
||||
$acc[] = $r->nofoto;
|
||||
$poli_ids[] = $r->poli_id;
|
||||
$hasil[] = $r; // simpan pointer row
|
||||
}
|
||||
|
||||
/** Jika tidak ada data */
|
||||
if (empty($hasil)) {
|
||||
return response()->json([]);
|
||||
}
|
||||
$accnumbers = $periksa->pluck('nofoto')->toArray();
|
||||
$komponen = DB::table('db_komponenjawaban')->whereIn('accnumber', $accnumbers)->whereIn('komponen', ['id_bakteri01','id_bakteri02','id_mikroorganisme'])->get()->groupBy('accnumber');
|
||||
$poli = DB::table('poli')->whereIn('id', $periksa->pluck('poli_id'))->get()->keyBy('id');
|
||||
$hasil = $periksa->map(function($row) use ($komponen, $poli){
|
||||
$k = $komponen[$row->nofoto] ?? collect([]);
|
||||
$row->id_bakteri01 = optional($k->firstWhere('komponen', 'id_bakteri01'))->isidata;
|
||||
$row->id_bakteri02 = optional($k->firstWhere('komponen', 'id_bakteri02'))->isidata;
|
||||
$row->bakteri = optional($k->firstWhere('komponen', 'bakteri'))->isidata;
|
||||
$row->filefoto = "<a href='".url('/hasil/'.$row->nofoto)."'>$row->nofoto</a>";
|
||||
$p = $poli[$row->poli_id] ?? null;
|
||||
$row->subpoli = $p->subpoli ?? null;
|
||||
$target = $p->modaliti2 ?? '1';
|
||||
$row->modaliti2 = $target;
|
||||
|
||||
/**
|
||||
* Ambil komponen terkait (1 query saja)
|
||||
*/
|
||||
$komponen = DB::table('db_komponenjawaban')
|
||||
->select('accnumber','komponen','isidata')
|
||||
->whereIn('accnumber', array_unique($acc))
|
||||
->whereIn('komponen', [
|
||||
'id_bakteri01','id_bakteri02','id_mikroorganisme'
|
||||
])
|
||||
->get()
|
||||
->groupBy('accnumber');
|
||||
|
||||
/**
|
||||
* Ambil poli terkait (1 query)
|
||||
*/
|
||||
$poli = DB::table('poli')
|
||||
->select('id','subpoli','modaliti2')
|
||||
->whereIn('id', array_unique($poli_ids))
|
||||
->get()
|
||||
->keyBy('id');
|
||||
|
||||
/**
|
||||
* Proses setiap row → tidak ada map() untuk hemat memori
|
||||
*/
|
||||
foreach ($hasil as $row) {
|
||||
|
||||
$k = $komponen[$row->nofoto] ?? collect([]);
|
||||
|
||||
$row->id_bakteri01 = optional($k->firstWhere('komponen','id_bakteri01'))->isidata;
|
||||
$row->id_bakteri02 = optional($k->firstWhere('komponen','id_bakteri02'))->isidata;
|
||||
$row->bakteri = optional($k->firstWhere('komponen','bakteri'))->isidata;
|
||||
|
||||
$row->filefoto = "<a href='".url('/hasil/'.$row->nofoto)."'>$row->nofoto</a>";
|
||||
|
||||
$p = $poli[$row->poli_id] ?? null;
|
||||
$row->subpoli = $p->subpoli ?? null;
|
||||
$row->modaliti2 = $p->modaliti2 ?? '1';
|
||||
|
||||
// Hitung status target
|
||||
if (!empty($row->verifikasi)) {
|
||||
$selisih_hari = Carbon::parse($row->mulai)->diffInDays(Carbon::parse($row->verifikasi));
|
||||
$target = intval($target);
|
||||
$hari = intval($selisih_hari);
|
||||
$row->selisih_hari = $selisih_hari;
|
||||
if ($hari <= $target) {
|
||||
$row->status_target = "MEMENUHI TARGET";
|
||||
} else {
|
||||
$row->status_target = "TIDAK MEMENUHI TARGET";
|
||||
}
|
||||
$selisih = Carbon::parse($row->mulai)->diffInDays($row->verifikasi);
|
||||
$row->selisih_hari = $selisih;
|
||||
$row->status_target = ($selisih <= intval($row->modaliti2))
|
||||
? "MEMENUHI TARGET"
|
||||
: "TIDAK MEMENUHI TARGET";
|
||||
} else {
|
||||
$row->selisih_hari = null;
|
||||
$row->selisih_hari = null;
|
||||
$row->status_target = "TIDAK ADA HASIL";
|
||||
}
|
||||
return $row;
|
||||
});
|
||||
if ($jenisreport == 'rekaptat'){
|
||||
$rekap = $hasil->groupBy('kd_spesimen')->map(function($group){
|
||||
|
||||
return [
|
||||
'jenis_pemeriksaan' => $group->first()->kd_spesimen,
|
||||
'memenuhi' => $group->where('status_target', 'MEMENUHI TARGET')->count(),
|
||||
'tidak_memenuhi' => $group->where('status_target', 'TIDAK MEMENUHI TARGET')->count(),
|
||||
'tidak_ada_hasil' => $group->where('status_target', 'TIDAK ADA HASIL')->count(),
|
||||
'total' => $group->count()
|
||||
];
|
||||
|
||||
})->values();
|
||||
return response()->json($rekap);
|
||||
} else {
|
||||
return response()->json($hasil);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return untuk dua mode (rekaptat / data penuh)
|
||||
*/
|
||||
if ($jenisreport == 'rekaptat') {
|
||||
$rekap = collect($hasil)->groupBy('kd_spesimen')->map(function($g){
|
||||
return [
|
||||
'jenis_pemeriksaan' => $g->first()->kd_spesimen,
|
||||
'memenuhi' => $g->where('status_target','MEMENUHI TARGET')->count(),
|
||||
'tidak_memenuhi' => $g->where('status_target','TIDAK MEMENUHI TARGET')->count(),
|
||||
'tidak_ada_hasil' => $g->where('status_target','TIDAK ADA HASIL')->count(),
|
||||
'total' => $g->count(),
|
||||
];
|
||||
})->values();
|
||||
|
||||
return response()->json($rekap);
|
||||
}
|
||||
|
||||
return response()->json($hasil);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
public function genRekapAntibiotik(Request $request) {
|
||||
$data = [];
|
||||
|
||||
Reference in New Issue
Block a user