public async Task <PatientCase> addPatientCase(string patientid, long patientRecId, int caseType, string clinicRecId)
        {
            SoapEntityRepository entityRepository = SoapEntityRepository.GetService();
            PatientCase          Case             = null;

            if (patientid != null && patientid != string.Empty)
            {
                Entity CaseEntity = new Entity("incident");

                if (!string.IsNullOrEmpty(patientid))
                {
                    CaseEntity.Attributes["customerid"] = new EntityReference("contact", new Guid(patientid));
                }

                CaseEntity.Attributes["mzk_casetype"] = new OptionSetValue(caseType);
                //CaseEntity.Attributes["mzk_axclinicrefrecid"] = Convert.ToDecimal(clinicRecId);
                CaseEntity.Attributes["mzk_casedate"] = DateTime.Now.Date;
                CaseId = entityRepository.CreateEntity(CaseEntity);

                Entity entity = entityRepository.GetEntity(xrm.Incident.EntityLogicalName, CaseId, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));

                xrm.Incident EntityCase = (xrm.Incident)entity;

                Case = new PatientCase();

                Case.CaseNumber = EntityCase.TicketNumber;
                Case.CaseId     = CaseId;
            }

            return(Case);
        }
        public async Task <bool> markFinancialDischarge(string caseGuid)
        {
            try
            {
                SoapEntityRepository entityRepository = SoapEntityRepository.GetService();

                xrm.Incident entity = (xrm.Incident)entityRepository.GetEntity(xrm.Incident.EntityLogicalName, new Guid(caseGuid), new Microsoft.Xrm.Sdk.Query.ColumnSet(true));

                if (entity != null)
                {
                    if (entity.Attributes.Contains("mzk_casestatus"))
                    {
                        mzk_casestatus caseStatus = (mzk_casestatus)((OptionSetValue)entity.Attributes["mzk_casestatus"]).Value;

                        if (caseStatus != mzk_casestatus.ClinicallyDischarged)
                        {
                            throw new ValidationException("Only Clinically discharged cases can be Financially discharged");
                        }

                        entity.mzk_casestatus = new OptionSetValue((int)mzk_casestatus.FinanciallyDischarged);

                        entityRepository.UpdateEntity(entity);

                        this.createStatusLog((int)mzk_casestatus.FinanciallyDischarged, caseGuid);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(true);
        }
        public async Task <PatientCase> getCaseDetails(string caseGuid)
        {
            PatientCase model = new PatientCase();

            try
            {
                SoapEntityRepository entityRepository = SoapEntityRepository.GetService();

                xrm.Incident entity = (xrm.Incident)entityRepository.GetEntity(xrm.Incident.EntityLogicalName, new Guid(caseGuid), new Microsoft.Xrm.Sdk.Query.ColumnSet(true));

                if (entity != null)
                {
                    if (entity.Attributes.Contains("ticketnumber"))
                    {
                        model.CaseNumber = entity["ticketnumber"].ToString();
                    }
                    if (entity.Attributes.Contains("createdon"))
                    {
                        model.CaseDate = Convert.ToDateTime(entity["createdon"]);
                    }
                    if (entity.Attributes.Contains("mzk_casetype"))
                    {
                        model.CaseTypeValue = ((OptionSetValue)entity.Attributes["mzk_casetype"]).Value;
                    }
                    if (entity.Attributes.Contains("mzk_casetype"))
                    {
                        model.CaseTypeName = entity.FormattedValues["mzk_casetype"].ToString();
                    }
                    if (entity.Attributes.Contains("mzk_casestatus"))
                    {
                        model.CaseStatusValue = ((OptionSetValue)entity.Attributes["mzk_casestatus"]).Value;
                    }
                    if (entity.Attributes.Contains("mzk_casestatus"))
                    {
                        model.CaseStatusName = entity.FormattedValues["mzk_casestatus"].ToString();
                    }
                    if (entity.CustomerId != null)
                    {
                        model.PatientId = entity.CustomerId.Id.ToString();
                    }
                    if (entity.Attributes.Contains("mzk_organizationalunit"))
                    {
                        model.ClinicId = entity.mzk_OrganizationalUnit.Id.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(model);
        }
        public async Task <bool> markPhysicalDischarge(string caseGuid)
        {
            try
            {
                SoapEntityRepository entityRepository = SoapEntityRepository.GetService();

                xrm.Incident entity = (xrm.Incident)entityRepository.GetEntity(xrm.Incident.EntityLogicalName, new Guid(caseGuid), new Microsoft.Xrm.Sdk.Query.ColumnSet(true));

                if (entity != null)
                {
                    if (entity.Attributes.Contains("mzk_casestatus"))
                    {
                        mzk_casestatus caseStatus = (mzk_casestatus)((OptionSetValue)entity.Attributes["mzk_casestatus"]).Value;

                        if (caseStatus != mzk_casestatus.FinanciallyDischarged)
                        {
                            throw new ValidationException("Only Financially discharged cases can be Physically discharged");
                        }

                        entity.mzk_casestatus = new OptionSetValue((int)mzk_casestatus.PhysicallyDischarged);

                        entityRepository.UpdateEntity(entity);

                        this.createStatusLog((int)mzk_casestatus.PhysicallyDischarged, caseGuid);

                        if (AppSettings.GetByKey("OperationsIntegration").ToLower() == true.ToString().ToLower())
                        {
                            CaseRepository caseRepo = new CaseRepository();

                            caseRepo.updateCaseStatus(caseGuid, HMCaseStatus.PhysicalDischarge);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(true);
        }
        public static mzk_casetype getCaseType(string encounterId, string caseId = "")
        {
            try
            {
                mzk_casetype caseType = mzk_casetype.OutPatient;

                PatientCase model = new PatientCase();

                QueryExpression query = new QueryExpression(xrm.Incident.EntityLogicalName);

                query.ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet("mzk_casetype");

                if (string.IsNullOrEmpty(caseId))
                {
                    LinkEntity enc = new LinkEntity(xrm.Incident.EntityLogicalName, mzk_patientencounter.EntityLogicalName, "incidentid", "mzk_caseid", JoinOperator.Inner);
                    enc.LinkCriteria.AddCondition("mzk_patientencounterid", ConditionOperator.Equal, new Guid(encounterId));
                    query.LinkEntities.Add(enc);
                }
                else
                {
                    query.Criteria.AddCondition("incidentid", ConditionOperator.Equal, new Guid(caseId));
                }

                SoapEntityRepository entityRepository = SoapEntityRepository.GetService();
                EntityCollection     entitycollection = entityRepository.GetEntityCollection(query);

                if (entitycollection != null && entitycollection.Entities != null && entitycollection.Entities.Count > 0)
                {
                    xrm.Incident entity = (xrm.Incident)entitycollection.Entities[0];
                    caseType = entity.mzk_CaseType != null ? (mzk_casetype)entity.mzk_CaseType.Value : caseType;
                }

                return(caseType);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }