Standardize OPD care context formatting and prevent duplicate session aliases

This commit is contained in:
DevSoftTech Server 2026-10-06 16:59:39 +05:30
parent b0a08112d1
commit 04eff5e5ba
3 changed files with 64 additions and 6 deletions

View File

@ -6215,8 +6215,9 @@ class AbdmGateway extends BaseController
$careContextsV3 = [];
$careContextsFull = [];
$seenRefs = [];
$seenOpdSessions = [];
$addContext = function (array $item) use (&$careContextsV3, &$careContextsFull, &$seenRefs, $readyOnly) {
$addContext = function (array $item) use (&$careContextsV3, &$careContextsFull, &$seenRefs, &$seenOpdSessions, $readyOnly) {
$ref = trim((string) ($item['referenceNumber'] ?? $item['careContextId'] ?? ''));
if ($ref === '' || isset($seenRefs[$ref])) {
return;
@ -6225,6 +6226,9 @@ class AbdmGateway extends BaseController
return;
}
$seenRefs[$ref] = true;
if (! empty($item['alias_reference'])) {
$seenRefs[trim((string) $item['alias_reference'])] = true;
}
$recType = trim((string) ($item['record_type'] ?? ''));
$hiType = $this->mapRecordTypeToAbdmHiType($recType);
@ -6669,6 +6673,15 @@ class AbdmGateway extends BaseController
'is_primary' => false,
]);
$seenRefs[$aliasRef] = true;
$seenRefs['OPD-' . $opdId . '-S' . $sessionId . '-' . $visitDate] = true;
$seenRefs['OPD-' . $opdId . '-S' . $sessionId . '-' . $cleanDate] = true;
$seenRefs['OPD-' . $patientId . '-S' . $sessionId . '-' . $visitDate] = true;
if ($sessionId > 0) {
$seenOpdSessions[$sessionId] = true;
}
if ($opdId > 0) {
$seenOpdSessions[$opdId] = true;
}
}
}
@ -6930,6 +6943,21 @@ class AbdmGateway extends BaseController
if (str_starts_with($ccRef, 'PRESC-') || str_starts_with($ccRef, 'WELLNESS-')) {
continue;
}
// Skip if this OPD session or OPD ID has already been included in step 4
if (str_starts_with($ccRef, 'OPD-')) {
if (preg_match('/-S(\d+)-/i', $ccRef, $sm)) {
$sId = (int) $sm[1];
if ($sId > 0 && isset($seenOpdSessions[$sId])) {
continue;
}
}
if (preg_match('/^OPD-(\d+)-/i', $ccRef, $om)) {
$oId = (int) $om[1];
if ($oId > 0 && isset($seenOpdSessions[$oId])) {
continue;
}
}
}
$hiType = trim((string) ($row['hi_type'] ?? 'HealthDocumentRecord'));
if (str_starts_with($ccRef, 'INVOICE-')) {
$hiType = 'InvoiceRecord';

View File

@ -5892,8 +5892,7 @@ class Opd_prescription extends BaseController
};
if ($sessionId > 0 && in_array('care_context_reference', $fields, true)) {
$careContextPrefix = 'OPD-' . $opdId . '-S' . $sessionId . '-';
$candidate = $findCandidate($careContextPrefix);
$candidate = $findCandidate('-S' . $sessionId . '-');
if ($candidate !== null) {
return $candidate;
}
@ -11923,6 +11922,16 @@ class Opd_prescription extends BaseController
]);
}
// Clean up redundant standalone bundles in opd_fhir_documents so only unified OPConsultRecord is kept
try {
$this->db->table('opd_fhir_documents')
->where('opd_id', $opdId)
->where('opd_session_id', $sessionId)
->whereIn('bundle_type', ['PrescriptionRecord', 'MedicationRequestBundle', 'WellnessRecord'])
->delete();
} catch (\Throwable) {
}
if (! $inserted) {
return false;
}
@ -11961,10 +11970,15 @@ class Opd_prescription extends BaseController
$this->db->table('health_records')->insert($hrPayload);
}
// Delete redundant standalone PRESC / WELLNESS records so only the single unified bundle is kept
// Delete redundant standalone PRESC / WELLNESS / alias OPD records so only the single unified bundle is kept
$redundantRefs = [
'PRESC-' . $patientId . '-S' . $sessionId . '-' . $cleanDate,
'WELLNESS-' . $sessionId . '-' . $cleanDate,
'OPD-' . $opdId . '-S' . $sessionId . '-' . $visitDate,
'OPD-' . $opdId . '-S' . $sessionId . '-' . $cleanDate,
'OPD-' . $patientId . '-S' . $sessionId . '-' . $visitDate,
'PRESC-' . $opdId . '-S' . $sessionId . '-' . $visitDate,
'WELLNESS-' . $opdId . '-S' . $sessionId . '-' . $visitDate,
];
$this->db->table('health_records')
->whereIn('care_context_reference', $redundantRefs)

View File

@ -112,6 +112,7 @@ class AbdmTaskBoardSyncService
->select('id, opd_id, opd_session_id, bundle_type, bundle_json, generated_at')
->whereIn('opd_id', $opdIds)
->whereIn('bundle_type', ['OPConsultRecord', 'MedicationRequestBundle', 'PrescriptionRecord'])
->orderBy("CASE WHEN bundle_type = 'OPConsultRecord' THEN 1 ELSE 2 END", 'ASC', false)
->orderBy('id', 'DESC')
->get()
->getResultArray();
@ -203,7 +204,8 @@ class AbdmTaskBoardSyncService
$consultDate = (string) ($row['apointment_date'] ?? '');
$visitDate = $consultDate !== '' ? date('Y-m-d', strtotime($consultDate)) : date('Y-m-d');
$derivedCcRef = 'OPD-' . $opdId . '-S' . ($sessionId > 0 ? $sessionId : 0) . '-' . $visitDate;
$cleanDate = str_replace('-', '', $visitDate);
$derivedCcRef = 'OPD-' . $patientId . '-S' . ($sessionId > 0 ? $sessionId : 0) . '-' . $cleanDate;
$careContextRef = trim((string) ($hr['care_context_reference'] ?? ''));
if ($careContextRef === '') {
@ -411,6 +413,16 @@ class AbdmTaskBoardSyncService
// 1. Upsert into health_records
$healthRecordId = (int) ($existingHr['id'] ?? 0);
if ($healthRecordId <= 0) {
$foundHr = $this->db->table('health_records')
->select('id')
->where('care_context_reference', $careContextRef)
->get(1)
->getRowArray();
if (! empty($foundHr['id'])) {
$healthRecordId = (int) $foundHr['id'];
}
}
$hrData = [
'patient_id' => $patientId,
'abha_id' => $effectiveAbha,
@ -763,6 +775,7 @@ class AbdmTaskBoardSyncService
if ($this->db->tableExists('opd_fhir_documents')) {
$docRow = $this->db->table('opd_fhir_documents')
->where('opd_id', $entityId)
->orderBy("CASE WHEN bundle_type = 'OPConsultRecord' THEN 1 ELSE 2 END", 'ASC', false)
->orderBy('id', 'DESC')
->get(1)
->getRowArray();
@ -778,7 +791,10 @@ class AbdmTaskBoardSyncService
$opdRow['abha_id'] = $abhaId;
$visitDate = ! empty($opdRow['apointment_date']) ? date('Y-m-d', strtotime((string) $opdRow['apointment_date'])) : date('Y-m-d');
$ccRef = 'OPD-' . $entityId . '-S' . ($docRow['opd_session_id'] ?? 0) . '-' . $visitDate;
$cleanDate = str_replace('-', '', $visitDate);
$targetPatientId = (int) ($opdRow['p_id'] ?? $patientId);
$targetSessionId = (int) ($docRow['opd_session_id'] ?? 0);
$ccRef = 'OPD-' . $targetPatientId . '-S' . $targetSessionId . '-' . $cleanDate;
return $this->linkAndPushOpdRecord($opdRow, $docRow, $ccRef, $visitDate, null);
}