diff --git a/app/Controllers/AbdmGateway.php b/app/Controllers/AbdmGateway.php index bcb2d4b..38557df 100644 --- a/app/Controllers/AbdmGateway.php +++ b/app/Controllers/AbdmGateway.php @@ -10674,9 +10674,10 @@ class AbdmGateway extends BaseController private function buildHealthDocumentRecordPayload(int $patientId, string $abhaId = '', int $recordId = 0, array $patientRow = []): ?array { + $hasPost = $this->request && method_exists($this->request, 'getPost'); $targetRecordId = $recordId > 0 ? $recordId - : (int) ($this->request->getPost('record_id') ?? $this->request->getPost('patient_doc_id') ?? $this->request->getPost('document_id') ?? $this->request->getPost('entity_id') ?? 0); + : (int) ($hasPost ? ($this->request->getPost('record_id') ?? $this->request->getPost('patient_doc_id') ?? $this->request->getPost('document_id') ?? $this->request->getPost('entity_id') ?? 0) : 0); if ($patientId <= 0 && $targetRecordId > 0) { if ($this->db->tableExists('file_upload_data')) { @@ -10719,7 +10720,7 @@ class AbdmGateway extends BaseController $gatewayPayload = $adapter->toGatewayPayload($output, $source, $hfrId); $isFromFile = isset($source['record_id']) && str_starts_with((string)$source['record_id'], 'file-'); - $reqCcRef = trim((string) ($this->request->getPost('care_context_reference') ?? '')); + $reqCcRef = trim((string) ($hasPost ? ($this->request->getPost('care_context_reference') ?? '') : '')); $resolvedCcRef = $reqCcRef !== '' ? $reqCcRef : (string) ($gatewayPayload['care_context_reference'] ?? ('DOC-' . ($source['record_id'] ?? $targetRecordId) . '-' . date('Ymd', strtotime((string)($source['visit_date'] ?? date('Y-m-d')))))); return [ @@ -10735,15 +10736,15 @@ class AbdmGateway extends BaseController ]; } } catch (\Throwable $e) { - // Fall through + log_message('warning', '[buildHealthDocumentRecordPayload] doctor doc bundle error: ' . $e->getMessage()); } } - $title = trim((string) ($this->request->getPost('document_title') ?? $this->request->getPost('title') ?? '')); - $documentText = trim((string) ($this->request->getPost('document_text') ?? $this->request->getPost('notes') ?? '')); - $contentType = trim((string) ($this->request->getPost('content_type') ?? '')); - $base64 = trim((string) ($this->request->getPost('document_base64') ?? '')); - $filePath = trim((string) ($this->request->getPost('file_path') ?? $this->request->getPost('attachment_path') ?? '')); + $title = trim((string) ($hasPost ? ($this->request->getPost('document_title') ?? $this->request->getPost('title') ?? '') : '')); + $documentText = trim((string) ($hasPost ? ($this->request->getPost('document_text') ?? $this->request->getPost('notes') ?? '') : '')); + $contentType = trim((string) ($hasPost ? ($this->request->getPost('content_type') ?? '') : '')); + $base64 = trim((string) ($hasPost ? ($this->request->getPost('document_base64') ?? '') : '')); + $filePath = trim((string) ($hasPost ? ($this->request->getPost('file_path') ?? $this->request->getPost('attachment_path') ?? '') : '')); if ($title === '') { $title = 'Health Document'; } diff --git a/app/Libraries/Abdm/EAtriaBridgeConnector.php b/app/Libraries/Abdm/EAtriaBridgeConnector.php index 0f927bb..3130286 100644 --- a/app/Libraries/Abdm/EAtriaBridgeConnector.php +++ b/app/Libraries/Abdm/EAtriaBridgeConnector.php @@ -1501,6 +1501,27 @@ class EAtriaBridgeConnector implements AbdmConnectorInterface $visitDate = (string) ($data['visit_date'] ?? date('Y-m-d')); $bundle = $data['record_data'] ?? $data['fhir_bundle'] ?? null; + if (empty($bundle) && $careContextReference !== '') { + try { + $db = \Config\Database::connect(); + if ($db->tableExists('health_records')) { + $hrRow = $db->table('health_records') + ->where('care_context_reference', $careContextReference) + ->orderBy('id', 'DESC') + ->get(1) + ->getRowArray(); + $raw = $hrRow['record_data'] ?? $hrRow['fhir_bundle'] ?? null; + if (! empty($raw)) { + $dec = json_decode((string) $raw, true); + if (is_array($dec) && ! empty($dec) && isset($dec['resourceType'])) { + $bundle = $dec; + } + } + } + } catch (\Throwable) { + } + } + if (! empty($bundle) && is_array($bundle)) { // When a FHIR bundle is attached, push to /api/v3/records/push so the bridge // validates the FHIR resources and holds the bundle for on-demand patient fetch. diff --git a/app/Libraries/Abdm/Sync/AbdmTaskBoardSyncService.php b/app/Libraries/Abdm/Sync/AbdmTaskBoardSyncService.php index cae73e4..c2fe027 100644 --- a/app/Libraries/Abdm/Sync/AbdmTaskBoardSyncService.php +++ b/app/Libraries/Abdm/Sync/AbdmTaskBoardSyncService.php @@ -956,23 +956,101 @@ class AbdmTaskBoardSyncService log_message('warning', '[processIndividualWorkTask] Invoice bundle generation error: ' . $inve->getMessage()); } } + } elseif (in_array($taskType, ['lab_report_publish', 'radiology_report_publish'], true)) { + if (empty($pushData['bundle']) && $this->db->tableExists('health_records')) { + $hr = $this->db->table('health_records') + ->where('entity_id', (string) $entityId) + ->where('hi_type', 'DiagnosticReportRecord') + ->orderBy('id', 'DESC') + ->get(1) + ->getRowArray(); + $raw = $hr['record_data'] ?? $hr['fhir_bundle'] ?? null; + if (! empty($raw)) { + $dec = json_decode((string) $raw, true); + if (is_array($dec) && ! empty($dec) && isset($dec['resourceType'])) { + $pushData['bundle'] = $dec; + $pushData['fhir_bundle'] = $dec; + $pushData['record_data'] = $dec; + $bundleJson = (string) $raw; + if (! empty($hr['care_context_reference'])) { + $careContextRef = (string) $hr['care_context_reference']; + $pushData['care_context_reference'] = $careContextRef; + $pushData['queue_id'] = $careContextRef; + } + } + } + } + if (empty($pushData['bundle']) && class_exists('\App\Controllers\AbdmGateway')) { + try { + $gw = new \App\Controllers\AbdmGateway(); + $diagRes = $gw->pushDiagnosisReportBundleInternal((int) $entityId, $patientId, $abhaAddress !== '' ? $abhaAddress : $abhaNumber, '', '', $careContextRef, true); + if (! empty($diagRes['health_record_id']) && $this->db->tableExists('health_records')) { + $genHr = $this->db->table('health_records')->where('id', (int) $diagRes['health_record_id'])->get(1)->getRowArray(); + $rawGen = $genHr['record_data'] ?? $genHr['fhir_bundle'] ?? null; + if (! empty($rawGen)) { + $decGen = json_decode((string) $rawGen, true); + if (is_array($decGen) && ! empty($decGen)) { + $pushData['bundle'] = $decGen; + $pushData['fhir_bundle'] = $decGen; + $pushData['record_data'] = $decGen; + $bundleJson = (string) $rawGen; + } + } + } + } catch (\Throwable $de) { + log_message('warning', '[processIndividualWorkTask] Diagnostic report bundle generation error: ' . $de->getMessage()); + } + } + } elseif ($taskType === 'health_document_publish') { + if (empty($pushData['bundle']) && class_exists('\App\Controllers\DoctorDocument')) { + try { + $docCtrl = new \App\Controllers\DoctorDocument(); + $src = $docCtrl->buildHealthDocumentSource((int) $entityId); + if (! empty($src)) { + $factory = new \App\Libraries\Abdm\Fhir\FhirGeneratorFactory(); + $gen = $factory->healthDocument()->generate($src); + $adapter = new \App\Libraries\Abdm\Fhir\Support\GatewayPayloadAdapter(); + $hfrId = (string) ($src['hfr_id'] ?? 'HFR-IN-HMS'); + $docPayload = $adapter->toGatewayPayload($gen, $src, $hfrId); + if (! empty($docPayload['fhir_bundle'])) { + $pushData['bundle'] = $docPayload['fhir_bundle']; + $pushData['fhir_bundle'] = $docPayload['fhir_bundle']; + $pushData['record_data'] = $docPayload['fhir_bundle']; + $bundleJson = (string) json_encode($docPayload['fhir_bundle'], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); + if (! empty($docPayload['care_context_reference'])) { + $careContextRef = (string) $docPayload['care_context_reference']; + $pushData['care_context_reference'] = $careContextRef; + $pushData['queue_id'] = $careContextRef; + } + } + } + } catch (\Throwable $he) { + log_message('warning', '[processIndividualWorkTask] Health document bundle generation error: ' . $he->getMessage()); + } + } } // Generic fallback: check if bundle already exists in health_records if (empty($pushData['bundle']) && $this->db->tableExists('health_records')) { $hr = $this->db->table('health_records') - ->where('entity_id', (string) $entityId) - ->where('patient_id', $patientId) + ->groupStart() + ->where('care_context_reference', $careContextRef) + ->orGroupStart() + ->where('entity_id', (string) $entityId) + ->where('patient_id', $patientId) + ->groupEnd() + ->groupEnd() ->orderBy('id', 'DESC') ->get(1) ->getRowArray(); - if (! empty($hr['fhir_bundle'])) { - $decoded = json_decode((string) $hr['fhir_bundle'], true); - if (is_array($decoded) && ! empty($decoded)) { + $rawBundle = $hr['record_data'] ?? $hr['fhir_bundle'] ?? null; + if (! empty($rawBundle)) { + $decoded = json_decode((string) $rawBundle, true); + if (is_array($decoded) && ! empty($decoded) && isset($decoded['resourceType'])) { $pushData['bundle'] = $decoded; $pushData['fhir_bundle'] = $decoded; $pushData['record_data'] = $decoded; - $bundleJson = (string) $hr['fhir_bundle']; + $bundleJson = (string) $rawBundle; if (! empty($hr['care_context_reference'])) { $careContextRef = (string) $hr['care_context_reference']; $pushData['care_context_reference'] = $careContextRef;