From 65a071928c06ea3221651b362169811dcee1fb0e Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Mon, 19 Oct 2026 11:13:20 +0530 Subject: [PATCH] fix(abdm): force fresh discharge PDF generation and add column safety to byIpdRows --- app/Controllers/AbdmGateway.php | 2 +- app/Controllers/Ipd_discharge.php | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/Controllers/AbdmGateway.php b/app/Controllers/AbdmGateway.php index 6ff8b09..2f047a1 100644 --- a/app/Controllers/AbdmGateway.php +++ b/app/Controllers/AbdmGateway.php @@ -4175,7 +4175,7 @@ class AbdmGateway extends BaseController $response = \Config\Services::response(); $logger = \Config\Services::logger(); $dischargeCtrl->initController($request, $response, $logger); - $generatedBinary = $dischargeCtrl->generateDischargeSummaryPdfBinary($ipdId, true, $abdmTemplateId > 0 ? $abdmTemplateId : null); + $generatedBinary = $dischargeCtrl->generateDischargeSummaryPdfBinary($ipdId, true, $abdmTemplateId > 0 ? $abdmTemplateId : null, true); } catch (\Throwable $e) { log_message('error', 'Auto-generating IPD Discharge Summary PDF failed for IPD {ipd}: {msg}', [ 'ipd' => $ipdId, diff --git a/app/Controllers/Ipd_discharge.php b/app/Controllers/Ipd_discharge.php index 5271ef3..66a3037 100644 --- a/app/Controllers/Ipd_discharge.php +++ b/app/Controllers/Ipd_discharge.php @@ -1971,14 +1971,25 @@ class Ipd_discharge extends BaseController $builder = $this->db->table($table)->where('ipd_id', $ipdId); if ($columns !== ['*']) { - $builder->select(implode(',', $columns)); + $existingCols = []; + foreach ($columns as $col) { + $cleanCol = trim($col); + if ($cleanCol !== '' && $this->db->fieldExists($cleanCol, $table)) { + $existingCols[] = $cleanCol; + } + } + if (! empty($existingCols)) { + $builder->select(implode(',', $existingCols)); + } } if ($orderBy !== '') { $parts = explode(' ', trim($orderBy)); $field = $parts[0] ?? 'id'; $dir = strtoupper($parts[1] ?? 'ASC'); - $builder->orderBy($field, $dir === 'DESC' ? 'DESC' : 'ASC'); + if ($this->db->fieldExists($field, $table)) { + $builder->orderBy($field, $dir === 'DESC' ? 'DESC' : 'ASC'); + } } return $builder->get()->getResultArray(); @@ -8873,7 +8884,7 @@ class Ipd_discharge extends BaseController } } - public function generateDischargeSummaryPdfBinary(int $ipdId, bool $withHeader = true, ?int $templateId = null): ?string + public function generateDischargeSummaryPdfBinary(int $ipdId, bool $withHeader = true, ?int $templateId = null, bool $forceFresh = false): ?string { if ($ipdId <= 0) { return null; @@ -8884,7 +8895,10 @@ class Ipd_discharge extends BaseController return null; } - $content = $this->getDischargeContent($ipdId); + $content = ''; + if (! $forceFresh) { + $content = $this->getDischargeContent($ipdId); + } if (trim(strip_tags($content)) === '') { $content = $this->buildAutoDischargeContent($ipdId, $panelData); if (trim(strip_tags($content)) !== '') { @@ -9046,7 +9060,7 @@ class Ipd_discharge extends BaseController $generatedBinary = null; try { - $generatedBinary = $this->generateDischargeSummaryPdfBinary($ipdId, true, $abdmTemplateId > 0 ? $abdmTemplateId : null); + $generatedBinary = $this->generateDischargeSummaryPdfBinary($ipdId, true, $abdmTemplateId > 0 ? $abdmTemplateId : null, true); } catch (\Throwable $e) { log_message('error', 'Auto-generating IPD Discharge Summary PDF failed for IPD {ipd}: {msg}', [ 'ipd' => $ipdId,