private static void DoResponsiblePractitioner(MedicationRequest m, ParticipantMaker a)
        {
            Extension e = FhirHelper.MakeExtension(null, "https://fhir.nhs.uk/R4/StructureDefinition/Extension-DM-ResponsiblePractitioner",
                                                   FhirHelper.MakeInternalReference(a.Role));

            m.Extension.Add(e);
        }
        private static MessageHeader MakeMessageHeader(ParticipantMaker a)
        {
            MessageHeader header = new MessageHeader
            {
                Id     = FhirHelper.MakeId(),
                Event  = FhirHelper.MakeCoding("https://fhir.nhs.uk/R4/CodeSystem/message-event", "prescription-order", "Prescription Order"),
                Sender = FhirHelper.MakeInternalReference(a.Role)
            };

            header.Sender.Display = a.Practitioner.Name[0].Text;
            header.Source         = MakeSource();
            return(header);
        }
        private static Bundle MakeBundle(System.Collections.Generic.List <string> rx, System.Collections.Generic.List <System.Collections.Generic.List <string> > items)
        {
            Bundle b = new Bundle
            {
                Id   = FhirHelper.MakeId(),
                Type = Bundle.BundleType.Message
            };
            ParticipantMaker author = new ParticipantMaker();

            author.Make(EMUData.AUTHORROLEPROFILE, rx);
            GetParticipantIdentifiers(author);
            Patient p = MakePatient(rx);

            if (resdir != null)
            {
                if (!practitioners.ContainsKey(currentPractitioner))
                {
                    FhirHelper.WriteResource(null, author.Practitioner, resdir, xml);
                    practitioners.Add(currentPractitioner, author.Practitioner.Id);
                }
                else
                {
                    author.Practitioner.Id = practitioners[currentPractitioner];
                }
                if (!organisations.ContainsKey(currentOrganisation))
                {
                    FhirHelper.WriteResource(null, author.Organisation, resdir, xml);
                    organisations.Add(currentOrganisation, author.Organisation.Id);
                }
                else
                {
                    author.Organisation.Id = organisations[currentOrganisation];
                }
                if (!roles.ContainsKey(currentRole))
                {
                    FhirHelper.WriteResource(null, author.Role, resdir, xml);
                    roles.Add(currentRole, author.Role.Id);
                }
                else
                {
                    author.Role.Id = roles[currentRole];
                }
                if (!patients.ContainsKey(currentNhsNumber))
                {
                    FhirHelper.WriteResource(null, p, resdir, xml);
                    patients.Add(currentNhsNumber, p.Id);
                }
                else
                {
                    p.Id = patients[currentNhsNumber];
                }
            }
            MessageHeader header = MakeMessageHeader(author);

            FhirHelper.AddEntryToBundle(b, header);
            FhirHelper.AddEntryToBundle(b, p);
            FhirHelper.AddEntryToBundle(b, author.Practitioner);
            FhirHelper.AddEntryToBundle(b, author.Organisation);
            FhirHelper.AddEntryToBundle(b, author.Role);
            ResourceReference nominatedPharmacy = GetNominatedPharmacyReference(rx);

            foreach (System.Collections.Generic.List <string> item in items)
            {
                MedicationRequest m = MakeMedicationRequest(p, rx, item, nominatedPharmacy, author);
                if (m != null)
                {
                    FhirHelper.AddEntryToBundle(b, m);
                    header.Focus.Add(FhirHelper.MakeInternalReference(m));
                    FhirHelper.WriteResource(null, m, resdir, xml);
                }
            }

            header.Focus.Add(FhirHelper.MakeInternalReference(p));
            header.Focus.Add(FhirHelper.MakeInternalReference(author.Role));
            return(b);
        }
        private static MedicationRequest MakeMedicationRequest(Patient p, System.Collections.Generic.List <string> rx,
                                                               System.Collections.Generic.List <string> item, ResourceReference nom, ParticipantMaker a)
        {
            MedicationRequest m = new MedicationRequest
            {
                Id = FhirHelper.MakeId()
            };

            m.Status  = MedicationRequest.medicationrequestStatus.Active;
            m.Intent  = MedicationRequest.medicationRequestIntent.Order;
            m.Subject = FhirHelper.MakeInternalReference(p);
            m.Identifier.Add(FhirHelper.MakeIdentifier("https://fhir.nhs.uk/Id/prescription-line-id", item[EMUData.LINEITEMID]));
            ResourceReference rq = FhirHelper.MakeInternalReference(a.Role);

            rq.Display        = a.Practitioner.Name[0].Text;
            m.Requester       = rq;
            m.AuthoredOn      = rx[EMUData.AUTHORPARTICIPATIONTIME];
            m.GroupIdentifier = MakeGroupIdentifier(rx);

            DoPrescriptionType(m, rx);
            DoResponsiblePractitioner(m, a);
            m.Medication          = DoMedication(item);
            m.CourseOfTherapyType = MakeCourseOfTherapyType(rx);
            if (item[EMUData.DOSAGEINTRUCTIONS].Trim().Length > 0)
            {
                Dosage di = new Dosage
                {
                    Text = item[EMUData.DOSAGEINTRUCTIONS]
                };
                m.DosageInstruction.Add(di);
                if (item[EMUData.ADDITIONALINSTRUCTIONS].Trim().Length > 0)
                {
                    di.PatientInstruction = item[EMUData.ADDITIONALINSTRUCTIONS];
                }
            }
            m.DispenseRequest = MakeDispenseRequest(nom, rx, item);
            return(m);
        }
 private static void GetParticipantIdentifiers(ParticipantMaker p)
 {
     currentOrganisation = p.Practitioner.Identifier[0].Value;
     currentRole         = p.Role.Identifier[0].Value;
     currentPractitioner = p.Practitioner.Identifier[0].Value;
 }