From 126a62b8d02b527b7593640b8a31b18846ad3e73 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Tue, 22 Sep 2026 23:40:05 +0530 Subject: [PATCH] fix(opd): fix undefined patLastName and make FHIR and audit logging fail-safe in opd_prescription_save --- app/Controllers/Opd_prescription.php | 23 +++++++++++++++++------ app/Libraries/ClinicalAuditTrail.php | 25 +++++++++++++++---------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/app/Controllers/Opd_prescription.php b/app/Controllers/Opd_prescription.php index a03e5da..2a92362 100644 --- a/app/Controllers/Opd_prescription.php +++ b/app/Controllers/Opd_prescription.php @@ -693,10 +693,19 @@ class Opd_prescription extends BaseController $afterRow = $this->db->table('opd_prescription')->where('id', $recordId)->where('opd_id', $opdId)->get(1)->getRowArray() ?? []; if (!empty($afterRow)) { - $this->clinicalAuditTrail->logChangedFields('opd_prescription', $recordId, $beforeRow, $afterRow, $this->getCurrentUserId()); + try { + $this->clinicalAuditTrail->logChangedFields('opd_prescription', $recordId, $beforeRow, $afterRow, $this->getCurrentUserId()); + } catch (\Throwable $auditEx) { + log_message('error', 'Audit trail logging failed on opd_prescription: ' . $auditEx->getMessage()); + } } - $fhirStored = $this->storePrescriptionFhirBundle((int) $opdId, (int) $recordId, $patientRow, (array) $opdRow); + $fhirStored = false; + try { + $fhirStored = $this->storePrescriptionFhirBundle((int) $opdId, (int) $recordId, $patientRow, (array) $opdRow); + } catch (\Throwable $fhirEx) { + log_message('error', 'FHIR bundle store failed during opd save: ' . $fhirEx->getMessage() . ' in ' . $fhirEx->getFile() . ':' . $fhirEx->getLine()); + } $this->syncPatientAddictionFlags((int) ($opdRow->p_id ?? 0), [ 'is_smoking' => (int) $this->request->getPost('is_smoking'), @@ -746,11 +755,11 @@ class Opd_prescription extends BaseController 'csrfHash' => csrf_hash(), ]); } catch (\Throwable $e) { - log_message('error', 'opd_prescription_save failed: {message}', ['message' => $e->getMessage()]); + log_message('error', 'opd_prescription_save failed: ' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine()); return $this->response->setJSON([ 'update' => 0, - 'error_text' => 'Unable to save prescription right now. Please retry.', + 'error_text' => 'Unable to save prescription right now. ' . $e->getMessage(), 'csrfName' => csrf_token(), 'csrfHash' => csrf_hash(), ]); @@ -4879,7 +4888,8 @@ class Opd_prescription extends BaseController $abhaAddress = $abhaField !== null ? trim((string) ($patientRow[$abhaField] ?? '')) : ''; $patFirstName = $this->sanitizePersonNamePart((string) ($patientRow['p_fname'] ?? '')); - $patFullName = $patFirstName; + $patLastName = $this->sanitizePersonNamePart((string) ($patientRow['p_lname'] ?? '')); + $patFullName = trim($patFirstName . ($patLastName !== '' ? ' ' . $patLastName : '')); if ($patFullName === '') { $patFullName = trim((string) ($opdRow['P_name'] ?? '')); } @@ -11694,7 +11704,8 @@ class Opd_prescription extends BaseController $abhaAddress = $abhaField !== null ? trim((string) ($patientRow[$abhaField] ?? '')) : ''; $patFirstName = $this->sanitizePersonNamePart((string) ($patientRow['p_fname'] ?? '')); - $patFullName = $patFirstName; + $patLastName = $this->sanitizePersonNamePart((string) ($patientRow['p_lname'] ?? '')); + $patFullName = trim($patFirstName . ($patLastName !== '' ? ' ' . $patLastName : '')); if ($patFullName === '') { $patFullName = trim((string) ($opdRow['P_name'] ?? '')); } diff --git a/app/Libraries/ClinicalAuditTrail.php b/app/Libraries/ClinicalAuditTrail.php index cb99cc2..503333e 100644 --- a/app/Libraries/ClinicalAuditTrail.php +++ b/app/Libraries/ClinicalAuditTrail.php @@ -40,16 +40,21 @@ class ClinicalAuditTrail $module . '|' . $recordIdText . '|' . $fieldName . '|' . $oldValueJson . '|' . $newValueJson . '|' . $userIdText . '|' . $actionAt ); - return (bool) $this->auditModel->insert([ - 'module' => $module, - 'record_id' => $recordIdText, - 'field_name' => $fieldName, - 'old_value' => $oldValueJson, - 'new_value' => $newValueJson, - 'user_id' => $userIdText, - 'action_at' => $actionAt, - 'hash' => $hash, - ]); + try { + return (bool) $this->auditModel->insert([ + 'module' => $module, + 'record_id' => $recordIdText, + 'field_name' => $fieldName, + 'old_value' => $oldValueJson, + 'new_value' => $newValueJson, + 'user_id' => $userIdText, + 'action_at' => $actionAt, + 'hash' => $hash, + ]); + } catch (\Throwable $e) { + log_message('error', 'Clinical audit insert failed: ' . $e->getMessage()); + return false; + } } /**