diff --git a/app/Controllers/Ipd_discharge.php b/app/Controllers/Ipd_discharge.php
index 06b54d6..d1d07e8 100644
--- a/app/Controllers/Ipd_discharge.php
+++ b/app/Controllers/Ipd_discharge.php
@@ -925,6 +925,128 @@ class Ipd_discharge extends BaseController
$sections[] = $html;
}
+ private function getDischargeDepartmentName(?object $ipd): string
+ {
+ $deptId = (int) ($ipd->dept_id ?? 0);
+ if ($deptId > 0 && $this->tableHasColumns('hc_department', ['id', 'vName'])) {
+ $row = $this->db->table('hc_department')
+ ->select('vName')
+ ->where('id', $deptId)
+ ->get(1)
+ ->getRowArray();
+ $name = trim((string) ($row['vName'] ?? ''));
+ if ($name !== '') {
+ return $name;
+ }
+ }
+
+ return '';
+ }
+
+ private function buildAutoDischargeSummaryTable(array $panelData): string
+ {
+ $ipd = $panelData['ipd_info'] ?? null;
+ $person = $panelData['person_info'] ?? null;
+ if (! $ipd || ! $person) {
+ return '';
+ }
+
+ $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 ?? ''));
+ $phone = trim((string) ($person->mphone1 ?? $ipd->P_mobile1 ?? $ipd->P_mobile2 ?? ''));
+ $address = trim((string) ($person->address_1 ?? $person->address ?? $person->p_address ?? ''));
+ $orgName = trim((string) ($ipd->ins_short_name ?? $ipd->ins_company_name ?? ''));
+ if ($orgName === '') {
+ $orgName = 'Direct';
+ }
+ $department = $this->getDischargeDepartmentName($ipd);
+
+ return '
Discharge Summary
'
+ . '
'
+ . ''
+ . '| 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 ?? '')) . ' |
'
+ . '| Address | ' . esc($address) . ' | Org. Name | ' . esc($orgName) . ' |
'
+ . '| Department | ' . esc($department) . ' | | |
'
+ . '
'
+ . '
';
+ }
+
+ private function buildNarrativeSection(string $title, array $rows, string $remark = ''): string
+ {
+ $lines = [];
+ foreach ($rows as $row) {
+ $report = trim((string) ($row['comp_report'] ?? ''));
+ $rowRemark = trim((string) ($row['comp_remark'] ?? ''));
+ if ($report === '' && $rowRemark === '') {
+ continue;
+ }
+
+ if ($report !== '' && $rowRemark !== '') {
+ $lines[] = esc($report) . ' ' . esc($rowRemark) . '';
+ } elseif ($report !== '') {
+ $lines[] = esc($report);
+ } else {
+ $lines[] = esc($rowRemark);
+ }
+ }
+
+ $remark = trim($remark);
+ if ($remark !== '') {
+ $lines[] = nl2br(esc($remark));
+ }
+
+ if ($lines === []) {
+ return '';
+ }
+
+ return '' . esc($title) . ' :
' . implode('
', $lines) . '
';
+ }
+
+ private function buildInlineExamSummary(array $rows): string
+ {
+ $parts = [];
+ foreach ($rows as $row) {
+ $label = trim((string) ($row['label'] ?? ''));
+ $value = trim((string) ($row['value'] ?? ''));
+ $unit = trim((string) ($row['unit'] ?? ''));
+ if ($label === '' || $value === '') {
+ continue;
+ }
+
+ $parts[] = esc($label) . ' :' . esc($value . ($unit !== '' ? $unit : '')) . '';
+ }
+
+ return implode(' ', $parts);
+ }
+
+ private function buildNamedDateSection(string $title, array $rows, string $nameField, string $dateField, string $dateLabel): string
+ {
+ $lines = [];
+ foreach ($rows as $row) {
+ $name = trim((string) ($row[$nameField] ?? ''));
+ if ($name === '') {
+ continue;
+ }
+ $dateText = trim((string) ($row[$dateField] ?? ''));
+ $line = esc($name);
+ if ($dateText !== '') {
+ $line .= ' / ' . esc($dateLabel) . ' : ' . esc($dateText);
+ }
+ $lines[] = $line;
+ }
+
+ if ($lines === []) {
+ return '';
+ }
+
+ return '' . esc($title) . ' :
' . implode('
', $lines) . '
';
+ }
+
private function buildAutoDischargeContent(int $ipdId, array $panelData): string
{
$ipd = $panelData['ipd_info'] ?? null;
@@ -956,13 +1078,17 @@ class Ipd_discharge extends BaseController
}
$sections = [];
+ $summaryTable = $this->buildAutoDischargeSummaryTable($panelData);
+ if ($summaryTable !== '') {
+ $sections[] = $summaryTable;
+ }
$complaints = $this->byIpdRows('ipd_discharge_complaint', ['comp_report', 'comp_remark'], 'id ASC', $ipdId);
- $this->addListSection($sections, 'Presenting Complaints and Reason for Admission', $complaints);
$complaintRemark = $this->firstRowByIpd('ipd_discharge_complaint_remark', $ipdId);
$complaintRemarkText = $this->normalizeRichText((string) ($complaintRemark['comp_remark'] ?? ''));
- if ($complaintRemarkText !== '') {
- $sections[] = '' . nl2br(esc($complaintRemarkText)) . '
';
+ $complaintBlock = $this->buildNarrativeSection('Presenting Complaints and Reason for Admission', $complaints, $complaintRemarkText);
+ if ($complaintBlock !== '') {
+ $sections[] = $complaintBlock;
}
$complaintMeta = $this->parseComplaintMetaPayload((string) ($complaintRemark['comp_report'] ?? ''));
@@ -990,41 +1116,10 @@ class Ipd_discharge extends BaseController
}
if (! empty($generalRows)) {
- $html = 'General Examination on Admission
'
- . '';
-
- $col = 0;
- $perRow = 4;
- foreach ($generalRows as $row) {
- if ($col % $perRow === 0) {
- $html .= '';
- }
-
- $label = (string) ($row['label'] ?? '');
- $value = trim((string) ($row['value'] ?? ''));
- $unit = trim((string) ($row['unit'] ?? ''));
- $html .= '| ' . esc($label) . ': ' . esc($value);
- if ($unit !== '') {
- $html .= ' ' . esc($unit);
- }
- $html .= ' | ';
-
- if ($col % $perRow === $perRow - 1) {
- $html .= '
';
- }
- $col++;
+ $generalSummary = $this->buildInlineExamSummary($generalRows);
+ if ($generalSummary !== '') {
+ $sections[] = 'General Examination on Admission :
' . $generalSummary . '
';
}
-
- while ($col % $perRow !== 0) {
- $html .= ' | ';
- if ($col % $perRow === $perRow - 1) {
- $html .= '';
- }
- $col++;
- }
-
- $html .= '
';
- $sections[] = $html;
}
$sysRows = $physicalExamRows['systemic'] ?? [];
@@ -1202,11 +1297,11 @@ class Ipd_discharge extends BaseController
}
$diagnosis = $this->byIpdRows('ipd_discharge_diagnosis', ['comp_report', 'comp_remark'], 'id ASC', $ipdId);
- $this->addListSection($sections, 'Final Diagnosis', $diagnosis);
$diagnosisRemark = $this->firstRowByIpd('ipd_discharge_diagnosis_remark', $ipdId);
$diagnosisRemarkText = $this->normalizeRichText((string) ($diagnosisRemark['comp_remark'] ?? ''));
- if ($diagnosisRemarkText !== '') {
- $sections[] = '' . nl2br(esc($diagnosisRemarkText)) . '
';
+ $diagnosisBlock = $this->buildNarrativeSection('Final Diagnosis', $diagnosis, $diagnosisRemarkText);
+ if ($diagnosisBlock !== '') {
+ $sections[] = $diagnosisBlock;
}
$inhosRow = $this->firstRowByIpd('ipd_discharge_investigtions_inhos', $ipdId);
@@ -1218,11 +1313,11 @@ class Ipd_discharge extends BaseController
}
$course = $this->byIpdRows('ipd_discharge_course', ['comp_report', 'comp_remark'], 'id ASC', $ipdId);
- $this->addListSection($sections, 'Course in the Hospital', $course);
$courseRemark = $this->firstRowByIpd('ipd_discharge_course_remark', $ipdId);
$courseRemarkText = $this->normalizeRichText((string) ($courseRemark['comp_remark'] ?? ''));
- if ($courseRemarkText !== '') {
- $sections[] = '' . nl2br(esc($courseRemarkText)) . '
';
+ $courseBlock = $this->buildNarrativeSection('Course in the hospital', $course, $courseRemarkText);
+ if ($courseBlock !== '') {
+ $sections[] = $courseBlock;
}
$nursingTrendSection = $this->buildNursingTrendSection($ipdId);
@@ -1241,79 +1336,22 @@ class Ipd_discharge extends BaseController
}
if (! empty($dischargeExamRows)) {
- $html = 'Examination on Discharge
'
- . '';
-
- $col = 0;
- $perRow = 4;
- foreach ($dischargeExamRows as $row) {
- if ($col % $perRow === 0) {
- $html .= '';
- }
-
- $label = (string) ($row['label'] ?? '');
- $value = trim((string) ($row['value'] ?? ''));
- $unit = trim((string) ($row['unit'] ?? ''));
- $html .= '| ' . esc($label) . ': ' . esc($value);
- if ($unit !== '') {
- $html .= ' ' . esc($unit);
- }
- $html .= ' | ';
-
- if ($col % $perRow === $perRow - 1) {
- $html .= '
';
- }
- $col++;
+ $dischargeSummary = $this->buildInlineExamSummary($dischargeExamRows);
+ if ($dischargeSummary !== '') {
+ $sections[] = 'Examination on Discharge : ' . $dischargeSummary . '
';
}
-
- while ($col % $perRow !== 0) {
- $html .= ' | ';
- if ($col % $perRow === $perRow - 1) {
- $html .= '';
- }
- $col++;
- }
-
- $html .= '
';
- $sections[] = $html;
}
$surgeryRows = $this->byIpdRows('ipd_discharge_surgery', ['surgery_name', 'surgery_date'], 'id ASC', $ipdId);
- if (! empty($surgeryRows)) {
- $html = 'Surgery
';
- foreach ($surgeryRows as $row) {
- $name = trim((string) ($row['surgery_name'] ?? ''));
- if ($name === '') {
- continue;
- }
- $dateText = $this->safeDate((string) ($row['surgery_date'] ?? ''));
- $html .= '- ' . esc($name);
- if ($dateText !== '') {
- $html .= ' (' . esc($dateText) . ')';
- }
- $html .= '
';
- }
- $html .= '
';
- $sections[] = $html;
+ $surgeryBlock = $this->buildNamedDateSection('Surgery', $surgeryRows, 'surgery_name', 'surgery_date', 'Date of Surgery');
+ if ($surgeryBlock !== '') {
+ $sections[] = $surgeryBlock;
}
$procedureRows = $this->byIpdRows('ipd_discharge_procedure', ['procedure_name', 'procedure_date'], 'id ASC', $ipdId);
- if (! empty($procedureRows)) {
- $html = 'Procedure
';
- foreach ($procedureRows as $row) {
- $name = trim((string) ($row['procedure_name'] ?? ''));
- if ($name === '') {
- continue;
- }
- $dateText = $this->safeDate((string) ($row['procedure_date'] ?? ''));
- $html .= '- ' . esc($name);
- if ($dateText !== '') {
- $html .= ' (' . esc($dateText) . ')';
- }
- $html .= '
';
- }
- $html .= '
';
- $sections[] = $html;
+ $procedureBlock = $this->buildNamedDateSection('Procedure', $procedureRows, 'procedure_name', 'procedure_date', 'Date of Procedure');
+ if ($procedureBlock !== '') {
+ $sections[] = $procedureBlock;
}
$drugRows = $this->byIpdRows('ipd_discharge_drug', ['drug_name', 'drug_dose', 'drug_day'], 'id ASC', $ipdId);
@@ -1433,7 +1471,15 @@ class Ipd_discharge extends BaseController
$reviewAfter = trim((string) ($first['review_after'] ?? ''));
if ($reviewAfter !== '') {
- $html .= 'Review after ' . esc($reviewAfter) . ' days / as and when required.
';
+ $reviewDate = '';
+ $dischargeDateRaw = trim((string) ($ipd->discharge_date ?? ''));
+ if ($dischargeDateRaw !== '' && is_numeric($reviewAfter)) {
+ $reviewTs = strtotime($dischargeDateRaw . ' +' . (int) $reviewAfter . ' days');
+ if ($reviewTs !== false) {
+ $reviewDate = ' (' . date('d-m-Y', $reviewTs) . ')';
+ }
+ }
+ $html .= 'Review after ' . esc($reviewAfter) . ' Days' . esc($reviewDate) . ' days / as and when required
';
}
$footerText = trim((string) ($first['footer_text'] ?? ''));
@@ -1444,12 +1490,20 @@ class Ipd_discharge extends BaseController
$sections[] = $html;
}
- $sections[] = ''
+ $sections[] = ''
+ . ''
+ . '| | | |
'
. ''
- . '____________________ Signature of Consultant | '
- . '____________________ Signature of Medical Officer | '
- . '____________________ Signature of Receiver / Date | '
+ . '_________________________ | '
+ . '_________________________ | '
+ . '_________________________ | '
. '
'
+ . ''
+ . '| Signature of Consultant | '
+ . 'Signature of Medical Officer | '
+ . 'Signature of Receiver / Date | '
+ . '
'
+ . ''
. '
';
return trim(implode("\n", $sections));
@@ -4760,13 +4814,9 @@ class Ipd_discharge extends BaseController
if ($withHeader) {
$headerHtml = $this->applyDischargeTemplateTokens(trim((string) ($templateSettings['header_html'] ?? '')), $templateTokenVars);
- if ($headerHtml === '') {
- $headerHtml = ''
- . '
Discharge Summary
'
- . '
IPD: ' . esc($ipdCode) . ' | Template: ' . esc($templateName) . '
'
- . '
';
+ if ($headerHtml !== '') {
+ $mpdf->SetHTMLHeader($headerHtml);
}
- $mpdf->SetHTMLHeader($headerHtml);
}
$footerHtml = $this->applyDischargeTemplateTokens(trim((string) ($templateSettings['footer_html'] ?? '')), $templateTokenVars);