Exemplo n.º 1
0
        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
        }
Exemplo n.º 2
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
        }