From e2bb0b107a0d67a133c54c47a9e79ccf50ba8352 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Tue, 6 Oct 2026 16:49:15 +0530 Subject: [PATCH] fix(abdm): enrich OPD and INVOICE care contexts with clinical components and bill summary --- app/Controllers/AbdmGateway.php | 39 ++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/app/Controllers/AbdmGateway.php b/app/Controllers/AbdmGateway.php index d12f709..369948e 100644 --- a/app/Controllers/AbdmGateway.php +++ b/app/Controllers/AbdmGateway.php @@ -6463,12 +6463,11 @@ class AbdmGateway extends BaseController if (str_starts_with($ccRef, 'PRESC-') || str_starts_with($ccRef, 'WELLNESS-')) { continue; } - $hiType = trim((string) ($row['hi_type'] ?? 'HealthDocumentRecord')); - if (str_starts_with($ccRef, 'INVOICE-') || stripos($hiType, 'invoice') !== false) { - $hiType = 'InvoiceRecord'; - } elseif (str_starts_with($ccRef, 'OPD-')) { - $hiType = 'OPConsultRecord'; + // OPD- and INVOICE- contexts are enriched with doctor/session/bill details in steps 4 & 6 + if (str_starts_with($ccRef, 'OPD-') || str_starts_with($ccRef, 'INVOICE-')) { + continue; } + $hiType = trim((string) ($row['hi_type'] ?? 'HealthDocumentRecord')); $dateStr = date('d M Y', strtotime((string) ($row['created_at'] ?? $row['updated_at'] ?? 'now'))); $display = $hiType . ' - ' . $dateStr; @@ -6921,6 +6920,36 @@ class AbdmGateway extends BaseController } } + // 6c. Fallback for any OPD- or INVOICE- health_records not matched in steps 4 or 6 + if (! empty($rows)) { + foreach ($rows as $row) { + $ccRef = trim((string) ($row['care_context_reference'] ?? '')); + if ($ccRef === '' || isset($seenRefs[$ccRef])) { + continue; + } + if (str_starts_with($ccRef, 'PRESC-') || str_starts_with($ccRef, 'WELLNESS-')) { + continue; + } + $hiType = trim((string) ($row['hi_type'] ?? 'HealthDocumentRecord')); + if (str_starts_with($ccRef, 'INVOICE-')) { + $hiType = 'InvoiceRecord'; + } elseif (str_starts_with($ccRef, 'OPD-')) { + $hiType = 'OPConsultRecord'; + } + $dateStr = date('d M Y', strtotime((string) ($row['created_at'] ?? $row['updated_at'] ?? 'now'))); + $display = $hiType . ' - ' . $dateStr; + $addContext([ + 'careContextId' => $ccRef, + 'referenceNumber' => $ccRef, + 'display' => $display, + 'record_type' => $hiType, + 'patient_id' => $patientId, + 'is_fhir_ready' => true, + 'is_primary' => false, + ]); + } + } + // 7. Fallback: default registration file care context if (empty($careContextsV3)) { $ccRef = 'REG-' . $patientRef;