clqms-be/app/Controllers/PatVisitController.php
mahdahar 305e605a60 Merge branch 'main' of github.com:mahdahar1/clqms01-be
Resolved conflicts and integrated remote changes:
- Keep PatVisitController fixes (validation, soft delete, proper HTTP status codes)
- Updated routes and controllers from remote
- Synced api-docs.yaml
2026-02-12 16:53:58 +07:00

173 lines
6.2 KiB
PHP

<?php
namespace App\Controllers;
use CodeIgniter\API\ResponseTrait;
use App\Controllers\BaseController;
use App\Models\PatVisit\PatVisitModel;
use App\Models\PatVisit\PatVisitADTModel;
use App\Models\Patient\PatientModel;
class PatVisitController extends BaseController {
use ResponseTrait;
protected $model;
public function __construct() {
$this->model = new PatVisitModel();
}
public function index() {
try {
<<<<<<< HEAD
$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());
=======
$page = $this->request->getVar('page') ?? 1;
$perPage = $this->request->getVar('per_page') ?? 50;
$rows = $this->model->paginate($perPage, 'default', $page);
$total = $this->model->countAllResults(false);
if($rows == []) { $message = "data not found"; }
else { $message = "data found"; }
return $this->respond(['status' => 'success', 'message'=> $message, 'data' => $rows, 'total' => $total, 'page' => $page, 'per_page' => $perPage ], 200);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong '.$e->getMessage());
>>>>>>> c38f9d2f914aa58a65dd6faf90b98f448cf4347d
}
}
public function show($PVID = null) {
try {
$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);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong '.$e->getMessage());
}
}
public function showByPatient($InternalPID = null) {
try {
$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);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong '.$e->getMessage());
}
}
public function update() {
$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);
}
$data = $this->model->updatePatVisit($input);
return $this->respond(['status' => 'success', 'message' => 'Data updated successfully', 'data' => $data], 200);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
public function create() {
$input = $this->request->getJSON(true);
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);
}
$data = $this->model->createPatVisit($input);
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 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());
}
}
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);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
}