fix(abdm): provide separate Document Reference section in InvoiceRecord so PHR renders DocumentReference accordion card with embedded PDF

This commit is contained in:
DevSoftTech Server 2026-10-21 04:36:54 +05:30
parent 2f0595a3a3
commit 41c70c0151
2 changed files with 41 additions and 52 deletions

View File

@ -11508,35 +11508,9 @@ class AbdmGateway extends BaseController
public function buildInvoiceRecordPayload(int $invoiceId, int $patientId, string $abhaId): ?array
{
if ($this->db->tableExists('invoice_master')) {
$invoice = $this->db->table('invoice_master')->where('id', $invoiceId)->get(1)->getRowArray() ?? [];
if (! empty($invoice)) {
if ($patientId <= 0) {
$patientId = (int) ($invoice['attach_id'] ?? 0);
}
$patientRow = $this->loadPatientRow($patientId);
if ($patientId > 0 && ! empty($patientRow)) {
$items = $this->db->tableExists('invoice_item')
? $this->db->table('invoice_item')->where('inv_master_id', $invoiceId)->orderBy('id', 'ASC')->get()->getResultArray()
: [];
$visitDate = (string) ($invoice['inv_date'] ?? date('Y-m-d'));
$patient = $this->buildAbdmPatientResource($patientRow, $patientId, $abhaId);
$bundle = $this->buildSimpleInvoiceBundle($patient, $invoice, $items);
return [
'hi_type' => 'InvoiceRecord',
'entity_type' => 'invoice',
'entity_id' => (string) $invoiceId,
'patient_id' => $patientId,
'patient_name' => $this->patientDisplayName($patientRow),
'visit_date' => date('Y-m-d', strtotime($visitDate)),
'care_context_reference' => 'INV-' . $invoiceId . '-' . date('Y-m-d', strtotime($visitDate)),
'care_context_display' => 'Invoice ' . (string) ($invoice['invoice_code'] ?? $invoiceId),
'bundle' => $bundle,
];
}
}
$payload = $this->buildInvoiceSourceRecordPayload('charges_invoice', $invoiceId, $patientId, $abhaId);
if ($payload !== null) {
return $payload;
}
// Support pharmacy invoice_med_master as well
@ -12650,13 +12624,41 @@ class AbdmGateway extends BaseController
];
}
$sectionEntries = [
['reference' => $invoiceRef, 'type' => 'Invoice'],
$sections = [
[
'title' => 'Invoice details',
'code' => ['coding' => [[
'system' => 'http://loinc.org',
'code' => '34775-2',
'display' => 'Hospital Invoice',
]]],
'text' => [
'status' => 'generated',
'div' => '<div xmlns="http://www.w3.org/1999/xhtml"><p>Billing details for invoice '
. $escape($invoiceNumber) . '</p></div>',
],
'entry' => [
['reference' => $invoiceRef, 'type' => 'Invoice'],
],
],
];
if ($documentReference !== null) {
$sectionEntries[] = [
'reference' => $documentReferenceRef,
'type' => 'DocumentReference',
$sections[] = [
'title' => 'Document Reference',
'code' => ['coding' => [[
'system' => 'http://snomed.info/sct',
'code' => '419891008',
'display' => 'Record artifact',
]]],
'text' => [
'status' => 'generated',
'div' => '<div xmlns="http://www.w3.org/1999/xhtml"><p>Document reference for invoice '
. $escape($invoiceNumber) . '</p></div>',
],
'entry' => [
['reference' => $documentReferenceRef, 'type' => 'DocumentReference'],
],
];
}
@ -12691,20 +12693,7 @@ class AbdmGateway extends BaseController
]],
'title' => 'Invoice ' . $invoiceNumber,
'custodian' => ['reference' => 'Organization/' . $organizationId],
'section' => [[
'title' => 'Invoice details',
'code' => ['coding' => [[
'system' => 'http://loinc.org',
'code' => '34775-2',
'display' => 'Hospital Invoice',
]]],
'text' => [
'status' => 'generated',
'div' => '<div xmlns="http://www.w3.org/1999/xhtml"><p>Billing details for invoice '
. $escape($invoiceNumber) . '</p></div>',
],
'entry' => $sectionEntries,
]],
'section' => $sections,
];
$resources = [$composition, $patient, $organization, $encounter, $invoiceResource];

View File

@ -422,7 +422,7 @@ final class FhirGeneratorsTest extends CIUnitTestCase
$composition['section'][0]['entry'][0]['type'] ?? null,
'NRCeS InvoiceRecord 6.5.0 requires Reference.type to be Invoice'
);
$this->assertCount(2, $composition['section'][0]['entry'] ?? []);
$this->assertCount(1, $composition['section'][0]['entry'] ?? []);
$this->assertNotEmpty($composition['author'][0]['reference'] ?? '');
$this->assertNotEmpty($composition['custodian']['reference'] ?? '');
$this->assertContains('https://nrces.in/ndhm/fhir/r4/StructureDefinition/Patient', $patient['meta']['profile'] ?? []);
@ -469,9 +469,9 @@ final class FhirGeneratorsTest extends CIUnitTestCase
$composition['section'][0]['entry'][0]['reference'] ?? null,
$documentReference['context']['related'][0]['reference'] ?? null
);
$this->assertCount(1, $composition['section'], 'NRCeS InvoiceRecord profile requires max 1 section');
$this->assertSame('DocumentReference', $composition['section'][0]['entry'][1]['type'] ?? null);
$this->assertSame($fullUrlByResourceType['DocumentReference'][0] ?? null, $composition['section'][0]['entry'][1]['reference'] ?? null, 'Document Reference entry must point to the DocumentReference');
$this->assertCount(2, $composition['section'], 'Composition sections for invoice and document reference');
$this->assertSame('DocumentReference', $composition['section'][1]['entry'][0]['type'] ?? null);
$this->assertSame($fullUrlByResourceType['DocumentReference'][0] ?? null, $composition['section'][1]['entry'][0]['reference'] ?? null, 'Document Reference entry must point to the DocumentReference');
$this->assertSame('725458005', $documentReference['type']['coding'][0]['code'] ?? null);
$this->assertSame('Receipt', $documentReference['type']['coding'][0]['display'] ?? null);
$binaries = array_values(array_filter(