public async Task <string> addPatientOrder(PatientRadiologyOrder patientRadiologyOrder, bool isActivityOrder = false)
        {
            SoapEntityRepository entityRepository   = SoapEntityRepository.GetService();
            mzk_patientorder     patientOrderEntity = new mzk_patientorder();

            try
            {
                mzk_casetype caseType = mzk_casetype.OutPatient;

                patientOrderEntity.mzk_appointable = true;

                if (!string.IsNullOrEmpty(patientRadiologyOrder.appointmentId))
                {
                    patientOrderEntity.mzk_orderingappointment    = new EntityReference("mzk_patientorder", new Guid(patientRadiologyOrder.appointmentId));
                    patientOrderEntity.mzk_fulfillmentappointment = new EntityReference("mzk_patientorder", new Guid(patientRadiologyOrder.appointmentId));

                    if (!string.IsNullOrEmpty(patientRadiologyOrder.PatientId))
                    {
                        patientOrderEntity.mzk_customer = new EntityReference("contact", new Guid(patientRadiologyOrder.PatientId));
                    }
                    if (!string.IsNullOrEmpty(patientRadiologyOrder.CaseTransRecId))
                    {
                        patientOrderEntity.mzk_AXCaseTransRefRecId = Convert.ToDecimal(patientRadiologyOrder.CaseTransRecId);
                    }
                    if (!string.IsNullOrEmpty(patientRadiologyOrder.CaseId))
                    {
                        patientOrderEntity.mzk_caseid = new EntityReference("incident", new Guid(patientRadiologyOrder.CaseId));
                    }

                    if (!string.IsNullOrEmpty(patientRadiologyOrder.TestName))
                    {
                        patientOrderEntity.mzk_ProductId = new EntityReference(xrm.Product.EntityLogicalName, Products.getProductId(patientRadiologyOrder.TestName));

                        if (patientOrderEntity.mzk_ProductId == null && patientOrderEntity.mzk_ProductId.Id == Guid.Empty)
                        {
                            throw new ValidationException("Product not found for the corresponding item. Please contact system administrator");
                        }
                    }

                    patientOrderEntity.Attributes["mzk_orderdate"] = DateTime.Now.Date;
                    patientOrderEntity.mzk_FulfillmentDate         = patientRadiologyOrder.FulfillmentDate;

                    if (patientRadiologyOrder.clinicRecId > 0)
                    {
                        patientOrderEntity.Attributes["mzk_axclinicrefrecid"] = Convert.ToDecimal(patientRadiologyOrder.clinicRecId);
                    }

                    if (!string.IsNullOrEmpty(patientRadiologyOrder.orderingLocationId))
                    {
                        patientOrderEntity.Attributes["mzk_orderinglocation"] = new EntityReference("mzk_organizationalunit", new Guid(patientRadiologyOrder.orderingLocationId));
                    }


                    if (!string.IsNullOrEmpty(patientRadiologyOrder.treatmentLocationId))
                    {
                        patientOrderEntity.Attributes["mzk_treatmentlocation"] = new EntityReference("mzk_organizationalunit", new Guid(patientRadiologyOrder.treatmentLocationId));
                    }
                }
                else if (patientRadiologyOrder.EncounterId != null && patientRadiologyOrder.EncounterId != string.Empty)
                {
                    if (patientRadiologyOrder.TestName != null && patientRadiologyOrder.TestName != string.Empty)
                    {
                        patientOrderEntity.Attributes["mzk_productid"] = new EntityReference("product", new Guid(patientRadiologyOrder.TestName));
                    }
                    if (patientRadiologyOrder.Frequency != null && patientRadiologyOrder.Frequency != string.Empty)
                    {
                        patientOrderEntity.Attributes["mzk_frequencyid"] = new EntityReference("mzk_ordersetup", new Guid(patientRadiologyOrder.Frequency));
                    }
                    if (patientRadiologyOrder.AssociatedDiagnosis != null && patientRadiologyOrder.AssociatedDiagnosis != string.Empty)
                    {
                        patientOrderEntity.Attributes["mzk_associateddiagnosisid"] = new EntityReference("mzk_concept", new Guid(patientRadiologyOrder.AssociatedDiagnosis));
                    }
                    if (patientRadiologyOrder.ClinicalNotes != null && patientRadiologyOrder.ClinicalNotes != string.Empty)
                    {
                        patientOrderEntity.Attributes["mzk_clinicalnotes"] = patientRadiologyOrder.ClinicalNotes;
                    }
                    if (patientRadiologyOrder.StudyDate != null && patientRadiologyOrder.StudyDate != string.Empty)
                    {
                        patientOrderEntity.Attributes["mzk_studydate"] = new OptionSetValue(Convert.ToInt32(patientRadiologyOrder.StudyDate));
                    }

                    if (patientRadiologyOrder.clinicRecId > 0)
                    {
                        patientOrderEntity.Attributes["mzk_axclinicrefrecid"] = Convert.ToDecimal(patientRadiologyOrder.clinicRecId);
                    }

                    if (!string.IsNullOrEmpty(patientRadiologyOrder.orderingLocationId))
                    {
                        patientOrderEntity.Attributes["mzk_orderinglocation"] = new EntityReference("mzk_organizationalunit", new Guid(patientRadiologyOrder.orderingLocationId));
                    }

                    if (!string.IsNullOrEmpty(patientRadiologyOrder.treatmentLocationId))
                    {
                        patientOrderEntity.Attributes["mzk_treatmentlocation"] = new EntityReference("mzk_organizationalunit", new Guid(patientRadiologyOrder.treatmentLocationId));
                    }

                    if (patientRadiologyOrder.OrderDate != DateTime.MinValue)
                    {
                        patientOrderEntity.Attributes["mzk_orderdate"] = patientRadiologyOrder.OrderDate;
                    }
                    else
                    {
                        patientOrderEntity.Attributes["mzk_orderdate"] = DateTime.Now.Date;
                    }

                    patientOrderEntity.Attributes["mzk_fulfillmentdate"] = patientOrderEntity.Attributes["mzk_orderdate"];
                }

                //for Radiology
                patientOrderEntity.Attributes["mzk_type"] = new OptionSetValue(3);

                if (patientRadiologyOrder.EncounterId != null && patientRadiologyOrder.EncounterId != string.Empty)
                {
                    patientOrderEntity.Attributes["mzk_patientencounterid"] = new EntityReference("mzk_patientencounter", new Guid(patientRadiologyOrder.EncounterId));
                    PatientEncounter encounter = new PatientEncounter();
                    encounter.EncounterId = patientRadiologyOrder.EncounterId;

                    List <PatientEncounter> listEncounter = encounter.encounterDetails(encounter).Result;

                    if (!string.IsNullOrEmpty(CaseId))
                    {
                        Speciality specialty = new Speciality();
                        patientOrderEntity.Attributes["mzk_specialtyname"] = specialty.getSpeciality(CaseId);
                    }

                    encounter = encounter.encounterDetails(encounter).Result.ToList().First <PatientEncounter>();
                    PatientId = encounter.PatientId;
                    caseType  = encounter.caseTypeValue;
                    patientOrderEntity.Attributes["mzk_customer"] = new EntityReference("contact", new Guid(PatientId));
                }

                StatusManager statusManager = StatusManager.getRootStatus(mzk_entitytype.RadiologyOrder, caseType, isActivityOrder);

                if (statusManager != null)
                {
                    patientOrderEntity.mzk_OrderStatus         = new OptionSetValue(statusManager.status);
                    patientOrderEntity.mzk_StatusManagerDetail = new EntityReference(mzk_statusmanagerdetails.EntityLogicalName, new Guid(statusManager.StatusId));
                }

                bool isDuplicateAllowed = false;
                if (!string.IsNullOrEmpty(patientRadiologyOrder.EncounterId) && !string.IsNullOrEmpty(patientRadiologyOrder.TestName))
                {
                    isDuplicateAllowed = new PatientEncounter().DuplicateDetection(patientRadiologyOrder.EncounterId, patientRadiologyOrder.TestName);
                }
                else
                {
                    isDuplicateAllowed = true;
                }

                if (isDuplicateAllowed == true)
                {
                    Id = Convert.ToString(entityRepository.CreateEntity(patientOrderEntity));

                    if (AppSettings.GetByKey("OperationsIntegration").ToLower() == true.ToString().ToLower())
                    {
                        if (!string.IsNullOrEmpty(patientRadiologyOrder.EncounterId))
                        {
                            if (patientOrderEntity.Attributes.Contains("mzk_treatmentlocation"))
                            {
                                Clinic clinic = new Clinic().getClinicDetails(patientOrderEntity.GetAttributeValue <EntityReference>("mzk_treatmentlocation").Id.ToString());
                                await this.createCaseTrans(patientRadiologyOrder.EncounterId, Id, patientRadiologyOrder.TestName, (mzk_orderstatus)patientOrderEntity.mzk_OrderStatus.Value, 1, "", AXRepository.AXServices.HMUrgency.None, "", "", "", 0, clinic.mzk_axclinicrefrecid);
                            }
                            else
                            {
                                await this.createCaseTrans(patientRadiologyOrder.EncounterId, Id, patientRadiologyOrder.TestName, (mzk_orderstatus)patientOrderEntity.mzk_OrderStatus.Value, 1, "", AXRepository.AXServices.HMUrgency.None, "", "", "", 0, 0);
                            }
                        }
                    }
                }
                else
                {
                    throw new ValidationException("Same Radiology Test cannot be added multiple times.");
                }

                if (!string.IsNullOrEmpty(Id) && !string.IsNullOrEmpty(patientRadiologyOrder.appointmentId) && AppSettings.GetByKey("RISIntegration").ToLower() == true.ToString().ToLower())
                {
                    RIS     ris     = new RIS();
                    Patient patient = new Patient();

                    if (patientRadiologyOrder.registered)
                    {
                        await patient.updatePatientRIS(patientRadiologyOrder.PatientId);
                    }
                    else
                    {
                        await patient.createPatientRIS(patientRadiologyOrder.appointmentId, patientRadiologyOrder.PatientId);
                    }
                }

                //await this.addContrastOrder(patientRadiologyOrder, Id);
            }
            catch (Exception ex)
            {
                if (!string.IsNullOrEmpty(Id))
                {
                    entityRepository.DeleteEntity(mzk_patientorder.EntityLogicalName, new Guid(Id));
                }

                throw ex;
            }
            return(Id.ToString());
        }
Esempio n. 2
0
        public async Task <bool> updatePatientOrder(PatientReferralOrder patientReferral)
        {
            try
            {
                SoapEntityRepository entityRepository = SoapEntityRepository.GetService();
                Entity referralEntity = entityRepository.GetEntity(mzk_patientorder.EntityLogicalName, new Guid(patientReferral.Id)
                {
                },
                                                                   new Microsoft.Xrm.Sdk.Query.ColumnSet("mzk_customer",
                                                                                                         "mzk_appointmentrecommendation",
                                                                                                         "mzk_associateddiagnosisid",
                                                                                                         "mzk_patientaware",
                                                                                                         "mzk_comments",
                                                                                                         "mzk_orderstatus",
                                                                                                         "mzk_referraltoid",
                                                                                                         "mzk_referraltype",
                                                                                                         "mzk_specialityid",
                                                                                                         "ownerid",
                                                                                                         "mzk_patientordernumber",
                                                                                                         "createdon",
                                                                                                         "mzk_referralcategory",
                                                                                                         "mzk_referraltoexternalid"));

                if (!string.IsNullOrEmpty(patientReferral.PatientId))
                {
                    referralEntity.Attributes["mzk_customer"] = new EntityReference("contact", new Guid(patientReferral.PatientId));
                }

                if (!string.IsNullOrEmpty(patientReferral.Diagnosis))
                {
                    referralEntity.Attributes["mzk_associateddiagnosisid"] = new EntityReference("mzk_concept", new Guid(patientReferral.Diagnosis));
                }

                if (!string.IsNullOrEmpty(patientReferral.ReferralComment))
                {
                    referralEntity.Attributes["mzk_comments"] = patientReferral.ReferralComment;
                }

                if (patientReferral.PatientAware.ToString() != "1")
                {
                    referralEntity.Attributes["mzk_patientaware"] = true;
                }
                else
                {
                    referralEntity.Attributes["mzk_patientaware"] = false;
                }

                if (patientReferral.Apptrecommendation != 0)
                {
                    referralEntity.Attributes["mzk_appointmentrecommendation"] = new OptionSetValue(patientReferral.Apptrecommendation);
                }

                //for ReferralType Consultation, Consultation & Treatment and Transfer of Care
                if (patientReferral.ReferralType != 0)
                {
                    referralEntity.Attributes["mzk_referraltype"] = new OptionSetValue(patientReferral.ReferralType);
                }

                //for Category Internal /External
                if (patientReferral.Category != 0 && patientReferral.Category == Convert.ToInt32(mzk_patientordermzk_ReferralCategory.Internal))
                {
                    referralEntity.Attributes["mzk_referralcategory"] = new OptionSetValue(Convert.ToInt32(patientReferral.Category));

                    if (!string.IsNullOrEmpty(patientReferral.ReferralId))
                    {
                        referralEntity.Attributes["mzk_referraltoid"] = new EntityReference("systemuser", new Guid(patientReferral.ReferralId));
                    }
                }
                else
                {
                    if (patientReferral.Category != 0 && patientReferral.Category == Convert.ToInt32(mzk_patientordermzk_ReferralCategory.External))
                    {
                        referralEntity.Attributes["mzk_referralcategory"] = new OptionSetValue(Convert.ToInt32(patientReferral.Category));

                        if (!string.IsNullOrEmpty(patientReferral.ReferralId))
                        {
                            referralEntity.Attributes["mzk_referraltoexternalid"] = new EntityReference("mzk_referringphysician", new Guid(patientReferral.ReferralId));
                        }
                    }
                }
                if (!string.IsNullOrEmpty(patientReferral.SpecialtyId))
                {
                    referralEntity.Attributes["mzk_specialityid"] = new EntityReference("characteristic", new Guid(patientReferral.SpecialtyId));

                    var specialityNameList = new List <string> {
                        patientReferral.SpecialtyId
                    };

                    Speciality speciality = new Speciality();

                    referralEntity.Attributes["mzk_specialtyname"] = speciality.getSpecialityList(specialityNameList).First <Speciality>().Description;
                }

                if (!string.IsNullOrEmpty(patientReferral.HospitalId))
                {
                    referralEntity.Attributes["mzk_hospitalid"] = new EntityReference("mzk_hospital", new Guid(patientReferral.HospitalId));
                }

                entityRepository.UpdateEntity(referralEntity);
                return(true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 3
0
        public async Task <List <PatientReferralOrder> > getReferral(PatientReferralOrder referral)
        {
            List <PatientReferralOrder> PatientReferral = new List <PatientReferralOrder>();
            List <Speciality>           speciality      = new Speciality().getSpecialityList(null, referral.CategoryName == mzk_patientordermzk_ReferralCategory.External.ToString() ? mzk_locationtype.External : mzk_locationtype.Internal).ToList <Speciality>();

            foreach (Speciality ss in speciality)
            {
                PatientReferralOrder model = new PatientReferralOrder();
                if (referral.CategoryName == mzk_patientordermzk_ReferralCategory.External.ToString())
                {
                    model.SpecialtyName          = ss.Description;
                    model.SpecialtyId            = ss.SpecialityId;
                    model.referringPhysicianList = new ReferringPhysician().getReferringPhysician(ss.SpecialityId);
                    model.Category = (int)mzk_patientordermzk_ReferralCategory.External;
                }
                else
                {
                    if (referral.CategoryName == mzk_patientordermzk_ReferralCategory.Internal.ToString())
                    {
                        model.SpecialtyName = ss.Description;
                        model.SpecialtyId   = ss.SpecialityId;

                        if (!string.IsNullOrEmpty(referral.UserId))
                        {
                            model.usersList = new User().getUsers(ss.SpecialityRefRecId, true, referral.UserId, ss.SpecialityId);
                        }
                        else
                        {
                            model.usersList = new User().getUsers(ss.SpecialityRefRecId, true, string.Empty, ss.SpecialityId);
                        }

                        model.Category = (int)mzk_patientordermzk_ReferralCategory.Internal;
                    }
                }
                PatientReferral.Add(model);
            }

            //QueryExpression query;
            //#region Search User Query
            //if (referral.CategoryName == mzk_patientordermzk_ReferralCategory.Internal.ToString())
            //{
            //    query = new QueryExpression("systemuser");
            //    FilterExpression childFilter = query.Criteria.AddFilter(LogicalOperator.And);
            //    childFilter.AddCondition("fullname", ConditionOperator.Like, "%" + referral.Referralto + "%");
            //    query.ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet("fullname", "address1_composite", "mzk_type", "mzk_axresourcerefrecid");
            //}
            //else
            //{
            //    query = new QueryExpression("mzk_referringphysician");
            //    FilterExpression childFilter = query.Criteria.AddFilter(LogicalOperator.And);
            //    childFilter.AddCondition("mzk_name", ConditionOperator.Like, "%" + referral.Referralto + "%");
            //    query.ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet("mzk_name", "mzk_contactid", "mzk_specialityid", "mzk_type");

            //    LinkEntity linkContact = new LinkEntity("mzk_referringphysician", "contact", "mzk_contactid", "contactid", JoinOperator.LeftOuter);
            //    linkContact.Columns = new Microsoft.Xrm.Sdk.Query.ColumnSet("address1_composite");

            //    LinkEntity linkSpeciality = new LinkEntity("mzk_referringphysician", "mzk_speciality", "mzk_specialityid", "mzk_specialityid", JoinOperator.LeftOuter);
            //    linkSpeciality.Columns = new Microsoft.Xrm.Sdk.Query.ColumnSet("mzk_description");
            //    query.LinkEntities.Add(linkContact);
            //    query.LinkEntities.Add(linkSpeciality);
            //}
            //#endregion
            //SoapEntityRepository entityRepository = SoapEntityRepository.GetService();
            //EntityCollection entitycollection = entityRepository.GetEntityCollection(query);

            //User user = new User();
            //foreach (Entity entity in entitycollection.Entities)
            //{
            //    PatientReferral model = new PatientReferral();

            //    if (referral.CategoryName == mzk_patientordermzk_ReferralCategory.Internal.ToString())
            //    {
            //        if (entity.Attributes.Contains("fullname"))
            //            model.Referralto = entity.Attributes["fullname"].ToString();

            //        if (entity.Attributes.Contains("address1_composite"))
            //            model.Address = entity.Attributes["address1_composite"].ToString();

            //        if (entity.Attributes.Contains("mzk_axresourcerefrecid"))
            //        {
            //            if (user.getSpecialities(Convert.ToInt64(entity.Attributes["mzk_axresourcerefrecid"])).Count > 0)
            //            {
            //                model.SpecialtyId = new User().getSpecialities(Convert.ToInt64(entity.Attributes["mzk_axresourcerefrecid"])).First<Speciality>().SpecialityId;
            //                model.SpecialtyName = new User().getSpecialities(Convert.ToInt64(entity.Attributes["mzk_axresourcerefrecid"])).First<Speciality>().Description;
            //            }
            //        }
            //        model.ReferralId = entity.Id.ToString();
            //    }
            //    else
            //    {
            //        if (referral.CategoryName == mzk_patientordermzk_ReferralCategory.External.ToString())
            //        {
            //            if (entity.Attributes.Contains("mzk_name"))
            //                model.Referralto = entity.Attributes["mzk_name"].ToString();

            //            if (entity.Attributes.Contains("contact1.address1_composite"))
            //                model.Address = (entity.Attributes["contact1.address1_composite"] as AliasedValue).Value.ToString();

            //            if (entity.Attributes.Contains("mzk_speciality2.mzk_description"))
            //            {
            //                model.SpecialtyName = (entity.Attributes["mzk_speciality2.mzk_description"] as AliasedValue).Value.ToString();
            //            }
            //            if (entity.Attributes.Contains("mzk_specialityid"))
            //            {
            //                model.SpecialtyId = ((EntityReference)entity.Attributes["mzk_specialityid"]).Id.ToString();
            //            }
            //            model.ReferralId = entity.Id.ToString();
            //        }
            //    }

            //    PatientReferral.Add(model);
            //}
            return(PatientReferral);
        }
Esempio n. 4
0
        public async Task <string> addPatientOrder(PatientReferralOrder _patientReferral)
        {
            SoapEntityRepository entityRepository = SoapEntityRepository.GetService();
            mzk_patientorder     referralEntity   = new mzk_patientorder();

            try
            {
                referralEntity.mzk_appointable = true;

                if (!string.IsNullOrEmpty(_patientReferral.appointmentId))
                {
                    referralEntity.mzk_orderingappointment    = new EntityReference("mzk_patientorder", new Guid(_patientReferral.appointmentId));
                    referralEntity.mzk_fulfillmentappointment = new EntityReference("mzk_patientorder", new Guid(_patientReferral.appointmentId));
                }

                if (!string.IsNullOrEmpty(_patientReferral.PatientId))
                {
                    referralEntity.Attributes["mzk_customer"] = new EntityReference("contact", new Guid(_patientReferral.PatientId));
                }

                if (!string.IsNullOrEmpty(_patientReferral.EncounterId))
                {
                    referralEntity.Attributes["mzk_patientencounterid"] = new EntityReference("mzk_patientencounter", new Guid(_patientReferral.EncounterId));
                    PatientEncounter encounter = new PatientEncounter();
                    encounter.EncounterId = _patientReferral.EncounterId;
                    //PatientId = new PatientCase().getCaseDetails(encounter.encounterDetails(encounter).Result.ToList().First<PatientEncounter>().CaseId).Result.PatientId;
                    PatientId = encounter.getEncounterDetails(encounter).Result.ToList().First <PatientEncounter>().PatientId;
                    referralEntity.Attributes["mzk_customer"] = new EntityReference("contact", new Guid(PatientId));
                }

                if (!string.IsNullOrEmpty(_patientReferral.Diagnosis))
                {
                    referralEntity.Attributes["mzk_associateddiagnosisid"] = new EntityReference("mzk_concept", new Guid(_patientReferral.Diagnosis));
                }

                if (_patientReferral.clinicRecId > 0)
                {
                    referralEntity.Attributes["mzk_axclinicrefrecid"] = Convert.ToDecimal(_patientReferral.clinicRecId);
                }

                if (!string.IsNullOrEmpty(_patientReferral.orderingLocationId))
                {
                    referralEntity.Attributes["mzk_orderinglocation"] = new EntityReference("mzk_organizationalunit", new Guid(_patientReferral.orderingLocationId));
                }

                if (!string.IsNullOrEmpty(_patientReferral.ReferralComment))
                {
                    referralEntity.Attributes["mzk_comments"] = _patientReferral.ReferralComment;
                }

                if (_patientReferral.PatientAware.ToString() != "1")
                {
                    referralEntity.Attributes["mzk_patientaware"] = true;
                }
                else
                {
                    referralEntity.Attributes["mzk_patientaware"] = false;
                }

                if (_patientReferral.Apptrecommendation != 0)
                {
                    referralEntity.Attributes["mzk_appointmentrecommendation"] = new OptionSetValue(_patientReferral.Apptrecommendation);
                }

                //for ReferralType Consultation, Consultation & Treatment and Transfer of Care
                if (_patientReferral.ReferralType != 0)
                {
                    referralEntity.Attributes["mzk_referraltype"] = new OptionSetValue(_patientReferral.ReferralType);
                }


                //for Category Internal /External
                if (_patientReferral.Category != 0 && _patientReferral.Category == Convert.ToInt32(mzk_patientordermzk_ReferralCategory.Internal))
                {
                    referralEntity.Attributes["mzk_referralcategory"] = new OptionSetValue(Convert.ToInt32(_patientReferral.Category));

                    if (!string.IsNullOrEmpty(_patientReferral.ReferralId))
                    {
                        referralEntity.Attributes["mzk_referraltoid"] = new EntityReference("systemuser", new Guid(_patientReferral.ReferralId));
                    }
                }
                else
                {
                    if (_patientReferral.Category != 0 && _patientReferral.Category == Convert.ToInt32(mzk_patientordermzk_ReferralCategory.External))
                    {
                        referralEntity.Attributes["mzk_referralcategory"] = new OptionSetValue(Convert.ToInt32(_patientReferral.Category));

                        if (!string.IsNullOrEmpty(_patientReferral.ReferralId))
                        {
                            referralEntity.Attributes["mzk_referraltoexternalid"] = new EntityReference("mzk_referringphysician", new Guid(_patientReferral.ReferralId));
                        }
                    }
                }

                if (!string.IsNullOrEmpty(_patientReferral.SpecialtyId))
                {
                    referralEntity.Attributes["mzk_specialityid"] = new EntityReference("characteristic", new Guid(_patientReferral.SpecialtyId));

                    var specialityNameList = new List <string> {
                        _patientReferral.SpecialtyId
                    };

                    Speciality speciality = new Speciality();

                    referralEntity.Attributes["mzk_specialtyname"] = speciality.getSpecialityList(specialityNameList).First <Speciality>().Description;
                }

                if (!string.IsNullOrEmpty(_patientReferral.HospitalId) && _patientReferral.HospitalId != "0")
                {
                    referralEntity.Attributes["mzk_hospitalid"] = new EntityReference("mzk_hospital", new Guid(_patientReferral.HospitalId));
                }

                referralEntity.Attributes["mzk_type"]        = new OptionSetValue(Convert.ToInt32(mzk_patientordermzk_Type.Referral));
                referralEntity.Attributes["mzk_orderstatus"] = new OptionSetValue((int)mzk_orderstatus.Ordered);

                referralEntity.Attributes["mzk_orderdate"]       = DateTime.Now.Date;
                referralEntity.Attributes["mzk_fulfillmentdate"] = referralEntity.Attributes["mzk_orderdate"];


                Id = Convert.ToString(entityRepository.CreateEntity(referralEntity));

                if (!string.IsNullOrEmpty(_patientReferral.EncounterId) && !string.IsNullOrEmpty(_patientReferral.SpecialtyId))
                {
                    mzk_casetype caseType = PatientCase.getCaseType(_patientReferral.EncounterId);

                    if (caseType == mzk_casetype.Emergency && _patientReferral.Category == Convert.ToInt32(mzk_patientordermzk_ReferralCategory.Internal))
                    {
                        List <string> specialtyIdList = new List <string>();
                        specialtyIdList.Add(_patientReferral.SpecialtyId);

                        Speciality sp = new Speciality().getSpecialityList(specialtyIdList).FirstOrDefault();

                        if (AppSettings.GetByKey("OperationsIntegration").ToLower() == true.ToString().ToLower())
                        {
                            if (sp != null && sp.SpecialityRefRecId > 0)
                            {
                                await this.createCaseTrans(_patientReferral.EncounterId, Id, "", mzk_orderstatus.Ordered, 1, "", HMUrgency.None, "", "", "", sp.SpecialityRefRecId);
                            }
                        }
                    }
                }

                return(Id.ToString());
            }
            catch (Exception ex)
            {
                if (!string.IsNullOrEmpty(Id))
                {
                    entityRepository.DeleteEntity(mzk_patientorder.EntityLogicalName, new Guid(Id));
                }

                throw ex;
            }
        }