diff --git a/app/Controllers/AbdmGateway.php b/app/Controllers/AbdmGateway.php index c2a246b..50fe269 100644 --- a/app/Controllers/AbdmGateway.php +++ b/app/Controllers/AbdmGateway.php @@ -4901,6 +4901,41 @@ class AbdmGateway extends BaseController continue; } + // Strategy 0: Live On-Demand Fresh Generation for OPD Consult Record + if (preg_match('/(?:OPD|PRESCRIPTION)-(\d+)(?:-S(\d+))?/i', $ref, $m)) { + $targetPatientId = (int) $m[1]; + $targetSessionId = (int) ($m[2] ?? 0); + $targetOpdId = 0; + + if ($targetSessionId > 0 && $db->tableExists('opd_prescription')) { + $pRow = $db->table('opd_prescription')->select('opd_id')->where('id', $targetSessionId)->get(1)->getRowArray(); + $targetOpdId = (int) ($pRow['opd_id'] ?? 0); + } + if ($targetOpdId <= 0 && $targetPatientId > 0 && $db->tableExists('opd_master')) { + $opdRow = $db->table('opd_master')->select('opd_id')->where('p_id', $targetPatientId)->orderBy('opd_id', 'DESC')->get(1)->getRowArray(); + $targetOpdId = (int) ($opdRow['opd_id'] ?? 0); + } + + if ($targetOpdId > 0) { + try { + $opdCtrl = new \App\Controllers\Opd_prescription(); + $opdCtrl->initController($this->request, $this->response, service('logger')); + $regen = $opdCtrl->regenerateFhirBundleInternal($targetOpdId, $targetSessionId); + if (!empty($regen['ok']) && !empty($regen['bundle'])) { + $records[] = [ + 'careContextReference' => $ref, + 'hiType' => 'OPConsultRecord', + 'display' => 'Consultation Record - ' . date('d M Y'), + 'bundle' => $regen['bundle'], + ]; + continue; + } + } catch (\Throwable $ex) { + log_message('warning', 'Live FHIR bundle generation in recordsFetch failed: ' . $ex->getMessage()); + } + } + } + // Strategy A: Check if health_records already has stored FHIR record_data $hrRow = null; if ($db->tableExists('health_records')) { @@ -4962,8 +4997,8 @@ class AbdmGateway extends BaseController if (is_array($bundleData)) { $records[] = [ 'careContextReference' => $ref, - 'hiType' => 'PrescriptionRecord', - 'display' => 'Prescription - ' . date('d M Y', strtotime($docRow['generated_at'] ?? 'now')), + 'hiType' => $docRow['bundle_type'] ?? 'OPConsultRecord', + 'display' => 'Consultation Record - ' . date('d M Y', strtotime($docRow['generated_at'] ?? 'now')), 'bundle' => $bundleData, ]; continue; diff --git a/app/Controllers/Opd_prescription.php b/app/Controllers/Opd_prescription.php index 5d69b1e..1c7a9ce 100644 --- a/app/Controllers/Opd_prescription.php +++ b/app/Controllers/Opd_prescription.php @@ -4843,7 +4843,7 @@ class Opd_prescription extends BaseController * * @return array */ - private function regenerateFhirBundleInternal(int $opdId, int $sessionId): array + public function regenerateFhirBundleInternal(int $opdId, int $sessionId): array { if ($opdId <= 0) { @@ -8799,6 +8799,14 @@ class Opd_prescription extends BaseController $this->trackMedicineUsage($medId); } + if ($opdId > 0 && $sessionId > 0) { + try { + $this->regenerateFhirBundleInternal($opdId, $sessionId); + } catch (\Throwable $fhirEx) { + log_message('warning', 'FHIR bundle auto-update on medicine_add failed: ' . $fhirEx->getMessage()); + } + } + $message = 'Medicine added'; if (! empty($masterInfo['is_new'])) { $message .= ' | New medicine saved in OPD master'; @@ -9210,6 +9218,20 @@ class Opd_prescription extends BaseController $this->trackMedicineUsage($masterMedId); } + $sessionId = (int) ($current['opd_pre_id'] ?? 0); + $opdId = 0; + if ($sessionId > 0 && $this->db->tableExists('opd_prescription')) { + $pRow = $this->db->table('opd_prescription')->select('opd_id')->where('id', $sessionId)->get(1)->getRowArray(); + $opdId = (int) ($pRow['opd_id'] ?? 0); + } + if ($opdId > 0 && $sessionId > 0) { + try { + $this->regenerateFhirBundleInternal($opdId, $sessionId); + } catch (\Throwable $fhirEx) { + log_message('warning', 'FHIR bundle auto-update on medicine_update failed: ' . $fhirEx->getMessage()); + } + } + $message = 'Medicine updated'; if (! empty($masterInfo['is_new'])) { $message .= ' | New medicine saved in OPD master'; @@ -9309,6 +9331,21 @@ class Opd_prescription extends BaseController $row = $this->db->table($table)->where('id', (int) $id)->get(1)->getRowArray() ?? []; $this->db->table($table)->where('id', (int) $id)->delete(); $this->auditClinicalUpdate('opd_prescription_medicine', 'removed', (int) $id, $row, null); + + $sessionId = (int) ($row['opd_pre_id'] ?? 0); + $opdId = 0; + if ($sessionId > 0 && $this->db->tableExists('opd_prescription')) { + $pRow = $this->db->table('opd_prescription')->select('opd_id')->where('id', $sessionId)->get(1)->getRowArray(); + $opdId = (int) ($pRow['opd_id'] ?? 0); + } + if ($opdId > 0 && $sessionId > 0) { + try { + $this->regenerateFhirBundleInternal($opdId, $sessionId); + } catch (\Throwable $fhirEx) { + log_message('warning', 'FHIR bundle auto-update on medicine_remove failed: ' . $fhirEx->getMessage()); + } + } + return $this->response->setJSON(['update' => 1, 'error_text' => 'Medicine removed']); } diff --git a/app/Views/abdm/task_board.php b/app/Views/abdm/task_board.php index 06bf606..d5b4a92 100644 --- a/app/Views/abdm/task_board.php +++ b/app/Views/abdm/task_board.php @@ -62,8 +62,8 @@ ['Patients Without ABHA', 'without_abha', 'danger'], ['ABHA Verified', 'abha_verified', 'success'], ['Verified In Range', 'verified_in_range', 'info'], - ['Records Pushed', 'records_pushed', 'warning'], - ['Pushed In Range', 'records_pushed_in_range', 'primary'], + ['Care Contexts Linked', 'records_pushed', 'success'], + ['Linked In Range', 'records_pushed_in_range', 'primary'], ['OPD Tokens', 'opd_tokens', 'secondary'], ['Tokens In Range', 'opd_tokens_in_range', 'success'], ] as [$label, $key, $tone]): ?> @@ -178,22 +178,14 @@ - - - - - - - - + + + + - - - - @@ -376,13 +368,7 @@ $bridgeRecordId = trim((string) ($r['bridge_record_id'] ?? '')); $hasFhir = (int) ($r['has_fhir'] ?? 0) === 1; $pushStatusRaw = strtolower(trim((string) ($r['push_status'] ?? ''))); - $canSubmitGateway = $hasFhir; - $submitButtonLabel = in_array($pushStatusRaw, ['queued', 'linked', 'failed'], true) - ? 'Submit Again' - : 'Submit to ABDM'; - $submitButtonClass = in_array($pushStatusRaw, ['queued', 'linked'], true) - ? 'btn-outline-secondary' - : 'btn-outline-warning'; + $isCareContextLinked = in_array(strtolower($linkStatus), ['linked', 'registered'], true) || in_array($pushStatusRaw, ['linked', 'pushed', 'queued'], true); $statusBadgeClass = 'badge bg-' . $statusTone; if ($statusTone === 'warning') { $statusBadgeClass .= ' text-dark'; @@ -417,13 +403,18 @@ - + + Linked + + data-patient-name="" + title="Link Care Context with patient ABHA (M2)" + > Link Context @@ -490,7 +481,11 @@ - + + Linked + + + @@ -538,8 +533,6 @@
- -
@@ -2999,7 +2992,8 @@ }); })(); - btnSubmitFhirToAbdm.addEventListener('click', function () { + if (btnSubmitFhirToAbdm) { + btnSubmitFhirToAbdm.addEventListener('click', function () { var btn = this; if (_fhirSubmitMode === 'invoice') { @@ -3374,6 +3368,7 @@ alert(' Error: ' + msg); }); }); + } document.getElementById('btnCopyFhirModal').addEventListener('click', function () { var btn = this; @@ -3484,6 +3479,17 @@ } }); }); + + document.querySelectorAll('.btn-opd-care-context-link, .btn-invoice-care-link').forEach(function (btn) { + btn.addEventListener('click', function () { + var patientId = parseInt(btn.getAttribute('data-patient-id') || '0', 10); + var abhaId = (btn.getAttribute('data-abha-id') || '').trim(); + var patientName = (btn.getAttribute('data-patient-name') || '').trim(); + if (typeof window.openAbdmHipLinkModal === 'function') { + window.openAbdmHipLinkModal(patientId, abhaId, { patient_name: patientName }); + } + }); + }); })();