Ejemplo n.º 1
0
        public static void InsertDataIntoElastic(IEnumerable <IEnumerable <IElasticMapper> > data, string index_name)
        {
            foreach (var data_list in data)
            {
                if (data_list.Any())
                {
                    switch (data_list.First().GetType().Name)
                    {
                    case "Case_Model": Add_New_Case.Import_Case_Data_to_ElasticDB(data_list.Cast <Case_Model>().ToList(), index_name); break;

                    case "Aftercare_Model": Add_New_Aftercare.Import_Aftercare_Data_to_ElasticDB(data_list.Cast <Aftercare_Model>().ToList(), index_name); break;

                    case "Settlement_Model": Add_new_Settlement.Import_Settlement_to_ElasticDB(data_list.Cast <Settlement_Model>().ToList(), index_name); break;

                    case "Order_Model": Add_New_Order.Import_Order_Data_to_ElasticDB(data_list.Cast <Order_Model>().ToList(), index_name); break;

                    case "Patient_Model": Add_New_Patient.Import_Patients_to_ElasticDB(data_list.Cast <Patient_Model>().ToList(), index_name); break;

                    case "Submitted_Case_Model": Add_New_Submitted_Case.Import_Submitted_Case_Data_to_ElasticDB(data_list.Cast <Submitted_Case_Model>().ToList(), index_name); break;

                    case "PatientDetailViewModel": Add_New_Patient.ImportPatientDetailsToElastic(data_list.Cast <PatientDetailViewModel>().ToList(), index_name); break;

                    case "Oct_Model": Add_New_Oct.Import_Oct_Data_to_ElasticDB(data_list.Cast <Oct_Model>().ToList(), index_name); break;

                    default: break;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static void SubmitCase(DbConnection Connection,
                                      DbTransaction Transaction, SessionSecurityTicket securityTicket,
                                      bool is_treatment, Guid treatment_doctor_id, Guid patient_id,
                                      Guid diagnose_id, Guid drug_id, Guid case_id, string localization,
                                      Guid aftercare_doctor_id, DateTime treatment_date, Guid practice_id, Guid aftercare_practice_id, DateTime status_date)
        {
            var treatment_doctor_details = cls_Get_Doctor_Details_for_DoctorID.Invoke(Connection, Transaction, new P_DO_GDDfDID_0823()
            {
                DoctorID = treatment_doctor_id
            }, securityTicket).Result.SingleOrDefault();
            var patient_details = cls_Get_Patient_Details_for_PatientID.Invoke(Connection, Transaction, new P_PA_GPDfPID_1729()
            {
                PatientID = patient_id
            }, securityTicket).Result;
            var diagnose_details = cls_Get_Diagnose_Details_for_DiagnoseID.Invoke(Connection, Transaction, new P_CAS_GDDfDID_1357()
            {
                DiagnoseID = diagnose_id
            }, securityTicket).Result;
            var drug_details = cls_Get_Drug_Details_for_DrugID.Invoke(Connection, Transaction, new P_CAS_GDDfDID_1614()
            {
                DrugID = drug_id
            }, securityTicket).Result;
            var treatment_practice_default_settings = cls_Get_Practice_Default_Settings_for_PracticeID.Invoke(Connection, Transaction, new P_DO_GPDSfPID_0909()
            {
                PracticeID = practice_id
            }, securityTicket).Result;
            var aftercare_practice_default_settings = cls_Get_Practice_Default_Settings_for_PracticeID.Invoke(Connection, Transaction, new P_DO_GPDSfPID_0909()
            {
                PracticeID = aftercare_practice_id
            }, securityTicket).Result;
            var treatment_practice_details = cls_Get_Practice_Details_for_PracticeID.Invoke(Connection, Transaction, new P_DO_GPDfPID_1432()
            {
                PracticeID = practice_id
            }, securityTicket).Result.FirstOrDefault();
            var aftercare_practice_details = cls_Get_Practice_Details_for_PracticeID.Invoke(Connection, Transaction, new P_DO_GPDfPID_1432()
            {
                PracticeID = aftercare_practice_id
            }, securityTicket).Result.FirstOrDefault();
            var aftercare_doctor_details = cls_Get_Doctor_Details_for_DoctorID.Invoke(Connection, Transaction, new P_DO_GDDfDID_0823()
            {
                DoctorID = aftercare_doctor_id
            }, securityTicket).Result.SingleOrDefault();

            #region IMPORT TO ELASTIC
            var submitted_case_id = Guid.Empty;
            if (is_treatment)
            {
                submitted_case_id = cls_Get_Treatment_Planned_Action_for_CaseID.Invoke(Connection, Transaction, new P_CAS_GTPAfCID_0946()
                {
                    CaseID = case_id
                }, securityTicket).Result.planned_action_id;
                Add_New_Case.Delete_Case_After_Submitting(securityTicket.TenantID.ToString(), "case", case_id.ToString());
            }
            else
            {
                submitted_case_id = cls_Get_Aftercare_Planned_Action_for_CaseID.Invoke(Connection, Transaction, new P_CAS_GAPAfCID_0959()
                {
                    CaseID = case_id
                }, securityTicket).Result.planned_action_id;
                Add_New_Aftercare.Delete_Aftercare_Submitting(securityTicket.TenantID.ToString(), "aftercare", submitted_case_id.ToString());
            }

            #region IMPORT SUBMITTED CASE TO ELASTIC
            Submitted_Case_Model submitted_case_model_elastic = new Submitted_Case_Model();
            submitted_case_model_elastic.diagnose                  = diagnose_details != null ? diagnose_details.diagnose_name + " (" + diagnose_details.catalog_display_name + ": " + diagnose_details.diagnose_icd_10 + ")" : "";
            submitted_case_model_elastic.id                        = submitted_case_id.ToString();
            submitted_case_model_elastic.case_id                   = case_id.ToString();
            submitted_case_model_elastic.localization              = localization;
            submitted_case_model_elastic.management_pauschale      = is_treatment ? treatment_practice_default_settings.WaiveServiceFee ? "waived" : "deducted" : aftercare_practice_default_settings.WaiveServiceFee ? "waived" : "deducted";
            submitted_case_model_elastic.patient_birthdate         = patient_details.birthday;
            submitted_case_model_elastic.patient_birthdate_string  = patient_details.birthday.ToString("dd.MM.yyyy");
            submitted_case_model_elastic.patient_name              = patient_details.patient_last_name + ", " + patient_details.patient_first_name;
            submitted_case_model_elastic.status                    = "FS1";
            submitted_case_model_elastic.status_date               = status_date;
            submitted_case_model_elastic.status_date_string        = status_date.ToString("dd.MM.yyyy");
            submitted_case_model_elastic.treatment_date            = treatment_date;
            submitted_case_model_elastic.treatment_date_day_month  = treatment_date.ToString("dd.MM.");
            submitted_case_model_elastic.treatment_date_month_year = treatment_date.ToString("MMMM yyyy", new System.Globalization.CultureInfo("de", true));
            submitted_case_model_elastic.drug                      = drug_details != null ? drug_details.drug_name : "";
            submitted_case_model_elastic.type                      = is_treatment ? "op" : "ac";
            submitted_case_model_elastic.treatment_date_string     = treatment_date.ToString("dd.MM.yyyy");
            submitted_case_model_elastic.patient_insurance_number  = patient_details.insurance_id;

            if (is_treatment)
            {
                submitted_case_model_elastic.doctor_name   = treatment_doctor_details != null ? treatment_doctor_details.title + " " + treatment_doctor_details.last_name + " " + treatment_doctor_details.first_name : "-";
                submitted_case_model_elastic.practice_name = treatment_doctor_details.practice;
                submitted_case_model_elastic.doctor_lanr   = treatment_doctor_details.lanr;
                submitted_case_model_elastic.practice_bsnr = treatment_practice_details.practice_BSNR;
            }
            else
            {
                submitted_case_model_elastic.doctor_name   = aftercare_doctor_details.title + " " + aftercare_doctor_details.last_name + " " + aftercare_doctor_details.first_name;
                submitted_case_model_elastic.practice_name = aftercare_doctor_details.practice;
                submitted_case_model_elastic.doctor_lanr   = aftercare_doctor_details.lanr;
                submitted_case_model_elastic.practice_bsnr = aftercare_practice_details.practice_BSNR;
            }

            submitted_case_model_elastic.hip_name = patient_details != null ? patient_details.health_insurance_provider : "-";

            List <Submitted_Case_Model> cases_to_submit = new List <Submitted_Case_Model>();
            cases_to_submit.Add(submitted_case_model_elastic);

            Add_New_Submitted_Case.Import_Submitted_Case_Data_to_ElasticDB(cases_to_submit, securityTicket.TenantID.ToString());
            #endregion

            if (is_treatment)
            {
                submitted_case_id = cls_Get_Aftercare_Planned_Action_for_CaseID.Invoke(Connection, Transaction, new P_CAS_GAPAfCID_0959()
                {
                    CaseID = case_id
                }, securityTicket).Result.planned_action_id;

                #region IMPORT AFTERCARE TO ELASTIC
                Aftercare_Model aftercare = new Aftercare_Model();
                aftercare.aftercare_doctor_name = aftercare_doctor_details.title + " " + aftercare_doctor_details.first_name + " " + aftercare_doctor_details.last_name;
                aftercare.diagnose                 = diagnose_details != null ? diagnose_details.diagnose_name + " (" + diagnose_details.catalog_display_name + ": " + diagnose_details.diagnose_icd_10 + ")" : "";
                aftercare.id                       = submitted_case_id.ToString();
                aftercare.localization             = localization;
                aftercare.patient_birthdate        = patient_details.birthday;
                aftercare.patient_birthdate_string = patient_details.birthday.ToString("dd.MM.yyyy");
                aftercare.patient_name             = patient_details != null ? patient_details.patient_last_name + ", " + patient_details.patient_first_name : "";
                aftercare.practice_id              = cls_Get_PracticeID_for_DoctorID.Invoke(Connection, Transaction, new P_DO_GPIDfDID_1353()
                {
                    DoctorID = aftercare_doctor_id
                }, securityTicket).Result.SingleOrDefault().HEC_MedicalPractiseID.ToString();
                aftercare.status                         = "AC1";
                aftercare.treatment_date                 = treatment_date;
                aftercare.treatment_date_day_month       = treatment_date.ToString("dd.MM.");
                aftercare.treatment_date_month_year      = treatment_date.ToString("MMMM yyyy", new System.Globalization.CultureInfo("de", true));
                aftercare.treatment_doctor_name          = treatment_doctor_details != null ? treatment_doctor_details.title + " " + treatment_doctor_details.first_name + " " + treatment_doctor_details.last_name : "-";
                aftercare.treatment_doctor_practice_name = treatment_doctor_details.practice;
                aftercare.case_id                        = case_id.ToString();
                var aftercare_doctor_account_id = cls_Get_Doctor_AccountID_for_DoctorID.Invoke(Connection, Transaction, new P_DO_GDAIDfDID_1549()
                {
                    DoctorID = aftercare_doctor_id
                }, securityTicket).Result.accountID;
                aftercare.aftercare_doctor_account_id = aftercare_doctor_account_id.ToString();

                List <Aftercare_Model> aftercares = new List <Aftercare_Model>()
                {
                    aftercare
                };
                Add_New_Aftercare.Import_Aftercare_Data_to_ElasticDB(aftercares, securityTicket.TenantID.ToString());
                #endregion
            }
            #endregion IMPORT TO ELASTIC
        }
Ejemplo n.º 3
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
        }