private void DoOrg(int b, List <string> rx)
        {
            organisation.Id = FhirHelper.MakeId();
            organisation.Identifier.Add(FhirHelper.MakeIdentifier("https://fhir.nhs.uk/Id/ods-organization-code", rx[b + EMUData.SDSORGANISATIONID]));
            ContactPoint cp = new ContactPoint
            {
                System = ContactPoint.ContactPointSystem.Phone,
                Use    = ContactPoint.ContactPointUse.Work,
                Value  = rx[b + EMUData.ORGANISATIONTELECOM]
            };

            organisation.Telecom.Add(cp);
            CodeableConcept cc = new CodeableConcept();

            cc.Coding.Add(FhirHelper.MakeCoding("https://fhir.nhs.uk/R4/CodeSystem/organisation-type", rx[b + EMUData.ORGANISATIONTYPE], null));
            organisation.Type.Add(cc);
            organisation.Name = rx[b + EMUData.ORGANISATIONNAME];
            organisation.Address.Add(MakeAddress(b, rx));
            Identifier        pct    = FhirHelper.MakeIdentifier("https://fhir.nhs.uk/Id/ods-organization-code", rx[b + EMUData.PCTORGANISATIONSDSID]);
            ResourceReference pctref = new ResourceReference
            {
                Identifier = pct
            };

            organisation.PartOf = pctref;
        }
        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 void DoRole(int b, List <string> rx)
        {
            role.Id = FhirHelper.MakeId();
            role.Identifier.Add(FhirHelper.MakeIdentifier("https://fhir.nhs.uk/Id/sds-role-profile-id", rx[b + EMUData.ROLEPROFILE]));
            role.Practitioner = FhirHelper.MakeInternalReference(practitioner);
            role.Organization = FhirHelper.MakeInternalReference(organisation);
            ContactPoint cp = new ContactPoint
            {
                System = ContactPoint.ContactPointSystem.Phone,
                Use    = ContactPoint.ContactPointUse.Work,
                Value  = rx[b + EMUData.ORGANISATIONTELECOM]
            };

            role.Telecom.Add(cp);
        }
        private void DoPractitioner(int b, List <string> rx)
        {
            practitioner.Id = FhirHelper.MakeId();
            HumanName h = new HumanName
            {
                Text = rx[b + EMUData.PERSONNAME]
            };
            List <HumanName> ah = new List <HumanName>
            {
                h
            };

            practitioner.Name = ah;
            practitioner.Identifier.Add(FhirHelper.MakeIdentifier("https://fhir.nhs.uk/Id/sds-user-id", rx[b + EMUData.SDSUSERID]));
        }
        private static Patient MakePatient(System.Collections.Generic.List <string> rx)
        {
            DateTime d = new DateTime();

            if (!FhirHelper.MakeDate(rx[EMUData.PATIENTBIRTHTIME], out d))
            {
                Console.WriteLine("Failed to parse birth date: " + rx[EMUData.PATIENTBIRTHTIME]);
            }
            Patient p = new Patient
            {
                Id        = FhirHelper.MakeId(),
                BirthDate = FhirHelper.FormatDate(d),
                Gender    = GetGender(rx[EMUData.PATIENTGENDER])
            };

            AddNhsNumber(p, rx);
            AddPatientName(p, rx);
            AddPatientAddress(p, rx);
            AddPatientGp(p, rx);
            return(p);
        }
        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 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);
        }