Merge branch 'main' of https://github.com/mahdahar/clqms-be
This commit is contained in:
commit
c667243b88
@ -3,31 +3,21 @@ namespace App\Controllers;
|
|||||||
|
|
||||||
use CodeIgniter\API\ResponseTrait;
|
use CodeIgniter\API\ResponseTrait;
|
||||||
use CodeIgniter\Controller;
|
use CodeIgniter\Controller;
|
||||||
|
|
||||||
|
|
||||||
use App\Models\CounterModel;
|
|
||||||
use App\Models\PatVisitModel;
|
use App\Models\PatVisitModel;
|
||||||
|
|
||||||
class PatVisit extends Controller {
|
class PatVisit extends Controller {
|
||||||
use ResponseTrait;
|
use ResponseTrait;
|
||||||
|
|
||||||
protected $db;
|
protected $modelPatVisit;
|
||||||
protected $visnum_prefix;
|
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$this->db = \Config\Database::connect();
|
$this->modelPatVisit = new PatVisitModel();
|
||||||
$this->visnum_prefix = "DV";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function show($PVID = null) {
|
public function show($PVID = null) {
|
||||||
try {
|
try {
|
||||||
$model = new PatVisitModel();
|
$row = $this->modelPatVisit->show($PVID);
|
||||||
$row = $model->show($PVID);
|
return $this->respond([ 'status' => 'success', 'message'=> "data found", 'data' => $row ], 200);
|
||||||
return $this->respond([
|
|
||||||
'status' => 'success',
|
|
||||||
'message'=> "data found",
|
|
||||||
'data' => $row,
|
|
||||||
], 200);
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
return $this->failServerError('Something went wrong '.$e->getMessage());
|
return $this->failServerError('Something went wrong '.$e->getMessage());
|
||||||
}
|
}
|
||||||
@ -35,13 +25,8 @@ class PatVisit extends Controller {
|
|||||||
|
|
||||||
public function showByPatient($InternalPID = null) {
|
public function showByPatient($InternalPID = null) {
|
||||||
try {
|
try {
|
||||||
$model = new PatVisitModel();
|
$row = $this->modelPatVisit->showByPatient($InternalPID);
|
||||||
$row = $model->showByPatient($InternalPID);
|
return $this->respond(['status' => 'success', 'message'=> "data found", 'data' => $row ], 200);
|
||||||
return $this->respond([
|
|
||||||
'status' => 'success',
|
|
||||||
'message'=> "data found",
|
|
||||||
'data' => $row,
|
|
||||||
], 200);
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
return $this->failServerError('Something went wrong '.$e->getMessage());
|
return $this->failServerError('Something went wrong '.$e->getMessage());
|
||||||
}
|
}
|
||||||
@ -50,128 +35,22 @@ class PatVisit extends Controller {
|
|||||||
public function update() {
|
public function update() {
|
||||||
$input = $this->request->getJSON(true);
|
$input = $this->request->getJSON(true);
|
||||||
try {
|
try {
|
||||||
|
|
||||||
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); }
|
if (!$input["InternalPVID"] || !is_numeric($input["InternalPVID"])) { return $this->respond(['status' => 'error', 'message' => 'Invalid or missing ID'], 400); }
|
||||||
$InternalPVID = $input["InternalPVID"];
|
$data = $this->modelPatVisit->updatePatVisit($input);
|
||||||
$dataPatVisit = $this->preparePatVisitData($input);
|
return $this->respond(['status' => 'success', 'message' => 'Data updated successfully', 'data' => $data], 201);
|
||||||
$dataPatDiag = $this->preparePatDiagData($input);
|
|
||||||
$dataPatVisitAdt = $this->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 ($this->db->transStatus() === false) {
|
|
||||||
$dbError = $this->db->error();
|
|
||||||
return $this->failServerError('Failed to create patient data (transaction rolled back): ' . ($dbError ?? 'Unknown error'));
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->respond([
|
|
||||||
'status' => 'success',
|
|
||||||
'message' => 'Data updated successfully',
|
|
||||||
'data' => $dataPatVisit
|
|
||||||
], 201);
|
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$this->db->transRollback();
|
|
||||||
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create() {
|
public function create() {
|
||||||
|
$input = $this->request->getJSON(true);
|
||||||
try {
|
try {
|
||||||
$input = $this->request->getJSON(true);
|
$data = $this->modelPatVisit->createPatVisit($input);
|
||||||
if (!$input) { return $this->respond(['status' => 'error', 'message' => 'Invalid JSON input'], 400); }
|
return $this->respond(['status' => 'success', 'message' => 'Data updated successfully', 'data' => $data], 201);
|
||||||
|
|
||||||
if($input['PVID'] =='' || !isset($input['PVID'])) {
|
|
||||||
$model = new CounterModel();
|
|
||||||
$input['PVID'] = $this->visnum_prefix .$model->use(2);
|
|
||||||
//$input['PVID'] = $this->preparePVID();
|
|
||||||
}
|
|
||||||
|
|
||||||
$dataPatVisit = $this->preparePatVisitData($input);
|
|
||||||
$dataPatDiag = $this->preparePatDiagData($input);
|
|
||||||
$dataPatVisitAdt = $this->preparePatVisitAdtData($input);
|
|
||||||
|
|
||||||
$this->db->transStart();
|
|
||||||
$this->db->table('patvisit')->insert($dataPatVisit);
|
|
||||||
$newInternalPVID = $this->db->insertID();
|
|
||||||
if(!empty($dataPatDiag)) {
|
|
||||||
$dataPatDiag['InternalPVID'] = $newInternalPVID;
|
|
||||||
$this->db->table('patdiag')->insert($dataPatDiag);
|
|
||||||
}
|
|
||||||
if(!empty($dataPatVisitAdt)) {
|
|
||||||
$dataPatVisitAdt['InternalPVID'] = $newInternalPVID;
|
|
||||||
$this->db->table('patvisitadt')->insert($dataPatVisitAdt);
|
|
||||||
}
|
|
||||||
|
|
||||||
$dbError = $this->db->error();
|
|
||||||
|
|
||||||
$this->db->transComplete();
|
|
||||||
|
|
||||||
if (!empty($dbError['message'])) {
|
|
||||||
$this->db->transRollback();
|
|
||||||
return $this->failServerError('create 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) {
|
} catch (\Exception $e) {
|
||||||
$this->db->transRollback();
|
|
||||||
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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,
|
|
||||||
"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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -3,16 +3,19 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use CodeIgniter\Model;
|
use CodeIgniter\Model;
|
||||||
|
use App\Models\CounterModel;
|
||||||
|
|
||||||
class PatVisitModel extends Model {
|
class PatVisitModel extends Model {
|
||||||
protected $table = 'patvisit';
|
protected $table = 'patvisit';
|
||||||
protected $primaryKey = 'InternalPVID';
|
protected $primaryKey = 'InternalPVID';
|
||||||
protected $allowedFields = ['PVID', 'InternalPID', 'EpisodeID', 'EndDate'];
|
protected $allowedFields = ['PVID', 'InternalPID', 'EpisodeID', 'EndDate'];
|
||||||
|
protected $db;
|
||||||
|
protected $visnum_prefix;
|
||||||
|
|
||||||
protected $useTimestamps = true;
|
public function __construct() {
|
||||||
protected $dateFormat = 'datetime';
|
$this->db = \Config\Database::connect();
|
||||||
protected $createdField = 'CreateDate';
|
$this->visnum_prefix = "DV";
|
||||||
protected $deletedField = 'EndDate';
|
}
|
||||||
|
|
||||||
public function show($PVID) {
|
public function show($PVID) {
|
||||||
$rows = $this->join('patdiag pd', 'pd.InternalPVID=patvisit.InternalPVID', 'left')
|
$rows = $this->join('patdiag pd', 'pd.InternalPVID=patvisit.InternalPVID', 'left')
|
||||||
@ -28,4 +31,75 @@ class PatVisitModel extends Model {
|
|||||||
return $rows;
|
return $rows;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function createPatVisit($input) {
|
||||||
|
try{
|
||||||
|
if(!isset($input['PVID'])) {
|
||||||
|
$counter = new CounterModel();
|
||||||
|
$input['PVID'] = $this->visnum_prefix .$counter->use(2);
|
||||||
|
}
|
||||||
|
$this->db->transStart();
|
||||||
|
|
||||||
|
$this->insert($input);
|
||||||
|
$InternalPVID = $this->getInsertID();
|
||||||
|
|
||||||
|
if(!empty($input['PatDiag'])) {
|
||||||
|
$input['PatDiag']['InternalPVID'] = $InternalPVID;
|
||||||
|
$this->db->table('patdiag')->insert($input['PatDiag']);
|
||||||
|
}
|
||||||
|
if(!empty($input['PatVisitADT'])) {
|
||||||
|
$input['PatVisitADT']['InternalPVID'] = $InternalPVID;
|
||||||
|
$this->db->table('patvisitadt')->insert($input['PatVisitADT']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->db->transComplete();
|
||||||
|
return $input['PVID'];
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->db->transRollback();
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updatePatVisit($input) {
|
||||||
|
$InternalPVID = $input['InternalPVID'];
|
||||||
|
try{
|
||||||
|
$this->db->transStart();
|
||||||
|
$this->where('InternalPVID',$InternalPVID)->set($input)->update();
|
||||||
|
|
||||||
|
// patdiag
|
||||||
|
$exist = $this->db->table('patdiag')->where('InternalPVID',$InternalPVID)->get()->getRow();
|
||||||
|
if($exist) {
|
||||||
|
if(!empty($input['PatDiag'])) {
|
||||||
|
$input['PatDiag']['InternalPVID'] = $InternalPVID;
|
||||||
|
$this->db->table('patdiag')->where('InternalPVID',$InternalPVID)->set($input['PatDiag'])->update();
|
||||||
|
} else { $this->db->table('patdiag')->where('InternalPVID',$InternalPVID)->delete(); }
|
||||||
|
} else {
|
||||||
|
if(!empty($input['PatDiag'])) {
|
||||||
|
$input['PatDiag']['InternalPVID'] = $InternalPVID;
|
||||||
|
$this->db->table('patdiag')->insert($input['PatDiag']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// patvisitadt
|
||||||
|
$exist = $this->db->table('patvisitadt')->where('InternalPVID',$InternalPVID)->get()->getRow();
|
||||||
|
if($exist) {
|
||||||
|
if(!empty($input['PatVisitADT'])) {
|
||||||
|
$input['PatVisitADT']['InternalPVID'] = $InternalPVID;
|
||||||
|
$this->db->table('patvisitadt')->where('InternalPVID',$InternalPVID)->set($input['PatVisitADT'])->update();
|
||||||
|
} else { $this->db->table('patvisitadt')->where('InternalPVID',$InternalPVID)->delete(); }
|
||||||
|
} else {
|
||||||
|
if(!empty($input['PatVisitADT'])) {
|
||||||
|
$input['PatVisitADT']['InternalPVID'] = $InternalPVID;
|
||||||
|
$this->db->table('patvisitadt')->insert($input['PatVisitADT']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->db->transComplete();
|
||||||
|
return $input['PVID'];
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->db->transRollback();
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user