public async Task <string> addPatientOrder(PatientSpecialTest patientSpecialTest, bool isActivityOrder = false)
        {
            SoapEntityRepository entityRepository = SoapEntityRepository.GetService();

            xrm.mzk_patientorder patientOrderEntity = new xrm.mzk_patientorder();
            HMUrgency            urgency            = HMUrgency.None;

            try
            {
                mzk_casetype caseType = mzk_casetype.OutPatient;

                patientOrderEntity.mzk_appointable = true;

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

                if (patientSpecialTest.TestName != string.Empty && patientSpecialTest.TestName != null)
                {
                    patientOrderEntity.Attributes["mzk_productid"] = new EntityReference("product", new Guid(patientSpecialTest.TestName));
                }
                if (patientSpecialTest.Frequency != string.Empty && patientSpecialTest.Frequency != null)
                {
                    patientOrderEntity.Attributes["mzk_frequencyid"] = new EntityReference("mzk_ordersetup", new Guid(patientSpecialTest.Frequency));
                }
                if (patientSpecialTest.AssociatedDiagnosis != string.Empty && patientSpecialTest.AssociatedDiagnosis != null)
                {
                    patientOrderEntity.Attributes["mzk_associateddiagnosisid"] = new EntityReference("mzk_concept", new Guid(patientSpecialTest.AssociatedDiagnosis));
                }
                if (patientSpecialTest.UrgencyId != string.Empty && patientSpecialTest.UrgencyId != null)
                {
                    patientOrderEntity.Attributes["mzk_urgency"] = new OptionSetValue(Convert.ToInt32(patientSpecialTest.UrgencyId));

                    mzk_patientordermzk_Urgency orderUrgency = (mzk_patientordermzk_Urgency)Convert.ToInt32(patientSpecialTest.UrgencyId);

                    if (orderUrgency == mzk_patientordermzk_Urgency.Routine)
                    {
                        urgency = HMUrgency.Routine;
                    }
                    else if (orderUrgency == mzk_patientordermzk_Urgency.Stat)
                    {
                        urgency = HMUrgency.Stat;
                    }
                }

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

                if (patientSpecialTest.OrderingPhysician != string.Empty && patientSpecialTest.OrderingPhysician != null)
                {
                    patientOrderEntity.Attributes["ownerid"] = new EntityReference("systemuser", new Guid(patientSpecialTest.OrderingPhysician));
                }
                if (patientSpecialTest.EncounterId != string.Empty && patientSpecialTest.EncounterId != null)
                {
                    patientOrderEntity.Attributes["mzk_patientencounterid"] = new EntityReference("mzk_patientencounter", new Guid(patientSpecialTest.EncounterId));
                    PatientEncounter encounter = new PatientEncounter();
                    encounter.EncounterId = patientSpecialTest.EncounterId;
                    encounter             = encounter.encounterDetails(encounter).Result.ToList().First <PatientEncounter>();
                    PatientId             = encounter.PatientId;
                    caseType = encounter.caseTypeValue;
                    patientOrderEntity.Attributes["mzk_customer"] = new EntityReference("contact", new Guid(PatientId));
                }
                //if (!string.IsNullOrEmpty(patientSpecialTest.OrderDate))
                //    patientOrderEntity.Attributes["mzk_orderdate"] =Convert.ToDateTime( patientSpecialTest.OrderDate);

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

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

                if (patientSpecialTest.ClinicalNotes != string.Empty && patientSpecialTest.ClinicalNotes != null)
                {
                    patientOrderEntity.Attributes["mzk_clinicalnotes"] = patientSpecialTest.ClinicalNotes;
                }

                patientOrderEntity.Attributes["mzk_type"] = new OptionSetValue(6);
                StatusManager statusManager = StatusManager.getRootStatus(mzk_entitytype.SpecialTestOrder, caseType, isActivityOrder);

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

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

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

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

                if (isDuplicateAllowed == true)
                {
                    Id = Convert.ToString(entityRepository.CreateEntity(patientOrderEntity));
                }
                else
                {
                    throw new ValidationException("Same Special Test cannot be added multiple times.");
                }

                if (AppSettings.GetByKey("OperationsIntegration").ToLower() == true.ToString().ToLower())
                {
                    if (!string.IsNullOrEmpty(patientSpecialTest.EncounterId))
                    {
                        if (patientOrderEntity.Attributes.Contains("mzk_treatmentlocation"))
                        {
                            Clinic clinic = new Clinic().getClinicDetails(patientOrderEntity.GetAttributeValue <EntityReference>("mzk_treatmentlocation").Id.ToString());
                            await this.createCaseTrans(patientSpecialTest.EncounterId, OrderNumber, patientSpecialTest.TestName, (mzk_orderstatus)patientOrderEntity.mzk_OrderStatus.Value, 1, "", urgency, "", "", "", 0, clinic.mzk_axclinicrefrecid);
                        }
                        else
                        {
                            await this.createCaseTrans(patientSpecialTest.EncounterId, OrderNumber, patientSpecialTest.TestName, (mzk_orderstatus)patientOrderEntity.mzk_OrderStatus.Value, 1, "", urgency, "", "", "", 0, 0);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (!string.IsNullOrEmpty(Id))
                {
                    entityRepository.DeleteEntity(mzk_patientorder.EntityLogicalName, new Guid(Id));
                }

                throw ex;
            }
            return(Id);
        }
Example #2
0
        public async Task <string> addPatientOrder(PatientLabOrder patientLabOrder, bool isActivityOrder = false)
        {
            if (patientLabOrder.specimensourcelist != null && patientLabOrder.specimensourcelist.Count > 0)
            {
                for (int specimentCount = 0; specimentCount < patientLabOrder.specimensourcelist.Count; specimentCount++)
                {
                    SoapEntityRepository entityRepository   = SoapEntityRepository.GetService();
                    xrm.mzk_patientorder patientOrderEntity = new xrm.mzk_patientorder();
                    try
                    {
                        mzk_casetype caseType = mzk_casetype.OutPatient;

                        patientOrderEntity.mzk_appointable = true;

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

                        if (!string.IsNullOrEmpty(patientLabOrder.TestName))
                        {
                            patientOrderEntity.Attributes["mzk_productid"] = new EntityReference("product", new Guid(patientLabOrder.TestName));
                        }
                        else
                        {
                            throw new ValidationException("Test Name must be selected");
                        }

                        if (!string.IsNullOrEmpty(patientLabOrder.Frequency))
                        {
                            patientOrderEntity.Attributes["mzk_frequencyid"] = new EntityReference("mzk_ordersetup", new Guid(patientLabOrder.Frequency));
                        }
                        if (!string.IsNullOrEmpty(patientLabOrder.AssociatedDiagnosis))
                        {
                            patientOrderEntity.Attributes["mzk_associateddiagnosisid"] = new EntityReference("mzk_concept", new Guid(patientLabOrder.AssociatedDiagnosis));
                        }
                        if (!string.IsNullOrEmpty(patientLabOrder.ClinicalNotes))
                        {
                            patientOrderEntity.Attributes["mzk_clinicalnotes"] = patientLabOrder.ClinicalNotes;
                        }
                        if (!string.IsNullOrEmpty(patientLabOrder.AntibioticsComments))
                        {
                            patientOrderEntity.Attributes["mzk_antibioticscomments"] = patientLabOrder.AntibioticsComments;
                        }
                        if (!string.IsNullOrEmpty(patientLabOrder.EncounterId))
                        {
                            patientOrderEntity.Attributes["mzk_patientencounterid"] = new EntityReference("mzk_patientencounter", new Guid(patientLabOrder.EncounterId));
                            PatientEncounter encounter = new PatientEncounter();
                            encounter.EncounterId = patientLabOrder.EncounterId;
                            encounter             = encounter.encounterDetails(encounter).Result.ToList().First <PatientEncounter>();
                            PatientId             = encounter.PatientId;
                            CaseId   = encounter.CaseId.ToString();
                            caseType = encounter.caseTypeValue;
                            patientOrderEntity.Attributes["mzk_customer"] = new EntityReference("contact", new Guid(PatientId));
                        }


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

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

                        if (patientLabOrder.UrgencyId != string.Empty && patientLabOrder.UrgencyId != null)
                        {
                            patientOrderEntity.Attributes["mzk_urgency"] = new OptionSetValue(Convert.ToInt32(patientLabOrder.UrgencyId));
                        }

                        if (!string.IsNullOrEmpty(patientLabOrder.Antibiotics) && patientLabOrder.Antibiotics == "1")
                        {
                            patientOrderEntity.Attributes["mzk_antibiotics"] = true;
                        }
                        else
                        {
                            patientOrderEntity.Attributes["mzk_antibiotics"] = false;
                        }

                        if (!string.IsNullOrEmpty(patientLabOrder.orderingLocationId))
                        {
                            patientOrderEntity.Attributes["mzk_orderinglocation"] = new EntityReference("mzk_organizationalunit", new Guid(patientLabOrder.orderingLocationId));
                            patientOrderEntity.Attributes["mzk_specialityname"]   = new Clinic().getClinicDetails(patientLabOrder.orderingLocationId).speciality;
                        }

                        if (!string.IsNullOrEmpty(patientLabOrder.ParentOrderId))
                        {
                            patientOrderEntity.Attributes["mzk_parentorder"] = new EntityReference("mzk_patientorder", new Guid(patientLabOrder.ParentOrderId));
                        }

                        if (!string.IsNullOrEmpty(patientLabOrder.Instructionstopatients))
                        {
                            patientOrderEntity.Attributes["mzk_instructionstopatients"] = patientLabOrder.Instructionstopatients;
                        }

                        patientOrderEntity.Attributes["mzk_type"] = new OptionSetValue(2);
                        StatusManager statusManager = StatusManager.getRootStatus(mzk_entitytype.LabOrder, caseType, isActivityOrder);

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

                        if (patientLabOrder.specimensourcelist[specimentCount] != null)
                        {
                            patientOrderEntity.Attributes["mzk_specimensource"] = new EntityReference("mzk_ordersetup", new Guid(patientLabOrder.specimensourcelist[specimentCount]));
                        }

                        bool isDuplicateAllowed = false;

                        Products listProduct = new Products().getProductDetails(patientLabOrder.TestName);
                        if (listProduct != null)
                        {
                            if (listProduct.IsSpecimenSource == false)
                            {
                                if (!string.IsNullOrEmpty(patientLabOrder.EncounterId) && !string.IsNullOrEmpty(patientLabOrder.TestName))
                                {
                                    isDuplicateAllowed = new PatientEncounter().DuplicateDetection(patientLabOrder.EncounterId, patientLabOrder.TestName);
                                }
                            }
                            else
                            {
                                if (!string.IsNullOrEmpty(patientLabOrder.EncounterId) && !string.IsNullOrEmpty(patientLabOrder.TestName) && !string.IsNullOrEmpty(patientLabOrder.specimensourcelist[specimentCount]))
                                {
                                    isDuplicateAllowed = new PatientEncounter().DuplicateDetection(patientLabOrder.EncounterId, patientLabOrder.TestName, patientLabOrder.specimensourcelist[specimentCount]);
                                }
                            }

                            if (isDuplicateAllowed == true)
                            {
                                Id = Convert.ToString(entityRepository.CreateEntity(patientOrderEntity));
                            }
                            else
                            {
                                throw new ValidationException("Same Lab Test cannot be added multiple times.");
                            }
                        }

                        if (AppSettings.GetByKey("OperationsIntegration").ToLower() == true.ToString().ToLower())
                        {
                            if (!string.IsNullOrEmpty(patientLabOrder.EncounterId))
                            {
                                await this.createCaseTrans(patientLabOrder.EncounterId, Id, patientLabOrder.TestName, (mzk_orderstatus)patientOrderEntity.mzk_OrderStatus.Value);
                            }
                        }

                        if (AppSettings.GetByKey("LISIntegration").ToLower() == true.ToString().ToLower() && statusManager.sendOrm)
                        {
                            this.sendORMLISMessage(PatientId, 0, patientLabOrder.EncounterId, Id, CaseId, statusManager.status.ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        if (!string.IsNullOrEmpty(Id))
                        {
                            entityRepository.DeleteEntity(mzk_patientorder.EntityLogicalName, new Guid(Id));
                        }

                        throw ex;
                    }
                }
            }
            else
            {
                SoapEntityRepository entityRepository   = SoapEntityRepository.GetService();
                xrm.mzk_patientorder patientOrderEntity = new xrm.mzk_patientorder();
                try
                {
                    mzk_casetype caseType = mzk_casetype.OutPatient;

                    patientOrderEntity.mzk_appointable = true;

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

                    if (!string.IsNullOrEmpty(patientLabOrder.TestName))
                    {
                        patientOrderEntity.Attributes["mzk_productid"] = new EntityReference("product", new Guid(patientLabOrder.TestName));
                    }
                    else
                    {
                        throw new ValidationException("Test Name must be selected");
                    }

                    if (!string.IsNullOrEmpty(patientLabOrder.Frequency))
                    {
                        patientOrderEntity.Attributes["mzk_frequencyid"] = new EntityReference("mzk_ordersetup", new Guid(patientLabOrder.Frequency));
                    }
                    if (!string.IsNullOrEmpty(patientLabOrder.AssociatedDiagnosis))
                    {
                        patientOrderEntity.Attributes["mzk_associateddiagnosisid"] = new EntityReference("mzk_concept", new Guid(patientLabOrder.AssociatedDiagnosis));
                    }
                    if (!string.IsNullOrEmpty(patientLabOrder.ClinicalNotes))
                    {
                        patientOrderEntity.Attributes["mzk_clinicalnotes"] = patientLabOrder.ClinicalNotes;
                    }
                    if (!string.IsNullOrEmpty(patientLabOrder.AntibioticsComments))
                    {
                        patientOrderEntity.Attributes["mzk_antibioticscomments"] = patientLabOrder.AntibioticsComments;
                    }
                    if (!string.IsNullOrEmpty(patientLabOrder.EncounterId))
                    {
                        patientOrderEntity.Attributes["mzk_patientencounterid"] = new EntityReference("mzk_patientencounter", new Guid(patientLabOrder.EncounterId));
                        PatientEncounter encounter = new PatientEncounter();
                        encounter.EncounterId = patientLabOrder.EncounterId;
                        encounter             = encounter.encounterDetails(encounter).Result.ToList().First <PatientEncounter>();
                        PatientId             = encounter.PatientId;
                        CaseId   = encounter.CaseId.ToString();
                        caseType = encounter.caseTypeValue;
                        patientOrderEntity.Attributes["mzk_customer"] = new EntityReference("contact", new Guid(PatientId));
                    }

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

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

                    if (patientLabOrder.UrgencyId != string.Empty && patientLabOrder.UrgencyId != null)
                    {
                        patientOrderEntity.Attributes["mzk_urgency"] = new OptionSetValue(Convert.ToInt32(patientLabOrder.UrgencyId));
                    }

                    if (!string.IsNullOrEmpty(patientLabOrder.Antibiotics) && patientLabOrder.Antibiotics == "1")
                    {
                        patientOrderEntity.Attributes["mzk_antibiotics"] = true;
                    }
                    else
                    {
                        patientOrderEntity.Attributes["mzk_antibiotics"] = false;
                    }

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

                        patientOrderEntity.Attributes["mzk_specialityname"] = new Clinic().getClinicDetails(patientLabOrder.orderingLocationId).speciality;
                    }

                    if (!string.IsNullOrEmpty(patientLabOrder.ParentOrderId))
                    {
                        patientOrderEntity.Attributes["mzk_parentorder"] = new EntityReference("mzk_patientorder", new Guid(patientLabOrder.ParentOrderId));
                    }

                    if (!string.IsNullOrEmpty(patientLabOrder.Instructionstopatients))
                    {
                        patientOrderEntity.Attributes["mzk_instructionstopatients"] = patientLabOrder.Instructionstopatients;
                    }

                    patientOrderEntity.Attributes["mzk_type"] = new OptionSetValue(2);
                    StatusManager statusManager = StatusManager.getRootStatus(mzk_entitytype.LabOrder, 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(patientLabOrder.EncounterId) && !string.IsNullOrEmpty(patientLabOrder.TestName))
                    {
                        isDuplicateAllowed = new PatientEncounter().DuplicateDetection(patientLabOrder.EncounterId, patientLabOrder.TestName);
                    }

                    if (isDuplicateAllowed == true)
                    {
                        Id = Convert.ToString(entityRepository.CreateEntity(patientOrderEntity));
                    }
                    else
                    {
                        throw new ValidationException("Same Lab Test cannot be added multiple times.");
                    }

                    if (AppSettings.GetByKey("OperationsIntegration").ToLower() == true.ToString().ToLower())
                    {
                        if (!string.IsNullOrEmpty(patientLabOrder.EncounterId))
                        {
                            await this.createCaseTrans(patientLabOrder.EncounterId, Id, patientLabOrder.TestName, (mzk_orderstatus)patientOrderEntity.mzk_OrderStatus.Value);
                        }
                    }

                    if (AppSettings.GetByKey("LISIntegration").ToLower() == true.ToString().ToLower() && statusManager.sendOrm)
                    {
                        this.sendORMLISMessage(PatientId, 0, patientLabOrder.EncounterId, Id, CaseId, statusManager.status.ToString());
                    }
                }
                catch (Exception ex)
                {
                    if (!string.IsNullOrEmpty(Id))
                    {
                        entityRepository.DeleteEntity(mzk_patientorder.EntityLogicalName, new Guid(Id));
                    }

                    throw ex;
                }
            }
            return(Id.ToString());
        }