268 lines
6.9 KiB
PHP
268 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class MonitoringController extends Controller
|
|
{
|
|
public function index($id_user)
|
|
{
|
|
// =========================
|
|
// DATA MEDIS TERBARU
|
|
// =========================
|
|
|
|
$detail = DB::table('pasien_medis')
|
|
->where('id_user', $id_user)
|
|
->where('status_kesehatan', 'Masih Proses')
|
|
->orderByDesc('id_medis')
|
|
->first();
|
|
|
|
if (!$detail) {
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Data medis belum diinput oleh Admin'
|
|
], 404);
|
|
}
|
|
|
|
// =========================
|
|
// RIWAYAT OBAT
|
|
// =========================
|
|
|
|
$riwayat = DB::table('monitoring_obat')
|
|
->where('id_medis', $detail->id_medis)
|
|
->where('status', 1)
|
|
->select('hari_ke', 'tahap')
|
|
->get();
|
|
|
|
// =========================
|
|
// FILE HASIL LAB
|
|
// =========================
|
|
|
|
$filePasien = DB::table('file_pasien')
|
|
->where('id_user', $id_user)
|
|
->orderByDesc('id_file')
|
|
->get();
|
|
|
|
// =========================
|
|
// RESPONSE
|
|
// =========================
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
|
|
'data_medis' => [
|
|
|
|
'id_medis' => $detail->id_medis,
|
|
|
|
'pengobatan_ke' => $detail->pengobatan_ke,
|
|
|
|
'diagnosa' => $detail->diagnosa,
|
|
|
|
'tgl_mulai_pengobatan' => $detail->tgl_mulai_pengobatan,
|
|
|
|
'tgl_estimasi_selesai' => $detail->tgl_estimasi_selesai,
|
|
|
|
'status_kesehatan' => $detail->status_kesehatan,
|
|
|
|
'jam_minum' => $detail->jam_minum,
|
|
],
|
|
|
|
'riwayat_obat' => $riwayat,
|
|
|
|
'file_pasien' => $filePasien
|
|
]);
|
|
}
|
|
/**
|
|
* Menyimpan konfirmasi minum obat
|
|
*/
|
|
public function konfirmasi(Request $request)
|
|
{
|
|
$request->validate([
|
|
'id_user' => 'required',
|
|
]);
|
|
|
|
$pasienMedis = DB::table('pasien_medis')
|
|
->where('id_user', $request->id_user)
|
|
->where('status_kesehatan', 'Masih Proses')
|
|
->orderByDesc('id_medis')
|
|
->first();
|
|
|
|
if (!$pasienMedis) {
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Data medis pasien tidak ditemukan'
|
|
], 404);
|
|
}
|
|
|
|
// ===============================
|
|
// VALIDASI JAM MINUM OBAT
|
|
// ===============================
|
|
|
|
if (!$pasienMedis->jam_minum) {
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Jam minum obat belum diatur admin'
|
|
]);
|
|
}
|
|
|
|
$now = now();
|
|
|
|
$jamMinum = \Carbon\Carbon::createFromFormat(
|
|
'H:i:s',
|
|
$pasienMedis->jam_minum
|
|
);
|
|
|
|
$jadwalHariIni = now()->setTime(
|
|
$jamMinum->hour,
|
|
$jamMinum->minute,
|
|
0
|
|
);
|
|
|
|
// BELUM BOLEH KONFIRMASI
|
|
if ($now->lt($jadwalHariIni)) {
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Belum waktunya minum obat'
|
|
]);
|
|
}
|
|
|
|
/*
|
|
=====================================================
|
|
HITUNG HARI KE PENGOBATAN
|
|
=====================================================
|
|
*/
|
|
|
|
$tanggalMulai = \Carbon\Carbon::parse(
|
|
$pasienMedis->tgl_mulai_pengobatan
|
|
)->startOfDay();
|
|
|
|
$hariKe = $tanggalMulai->diffInDays(
|
|
now()->startOfDay()
|
|
) + 1;
|
|
|
|
/*
|
|
=====================================================
|
|
CEK MASIH DALAM MASA PENGOBATAN
|
|
=====================================================
|
|
*/
|
|
|
|
if ($hariKe < 1 || $hariKe > 168) {
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Masa pengobatan telah selesai'
|
|
]);
|
|
}
|
|
|
|
/*
|
|
=====================================================
|
|
TENTUKAN TAHAP
|
|
=====================================================
|
|
*/
|
|
|
|
$tahap = ($hariKe <= 56)
|
|
? 'awal'
|
|
: 'lanjutan';
|
|
|
|
// ===============================
|
|
// CEK DUPLIKAT
|
|
// ===============================
|
|
|
|
$exists = DB::table('monitoring_obat')
|
|
->where('id_medis', $pasienMedis->id_medis)
|
|
->whereDate('tanggal', now()->toDateString())
|
|
->exists();
|
|
|
|
if ($exists) {
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Hari ini sudah dikonfirmasi!'
|
|
]);
|
|
}
|
|
|
|
// ===============================
|
|
// SIMPAN
|
|
// ===============================
|
|
|
|
$insert = DB::table('monitoring_obat')->insert([
|
|
'id_medis' => $pasienMedis->id_medis,
|
|
'hari_ke' => $hariKe,
|
|
'tahap' => $tahap,
|
|
'tanggal' => now()->toDateString(),
|
|
'jam_konfirmasi' => now()->toTimeString(),
|
|
'status' => 1,
|
|
'created_at' => now(),
|
|
'updated_at' => now()
|
|
]);
|
|
|
|
if ($insert) {
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Konfirmasi berhasil disimpan'
|
|
]);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Gagal menyimpan ke database'
|
|
], 500);
|
|
}
|
|
/**
|
|
* Verifikasi password pasien sebelum membuka file lab
|
|
*/
|
|
public function verifikasiPassword(Request $request)
|
|
{
|
|
$request->validate([
|
|
'id_user' => 'required',
|
|
'password' => 'required'
|
|
]);
|
|
|
|
$user = DB::table('users')
|
|
->where('id_user', $request->id_user)
|
|
->first();
|
|
|
|
if (!$user) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'User tidak ditemukan'
|
|
], 404);
|
|
}
|
|
|
|
if (!Hash::check($request->password, $user->password)) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Password salah'
|
|
], 401);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Verifikasi berhasil'
|
|
]);
|
|
}
|
|
|
|
public function downloadFilePasien($id_file)
|
|
{
|
|
$file = DB::table('file_pasien')->where('id_file', $id_file)->first();
|
|
|
|
if (!$file) return response()->json(['success'=>false,'message'=>'File tidak ditemukan'],404);
|
|
|
|
$filePath = $file->path_file; // kolom path_file di DB
|
|
if (!Storage::exists($filePath)) return response()->json(['success'=>false,'message'=>'File tidak ditemukan di server'],404);
|
|
|
|
return Storage::download($filePath, $file->nama_file ?? 'dokumen.pdf');
|
|
}
|
|
}
|