diff --git a/app/Controllers/AbdmGateway.php b/app/Controllers/AbdmGateway.php index ad6110b..896f5a6 100644 --- a/app/Controllers/AbdmGateway.php +++ b/app/Controllers/AbdmGateway.php @@ -6225,6 +6225,39 @@ class AbdmGateway extends BaseController } } + // 4b. Query opd_master (OPD consultation appointments/visits not already in prescriptions) + if ($this->db->tableExists('opd_master')) { + $opdMasterRows = $this->db->table('opd_master') + ->select('opd_id, p_id, apointment_date, opd_book_date, doc_name') + ->where('p_id', $patientId) + ->orderBy('opd_id', 'DESC') + ->limit(30) + ->get() + ->getResultArray(); + + foreach ($opdMasterRows as $om) { + $visitRaw = ! empty($om['apointment_date']) && $om['apointment_date'] !== '0000-00-00 00:00:00' + ? $om['apointment_date'] + : (! empty($om['opd_book_date']) ? $om['opd_book_date'] : date('Y-m-d')); + $visitDate = date('Y-m-d', strtotime((string) $visitRaw)); + $dateStr = date('d M Y', strtotime($visitDate)); + $opdId = (int) $om['opd_id']; + $ccRef = 'OPD-' . $patientId . '-S' . $opdId . '-' . str_replace('-', '', $visitDate); + $docName = trim((string) ($om['doc_name'] ?? '')); + $display = 'OPConsultRecord - ' . ($docName !== '' ? 'Dr. ' . $docName . ' - ' : '') . $dateStr; + + $addContext([ + 'careContextId' => $ccRef, + 'referenceNumber' => $ccRef, + 'display' => $display, + 'record_type' => 'OPConsultRecord', + 'patient_id' => $patientId, + 'is_fhir_ready' => true, + 'is_primary' => false, + ]); + } + } + // 5. Query ipd_master (Discharge summaries) if ($this->db->tableExists('ipd_master')) { $ipdRows = $this->db->table('ipd_master') @@ -10917,7 +10950,16 @@ class AbdmGateway extends BaseController if ($patientId > 0) { $builder->where('id', $patientId); } elseif ($abhaAddress !== '') { - $builder->where('abha_address', $abhaAddress); + $cleanAbha = preg_replace('/\D/', '', $abhaAddress); + if (strlen($cleanAbha) === 14) { + $builder->groupStart() + ->where('abha_address', $abhaAddress) + ->orWhere('abha_id', $cleanAbha) + ->orWhere('abha_id', $abhaAddress) + ->groupEnd(); + } else { + $builder->where('abha_address', $abhaAddress); + } } elseif ($pCode !== '') { $builder->where('p_code', $pCode); } diff --git a/app/Views/billing/Patient_V.php b/app/Views/billing/Patient_V.php index 03362b8..274c3f7 100644 --- a/app/Views/billing/Patient_V.php +++ b/app/Views/billing/Patient_V.php @@ -45,6 +45,7 @@ +
@@ -105,6 +106,7 @@ +
@@ -410,7 +412,7 @@
-
+
@@ -428,7 +430,7 @@
-
+
@@ -449,7 +451,7 @@
-
+
@@ -465,6 +467,31 @@
+ +
+
+
+
+ +
Link Records to ABHA (HIP)
+
+

Link hospital visits & diagnostic records to patient's ABHA address using ABDM M2 HIP workflows.

+
    +
  • Demographic Auth (Method 4)
  • +
  • Link OPD & Diagnostics
  • +
  • Deep Link SMS to ABHA App
  • +
+
+ + +
+
+
+
diff --git a/tests/unit/Abdm/CareContextDiscoveryTest.php b/tests/unit/Abdm/CareContextDiscoveryTest.php index 3c53525..27b186b 100644 --- a/tests/unit/Abdm/CareContextDiscoveryTest.php +++ b/tests/unit/Abdm/CareContextDiscoveryTest.php @@ -46,6 +46,14 @@ final class CareContextDiscoveryTest extends CIUnitTestCase p_datetime TEXT )"); + $db->query("CREATE TABLE IF NOT EXISTS " . $db->prefixTable('opd_master') . " ( + opd_id INTEGER PRIMARY KEY, + p_id INTEGER, + apointment_date TEXT, + opd_book_date TEXT, + doc_name TEXT + )"); + $db->query("CREATE TABLE IF NOT EXISTS " . $db->prefixTable('health_records') . " ( id INTEGER PRIMARY KEY, patient_id INTEGER, @@ -92,6 +100,7 @@ final class CareContextDiscoveryTest extends CIUnitTestCase $db->table('charge_master')->emptyTable(); $db->table('lab_request')->emptyTable(); $db->table('opd_prescription')->emptyTable(); + $db->table('opd_master')->emptyTable(); $db->table('health_records')->emptyTable(); $db->table('patient_master')->emptyTable(); $db->table('ipd_master')->emptyTable(); @@ -323,5 +332,26 @@ final class CareContextDiscoveryTest extends CIUnitTestCase $this->assertSame(1, $body['ok'] ?? 0); $this->assertSame('RAD-31-20260929', $body['care_context_reference'] ?? ''); } + + public function testDiscoversOpdMasterVisitWhenPrescriptionNotCreated(): void + { + $db = \Config\Database::connect(); + $db->table('opd_master')->insert([ + 'opd_id' => 77, + 'p_id' => 12, + 'apointment_date' => '2026-09-29 14:30:00', + 'opd_book_date' => '2026-09-29 14:00:00', + 'doc_name' => 'NIDHI PANDEY', + ]); + + $refGateway = new ReflectionClass($this->gateway); + $method = $refGateway->getMethod('findCareContextsForPatient'); + $method->setAccessible(true); + + [$v3, $full] = $method->invoke($this->gateway, 12, 'P26071000012', 'KESHAV SINGH'); + + $refs = array_column($v3, 'referenceNumber'); + $this->assertContains('OPD-12-S77-20260929', $refs); + } }