diff --git a/app/Config/AbdmConnector.php b/app/Config/AbdmConnector.php index bed94cc..a06ecb9 100644 --- a/app/Config/AbdmConnector.php +++ b/app/Config/AbdmConnector.php @@ -154,7 +154,7 @@ class AbdmConnector extends BaseConfig public bool $autoLinkEnabled = true; /** Default cooling period for OPD, Lab, Radiology, and all other HI types (in minutes). 0 = immediate 1-minute cron linking. */ - public int $autoLinkDelayMinutes = 0; + public int $autoLinkDelayMinutes = 1; /** Cooling period for IPD Discharge Summary (in hours). 0 = immediate 1-minute cron linking. */ public int $autoLinkDelayDischargeHours = 0; diff --git a/app/Controllers/Opd.php b/app/Controllers/Opd.php index 4241cc1..a5a5fc3 100644 --- a/app/Controllers/Opd.php +++ b/app/Controllers/Opd.php @@ -2186,6 +2186,15 @@ class Opd extends BaseController $data['print_layout_mode'] = $layoutMode; + if ($includeContent) { + try { + $rxCtrl = new \App\Controllers\Opd_prescription(); + $rxCtrl->storePrescriptionFhirBundle($opdId, $sessionId); + } catch (\Throwable $e) { + log_message('warning', '[opd_lettre_pdf] storePrescriptionFhirBundle error: ' . $e->getMessage()); + } + } + $isComposedTemplate = $templateKey !== '' && str_starts_with($templateKey, 'compose_'); if ($isComposedTemplate) { $html = $this->renderComposedOpdPrint($data, $templateKey); diff --git a/app/Controllers/Opd_prescription.php b/app/Controllers/Opd_prescription.php index 7b54f18..49ba1e1 100644 --- a/app/Controllers/Opd_prescription.php +++ b/app/Controllers/Opd_prescription.php @@ -13952,6 +13952,12 @@ OPD SNAPSHOT JSON: " . $payload; $this->createOpdPrescriptionWorkTask($opdId, $opdSessionId); $this->markOpdVisitedOnConsultCompletion((int) $opdId); + // Regenerate complete FHIR records with latest medications and PDF on print action + try { + $this->storePrescriptionFhirBundle($opdId, $opdSessionId); + } catch (\Throwable $e) { + log_message('warning', '[opd_prescription_print] storePrescriptionFhirBundle error: ' . $e->getMessage()); + } $printConfig = $this->resolvePrescriptionLayoutByDoctorField($opdId, $printType); $layoutMode = (string) ($printConfig['layout'] ?? 'content_only'); diff --git a/app/Libraries/FhirR4Builder.php b/app/Libraries/FhirR4Builder.php index cc5ee89..b7c6b50 100644 --- a/app/Libraries/FhirR4Builder.php +++ b/app/Libraries/FhirR4Builder.php @@ -374,12 +374,9 @@ class FhirR4Builder $drugName = trim((string) ($medication['drug_name'] ?? '')); $genericName = trim((string) ($medication['generic_name'] ?? '')); $medType = trim((string) ($medication['med_type'] ?? '')); - $displayText = $drugName; - if ($genericName !== '' && stripos($drugName, $genericName) === false) { - $displayText = trim($drugName . ' (' . $genericName . ')'); - } + $displayText = $drugName !== '' ? $drugName : $genericName; - $medicationCodeableConcept = ['text' => $displayText !== '' ? $displayText : $drugName]; + $medicationCodeableConcept = ['text' => $displayText !== '' ? $displayText : 'Medication']; $snomedCode = trim((string) ($medication['snomed_code'] ?? '')); $atcCode = strtoupper(trim((string) ($medication['atc_code'] ?? ''))); @@ -478,6 +475,19 @@ class FhirR4Builder 'authoredOn' => $issuedAt, 'dosageInstruction' => [$dosageInstruction], ]; + + // Reason / Indication: Map salt composition if available; otherwise fallback to diagnosis or blank space + $saltReasonText = $genericName; + if ($saltReasonText === '') { + $firstCond = $conditions[0]['name'] ?? ($conditions[0]['text'] ?? ($context['diagnosis'] ?? '')); + $saltReasonText = is_string($firstCond) && trim($firstCond) !== '' ? trim($firstCond) : ' '; + } + if ($saltReasonText !== '') { + $medRes['reasonCode'] = [[ + 'text' => $saltReasonText, + ]]; + } + if ($timingResult['asNeeded']) { $medRes['asNeededBoolean'] = true; } @@ -3399,7 +3409,8 @@ class FhirR4Builder } $medicationRefs[] = ['reference' => 'MedicationRequest/' . $medId]; - $entries[] = ['resource' => [ + $genericName = is_array($med) ? trim((string) ($med['generic_name'] ?? '')) : ''; + $dsMedRes = [ 'resourceType' => 'MedicationRequest', 'id' => $medId, 'status' => 'active', @@ -3408,7 +3419,11 @@ class FhirR4Builder 'encounter' => ['reference' => $encounterRef], 'medicationCodeableConcept' => $mc, 'dosageInstruction' => [['text' => is_array($med) ? (string) ($med['dosage'] ?? '') : '']], - ]]; + ]; + if ($genericName !== '') { + $dsMedRes['reasonCode'] = [['text' => $genericName]]; + } + $entries[] = ['resource' => $dsMedRes]; } if (! empty($medicationRefs)) { $sections[] = ['title' => 'Medications', 'entry' => $medicationRefs];