2025-09-19 16:43:03 +07:00
|
|
|
<?php
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
|
|
|
|
use CodeIgniter\API\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;
|
2026-02-12 16:50:21 +07:00
|
|
|
use App\Models\Patient\PatientModel;
|
2025-09-23 15:57:19 +07:00
|
|
|
|
2026-01-05 16:55:34 +07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2026-02-12 16:50:21 +07:00
|
|
|
public function index() {
|
|
|
|
|
try {
|
|
|
|
|
$InternalPID = $this->request->getVar('InternalPID');
|
|
|
|
|
$PVID = $this->request->getVar('PVID');
|
|
|
|
|
|
|
|
|
|
$builder = $this->model->select('patvisit.*, patient.NameFirst, patient.NameLast, patient.PatientID')
|
|
|
|
|
->join('patient', 'patient.InternalPID=patvisit.InternalPID', 'left');
|
|
|
|
|
|
|
|
|
|
if ($InternalPID) {
|
|
|
|
|
$builder->where('patvisit.InternalPID', $InternalPID);
|
|
|
|
|
}
|
|
|
|
|
if ($PVID) {
|
|
|
|
|
$builder->where('patvisit.PVID', $PVID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$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);
|
2025-12-29 12:55:31 +07:00
|
|
|
if (empty($row)) {
|
2026-02-12 16:50:21 +07:00
|
|
|
return $this->respond([ 'status' => 'success', 'message'=> "data not found", 'data' => [] ], 200);
|
2025-12-29 12:55:31 +07:00
|
|
|
}
|
|
|
|
|
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 {
|
2026-02-12 16:50:21 +07:00
|
|
|
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);
|
2026-02-12 16:50:21 +07:00
|
|
|
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 {
|
2026-02-12 16:50:21 +07:00
|
|
|
// 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
|
|
|
|
2026-02-12 16:50:21 +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);
|
2026-02-12 16:50:21 +07:00
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-15 21:05:25 +07:00
|
|
|
|
|
|
|
|
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 IS 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 IS 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());
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-05 16:55:34 +07:00
|
|
|
}
|