clqms-be/app/Controllers/PatVisitController.php

277 lines
11 KiB
PHP
Raw Normal View History

2025-09-19 16:43:03 +07:00
<?php
namespace App\Controllers;
use App\Traits\ResponseTrait;
2025-10-16 12:55:55 +07:00
use App\Controllers\BaseController;
use App\Models\PatVisit\PatVisitModel;
2025-10-23 12:16:52 +07:00
use App\Models\PatVisit\PatVisitADTModel;
use App\Models\Patient\PatientModel;
class PatVisitController extends BaseController {
2025-09-19 16:43:03 +07:00
use ResponseTrait;
2025-10-03 10:28:59 +07:00
2025-10-13 12:28:09 +07:00
protected $model;
2025-09-19 16:43:03 +07:00
2025-10-03 10:28:59 +07:00
public function __construct() {
2025-10-13 12:28:09 +07:00
$this->model = new PatVisitModel();
2025-09-19 16:43:03 +07:00
}
public function index() {
try {
$InternalPID = $this->request->getVar('InternalPID');
$PVID = $this->request->getVar('PVID');
$PatientID = $this->request->getVar('PatientID');
$PatientName = $this->request->getVar('PatientName');
$CreateDateFrom = $this->request->getVar('CreateDateFrom');
$CreateDateTo = $this->request->getVar('CreateDateTo');
$builder = $this->model->select('patvisit.*, patient.NameFirst, patient.NameLast, patient.PatientID, location.LocFull as LastLocation')
->join('patient', 'patient.InternalPID=patvisit.InternalPID', 'left')
->join('(SELECT a1.*
FROM patvisitadt a1
INNER JOIN (
SELECT InternalPVID, MAX(PVADTID) AS MaxID
FROM patvisitadt
GROUP BY InternalPVID
) a2 ON a1.InternalPVID = a2.InternalPVID AND a1.PVADTID = a2.MaxID
) AS latest_patvisitadt', 'latest_patvisitadt.InternalPVID = patvisit.InternalPVID', 'left')
->join('location', 'location.LocationID = latest_patvisitadt.LocationID', 'left');
if ($InternalPID) {
$builder->where('patvisit.InternalPID', $InternalPID);
}
if ($PVID) {
$builder->like('patvisit.PVID', $PVID, 'both');
}
if ($PatientID) {
$builder->like('patient.PatientID', $PatientID, 'both');
}
if ($PatientName) {
$builder->groupStart()
->like('patient.NameFirst', $PatientName, 'both')
->orLike('patient.NameLast', $PatientName, 'both')
->groupEnd();
}
if ($CreateDateFrom) {
$builder->where('patvisit.CreateDate >=', $CreateDateFrom);
}
if ($CreateDateTo) {
$builder->where('patvisit.CreateDate <=', $CreateDateTo);
}
$rows = $builder->orderBy('patvisit.CreateDate', 'DESC')->findAll();
if (empty($rows)) {
return $this->respond(['status' => 'success', 'message' => 'data not found', 'data' => []], 200);
}
return $this->respond(['status' => 'success', 'message' => 'data found', 'data' => $rows], 200);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
2025-09-22 13:25:31 +07:00
public function show($PVID = null) {
try {
2025-10-13 12:28:09 +07:00
$row = $this->model->show($PVID);
if (empty($row)) {
return $this->respond([ 'status' => 'success', 'message'=> "data not found", 'data' => [] ], 200);
}
return $this->respond([ 'status' => 'success', 'message'=> "data found", 'data' => $row ], 200);
2025-09-22 13:25:31 +07:00
} catch (\Exception $e) {
return $this->failServerError('Something went wrong '.$e->getMessage());
}
2025-09-19 16:43:03 +07:00
}
2025-09-22 13:25:31 +07:00
public function showByPatient($InternalPID = null) {
2025-09-19 16:43:03 +07:00
try {
2025-10-21 09:55:28 +07:00
$rows = $this->model->showByPatient($InternalPID);
if($rows == []) { $message = "data not found"; }
else { $message = "data found"; }
return $this->respond(['status' => 'success', 'message'=> $message, 'data' => $rows ], 200);
2025-09-19 16:43:03 +07:00
} catch (\Exception $e) {
return $this->failServerError('Something went wrong '.$e->getMessage());
}
}
public function update() {
2025-09-26 15:03:11 +07:00
$input = $this->request->getJSON(true);
2025-10-20 14:09:09 +07:00
try {
if (!isset($input["InternalPVID"]) || !is_numeric($input["InternalPVID"])) {
return $this->respond(['status' => 'error', 'message' => 'Invalid or missing ID'], 400);
}
// Check if visit exists
$visit = $this->model->find($input["InternalPVID"]);
if (!$visit) {
return $this->respond(['status' => 'error', 'message' => 'Visit not found'], 404);
}
2025-10-13 12:28:09 +07:00
$data = $this->model->updatePatVisit($input);
return $this->respond(['status' => 'success', 'message' => 'Data updated successfully', 'data' => $data], 200);
2025-09-19 16:43:03 +07:00
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
public function create() {
2025-10-03 10:28:59 +07:00
$input = $this->request->getJSON(true);
2025-09-19 16:43:03 +07:00
try {
// Validate required fields
if (!isset($input['InternalPID']) || !is_numeric($input['InternalPID'])) {
return $this->respond(['status' => 'error', 'message' => 'InternalPID is required and must be numeric'], 400);
}
// Check if patient exists
$patientModel = new PatientModel();
$patient = $patientModel->find($input['InternalPID']);
if (!$patient) {
return $this->respond(['status' => 'error', 'message' => 'Patient not found'], 404);
}
2025-10-13 12:28:09 +07:00
$data = $this->model->createPatVisit($input);
2025-10-13 13:59:52 +07:00
return $this->respond(['status' => 'success', 'message' => 'Data created successfully', 'data' => $data], 201);
2025-09-19 16:43:03 +07:00
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
2025-09-22 13:25:31 +07:00
public function delete() {
$input = $this->request->getJSON(true);
try {
if (!isset($input["InternalPVID"]) || !is_numeric($input["InternalPVID"])) {
return $this->respond(['status' => 'error', 'message' => 'Invalid or missing ID'], 400);
}
// Check if visit exists
$visit = $this->model->find($input["InternalPVID"]);
if (!$visit) {
return $this->respond(['status' => 'error', 'message' => 'Visit not found'], 404);
}
// Soft delete using EndDate (configured in model)
$result = $this->model->delete($input["InternalPVID"]);
if ($result) {
return $this->respond(['status' => 'success', 'message' => 'Data deleted successfully'], 200);
} else {
return $this->respond(['status' => 'error', 'message' => 'Failed to delete data'], 500);
}
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
2025-10-23 12:16:52 +07:00
public function createADT() {
$input = $this->request->getJSON(true);
if (!$input["InternalPVID"] || !is_numeric($input["InternalPVID"])) { return $this->respond(['status' => 'error', 'message' => 'Invalid or missing ID'], 400); }
$modelPVA = new PatVisitADTModel();
try {
$data = $modelPVA->insert($input, true);
return $this->respond(['status' => 'success', 'message' => 'Data created successfully', 'data' => $data], 201);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
public function updateADT() {
$input = $this->request->getJSON(true);
if (!$input["PVADTID"] || !is_numeric($input["PVADTID"])) { return $this->respond(['status' => 'error', 'message' => 'Invalid or missing ID'], 400); }
$modelPVA = new PatVisitADTModel();
try {
$data = $modelPVA->update($input['PVADTID'], $input);
return $this->respond(['status' => 'success', 'message' => 'Data updated successfully', 'data' => $data], 200);
2025-10-23 12:16:52 +07:00
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
public function getADTByVisit($InternalPVID = null) {
try {
if (!$InternalPVID || !is_numeric($InternalPVID)) {
return $this->respond(['status' => 'error', 'message' => 'Invalid or missing InternalPVID'], 400);
}
$modelPVA = new PatVisitADTModel();
$rows = $modelPVA->select('patvisitadt.*, location.LocFull as LocationName,
attDoc.NameFirst as AttDocFirstName, attDoc.NameLast as AttDocLastName,
refDoc.NameFirst as RefDocFirstName, refDoc.NameLast as RefDocLastName,
admDoc.NameFirst as AdmDocFirstName, admDoc.NameLast as AdmDocLastName,
cnsDoc.NameFirst as CnsDocFirstName, cnsDoc.NameLast as CnsDocLastName')
->join('location', 'location.LocationID = patvisitadt.LocationID', 'left')
->join('contact attDoc', 'attDoc.ContactID = patvisitadt.AttDoc', 'left')
->join('contact refDoc', 'refDoc.ContactID = patvisitadt.RefDoc', 'left')
->join('contact admDoc', 'admDoc.ContactID = patvisitadt.AdmDoc', 'left')
->join('contact cnsDoc', 'cnsDoc.ContactID = patvisitadt.CnsDoc', 'left')
->where('patvisitadt.InternalPVID', $InternalPVID)
->where('patvisitadt.DelDate', null)
->orderBy('patvisitadt.CreateDate', 'ASC')
->findAll();
if (empty($rows)) {
return $this->respond(['status' => 'success', 'message' => 'No ADT history found', 'data' => []], 200);
}
return $this->respond(['status' => 'success', 'message' => 'ADT history retrieved', 'data' => $rows], 200);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
public function showADT($PVADTID = null) {
try {
if (!$PVADTID || !is_numeric($PVADTID)) {
return $this->respond(['status' => 'error', 'message' => 'Invalid or missing PVADTID'], 400);
}
$modelPVA = new PatVisitADTModel();
$row = $modelPVA->select('patvisitadt.*, location.LocFull as LocationName,
attDoc.NameFirst as AttDocFirstName, attDoc.NameLast as AttDocLastName,
refDoc.NameFirst as RefDocFirstName, refDoc.NameLast as RefDocLastName,
admDoc.NameFirst as AdmDocFirstName, admDoc.NameLast as AdmDocLastName,
cnsDoc.NameFirst as CnsDocFirstName, cnsDoc.NameLast as CnsDocLastName')
->join('location', 'location.LocationID = patvisitadt.LocationID', 'left')
->join('contact attDoc', 'attDoc.ContactID = patvisitadt.AttDoc', 'left')
->join('contact refDoc', 'refDoc.ContactID = patvisitadt.RefDoc', 'left')
->join('contact admDoc', 'admDoc.ContactID = patvisitadt.AdmDoc', 'left')
->join('contact cnsDoc', 'cnsDoc.ContactID = patvisitadt.CnsDoc', 'left')
->where('patvisitadt.PVADTID', $PVADTID)
->where('patvisitadt.DelDate', null)
->first();
if (empty($row)) {
return $this->respond(['status' => 'success', 'message' => 'ADT record not found', 'data' => []], 200);
}
return $this->respond(['status' => 'success', 'message' => 'ADT record retrieved', 'data' => $row], 200);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
public function deleteADT() {
$input = $this->request->getJSON(true);
try {
if (!isset($input["PVADTID"]) || !is_numeric($input["PVADTID"])) {
return $this->respond(['status' => 'error', 'message' => 'Invalid or missing PVADTID'], 400);
}
$modelPVA = new PatVisitADTModel();
$adt = $modelPVA->find($input["PVADTID"]);
if (!$adt) {
return $this->respond(['status' => 'error', 'message' => 'ADT record not found'], 404);
}
$result = $modelPVA->delete($input["PVADTID"]);
if ($result) {
return $this->respond(['status' => 'success', 'message' => 'ADT record deleted successfully'], 200);
} else {
return $this->respond(['status' => 'error', 'message' => 'Failed to delete ADT record'], 500);
}
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
}