Example #1
0
        public ACAutocompleteModel GetACDoctorsAndPracticesForAutocomplete(string search_criteria, string sessionTicket, string connectionString, out TransactionalInformation transaction)
        {
            var method = MethodInfo.GetCurrentMethod();
            var ipInfo = Util.GetIPInfo(HttpContext.Current.Request);

            transaction = new TransactionalInformation();
            ACAutocompleteModel autocomplete_model = new ACAutocompleteModel();
            var last_used_doctors = new List <AutocompleteModel>();
            var doctors           = new List <AutocompleteModel>();
            var securityTicket    = VerifySessionToken(sessionTicket);
            var data = cls_Get_Account_Information_with_PracticeID.Invoke(connectionString, securityTicket).Result;

            try
            {
                var last_used_practices_doctors = Get_Practices_and_Doctors.Get_Last_Used_Doctors_Practices(data.PracticeID, securityTicket);
                foreach (var doctor_practice in last_used_practices_doctors)
                {
                    last_used_doctors.Add(new AutocompleteModel()
                    {
                        display_name = doctor_practice.display_name.Replace("()", "(-)"),
                        id           = Guid.Parse(doctor_practice.id),
                        type         = "LABEL_LAST_USED"
                    });
                }

                if (!string.IsNullOrEmpty(search_criteria))
                {
                    var doctors_practices_from_elastic = Get_Practices_and_Doctors.Get_AC_Doctors_and_Practices(new Practice_Parameter()
                    {
                        role            = "ac",
                        search_criteria = search_criteria,
                        sortfield       = "name_untouched",
                        ascending       = true,
                        practice_id     = data.PracticeID
                    }, securityTicket);
                    doctors = doctors_practices_from_elastic.Select(doc =>
                    {
                        var bsnr_lanr = doc.bsnr_lanr == "" ? "-" : doc.bsnr_lanr;
                        return(new AutocompleteModel()
                        {
                            display_name = doc.autocomplete_name + " (" + bsnr_lanr + ")",
                            id = Guid.Parse(doc.id),
                            type = doc.type,
                            secondary_display_name = doc.practice_name_for_doctor
                        });
                    }).ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.LogDocAppInfo(new LogEntry(ipInfo.address, ipInfo.agent, connectionString, method, securityTicket, ex), data.PracticeName);

                transaction.ReturnMessage = new List <string>();
                string errorMessage = ex.Message;
                transaction.ReturnStatus = false;
                transaction.ReturnMessage.Add(errorMessage);
                transaction.IsAuthenicated = true;
                transaction.IsException    = true;
            }

            autocomplete_model.last_used = last_used_doctors;
            autocomplete_model.doctors   = doctors;
            return(autocomplete_model);
        }
        protected static FR_Guids Execute(DbConnection Connection, DbTransaction Transaction, P_CAS_SCME_1741 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_Guids();
            //Put your code here


            ORM_CMN_Language.Query all_languagesQ = new ORM_CMN_Language.Query();
            all_languagesQ.Tenant_RefID = securityTicket.TenantID;
            all_languagesQ.IsDeleted    = false;

            var all_languagesL = ORM_CMN_Language.Query.Search(Connection, Transaction, all_languagesQ).ToArray();

            var treatment_doctor_details = cls_Get_Doctor_Details_for_DoctorID.Invoke(Connection, Transaction, new P_DO_GDDfDID_0823()
            {
                DoctorID = Parameter.treatment_doctor_id
            }, securityTicket).Result.SingleOrDefault();
            var aftercare_doctor_details = cls_Get_Doctor_Details_for_DoctorID.Invoke(Connection, Transaction, new P_DO_GDDfDID_0823()
            {
                DoctorID = Parameter.aftercare_doctor_id
            }, securityTicket).Result.SingleOrDefault();
            var aftercare_practice_details = cls_Get_Practice_Details_for_PracticeID.Invoke(Connection, Transaction, new P_DO_GPDfPID_1432()
            {
                PracticeID = Parameter.aftercare_doctor_id
            }, securityTicket).Result.FirstOrDefault();
            List <string> aftercare_ids           = new List <string>();
            List <string> withdrawn_aftercare_ids = new List <string>();
            List <string> new_aftercare_ids       = new List <string>();

            ORM_USR_Account treatment_doctor_account = null;
            if (Parameter.treatment_doctor_id != Guid.Empty)
            {
                treatment_doctor_account = ORM_USR_Account.Query.Search(Connection, Transaction, new ORM_USR_Account.Query()
                {
                    USR_AccountID = cls_Get_Doctor_AccountID_for_DoctorID.Invoke(Connection, Transaction, new P_DO_GDAIDfDID_1549()
                    {
                        DoctorID = Parameter.treatment_doctor_id
                    }, securityTicket).Result.accountID,
                    Tenant_RefID = securityTicket.TenantID,
                    IsDeleted    = false
                }).SingleOrDefault();
            }

            ORM_USR_Account aftercare_doctor_account = null;
            if (Parameter.aftercare_doctor_id != Guid.Empty)
            {
                if (aftercare_doctor_details != null)
                {
                    aftercare_doctor_account = ORM_USR_Account.Query.Search(Connection, Transaction, new ORM_USR_Account.Query()
                    {
                        USR_AccountID = cls_Get_Doctor_AccountID_for_DoctorID.Invoke(Connection, Transaction, new P_DO_GDAIDfDID_1549()
                        {
                            DoctorID = Parameter.aftercare_doctor_id
                        }, securityTicket).Result.accountID,
                        Tenant_RefID = securityTicket.TenantID,
                        IsDeleted    = false
                    }).SingleOrDefault();
                }
                else
                {
                    aftercare_doctor_account = ORM_USR_Account.Query.Search(Connection, Transaction, new ORM_USR_Account.Query()
                    {
                        USR_AccountID = cls_Get_Practice_AccountID_for_PracticeID.Invoke(Connection, Transaction, new P_DO_GPAIDfPID_1351()
                        {
                            PracticeID = Parameter.aftercare_doctor_id
                        }, securityTicket).Result.accountID,
                        Tenant_RefID = securityTicket.TenantID,
                        IsDeleted    = false
                    }).SingleOrDefault();
                }
            }

            foreach (var case_id in Parameter.case_ids)
            {
                if (treatment_doctor_details != null && Parameter.is_treatment)
                {
                    #region UPDATE TREATMENT DOCTOR
                    var treatment_planned_action_id = cls_Get_Treatment_Planned_Action_for_CaseID.Invoke(Connection, Transaction, new P_CAS_GTPAfCID_0946()
                    {
                        CaseID = case_id
                    }, securityTicket).Result;
                    if (treatment_planned_action_id != null)
                    {
                        var treatment_planned_action = ORM_HEC_ACT_PlannedAction.Query.Search(Connection, Transaction, new ORM_HEC_ACT_PlannedAction.Query()
                        {
                            HEC_ACT_PlannedActionID = treatment_planned_action_id.planned_action_id,
                            Tenant_RefID            = securityTicket.TenantID,
                            IsDeleted   = false,
                            IsCancelled = false
                        }).SingleOrDefault();

                        if (treatment_planned_action != null)
                        {
                            treatment_planned_action.Modification_Timestamp = DateTime.Now;
                            treatment_planned_action.ToBePerformedBy_BusinessParticipant_RefID = treatment_doctor_account.BusinessParticipant_RefID;

                            treatment_planned_action.Save(Connection, Transaction);
                        }
                    }
                    #endregion
                }

                #region UPDATE AFTERCARE DOCTOR
                if (!string.IsNullOrEmpty(Parameter.aftercare_performed_date) || aftercare_doctor_details != null || aftercare_practice_details != null)
                {
                    var aftercare_planned_action_id = cls_Get_Aftercare_Planned_Action_for_CaseID.Invoke(Connection, Transaction, new P_CAS_GAPAfCID_0959()
                    {
                        CaseID = case_id
                    }, securityTicket).Result;
                    if (aftercare_planned_action_id != null)
                    {
                        aftercare_ids.Add(aftercare_planned_action_id.planned_action_id.ToString());
                        if (aftercare_doctor_details != null || aftercare_practice_details != null)
                        {
                            var aftercare_planned_action = ORM_HEC_ACT_PlannedAction.Query.Search(Connection, Transaction, new ORM_HEC_ACT_PlannedAction.Query()
                            {
                                HEC_ACT_PlannedActionID = aftercare_planned_action_id.planned_action_id,
                                Tenant_RefID            = securityTicket.TenantID,
                                IsDeleted   = false,
                                IsCancelled = false
                            }).SingleOrDefault();

                            if (aftercare_planned_action != null)
                            {
                                if (aftercare_planned_action.ToBePerformedBy_BusinessParticipant_RefID != Guid.Empty)
                                {
                                    if (aftercare_doctor_account.BusinessParticipant_RefID != aftercare_planned_action.ToBePerformedBy_BusinessParticipant_RefID)
                                    {
                                        bool is_current_aftercare_practice = false;
                                        CAS_GPIDfPBPTID_1336 current_aftercare_practice = null;
                                        var current_aftercare_doctor = cls_Get_PracticeID_for_Doctor_BusinessParticipantID.Invoke(
                                            Connection,
                                            Transaction,
                                            new P_CAS_GPIDfDBPTID_1205()
                                        {
                                            BusinessParticipantID = aftercare_planned_action.ToBePerformedBy_BusinessParticipant_RefID
                                        },
                                            securityTicket).Result;

                                        if (current_aftercare_doctor == null)
                                        {
                                            current_aftercare_practice = cls_Get_PracticeID_for_Practice_BusinessParticipantID.Invoke(Connection, Transaction, new P_CAS_GPIDfPBPTID_1336()
                                            {
                                                BusinessParticipantID = aftercare_planned_action.ToBePerformedBy_BusinessParticipant_RefID
                                            }, securityTicket).Result;
                                            is_current_aftercare_practice = true;
                                        }

                                        var new_aftercare_practice_id     = aftercare_doctor_details != null ? aftercare_doctor_details.practice_id : aftercare_practice_details.practiceID;
                                        var current_aftercare_practice_id = is_current_aftercare_practice ? current_aftercare_practice.practice_id : current_aftercare_doctor.practice_id;

                                        if (new_aftercare_practice_id != current_aftercare_practice_id)
                                        {
                                            aftercare_planned_action.IsCancelled            = true;
                                            aftercare_planned_action.Modification_Timestamp = DateTime.Now;
                                            aftercare_planned_action.Save(Connection, Transaction);

                                            withdrawn_aftercare_ids.Add(aftercare_planned_action.HEC_ACT_PlannedActionID.ToString());

                                            #region NEW AFTERCARE PLANNED ACTION

                                            #region DELETE CURRENT PLANNED ACTION TO CASE
                                            ORM_HEC_CAS_Case_RelevantPlannedAction.Query current_aftercare_action_2_caseQ = new ORM_HEC_CAS_Case_RelevantPlannedAction.Query();
                                            current_aftercare_action_2_caseQ.Case_RefID          = case_id;
                                            current_aftercare_action_2_caseQ.PlannedAction_RefID = aftercare_planned_action_id.planned_action_id;
                                            current_aftercare_action_2_caseQ.Tenant_RefID        = securityTicket.TenantID;
                                            current_aftercare_action_2_caseQ.IsDeleted           = false;

                                            var current_aftercare_action_2_case = ORM_HEC_CAS_Case_RelevantPlannedAction.Query.Search(Connection, Transaction, current_aftercare_action_2_caseQ).SingleOrDefault();
                                            if (current_aftercare_action_2_case != null)
                                            {
                                                current_aftercare_action_2_case.IsDeleted = true;
                                                current_aftercare_action_2_case.Save(Connection, Transaction);
                                            }
                                            #endregion

                                            var new_aftercare_id = cls_Create_Aftercare_Planned_Action.Invoke(Connection, Transaction, new P_CAS_CAPA_1237()
                                            {
                                                aftercare_doctor_practice_id = Parameter.aftercare_doctor_id,
                                                all_languagesL = all_languagesL,
                                                case_id        = case_id,
                                                patient_id     = aftercare_planned_action.Patient_RefID,
                                                practice_id    = Parameter.practice_id,
                                                treatment_date = string.IsNullOrEmpty(Parameter.aftercare_performed_date) ? DateTime.Now : DateTime.ParseExact(Parameter.aftercare_performed_date, "dd.MM.yyyy", new System.Globalization.CultureInfo("de", true))
                                            }, securityTicket).Result;

                                            new_aftercare_ids.Add(new_aftercare_id.ToString());

                                            #endregion NEW AFTERCARE PLANNED ACTION
                                        }
                                        else
                                        {
                                            aftercare_planned_action.Modification_Timestamp = DateTime.Now;
                                            aftercare_planned_action.ToBePerformedBy_BusinessParticipant_RefID = aftercare_doctor_account.BusinessParticipant_RefID;

                                            aftercare_planned_action.Save(Connection, Transaction);
                                        }
                                    }
                                }
                                else
                                {
                                    aftercare_planned_action.Modification_Timestamp = DateTime.Now;
                                    aftercare_planned_action.ToBePerformedBy_BusinessParticipant_RefID = aftercare_doctor_account.BusinessParticipant_RefID;

                                    aftercare_planned_action.Save(Connection, Transaction);
                                }
                            }
                        }
                    }
                    else
                    {
                        var treatment_planned_action_id = cls_Get_Treatment_Planned_Action_for_CaseID.Invoke(Connection, Transaction, new P_CAS_GTPAfCID_0946()
                        {
                            CaseID = case_id
                        }, securityTicket).Result;
                        if (treatment_planned_action_id != null)
                        {
                            var treatment_planned_action = ORM_HEC_ACT_PlannedAction.Query.Search(Connection, Transaction, new ORM_HEC_ACT_PlannedAction.Query()
                            {
                                HEC_ACT_PlannedActionID = treatment_planned_action_id.planned_action_id,
                                Tenant_RefID            = securityTicket.TenantID,
                                IsDeleted   = false,
                                IsCancelled = false
                            }).SingleOrDefault();

                            var result = cls_Create_Aftercare_Planned_Action.Invoke(Connection, Transaction, new P_CAS_CAPA_1237()
                            {
                                aftercare_doctor_practice_id = Parameter.aftercare_doctor_id,
                                all_languagesL = ORM_CMN_Language.Query.Search(Connection, Transaction, new ORM_CMN_Language.Query()
                                {
                                    Tenant_RefID = securityTicket.TenantID, IsDeleted = false
                                }).ToArray(),
                                case_id        = case_id,
                                patient_id     = treatment_planned_action.Patient_RefID,
                                practice_id    = aftercare_doctor_details != null ? aftercare_doctor_details.practice_id : Parameter.aftercare_doctor_id,
                                treatment_date = treatment_planned_action.PlannedFor_Date
                            }, securityTicket);

                            aftercare_ids.Add(result.Result.ToString());
                        }
                    }
                }
                #endregion
            }

            #region UPDATE LAST USED AFTERCARES
            if (aftercare_doctor_details != null || aftercare_practice_details != null)
            {
                var aftercare_name = aftercare_doctor_details == null ? aftercare_practice_details.practice_name :
                                     GenericUtils.GetDoctorName(aftercare_doctor_details);

                var ac_practice_id = aftercare_doctor_details != null ? aftercare_doctor_details.practice_id : aftercare_practice_details.practiceID;

                var last_used_practices_doctors = Get_Practices_and_Doctors.Get_Last_Used_Doctors_Practices(Guid.Empty, securityTicket);
                if (last_used_practices_doctors.Count != 0)
                {
                    last_used_practices_doctors = last_used_practices_doctors.OrderBy(l => l.date_of_use).ToList();
                    var last_used = last_used_practices_doctors.SingleOrDefault(l => l.id.ToLower().Equals(Parameter.aftercare_doctor_id.ToString().ToLower()));
                    if (last_used != null)
                    {
                        last_used.date_of_use = DateTime.Now;
                    }
                    else
                    {
                        Practice_Doctor_Last_Used_Model practice_last_used_model = new Practice_Doctor_Last_Used_Model();
                        practice_last_used_model.id           = Parameter.aftercare_doctor_id.ToString();
                        practice_last_used_model.display_name = aftercare_name;
                        practice_last_used_model.date_of_use  = DateTime.Now;
                        practice_last_used_model.practice_id  = ac_practice_id.ToString();

                        last_used_practices_doctors.Add(practice_last_used_model);
                    }
                }
                else
                {
                    Practice_Doctor_Last_Used_Model practice_last_used_model = new Practice_Doctor_Last_Used_Model();
                    practice_last_used_model.id           = Parameter.aftercare_doctor_id.ToString();
                    practice_last_used_model.display_name = aftercare_name;
                    practice_last_used_model.date_of_use  = DateTime.Now;
                    practice_last_used_model.practice_id  = ac_practice_id.ToString();

                    last_used_practices_doctors.Add(practice_last_used_model);
                }

                Add_New_Practice_Last_Used.Import_Practice_Last_Used_Data_to_ElasticDB(last_used_practices_doctors, securityTicket.TenantID.ToString(), securityTicket.AccountID.ToString());

                last_used_practices_doctors = Get_Practices_and_Doctors.Get_Last_Used_Doctors_Practices(Guid.Empty, securityTicket);

                if (last_used_practices_doctors.Count() > 3)
                {
                    var id_to_delete = last_used_practices_doctors.OrderBy(pd => pd.date_of_use).First().id;
                    Add_New_Practice_Last_Used.Delete_Practice_Last_Used(securityTicket.TenantID.ToString(), "user_" + securityTicket.AccountID.ToString(), id_to_delete);
                }
            }
            #endregion

            returnValue.Result = Parameter.case_ids;
            return(returnValue);

            #endregion UserCode
        }
Example #3
0
        public static IEnumerable <IEnumerable <IElasticMapper> > GetUpdatedData(string index_name, string id, string type, string[] values)
        {
            var planning_cases  = new List <Case_Model>();
            var open_aftercares = new List <Aftercare_Model>();
            var error_correction_settlements = new List <Settlement_Model>();
            var error_correction_treatments  = new List <Submitted_Case_Model>();
            var patients        = new List <Patient_Model>();
            var patient_details = new List <PatientDetailViewModel>();
            var orders          = new List <Order_Model>();
            var octs            = new List <Oct_Model>();

            switch (type)
            {
            case "doctor":

                #region UPDATE PLANNING CASES
                var cases_from_elastic = Get_Cases.GetCasesWhereIDPresent <Case_Model>(id, "treatment_doctor_id", index_name, "case");
                if (cases_from_elastic.Any())
                {
                    planning_cases = cases_from_elastic.Select(cas =>
                    {
                        cas.treatment_doctor_name = values[0];
                        cas.treatment_doctor_lanr = values[1];

                        return(cas);
                    }).ToList();
                }

                cases_from_elastic = Get_Cases.GetCasesWhereIDPresent <Case_Model>(id, "aftercare_doctor_practice_id", index_name, "case");
                if (cases_from_elastic.Any())
                {
                    planning_cases.AddRange(cases_from_elastic.Select(cas =>
                    {
                        planning_cases.RemoveAll(pc => pc.id == cas.id);

                        cas.treatment_doctor_name = values[0];
                        cas.treatment_doctor_lanr = values[1];


                        cas.aftercare_name        = values[0];
                        cas.aftercare_doctor_lanr = values[1];

                        return(cas);
                    }).ToList());
                }
                #endregion

                #region UPDATE AFTERCARES
                var aftercares_from_elastic = Get_Cases.GetCasesWhereIDPresent <Aftercare_Model>(id, "treatment_doctor_id", index_name, "aftercare");
                if (aftercares_from_elastic.Any())
                {
                    open_aftercares = aftercares_from_elastic.Select(ac =>
                    {
                        ac.treatment_doctor_name = values[0];
                        ac.op_lanr = values[1];

                        return(ac);
                    }).ToList();
                }

                aftercares_from_elastic = Get_Cases.GetCasesWhereIDPresent <Aftercare_Model>(id, "aftercare_doctor_practice_id", index_name, "aftercare");
                if (aftercares_from_elastic.Any())
                {
                    open_aftercares.AddRange(aftercares_from_elastic.Select(ac =>
                    {
                        open_aftercares.RemoveAll(oac => oac.id == ac.id);

                        ac.treatment_doctor_name = values[0];
                        ac.ac_lanr = values[1];


                        ac.aftercare_doctor_name = values[0];

                        return(ac);
                    }).ToList());
                }
                #endregion

                #region UPDATE SETTLEMENTS IN ERROR CORRECTION
                var settlements_from_elastic = Get_Settlement.GetCasesWhereIDPresent(id, "treatment_doctor_id", index_name, "op");
                if (settlements_from_elastic.Any())
                {
                    error_correction_settlements = settlements_from_elastic.Select(cas =>
                    {
                        cas.doctor = values[0];
                        cas.lanr   = values[1];

                        return(cas);
                    }).ToList();
                }

                settlements_from_elastic = Get_Settlement.GetCasesWhereIDPresent(id, "aftercare_doctor_practice_id", index_name, "ac");
                if (settlements_from_elastic.Any())
                {
                    error_correction_settlements.AddRange(settlements_from_elastic.Select(cas =>
                    {
                        error_correction_settlements.RemoveAll(ecs => ecs.id == cas.id);

                        cas.doctor = values[0];
                        cas.lanr   = values[1];


                        cas.doctor = values[0];
                        cas.lanr   = values[1];

                        return(cas);
                    }).ToList());
                }
                #endregion

                #region UPDATE MM TREATMENTS IN ERROR CORRECTION
                var treatments_from_elastic = Get_Cases.GetSubmittedCasesInErrorCorrectionAndWhereIDPresent(id, "doctor_id", index_name, "op");
                if (treatments_from_elastic.Any())
                {
                    error_correction_treatments = treatments_from_elastic.Select(cas =>
                    {
                        cas.doctor_name = values[0];
                        cas.doctor_lanr = values[1];

                        return(cas);
                    }).ToList();
                }

                treatments_from_elastic = Get_Cases.GetSubmittedCasesInErrorCorrectionAndWhereIDPresent(id, "doctor_id", index_name, "ac");
                if (treatments_from_elastic.Any())
                {
                    error_correction_treatments.AddRange(treatments_from_elastic.Select(cas =>
                    {
                        error_correction_treatments.RemoveAll(ect => ect.id == cas.id);

                        cas.doctor_name = values[0];
                        cas.doctor_lanr = values[1];


                        cas.doctor_name = values[0];
                        cas.doctor_lanr = values[1];

                        return(cas);
                    }).ToList());
                }
                #endregion

                #region UPDATE ORDERS
                var orders_from_elastic = Get_Orders.GetOrdersWhereForIdAndOrdinal(id, "doctor_id", index_name);
                if (orders_from_elastic.Any())
                {
                    orders = orders_from_elastic.Select(ord =>
                    {
                        ord.treatment_doctor_name = values[0];
                        ord.lanr = values[1];

                        return(ord);
                    }).ToList();
                }
                #endregion

                #region UPDATE PATIENTS
                var patients_from_elastic = Retrieve_Patients.GetPatientsWhereIDPresent(id, "op_doctor_id", index_name);
                if (patients_from_elastic.Any())
                {
                    patients = patients_from_elastic.Select(patient =>
                    {
                        patient.last_first_op_doctor_name = values[0];
                        patient.op_doctor_lanr            = values[1];

                        return(patient);
                    }).ToList();
                }

                patients_from_elastic = Retrieve_Patients.GetPatientsWhereIDPresent(id, "ac_doctor_id", index_name);
                if (patients_from_elastic.Any())
                {
                    patients.AddRange(patients_from_elastic.Select(patient =>
                    {
                        patients.RemoveAll(pat => pat.id == patient.id);

                        patient.last_first_op_doctor_name = values[0];
                        patient.op_doctor_lanr            = values[1];
                        patient.last_first_ac_doctor_name = values[0];
                        patient.ac_doctor_lanr            = values[1];

                        return(patient);
                    }).ToList());
                }
                #endregion

                #region UPDATE PATIENT DETAILS
                var patients_details_from_elastic = Retrieve_Patients.GetPatientDetailsForIdAndOrdinal(id, "treatment_doctor_id", index_name);
                if (patients_details_from_elastic.Any())
                {
                    patient_details = patients_details_from_elastic.Select(patient =>
                    {
                        patient.doctor = values[0];

                        return(patient);
                    }).ToList();
                }

                patients_details_from_elastic = Retrieve_Patients.GetPatientDetailsForIdAndOrdinal(id, "aftercare_doctor_practice_id", index_name);
                if (patients_details_from_elastic.Any())
                {
                    patient_details.AddRange(patients_details_from_elastic.Select(patient =>
                    {
                        patient.doctor = values[0];

                        return(patient);
                    }).ToList());
                }
                #endregion

                #region UPDATE OCT

                octs.AddRange(Retrieve_Octs.GetOctsWhereFieldsHaveValues(new List <FieldValueParameter>()
                {
                    new FieldValueParameter()
                    {
                        FieldName = "oct_doctor_id", FieldValue = id
                    },
                    new FieldValueParameter()
                    {
                        FieldName = "treatment_doctor_id", FieldValue = id, Negation = true
                    }
                }, null, index_name)
                              .Select(t =>
                {
                    t.oct_doctor_name = values[0];

                    return(t);
                }).ToList());

                octs.AddRange(Retrieve_Octs.GetOctsWhereFieldsHaveValues(new List <FieldValueParameter>()
                {
                    new FieldValueParameter()
                    {
                        FieldName = "treatment_doctor_id", FieldValue = id
                    },
                    new FieldValueParameter()
                    {
                        FieldName = "oct_doctor_id", FieldValue = id, Negation = true
                    }
                }, null, index_name)
                              .Select(t =>
                {
                    t.treatment_doctor_name = values[0];

                    return(t);
                }).ToList());

                octs.AddRange(Retrieve_Octs.GetOctsWhereFieldsHaveValues(new List <FieldValueParameter>()
                {
                    new FieldValueParameter()
                    {
                        FieldName = "treatment_doctor_id", FieldValue = id
                    },
                    new FieldValueParameter()
                    {
                        FieldName = "oct_doctor_id", FieldValue = id
                    }
                }, null, index_name)
                              .Select(t =>
                {
                    t.oct_doctor_name       = values[0];
                    t.treatment_doctor_name = values[0];

                    return(t);
                }).ToList());

                #endregion

                #region UPDATE AC DOCTORS
                var types = Elastic_Utils.GetAllTypes(index_name);

                HashSet <string> last_used_types = new HashSet <string>();

                var     newTypes     = types.Replace(index_name, "allData");
                dynamic data         = JObject.Parse(newTypes);
                var     mappings     = data.allData.mappings;
                var     neededLength = Guid.Empty.ToString().Length + 5;
                var     elasticType  = "user_";

                foreach (var acType in mappings)
                {
                    if (acType.Name.Contains(elasticType) && acType.Name.Length == neededLength)
                    {
                        var dataForType = Get_Practices_and_Doctors.GetACDoctorsWhereIDPresent(id, "id", index_name, acType.Name);

                        foreach (var acDoc in dataForType)
                        {
                            acDoc.display_name = String.Format("{0} ({1})", values[0], values[1]);
                        }

                        var accountID = acType.Name.Split(new string[] { elasticType }, StringSplitOptions.None)[1];
                        UpdateAftercareDoctor(dataForType, index_name, accountID, elasticType);
                    }
                }
                ;
                #endregion

                break;

            case "practice":

                #region UPDATE PLANNING CASES
                var practice_cases_from_elastic = Get_Cases.GetCasesWhereIDPresent <Case_Model>(id, "aftercare_doctors_practice_id", index_name, "case");
                if (practice_cases_from_elastic.Any())
                {
                    planning_cases = practice_cases_from_elastic.Select(cas =>
                    {
                        cas.aftercare_doctors_practice_name = values[0];
                        cas.aftercare_practice_bsnr         = values[1];

                        return(cas);
                    }).ToList();
                }

                practice_cases_from_elastic = Get_Cases.GetCasesWhereIDPresent <Case_Model>(id, "aftercare_doctor_practice_id", index_name, "case");
                if (practice_cases_from_elastic.Any())
                {
                    planning_cases.AddRange(practice_cases_from_elastic.Select(cas =>
                    {
                        planning_cases.RemoveAll(pc => pc.id == cas.id);

                        cas.aftercare_doctors_practice_name = values[0];
                        cas.aftercare_practice_bsnr         = values[1];


                        cas.aftercare_name          = values[0];
                        cas.aftercare_practice_bsnr = values[1];

                        return(cas);
                    }).ToList());
                }
                #endregion

                #region UPDATE AFTERCARES
                var practice_aftercares_from_elastic = Get_Cases.GetCasesWhereIDPresent <Aftercare_Model>(id, "treatment_doctors_practice_id", index_name, "aftercare");
                if (practice_aftercares_from_elastic.Any())
                {
                    open_aftercares = practice_aftercares_from_elastic.Select(ac =>
                    {
                        ac.treatment_doctor_practice_name = values[0];
                        ac.bsnr = values[1];

                        return(ac);
                    }).ToList();
                }
                #endregion

                #region UPDATE MM TREATMENTS IN ERROR CORRECTION
                var practice_treatments_from_elastic = Get_Cases.GetSubmittedCasesInErrorCorrectionAndWhereIDPresent(id, "practice_id", index_name);
                if (practice_treatments_from_elastic.Any())
                {
                    error_correction_treatments = practice_treatments_from_elastic.Select(cas =>
                    {
                        cas.practice_name = values[0];
                        cas.practice_bsnr = values[1];

                        return(cas);
                    }).ToList();
                }
                #endregion

                #region UPDATE ORDERS
                var practice_orders_from_elastic = Get_Orders.GetOrdersWhereIDPresent(id, "practice_id", index_name);
                if (practice_orders_from_elastic.Any())
                {
                    orders = practice_orders_from_elastic.Select(ord =>
                    {
                        ord.treatment_doctor_practice_name = values[0];
                        ord.bsnr = values[1];

                        return(ord);
                    }).ToList();
                }
                #endregion

                #region UPDATE PATIENTS
                var pratice_patients_from_elastic = Retrieve_Patients.GetPatientsWhereIDPresent(id, "practice_id", index_name);
                if (pratice_patients_from_elastic.Any())
                {
                    patients = pratice_patients_from_elastic.Select(patient =>
                    {
                        patient.practice      = values[0];
                        patient.practice_bsnr = values[1];

                        return(patient);
                    }).ToList();
                }

                pratice_patients_from_elastic = Retrieve_Patients.GetPatientsWhereIDPresent(id, "ac_practice_id", index_name);
                if (pratice_patients_from_elastic.Any())
                {
                    patients.AddRange(pratice_patients_from_elastic.Select(patient =>
                    {
                        patients.RemoveAll(pat => pat.id == patient.id);

                        patient.practice      = values[0];
                        patient.practice_bsnr = values[1];


                        patient.ac_practice      = values[0];
                        patient.ac_practice_bsnr = values[1];

                        return(patient);
                    }).ToList());
                }
                #endregion

                break;

            case "patient":
                IFormatProvider culture = new System.Globalization.CultureInfo("de", true);

                #region UPDATE PLANNING CASES
                var patient_cases_from_elastic = Get_Cases.GetCasesWhereIDPresent <Case_Model>(id, "patient_id", index_name, "case");
                if (patient_cases_from_elastic.Any())
                {
                    planning_cases = patient_cases_from_elastic.Select(cas =>
                    {
                        cas.patient_name             = values[0];
                        cas.patient_insurance_number = values[1];
                        cas.patient_birthdate        = DateTime.Parse(values[2], culture, System.Globalization.DateTimeStyles.AssumeLocal);
                        cas.patient_birthdate_string = values[2];

                        return(cas);
                    }).ToList();
                }
                #endregion

                #region UPDATE AFTERCARES
                var patient_aftercares_from_elastic = Get_Cases.GetCasesWhereIDPresent <Aftercare_Model>(id, "patient_id", index_name, "aftercare");
                if (patient_aftercares_from_elastic.Any())
                {
                    open_aftercares = patient_aftercares_from_elastic.Select(ac =>
                    {
                        ac.patient_name             = values[0];
                        ac.patient_insurance_number = values[1];
                        ac.patient_birthdate        = DateTime.Parse(values[2], culture, System.Globalization.DateTimeStyles.AssumeLocal);
                        ac.patient_birthdate_string = values[2];

                        return(ac);
                    }).ToList();
                }
                #endregion

                #region UPDATE SETTLEMENTS IN ERROR CORRECTION
                var patient_settlements_from_elastic = Get_Settlement.GetCasesWhereIDPresent(id, "patient_id", index_name);
                if (patient_settlements_from_elastic.Any())
                {
                    error_correction_settlements = patient_settlements_from_elastic.Select(cas =>
                    {
                        cas.patient_full_name        = values[0];
                        cas.patient_insurance_number = values[1];
                        cas.birthday = values[2];

                        return(cas);
                    }).ToList();
                }
                #endregion

                #region UPDATE MM TREATMENTS IN ERROR CORRECTION
                var patient_treatments_from_elastic = Get_Cases.GetSubmittedCasesInErrorCorrectionAndWhereIDPresent(id, "patient_id", index_name);
                if (patient_treatments_from_elastic.Any())
                {
                    error_correction_treatments = patient_treatments_from_elastic.Select(cas =>
                    {
                        cas.patient_name             = values[0];
                        cas.patient_insurance_number = values[1];
                        cas.patient_birthdate        = DateTime.Parse(values[2], culture, System.Globalization.DateTimeStyles.AssumeLocal);
                        cas.patient_birthdate_string = values[2];

                        return(cas);
                    }).ToList();
                }
                #endregion

                #region UPDATE ORDERS
                var patient_orders_from_elastic = Get_Orders.GetOrdersWhereIDPresent(id, "patient_id", index_name);
                if (patient_orders_from_elastic.Any())
                {
                    orders = patient_orders_from_elastic.Select(ord =>
                    {
                        ord.patient_name             = values[0];
                        ord.patient_insurance_number = values[1];
                        ord.patient_birthdate        = DateTime.Parse(values[2], culture, System.Globalization.DateTimeStyles.AssumeLocal);
                        ord.patient_birthdate_string = values[2];

                        return(ord);
                    }).ToList();
                }
                #endregion

                #region UPDATE OCT
                octs = Retrieve_Octs.GetOctsWhereFieldsHaveValues(new List <FieldValueParameter>()
                {
                    new FieldValueParameter()
                    {
                        FieldName = "patient_id", FieldValue = id
                    },
                    new FieldValueParameter()
                    {
                        FieldName = "status.lower_case_sort", FieldValue = "oct1"
                    },
                }, null, index_name)
                       .Select(t =>
                {
                    t.patient_name             = values[0];
                    t.patient_insurance_number = values[1];
                    t.patient_birthdate        = DateTime.Parse(values[2], culture, System.Globalization.DateTimeStyles.AssumeLocal);

                    return(t);
                }).ToList();
                #endregion

                break;

            case "drug":

                #region UPDATE PLANNING CASES
                var drug_cases_from_elastic = Get_Cases.GetCasesWhereIDPresent <Case_Model>(id, "drug_id", index_name, "case");
                if (drug_cases_from_elastic.Any())
                {
                    planning_cases = drug_cases_from_elastic.Select(cas =>
                    {
                        cas.drug = values[0];

                        return(cas);
                    }).ToList();
                }
                #endregion

                #region UPDATE SETTLEMENTS IN ERROR CORRECTION
                var drug_settlements_from_elastic = Get_Settlement.GetCasesWhereIDPresent(id, "drug_id", index_name);
                if (drug_settlements_from_elastic.Any())
                {
                    error_correction_settlements = drug_settlements_from_elastic.Select(cas =>
                    {
                        cas.drug = values[0];

                        return(cas);
                    }).ToList();
                }
                #endregion

                #region UPDATE MM TREATMENTS IN ERROR CORRECTION
                var drug_treatments_from_elastic = Get_Cases.GetSubmittedCasesInErrorCorrectionAndWhereIDPresent(id, "drug_id", index_name);
                if (drug_treatments_from_elastic.Any())
                {
                    error_correction_treatments = drug_treatments_from_elastic.Select(cas =>
                    {
                        cas.drug = values[0];

                        return(cas);
                    }).ToList();
                }
                #endregion

                #region UPDATE ORDERS
                var drug_orders_from_elastic = Get_Orders.GetOrdersWhereIDPresent(id, "drug_id", index_name);
                if (drug_orders_from_elastic.Any())
                {
                    orders = drug_orders_from_elastic.Select(ord =>
                    {
                        ord.drug = values[0];

                        return(ord);
                    }).ToList();
                }
                #endregion

                #region UPDATE PATIENT DETAILS
                var drug_patients_details_from_elastic = Retrieve_Patients.GetPatientDetailsWhereIDPresent(id, "drug_id", index_name);
                if (drug_patients_details_from_elastic.Any())
                {
                    patient_details = drug_patients_details_from_elastic.Select(patient =>
                    {
                        patient.drug = values[0];
                        if (patient.detail_type == "order")
                        {
                            patient.diagnose_or_medication = values[0];
                        }

                        return(patient);
                    }).ToList();
                }
                #endregion

                break;

            case "diagnose":

                #region UPDATE PLANNING CASES
                var diagnose_cases_from_elastic = Get_Cases.GetCasesWhereIDPresent <Case_Model>(id, "diagnose_id", index_name, "case");
                if (diagnose_cases_from_elastic.Any())
                {
                    planning_cases = diagnose_cases_from_elastic.Select(cas =>
                    {
                        cas.diagnose = values[0];

                        return(cas);
                    }).ToList();
                }
                #endregion

                #region UPDATE SETTLEMENTS IN ERROR CORRECTION
                var diagnose_settlements_from_elastic = Get_Settlement.GetCasesWhereIDPresent(id, "diagnose_id", index_name);
                if (diagnose_settlements_from_elastic.Any())
                {
                    error_correction_settlements = diagnose_settlements_from_elastic.Select(cas =>
                    {
                        cas.diagnose = values[0];

                        return(cas);
                    }).ToList();
                }
                #endregion

                #region UPDATE MM TREATMENTS IN ERROR CORRECTION
                var diagnose_treatments_from_elastic = Get_Cases.GetSubmittedCasesInErrorCorrectionAndWhereIDPresent(id, "diagnose_id", index_name);
                if (diagnose_treatments_from_elastic.Any())
                {
                    error_correction_treatments = diagnose_treatments_from_elastic.Select(cas =>
                    {
                        cas.diagnose = values[0];

                        return(cas);
                    }).ToList();
                }
                #endregion

                #region UPDATE ORDERS
                var diagnose_orders_from_elastic = Get_Orders.GetOrdersWhereIDPresent(id, "diagnose_id", index_name);
                if (diagnose_orders_from_elastic.Any())
                {
                    orders = diagnose_orders_from_elastic.Select(ord =>
                    {
                        ord.diagnose = values[0];

                        return(ord);
                    }).ToList();
                }
                #endregion

                #region UPDATE PATIENT DETAILS
                var diagnose_patients_details_from_elastic = Retrieve_Patients.GetPatientDetailsWhereIDPresent(id, "diagnose_id", index_name);
                if (diagnose_patients_details_from_elastic.Any())
                {
                    patient_details = diagnose_patients_details_from_elastic.Select(patient =>
                    {
                        if (patient.detail_type == "op" || patient.detail_type == "ac")
                        {
                            patient.diagnose_or_medication = values[0];
                        }

                        return(patient);
                    }).ToList();
                }
                #endregion

                break;

            default:
                break;
            }

            return(new List <IEnumerable <IElasticMapper> >()
            {
                planning_cases,
                open_aftercares,
                error_correction_settlements,
                error_correction_treatments,
                orders,
                patients,
                patient_details,
                octs
            });
        }
Example #4
0
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_DO_MD_1321 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_Guid();
            //Put your code here
            returnValue.Result = Parameter.DoctorID;

            var         temporary_doctor_bpt_id = Guid.Empty;
            List <Guid> case_ids = new List <Guid>();
            var         BusinessParticipantID = Guid.Empty;

            #region PURGE TEMPORARY DOCTOR
            var temporary_doctor = ORM_HEC_Doctor.Query.Search(Connection, Transaction, new ORM_HEC_Doctor.Query()
            {
                HEC_DoctorID = Parameter.TemporaryDoctorID, IsDeleted = false, Tenant_RefID = securityTicket.TenantID
            }).SingleOrDefault();
            if (temporary_doctor != null)
            {
                temporary_doctor.IsDeleted = true;
                temporary_doctor.Save(Connection, Transaction);

                var temporary_doctor_bpt = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, new ORM_CMN_BPT_BusinessParticipant.Query()
                {
                    CMN_BPT_BusinessParticipantID = temporary_doctor.BusinessParticipant_RefID, IsDeleted = false, Tenant_RefID = securityTicket.TenantID
                }).SingleOrDefault();
                if (temporary_doctor_bpt != null)
                {
                    temporary_doctor_bpt_id        = temporary_doctor_bpt.CMN_BPT_BusinessParticipantID;
                    temporary_doctor_bpt.IsDeleted = true;
                    temporary_doctor_bpt.Save(Connection, Transaction);

                    var temporary_doctor_person_info = ORM_CMN_PER_PersonInfo.Query.Search(Connection, Transaction, new ORM_CMN_PER_PersonInfo.Query()
                    {
                        CMN_PER_PersonInfoID = temporary_doctor_bpt.IfNaturalPerson_CMN_PER_PersonInfo_RefID, IsDeleted = false, Tenant_RefID = securityTicket.TenantID
                    }).SingleOrDefault();
                    if (temporary_doctor_person_info != null)
                    {
                        temporary_doctor_person_info.IsDeleted = true;
                        temporary_doctor_person_info.Save(Connection, Transaction);

                        var temporary_doctor_communication_contact = ORM_CMN_PER_CommunicationContact.Query.Search(Connection, Transaction, new ORM_CMN_PER_CommunicationContact.Query()
                        {
                            PersonInfo_RefID = temporary_doctor_person_info.CMN_PER_PersonInfoID, IsDeleted = false, Tenant_RefID = securityTicket.TenantID
                        });
                        foreach (var tdc in temporary_doctor_communication_contact)
                        {
                            tdc.IsDeleted = true;
                            tdc.Save(Connection, Transaction);
                        }
                    }
                }

                var temporary_doctor_universal_property_value = ORM_HEC_Doctor_UniversalPropertyValue.Query.Search(Connection, Transaction, new ORM_HEC_Doctor_UniversalPropertyValue.Query()
                {
                    HEC_Doctor_RefID = temporary_doctor.HEC_DoctorID, IsDeleted = false, Tenant_RefID = securityTicket.TenantID
                }).SingleOrDefault();
                if (temporary_doctor_universal_property_value != null)
                {
                    temporary_doctor_universal_property_value.IsDeleted = true;
                    temporary_doctor_universal_property_value.Save(Connection, Transaction);

                    var temporary_doctor_universal_property = ORM_HEC_Doctor_UniversalProperty.Query.Search(Connection, Transaction, new ORM_HEC_Doctor_UniversalProperty.Query()
                    {
                        PropertyName = "Comment", HEC_Doctor_UniversalPropertyID = temporary_doctor_universal_property_value.HEC_Doctor_UniversalPropertyValueID, IsDeleted = false, Tenant_RefID = securityTicket.TenantID
                    }).SingleOrDefault();
                    if (temporary_doctor_universal_property != null)
                    {
                        temporary_doctor_universal_property.IsDeleted = true;
                        temporary_doctor_universal_property.Save(Connection, Transaction);
                    }
                }
            }
            #endregion

            #region GET MERGED DOCTOR BPT
            var merge_doctor = ORM_HEC_Doctor.Query.Search(Connection, Transaction, new ORM_HEC_Doctor.Query()
            {
                HEC_DoctorID = Parameter.DoctorID, Tenant_RefID = securityTicket.TenantID, IsDeleted = false
            }).SingleOrDefault();
            if (merge_doctor != null)
            {
                BusinessParticipantID = merge_doctor.BusinessParticipant_RefID;
            }
            else
            {
                throw new Exception("Doctor not found");
            }
            #endregion

            #region LINK AFTERCARES TO MERGED DOCTOR
            var aftercare_planned_actions = ORM_HEC_ACT_PlannedAction.Query.Search(Connection, Transaction, new ORM_HEC_ACT_PlannedAction.Query()
            {
                Tenant_RefID = securityTicket.TenantID,
                IsDeleted    = false,
                IsPerformed  = false,
                IsCancelled  = false,
                ToBePerformedBy_BusinessParticipant_RefID = temporary_doctor_bpt_id
            });

            foreach (var aftercare in aftercare_planned_actions)
            {
                aftercare.ToBePerformedBy_BusinessParticipant_RefID = BusinessParticipantID;
                aftercare.Modification_Timestamp = DateTime.Now;

                aftercare.Save(Connection, Transaction);

                var case_id = ORM_HEC_CAS_Case_RelevantPlannedAction.Query.Search(Connection, Transaction, new ORM_HEC_CAS_Case_RelevantPlannedAction.Query()
                {
                    PlannedAction_RefID = aftercare.HEC_ACT_PlannedActionID, IsDeleted = false, Tenant_RefID = securityTicket.TenantID
                }).SingleOrDefault();
                if (case_id != null)
                {
                    case_ids.Add(case_id.Case_RefID);
                }
            }
            #endregion

            #region UPDATE ELASTIC
            List <Case_Model>                    cases                         = new List <Case_Model>();
            List <Aftercare_Model>               aftercares                    = new List <Aftercare_Model>();
            List <PatientDetailViewModel>        patientDetailList             = new List <PatientDetailViewModel>();
            List <Settlement_Model>              settlements                   = new List <Settlement_Model>();
            Dictionary <Guid, CAS_GDDfDID_1608>  diagnose_data_cache           = new Dictionary <Guid, CAS_GDDfDID_1608>();
            Dictionary <Guid, CAS_GDDfDID_1614>  drug_data_cache               = new Dictionary <Guid, CAS_GDDfDID_1614>();
            Dictionary <Guid, DO_GDDfDID_0823>   treatment_doctor_data_cache   = new Dictionary <Guid, DO_GDDfDID_0823>();
            Dictionary <Guid, P_PA_GPDfPID_1124> patient_data_cache            = new Dictionary <Guid, P_PA_GPDfPID_1124>();
            Dictionary <Guid, DO_GPDfPID_1432>   treatment_practice_data_cache = new Dictionary <Guid, DO_GPDfPID_1432>();
            var merged_doctor_details = cls_Get_Doctor_Details_for_DoctorID.Invoke(Connection, Transaction, new P_DO_GDDfDID_0823()
            {
                DoctorID = Parameter.DoctorID
            }, securityTicket).Result.SingleOrDefault();

            var doctor_details = cls_Get_Doctor_Details_for_DoctorID.Invoke(Connection, Transaction, new P_DO_GDDfDID_0823()
            {
                DoctorID = Parameter.DoctorID
            }, securityTicket).Result.SingleOrDefault();
            if (doctor_details != null)
            {
                foreach (var case_id in case_ids)
                {
                    var treatment_planned_action = cls_Get_Treatment_Planned_Action_for_CaseID.Invoke(Connection, Transaction, new P_CAS_GTPAfCID_0946()
                    {
                        CaseID = case_id
                    }, securityTicket).Result;
                    if (treatment_planned_action != null)
                    {
                        #region IF CASE SUBMITTED, CREATE AFTERCARE
                        if (treatment_planned_action.is_treatment_performed)
                        {
                            var case_details = cls_Get_Case_Details_for_CaseID.Invoke(Connection, Transaction, new P_CAS_GCDfCID_1435()
                            {
                                CaseID = case_id
                            }, securityTicket).Result;

                            #region CACHE
                            if (!diagnose_data_cache.ContainsKey(case_details.diagnose_id))
                            {
                                diagnose_data_cache.Add(case_details.diagnose_id, cls_Get_Diagnose_Details_for_DiagnoseID.Invoke(Connection, Transaction, new P_CAS_GDDfDID_1608()
                                {
                                    DiagnoseID = case_details.diagnose_id
                                }, securityTicket).Result);
                            }
                            var diagnose_details = diagnose_data_cache[case_details.diagnose_id];

                            if (!drug_data_cache.ContainsKey(case_details.drug_id))
                            {
                                drug_data_cache.Add(case_details.drug_id, cls_Get_Drug_Details_for_DrugID.Invoke(Connection, Transaction, new P_CAS_GDDfDID_1614()
                                {
                                    DrugID = case_details.drug_id
                                }, securityTicket).Result);
                            }
                            var drug_details = drug_data_cache[case_details.drug_id];

                            if (!treatment_doctor_data_cache.ContainsKey(case_details.op_doctor_id))
                            {
                                treatment_doctor_data_cache.Add(case_details.op_doctor_id, cls_Get_Doctor_Details_for_DoctorID.Invoke(Connection, Transaction, new P_DO_GDDfDID_0823()
                                {
                                    DoctorID = case_details.op_doctor_id
                                }, securityTicket).Result.SingleOrDefault());
                            }
                            var treatment_doctor_details = treatment_doctor_data_cache[case_details.op_doctor_id];

                            if (!patient_data_cache.ContainsKey(case_details.patient_id))
                            {
                                patient_data_cache.Add(case_details.patient_id, cls_Get_Patient_Details_for_PatientID.Invoke(Connection, Transaction, new P_P_PA_GPDfPID_1124()
                                {
                                    PatientID = case_details.patient_id
                                }, securityTicket).Result);
                            }
                            var patient_details = patient_data_cache[case_details.patient_id];

                            if (!treatment_practice_data_cache.ContainsKey(case_details.practice_id))
                            {
                                treatment_practice_data_cache.Add(case_details.practice_id, cls_Get_Practice_Details_for_PracticeID.Invoke(Connection, Transaction, new P_DO_GPDfPID_1432()
                                {
                                    PracticeID = case_details.practice_id
                                }, securityTicket).Result.FirstOrDefault());
                            }
                            var treatment_practice_details = treatment_practice_data_cache[case_details.practice_id];

                            #endregion

                            if (case_details != null)
                            {
                                #region CREATE NEW AFTERCARE
                                Aftercare_Model aftercare = new Aftercare_Model();
                                aftercare.hip_ik_number         = patient_details.HealthInsurance_IKNumber;
                                aftercare.aftercare_doctor_name = MMDocConnectDocApp.GenericUtils.GetDoctorName(merged_doctor_details);
                                aftercare.diagnose                  = diagnose_details != null ? diagnose_details.diagnose_name + " (" + diagnose_details.catalog_display_name + ": " + diagnose_details.diagnose_icd_10 + ")" : "";
                                aftercare.id                        = case_details.aftercare_planned_action_id.ToString();
                                aftercare.localization              = case_details.localization;
                                aftercare.patient_birthdate         = case_details.Patient_BirthDate;
                                aftercare.patient_birthdate_string  = case_details.Patient_BirthDate.ToString("dd.MM.yyyy");
                                aftercare.patient_name              = patient_details != null ? patient_details.patient_last_name + ", " + patient_details.patient_first_name : "";
                                aftercare.practice_id               = merged_doctor_details.practice_id.ToString();
                                aftercare.status                    = "AC1";
                                aftercare.treatment_date            = case_details.treatment_date;
                                aftercare.treatment_date_day_month  = case_details.treatment_date.ToString("dd.MM.");
                                aftercare.treatment_date_month_year = case_details.treatment_date.ToString("MMMM yyyy", new System.Globalization.CultureInfo("de", true));
                                aftercare.treatment_doctor_name     = treatment_doctor_details != null?MMDocConnectDocApp.GenericUtils.GetDoctorName(treatment_doctor_details) : "-";

                                aftercare.treatment_doctor_practice_name = treatment_doctor_details.practice;
                                aftercare.case_id = case_id.ToString();
                                aftercare.hip     = patient_details.health_insurance_provider;
                                aftercare.patient_insurance_number = patient_details.insurance_id;
                                aftercare.op_lanr = treatment_doctor_details == null ? "" : treatment_doctor_details.lanr;
                                aftercare.ac_lanr = merged_doctor_details.lanr;
                                aftercare.bsnr    = treatment_doctor_details == null ? "" : treatment_doctor_details.BSNR;
                                aftercare.aftercare_doctor_account_id  = merged_doctor_details.doctor_account_id.ToString();
                                aftercare.aftercare_doctor_practice_id = Parameter.DoctorID.ToString();
                                aftercare.treatment_doctor_id          = treatment_doctor_details == null ? "" : treatment_doctor_details.id.ToString();
                                aftercare.diagnose_id = case_details.diagnose_id.ToString();
                                aftercare.drug_id     = case_details.drug_id.ToString();
                                aftercare.patient_id  = case_details.patient_id.ToString();
                                aftercare.treatment_doctors_practice_id = case_details.practice_id.ToString();

                                aftercares.Add(aftercare);

                                PatientDetailViewModel patientDetal_elastic = new PatientDetailViewModel();
                                patientDetal_elastic.id                           = aftercare.id;
                                patientDetal_elastic.drug_id                      = aftercare.drug_id;
                                patientDetal_elastic.practice_id                  = aftercare.practice_id;
                                patientDetal_elastic.case_id                      = aftercare.case_id;
                                patientDetal_elastic.date                         = aftercare.treatment_date;
                                patientDetal_elastic.date_string                  = aftercare.treatment_date.ToString("dd.MM.");
                                patientDetal_elastic.detail_type                  = "ac";
                                patientDetal_elastic.diagnose_or_medication       = aftercare.diagnose;
                                patientDetal_elastic.doctor                       = aftercare.aftercare_doctor_name;
                                patientDetal_elastic.localisation                 = aftercare.localization;
                                patientDetal_elastic.patient_id                   = aftercare.patient_id;
                                patientDetal_elastic.treatment_doctor_id          = aftercare.treatment_doctor_id;
                                patientDetal_elastic.aftercare_doctor_practice_id = aftercare.aftercare_doctor_practice_id;
                                patientDetal_elastic.diagnose_id                  = aftercare.diagnose_id;
                                patientDetal_elastic.status                       = "FS0";
                                patientDetal_elastic.hip_ik                       = patient_details.HealthInsurance_IKNumber;

                                patientDetailList.Add(patientDetal_elastic);
                                #endregion

                                #region Update settlement
                                var settlement = Get_Settlement.GetSettlementForID(case_details.treatment_planned_action_id.ToString(), securityTicket);
                                if (settlement != null)
                                {
                                    settlement.acpractice = merged_doctor_details.practice;
                                    settlement.aftercare_doctor_practice_id = merged_doctor_details.id.ToString();

                                    settlements.Add(settlement);
                                }
                                #endregion
                            }
                        }
                        #endregion

                        #region IF CASE NOT SUMBITTED, EDIT AFTERCARE DATA
                        else
                        {
                            var planning_case = Get_Cases.GetCaseforCaseID(case_id.ToString(), securityTicket);
                            planning_case.aftercare_doctor_lanr        = merge_doctor.DoctorIDNumber;
                            planning_case.aftercare_doctor_practice_id = merge_doctor.HEC_DoctorID.ToString();

                            var practice_details = cls_Get_Practice_Details_for_PracticeID.Invoke(Connection, Transaction, new P_DO_GPDfPID_1432()
                            {
                                PracticeID = doctor_details.practice_id
                            }, securityTicket).Result.FirstOrDefault();
                            if (practice_details != null)
                            {
                                planning_case.aftercare_doctors_practice_name = practice_details.practice_name;
                                planning_case.aftercare_name          = MMDocConnectDocApp.GenericUtils.GetDoctorName(doctor_details);
                                planning_case.aftercare_practice_bsnr = practice_details.practice_BSNR;
                                planning_case.is_aftercare_doctor     = true;
                            }

                            cases.Add(planning_case);
                        }
                        #endregion
                    }
                }

                #region UPDATE LAST USED AFTERCARE DOCTORS LIST
                var types  = Elastic_Utils.GetAllTypes(securityTicket.TenantID.ToString());
                var length = Guid.Empty.ToString().Length;
                length += 5;
                List <string> last_used_types = new List <string>();
                for (int i = 0; i < types.Length; i += length)
                {
                    int index = types.IndexOf("user_", i);
                    if (index != -1 && !last_used_types.Contains(types.Substring(index, length)))
                    {
                        last_used_types.Add(types.Substring(index, length));
                    }
                }

                foreach (var type in last_used_types)
                {
                    Practice_Doctor_Last_Used_Model new_last_used = new Practice_Doctor_Last_Used_Model();
                    try
                    {
                        var last_used = Get_Practices_and_Doctors.GetLastUsedDoctorPracticeForID(Parameter.TemporaryDoctorID.ToString(), type, securityTicket);

                        new_last_used.display_name = GenericUtils.GetDoctorName(doctor_details) + "(" + doctor_details.lanr + ")";
                        new_last_used.id           = Parameter.DoctorID.ToString();
                        new_last_used.practice_id  = doctor_details.practice_id.ToString();
                        new_last_used.date_of_use  = last_used.date_of_use;

                        Add_New_Practice_Last_Used.Delete_Practice_Last_Used(securityTicket.TenantID.ToString(), type, Parameter.TemporaryDoctorID.ToString());
                        Add_New_Practice_Last_Used.Import_Practice_Last_Used_Data_to_ElasticDB(new List <Practice_Doctor_Last_Used_Model>()
                        {
                            new_last_used
                        }, securityTicket.TenantID.ToString(), type.Substring(5));
                    }
                    catch (Exception ex)
                    {
                        // left empty because it's not really an exception, rather a check whether object with given id exists
                    }
                }
                #endregion

                if (aftercares.Count != 0)
                {
                    Add_New_Aftercare.Import_Aftercare_Data_to_ElasticDB(aftercares, securityTicket.TenantID.ToString());
                }

                if (patientDetailList.Count != 0)
                {
                    Add_New_Patient.ImportPatientDetailsToElastic(patientDetailList, securityTicket.TenantID.ToString());
                }

                if (cases.Count != 0)
                {
                    Add_New_Case.Import_Case_Data_to_ElasticDB(cases, securityTicket.TenantID.ToString());
                }

                if (settlements.Count != 0)
                {
                    Add_new_Settlement.Import_Settlement_to_ElasticDB(settlements, securityTicket.TenantID.ToString());
                }

                Add_New_Practice.Delete_Doctor_Practice(securityTicket.TenantID.ToString(), Parameter.TemporaryDoctorID.ToString());
            }
            else
            {
                throw new Exception("Doctor not found");
            }

            return(returnValue);

            #endregion

            #endregion UserCode
        }