db = \Config\Database::connect(); $this->visnum_prefix = "DV"; } public function show($PVID = null) { try { $model = new PatVisitModel(); $row = $model->show($PVID); 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 { $model = new PatVisitModel(); $row = $model->showByPatient($InternalPID); 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() { $input = $this->request->getJSON(true); 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); } $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); } 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); } 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) { $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; } }