diff --git a/app/Controllers/Ipd_discharge.php b/app/Controllers/Ipd_discharge.php index 138bba2..8b73486 100644 --- a/app/Controllers/Ipd_discharge.php +++ b/app/Controllers/Ipd_discharge.php @@ -136,11 +136,32 @@ class Ipd_discharge extends BaseController . '' . '' . 'Age/Gender: {{AGE_GENDER}}' + . 'Guardian: {{GUARDIAN_RELATION}}{{GUARDIAN_NAME}}' + . 'Address: {{PATIENT_ADDRESS}}' + . '' + . '' . 'Admit Date: {{ADMIT_DATE}}' . 'Discharge Date: {{DISCHARGE_DATE}}' + . 'Prepared On: {{CURRENT_DATE}}' . '' . '' - . '
{{CONTENT}}
'; + . '
{{DISCHARGE_SUMMARY}}
' + . '
{{PRESENTING_COMPLAINTS}}
' + . '
{{PAIN_MEASUREMENT_SCALE}}
' + . '
{{GENERAL_EXAM_ADMISSION}}
' + . '
{{CLINICAL_INVESTIGATION_REPORTS}}
' + . '
{{FINAL_DIAGNOSIS}}
' + . '
{{COURSE_IN_HOSPITAL}}
' + . '
{{EXAMINATION_ON_DISCHARGE}}
' + . '
{{SURGERY}}
' + . '
{{PROCEDURE}}
' + . '
{{PERSONAL_HISTORY}}
' + . '
{{DRUG_ALLERGY_ADR}}
' + . '
{{CO_MORBIDITIES}}
' + . '
{{DISCHARGE_MEDICATIONS}}
' + . '
{{DIETARY_ADVICE}}
' + . '
{{DISCHARGE_INSTRUCTIONS}}
' + . '
{{SIGNATURE_BLOCK}}
'; } private function defaultDischargeTemplateSettings(): array @@ -172,17 +193,34 @@ class Ipd_discharge extends BaseController . '' . '' . 'Age/Gender: {{AGE_GENDER}}' - . 'Date of Admission: {{ADMIT_DATE}}' - . 'Date of Discharge: {{DISCHARGE_DATE}}' + . 'Guardian: {{GUARDIAN_RELATION}}{{GUARDIAN_NAME}}' + . 'Address: {{PATIENT_ADDRESS}}' . '' . '' - . 'Prepared On: {{CURRENT_DATE}}' + . 'Date of Admission: {{ADMIT_DATE}}' + . 'Date of Discharge: {{DISCHARGE_DATE}}' + . 'Prepared On: {{CURRENT_DATE}}' . '' . '' . '
' . 'NABH guidance note: Ensure diagnosis, procedures, clinical course, condition at discharge, medication with dose/duration, follow-up advice, red-flag signs, and emergency contact are documented.' . '
' - . '
{{CONTENT}}
' + . '
{{DISCHARGE_SUMMARY}}
' + . '
{{PRESENTING_COMPLAINTS}}
' + . '
{{PAIN_MEASUREMENT_SCALE}}
' + . '
{{GENERAL_EXAM_ADMISSION}}
' + . '
{{CLINICAL_INVESTIGATION_REPORTS}}
' + . '
{{FINAL_DIAGNOSIS}}
' + . '
{{COURSE_IN_HOSPITAL}}
' + . '
{{EXAMINATION_ON_DISCHARGE}}
' + . '
{{SURGERY}}
' + . '
{{PROCEDURE}}
' + . '
{{PERSONAL_HISTORY}}
' + . '
{{DRUG_ALLERGY_ADR}}
' + . '
{{CO_MORBIDITIES}}
' + . '
{{DISCHARGE_MEDICATIONS}}
' + . '
{{DIETARY_ADVICE}}
' + . '
{{DISCHARGE_INSTRUCTIONS}}
' . '

Counselling & Handover Confirmation

' . '' . '' @@ -316,8 +354,8 @@ class Ipd_discharge extends BaseController 'UHID' => esc($patientCode), 'IPD_CODE' => esc($ipdCode), 'AGE_GENDER' => esc($ageGender), - 'ADMIT_DATE' => esc((string) ($ipd->str_register_date ?? '')), - 'DISCHARGE_DATE' => esc((string) ($ipd->str_discharge_date ?? '')), + 'ADMIT_DATE' => esc($this->safeDate((string) ($ipd->str_register_date ?? $ipd->register_date ?? ''))), + 'DISCHARGE_DATE' => esc($this->safeDate((string) ($ipd->str_discharge_date ?? $ipd->discharge_date ?? ''))), 'CURRENT_DATE' => esc(date('d-m-Y')), 'PRINT_TIME' => esc(date('d-m-Y H:i:s')), 'H_Name' => esc($hName), @@ -333,6 +371,11 @@ class Ipd_discharge extends BaseController 'hospital_email' => esc($hEmail), ]; + $summaryTokens = $this->buildDischargeSummaryTokenVars($panelData); + foreach ($summaryTokens as $key => $value) { + $tokens[$key] = $value; + } + $sectionVars = $this->buildDischargeSectionTokenVars($content); foreach ($sectionVars as $key => $value) { $tokens[$key] = $value; @@ -361,6 +404,52 @@ class Ipd_discharge extends BaseController return $tokens; } + private function buildDischargeSummaryTokenVars(array $panelData): array + { + $ipd = $panelData['ipd_info'] ?? null; + $person = $panelData['person_info'] ?? null; + + $guardianRelation = ''; + $guardianName = ''; + $address = ''; + + if ($person) { + $guardianRelation = trim((string) ($person->p_relative ?? $person->relation ?? $person->guardian_relation ?? '')); + $guardianName = trim((string) ($person->p_rname ?? $person->relative_name ?? $person->guardian_name ?? '')); + + $addressParts = []; + foreach (['address_1', 'address', 'p_address', 'add1', 'add2', 'city', 'district', 'state', 'zip'] as $field) { + $part = trim((string) ($person->{$field} ?? '')); + if ($part === '') { + continue; + } + + if (! in_array($part, $addressParts, true)) { + $addressParts[] = $part; + } + } + $address = trim(implode(', ', $addressParts)); + } + + if ($address === '' && $ipd) { + $address = trim((string) ($ipd->address ?? $ipd->patient_address ?? $ipd->contact_address ?? '')); + } + + $guardianCombined = trim($guardianRelation . ($guardianName !== '' ? ' of ' . $guardianName : '')); + if ($guardianCombined === '') { + $guardianCombined = $guardianName !== '' ? $guardianName : $guardianRelation; + } + + return [ + 'GUARDIAN_RELATION' => esc($guardianRelation !== '' ? $guardianRelation . ' of ' : ''), + 'GUARDIAN_NAME' => esc($guardianName), + 'GUARDIAN' => esc($guardianCombined), + 'PATIENT_ADDRESS' => esc($address), + 'ADMIT_DATE_ONLY' => esc($this->safeDate((string) ($ipd->str_register_date ?? $ipd->register_date ?? ''))), + 'DISCHARGE_DATE_ONLY' => esc($this->safeDate((string) ($ipd->str_discharge_date ?? $ipd->discharge_date ?? ''))), + ]; + } + private function buildDischargeSectionTokenVars(string $content): array { $full = trim($content); @@ -371,6 +460,7 @@ class Ipd_discharge extends BaseController $allMarkers = [ 'Discharge Summary', 'Presenting Complaints and Reason for Admission', + 'Pain Measurement Scale', 'General Examination on Admission', 'Clinical Investigation Reports', 'Final Diagnosis', @@ -379,12 +469,12 @@ class Ipd_discharge extends BaseController 'Surgery', 'Procedure', 'Personal History', + 'Drug Allergy / ADR', + 'Co-Morbidities', 'Discharge Medications', 'Discharge Advice/Instructions/Summary', 'Dietary Advice', 'Signature of Consultant', - 'Drug Allergy / ADR', - 'Co-Morbidities', 'Summary of key investigations during Hospitalization', 'Nursing Trend', ]; @@ -399,6 +489,7 @@ class Ipd_discharge extends BaseController $procedure = $section(['Procedure']); $personalHistory = $section(['Personal History']); $presentingComplaints = $section(['Presenting Complaints and Reason for Admission']); + $painMeasurement = $section(['Pain Measurement Scale']); $generalExam = $section(['General Examination on Admission']); $clinicalInvestigations = $section(['Clinical Investigation Reports']); $courseInHospital = $section(['Course in the hospital']); @@ -406,6 +497,8 @@ class Ipd_discharge extends BaseController $dischargeMedications = $section(['Discharge Medications']); $dietaryAdvice = $section(['Dietary Advice']); $dischargeInstructions = $section(['Discharge Advice/Instructions/Summary']); + $drugAllergyAdr = $section(['Drug Allergy / ADR']); + $coMorbidities = $section(['Co-Morbidities']); $signatureBlock = $section(['Signature of Consultant'], ['Signature of Medical Officer', 'Signature of Receiver / Date']); $vars = [ @@ -422,6 +515,9 @@ class Ipd_discharge extends BaseController 'DISCHARGE_MEDICATIONS' => $dischargeMedications, 'DIETARY_ADVICE' => $dietaryAdvice, 'DISCHARGE_INSTRUCTIONS' => $dischargeInstructions, + 'PAIN_MEASUREMENT_SCALE' => $painMeasurement, + 'DRUG_ALLERGY_ADR' => $drugAllergyAdr, + 'CO_MORBIDITIES' => $coMorbidities, 'SIGNATURE_BLOCK' => $signatureBlock, // Legacy style aliases to ease migration from CI3-style template variables. 'FinalDiagnosis' => $finalDiagnosis, @@ -436,6 +532,9 @@ class Ipd_discharge extends BaseController 'Discharge_Medications' => $dischargeMedications, 'diet_advice' => $dietaryAdvice, 'Discharge_Instructions' => $dischargeInstructions, + 'Pain_Measurement_Scale' => $painMeasurement, + 'Drug_Allergy_ADR' => $drugAllergyAdr, + 'Co_Morbidities' => $coMorbidities, ]; return $vars; @@ -1098,6 +1197,7 @@ class Ipd_discharge extends BaseController private function buildAutoDischargeSummaryTable(array $panelData): string { + $summary = $this->buildDischargeSummaryTokenVars($panelData); $ipd = $panelData['ipd_info'] ?? null; $person = $panelData['person_info'] ?? null; if (! $ipd || ! $person) { @@ -1107,28 +1207,45 @@ class Ipd_discharge extends BaseController $age = get_age_1($person->dob ?? null, $person->age ?? '', $person->age_in_month ?? '', $person->estimate_dob ?? ''); $patientName = trim((string) ($person->p_fname ?? '')); $patientCode = trim((string) ($person->uhid ?? $person->UHID ?? $person->patient_code ?? $person->p_code ?? $person->reg_no ?? '')); - $guardian = trim((string) ($ipd->contact_person_Name ?? $person->p_rname ?? '')); + $guardian = trim((string) ($summary['GUARDIAN'] ?? '')); $phone = trim((string) ($person->mphone1 ?? $ipd->P_mobile1 ?? $ipd->P_mobile2 ?? '')); - $address = trim((string) ($person->address_1 ?? $person->address ?? $person->p_address ?? '')); + $address = trim((string) ($summary['PATIENT_ADDRESS'] ?? '')); $orgName = trim((string) ($ipd->ins_short_name ?? $ipd->ins_company_name ?? '')); if ($orgName === '') { $orgName = 'Direct'; } $department = $this->getDischargeDepartmentName($ipd); + $admitDate = $this->safeDate((string) ($ipd->str_register_date ?? $ipd->register_date ?? '')); + $dischargeDate = $this->safeDate((string) ($ipd->str_discharge_date ?? $ipd->discharge_date ?? '')); return '

Discharge Summary

' . '
' . '
Medication explained to patient/attendantRemarks:
' . '' . '' - . '' - . '' + . '' + . '' . '' . '' . '
Name' . esc($patientName) . 'UHID' . esc($patientCode) . '
Age & Gender' . esc(trim($age . ' / ' . ((string) ($person->xgender ?? '')))) . 'IPD No.' . esc((string) ($ipd->ipd_code ?? '')) . '
Guardian' . esc($guardian) . 'Admission' . esc((string) ($ipd->str_register_date ?? '')) . '
Phone No.' . esc($phone) . 'Discharge' . esc((string) ($ipd->str_discharge_date ?? '')) . '
Guardian' . esc($guardian) . 'Admission' . esc($admitDate) . '
Phone No.' . esc($phone) . 'Discharge' . esc($dischargeDate) . '
Address' . esc($address) . 'Org. Name' . esc($orgName) . '
Department' . esc($department) . '
' . '
'; } + private function renderStoredHtmlFragment(string $raw): string + { + $value = trim($raw); + if ($value === '') { + return ''; + } + + $value = html_entity_decode($value, ENT_QUOTES | ENT_HTML5, 'UTF-8'); + if (stripos($value, '<') === false) { + return nl2br(esc($value)); + } + + return $value; + } + private function buildNarrativeSection(string $title, array $rows, string $remark = ''): string { $lines = []; @@ -1247,12 +1364,13 @@ class Ipd_discharge extends BaseController $complaintMeta = $this->parseComplaintMetaPayload((string) ($complaintRemark['comp_report'] ?? '')); $painValue = (string) ($complaintMeta['pain_value'] ?? ''); $painLabel = $this->painScaleLabel($painValue); + $painSection = ''; if ($painLabel !== '') { - $sections[] = '
Pain Measurement Scale: ' . esc($painLabel) . ' (' . esc($painValue) . ')
'; + $painSection = '
Pain Measurement Scale: ' . esc($painLabel) . ' (' . esc($painValue) . ')
'; } elseif ((string) ($opdHistory['pain_label'] ?? '') !== '') { $opdPainLabel = (string) ($opdHistory['pain_label'] ?? ''); $opdPainValue = (string) ($opdHistory['pain_value'] ?? ''); - $sections[] = '
Pain Measurement Scale: ' . esc($opdPainLabel) + $painSection = '
Pain Measurement Scale: ' . esc($opdPainLabel) . ($opdPainValue !== '' ? ' (' . esc($opdPainValue) . ')' : '') . '
'; } @@ -1316,6 +1434,7 @@ class Ipd_discharge extends BaseController $sections[] = '

Personal History

' . esc(implode(', ', $personalHistory)) . '
'; } + $allergySection = ''; $allergyLines = []; if ((string) ($opdHistory['drug_allergy_status'] ?? '') !== '') { $allergyLines[] = '
Drug Allergy Status: ' . esc((string) ($opdHistory['drug_allergy_status'] ?? '')) . '
'; @@ -1330,7 +1449,7 @@ class Ipd_discharge extends BaseController $allergyLines[] = '
Current Medications: ' . esc((string) ($opdHistory['current_medications'] ?? '')) . '
'; } if (! empty($allergyLines)) { - $sections[] = '

Drug Allergy / ADR

' . implode('', $allergyLines); + $allergySection = '

Drug Allergy / ADR

' . implode('', $allergyLines); } $coMorbText = trim((string) ($opdHistory['co_morbidities'] ?? '')); @@ -1473,6 +1592,14 @@ class Ipd_discharge extends BaseController $sections[] = $courseBlock; } + if ($painSection !== '') { + $sections[] = $painSection; + } + + if ($allergySection !== '') { + $sections[] = $allergySection; + } + $nursingTrendSection = $this->buildNursingTrendSection($ipdId); if ($nursingTrendSection !== '') { $sections[] = $nursingTrendSection; @@ -1614,12 +1741,12 @@ class Ipd_discharge extends BaseController $otherText = trim((string) ($instructionMeta['other_text'] ?? '')); if ($otherText !== '') { - $html .= '
Other Advice: ' . nl2br(esc($otherText)) . '
'; + $html .= '
Other Advice: ' . $this->renderStoredHtmlFragment($otherText) . '
'; } $remark = trim((string) ($first['comp_remark'] ?? '')); if ($remark !== '') { - $html .= '
' . nl2br(esc($remark)) . '
'; + $html .= '
' . $this->renderStoredHtmlFragment($remark) . '
'; } $reviewAfter = trim((string) ($first['review_after'] ?? '')); @@ -1637,7 +1764,7 @@ class Ipd_discharge extends BaseController $footerText = trim((string) ($first['footer_text'] ?? '')); if ($footerText !== '') { - $html .= '
' . nl2br(esc($footerText)) . '
'; + $html .= '
' . $this->renderStoredHtmlFragment($footerText) . '
'; } $sections[] = $html; diff --git a/app/Views/Setting/Template/discharge_templates.php b/app/Views/Setting/Template/discharge_templates.php index c9a5e3a..8f1ebf3 100644 --- a/app/Views/Setting/Template/discharge_templates.php +++ b/app/Views/Setting/Template/discharge_templates.php @@ -36,13 +36,15 @@ $status = (int) ($edit['status'] ?? 1);
Available placeholders: {{CONTENT}}, {{PATIENT_NAME}}, {{UHID}}, {{IPD_CODE}}, - {{AGE_GENDER}}, {{ADMIT_DATE}}, {{DISCHARGE_DATE}}, {{CURRENT_DATE}}, + {{AGE_GENDER}}, {{GUARDIAN}}, {{GUARDIAN_RELATION}}, {{GUARDIAN_NAME}}, {{PATIENT_ADDRESS}}, + {{ADMIT_DATE}}, {{DISCHARGE_DATE}}, {{ADMIT_DATE_ONLY}}, {{DISCHARGE_DATE_ONLY}}, {{CURRENT_DATE}}, {{H_Name}}, {{H_address_1}}, {{H_address_2}}, {{H_phone_No}}, {{H_Email}}, {{hospital_name}}, {{hospital_address}}, {{hospital_phone}}, {{hospital_email}}, {{PRINT_TIME}}.
Section placeholders for custom layout/order: {{DISCHARGE_SUMMARY}}, {{FINAL_DIAGNOSIS}}, {{SURGERY}}, - {{PROCEDURE}}, {{PERSONAL_HISTORY}}, {{PRESENTING_COMPLAINTS}}, {{GENERAL_EXAM_ADMISSION}}, - {{CLINICAL_INVESTIGATION_REPORTS}}, {{COURSE_IN_HOSPITAL}}, {{EXAMINATION_ON_DISCHARGE}}, + {{PROCEDURE}}, {{PERSONAL_HISTORY}}, {{PRESENTING_COMPLAINTS}}, {{PAIN_MEASUREMENT_SCALE}}, + {{GENERAL_EXAM_ADMISSION}}, {{CLINICAL_INVESTIGATION_REPORTS}}, {{COURSE_IN_HOSPITAL}}, + {{EXAMINATION_ON_DISCHARGE}}, {{DRUG_ALLERGY_ADR}}, {{CO_MORBIDITIES}}, {{DISCHARGE_MEDICATIONS}}, {{DIETARY_ADVICE}}, {{DISCHARGE_INSTRUCTIONS}}, {{SIGNATURE_BLOCK}}.
@@ -159,7 +161,7 @@ $status = (int) ($edit['status'] ?? 1);
- Use the editor to format layout. Use the Source button for raw HTML. You can use one {{CONTENT}} block or arrange section placeholders in any custom order. + Use the editor to format layout. Use the Source button for raw HTML. You can use one {{CONTENT}} block or arrange the section placeholders in any custom order.