From c83fc8e8d22d82f335687291719e66691b01cd75 Mon Sep 17 00:00:00 2001 From: mahdahar <89adham@gmail.com> Date: Fri, 3 Oct 2025 10:28:59 +0700 Subject: [PATCH] add patvisit --- app/Controllers/PatVisit.php | 149 ++++------------------------------- app/Models/PatVisitModel.php | 84 ++++++++++++++++++-- 2 files changed, 93 insertions(+), 140 deletions(-) diff --git a/app/Controllers/PatVisit.php b/app/Controllers/PatVisit.php index 77c72de..7c066f3 100644 --- a/app/Controllers/PatVisit.php +++ b/app/Controllers/PatVisit.php @@ -3,31 +3,21 @@ namespace App\Controllers; use CodeIgniter\API\ResponseTrait; use CodeIgniter\Controller; - - -use App\Models\CounterModel; use App\Models\PatVisitModel; class PatVisit extends Controller { use ResponseTrait; + + protected $modelPatVisit; - protected $db; - protected $visnum_prefix; - - public function __construct() { - $this->db = \Config\Database::connect(); - $this->visnum_prefix = "DV"; + public function __construct() { + $this->modelPatVisit = new PatVisitModel(); } public function show($PVID = null) { try { - $model = new PatVisitModel(); - $row = $model->show($PVID); - return $this->respond([ - 'status' => 'success', - 'message'=> "data found", - 'data' => $row, - ], 200); + $row = $this->modelPatVisit->show($PVID); + return $this->respond([ 'status' => 'success', 'message'=> "data found", 'data' => $row ], 200); } catch (\Exception $e) { return $this->failServerError('Something went wrong '.$e->getMessage()); } @@ -35,13 +25,8 @@ class PatVisit extends Controller { public function showByPatient($InternalPID = null) { try { - $model = new PatVisitModel(); - $row = $model->showByPatient($InternalPID); - return $this->respond([ - 'status' => 'success', - 'message'=> "data found", - 'data' => $row, - ], 200); + $row = $this->modelPatVisit->showByPatient($InternalPID); + return $this->respond(['status' => 'success', 'message'=> "data found", 'data' => $row ], 200); } catch (\Exception $e) { return $this->failServerError('Something went wrong '.$e->getMessage()); } @@ -49,129 +34,23 @@ class PatVisit extends Controller { public function update() { $input = $this->request->getJSON(true); - try { - - if (!$input) { return $this->respond( ['status' => 'error', 'message' => 'Invalid JSON input'], 400); } - + try { if (!$input["InternalPVID"] || !is_numeric($input["InternalPVID"])) { return $this->respond(['status' => 'error', 'message' => 'Invalid or missing ID'], 400); } - $InternalPVID = $input["InternalPVID"]; - $dataPatVisit = $this->preparePatVisitData($input); - $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); - + $data = $this->modelPatVisit->updatePatVisit($input); + return $this->respond(['status' => 'success', 'message' => 'Data updated successfully', 'data' => $data], 201); } catch (\Exception $e) { - $this->db->transRollback(); return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } public function create() { + $input = $this->request->getJSON(true); try { - $input = $this->request->getJSON(true); - if (!$input) { return $this->respond(['status' => 'error', 'message' => 'Invalid JSON input'], 400); } - - 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); + $data = $this->modelPatVisit->createPatVisit($input); + return $this->respond(['status' => 'success', 'message' => 'Data updated successfully', 'data' => $data], 201); } catch (\Exception $e) { - $this->db->transRollback(); 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; - } } \ No newline at end of file diff --git a/app/Models/PatVisitModel.php b/app/Models/PatVisitModel.php index 6a17263..ef732c2 100644 --- a/app/Models/PatVisitModel.php +++ b/app/Models/PatVisitModel.php @@ -3,16 +3,19 @@ namespace App\Models; use CodeIgniter\Model; +use App\Models\CounterModel; class PatVisitModel extends Model { protected $table = 'patvisit'; protected $primaryKey = 'InternalPVID'; - protected $allowedFields = ['PVID', 'InternalPID', 'EpisodeID', 'EndDate']; + protected $allowedFields = ['PVID', 'InternalPID', 'EpisodeID', 'EndDate']; + protected $db; + protected $visnum_prefix; - protected $useTimestamps = true; - protected $dateFormat = 'datetime'; - protected $createdField = 'CreateDate'; - protected $deletedField = 'EndDate'; + public function __construct() { + $this->db = \Config\Database::connect(); + $this->visnum_prefix = "DV"; + } public function show($PVID) { $rows = $this->join('patdiag pd', 'pd.InternalPVID=patvisit.InternalPVID', 'left') @@ -28,4 +31,75 @@ class PatVisitModel extends Model { 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; + } + } }