154 lines
4.7 KiB
PHP
154 lines
4.7 KiB
PHP
<?php
|
|
namespace App\Controllers;
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
use CodeIgniter\Controller;
|
|
use CodeIgniter\Database\RawSql;
|
|
|
|
class PatVisit extends Controller {
|
|
use ResponseTrait;
|
|
|
|
public function __construct() {
|
|
$this->db = \Config\Database::connect();
|
|
}
|
|
|
|
private function preparePatVisitData(array $input): array {
|
|
$data = [
|
|
"PVID" => $input['PVID'] ?? null,
|
|
"InternalPID" => $input['InternalPID'] ?? null,
|
|
"EpisodeID" => $input['EpisodeID'] ?? null
|
|
];
|
|
|
|
if(!empty($input['InternalPVID'])) { $data["InternalPVID"] = $input["InternalPVID"]; }
|
|
|
|
return $data;
|
|
}
|
|
|
|
private function preparePatDiagData(array $input): array {
|
|
$data = [
|
|
"InternalPVID" => $input['InternalPVID'] ?? null,
|
|
"InternalPID" => $input['InternalPID'] ?? null,
|
|
"DiagCode" => $input['DiagCode'] ?? null,
|
|
"Diagnosis" => $input['Diagnosis'] ?? null
|
|
];
|
|
|
|
return $data;
|
|
}
|
|
|
|
private function preparePatVisitAdtData(array $input): array {
|
|
$data = [
|
|
"InternalPVID" => $input['InternalPVID'] ?? null,
|
|
"InternalPID" => $input['InternalPID'] ?? null,
|
|
"ADTCode" => $input['ADTCode'] ?? null,
|
|
"LocationID" => $input['LocationID'] ?? null,
|
|
"AttDoc" => $input['AttDoc'] ?? null,
|
|
"RefDoc" => $input['RefDoc'] ?? null,
|
|
"AdmDoc" => $input['AdmDoc'] ?? null,
|
|
"CnsDoc" => $input['CnsDoc'] ?? null
|
|
];
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function show($PVID = null) {
|
|
try {
|
|
$row = $this->db->table('patvisit pv')
|
|
->join('patdiag pd', 'pd.InternalPVID=pv.Internal.PVID', 'left')
|
|
->join('patvisitadt pva', 'pd.InternalPVID=pva.InternalPVID', 'left')
|
|
->get()->getResultArray();
|
|
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message'=> "data found",
|
|
'data' => $row,
|
|
], 200);
|
|
} catch (\Exception $e) {
|
|
return $this->failServerError('Something went wrong '.$e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function update() {
|
|
try {
|
|
$input = $this->request->getJSON(true);
|
|
if (!$input) { return $this->respond(['status' => 'error', 'message' => 'Invalid JSON input'], 400); }
|
|
|
|
if (!$input["InternalPVID"] || !is_numeric($input["InternalPVID"])) { return $this->respond(['status' => 'error', 'message' => 'Invalid or missing ID'], 400); }
|
|
$InternalPVID = $input["InternalPVID"];
|
|
$dataPatVisit = preparePatVisitData($input);
|
|
$dataPatDiag = preparePatDiagData($input);
|
|
$dataPatVisitAdt = preparePatVisitAdtData($input);
|
|
|
|
$this->db->transStart();
|
|
$this->db->table('patvisit')->where('InternalPVID', $InternalPVID)->update($dataPatVisit);
|
|
$this->db->table('patdiag')->where('InternalPVID', $InternalPVID)->update($dataPatDiag);
|
|
$this->db->table('patvisitadt')->where('InternalPVID', $InternalPVID)->update($dataPatVisitAdt);
|
|
|
|
$dbError = $this->db->error();
|
|
|
|
$this->db->transComplete();
|
|
|
|
if (!empty($dbError['message'])) {
|
|
$this->db->transRollback();
|
|
return $this->failServerError('Update failed: ' . $dbError['message']);
|
|
}
|
|
|
|
|
|
if ($this->db->transStatus() === false) {
|
|
$dbError = $this->db->error();
|
|
return $this->failServerError('Failed to update patient data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown error'));
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => 'Data updated successfully',
|
|
'data' => $dataPatVisit
|
|
], 201);
|
|
|
|
} catch (\Exception $e) {
|
|
$this->db->transRollback();
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function create() {
|
|
try {
|
|
$input = $this->request->getJSON(true);
|
|
if (!$input) { return $this->respond(['status' => 'error', 'message' => 'Invalid JSON input'], 400); }
|
|
|
|
$InternalPVID = $input["InternalPVID"];
|
|
$dataPatVisit = preparePatVisitData($input);
|
|
$dataPatDiag = preparePatDiagData($input);
|
|
$dataPatVisitAdt = preparePatVisitAdtData($input);
|
|
|
|
$this->db->transStart();
|
|
$this->db->table('patvisit')->insert($dataPatVisit);
|
|
if(!empty($dataPatDiag)) { $this->db->table('patdiag')->insert($dataPatDiag); }
|
|
if(!empty($dataPatVisitAdt)) {$this->db->table('patvisitadt')->insert($dataPatVisitAdt); }
|
|
|
|
$dbError = $this->db->error();
|
|
|
|
$this->db->transComplete();
|
|
|
|
if (!empty($dbError['message'])) {
|
|
$this->db->transRollback();
|
|
return $this->failServerError('Update failed: ' . $dbError['message']);
|
|
}
|
|
|
|
|
|
if ($this->db->transStatus() === false) {
|
|
$dbError = $this->db->error();
|
|
return $this->failServerError('Failed to update patient data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown error'));
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => 'Data insert success',
|
|
'data' => $dataPatVisit
|
|
], 201);
|
|
|
|
} catch (\Exception $e) {
|
|
$this->db->transRollback();
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
} |