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);
        }
        public long createCaseTrans(string itemId, HMServiceStatus serviceStatus, string caseId, long appointmentRecId, decimal quantity, string orderId, string unit = "", HMUrgency urgency = HMUrgency.None, string notesToPharmacy = "", string patientInstructionsEng = "", string patientInstructionsArabic = "", long specialityRecId = 0, long treatmentLocationRecId = 0)
        {
            long caseTransRecId = 0;

            //HMCaseServiceClient client = null;

            //try
            //{
            //    client = new HMCaseServiceClient();

            //    CallContext callContext = new CallContext { Company = AppSettings.GetByKey("axCompany") };
            //    client.ClientCredentials.Windows.ClientCredential = new NetworkCredential { Domain = AppSettings.GetByKey("axDomain"), UserName = AppSettings.GetByKey("axUserName"), Password = AppSettings.GetByKey("axPassword") };

            //    caseTransRecId = client.createCaseTrans(callContext, itemId, serviceStatus, new Guid(caseId), appointmentRecId, quantity, new Guid(orderId), unit, urgency, notesToPharmacy, patientInstructionsEng, patientInstructionsArabic, specialityRecId, prnIndication, treatmentLocationRecId);
            //}
            //catch (System.ServiceModel.FaultException<AifFault> aiffaultException)
            //{
            //    throw ValidationException.create(aiffaultException.Message, aiffaultException.HelpLink, aiffaultException.Source);
            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}
            //finally
            //{
            //    client.Close();
            //}
            //return caseTransRecId;

            try
            {
                var endpointAddress = SoapHelper.GetEndPointAddress();
                var binding         = SoapHelper.GetBinding();

                var client  = new HMCaseServiceClient(binding, endpointAddress);
                var channel = client.InnerChannel;
                using (OperationContextScope operationContextScope = new OperationContextScope(channel))
                {
                    SoapHelper.channelHelper();
                    createCaseTransResponse obj = ((HMCaseService)channel).createCaseTrans(new createCaseTrans()
                    {
                        _itemid = itemId, _serviceStatus = serviceStatus, _caseGuid = new Guid(caseId), _apptRecId = appointmentRecId, _quantity = quantity, _orderId = string.IsNullOrEmpty(orderId) ? Guid.Empty : new Guid(orderId), _unit = unit, _urgency = urgency, notesToPharmacy = notesToPharmacy, patientInstructionsEng = patientInstructionsEng, patientInstructionsArabic = patientInstructionsArabic, _specialityRecId = specialityRecId, _treatmentLocation = treatmentLocationRecId
                    });
                    if (obj.result > 0)
                    {
                        caseTransRecId = obj.result;
                    }
                    else
                    {
                        throw new ValidationException(CommonRepository.getErrorMessage(obj.Infolog));
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(caseTransRecId);
        }