protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_DO_SAHD_1815 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_Guid();
            //Put your code here

            #region EMAIL
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("de-DE");

            string        urlMM        = System.Configuration.ConfigurationManager.AppSettings["mmdocconnect.dashboard.url"];
            List <String> mailToL      = new List <String>();
            var           accountMails = cls_Get_All_Account_LoginEmails_Who_Receive_Notifications.Invoke(Connection, Transaction, securityTicket).Result.ToList();
            foreach (var mail in accountMails)
            {
                mailToL.Add(mail.LoginEmail);
            }

            string appName  = WebConfigurationManager.AppSettings["mmAppUrl"];
            var    prefix   = HttpContext.Current.Request.Url.AbsoluteUri.Contains("https") ? "https://" : "http://";
            var    imageUrl = HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("api")) + "Content/images/logo.png";

            var     email_template = File.ReadAllText(HttpContext.Current.Server.MapPath("~/EmailTemplates/NewTemporaryDoctorEmailTemplate.html"));
            var     subjectsJson   = File.ReadAllText(HttpContext.Current.Server.MapPath("~/EmailTemplates/EmailSubjects.json"));
            dynamic subjects       = Newtonsoft.Json.JsonConvert.DeserializeObject(subjectsJson);
            var     subjectMail    = subjects["NewTemporaryDoctorSubject"].ToString();

            email_template = EmailTemplater.SetTemplateData(email_template, new
            {
                name                    = Parameter.name,
                street                  = String.IsNullOrEmpty(Parameter.street) ? "-" : Parameter.street,
                house_number            = String.IsNullOrEmpty(Parameter.house_number) ? "-" : Parameter.house_number,
                zip                     = String.IsNullOrEmpty(Parameter.zip) ? "-" : Parameter.zip,
                city                    = String.IsNullOrEmpty(Parameter.city) ? "-" : Parameter.city,
                phone                   = String.IsNullOrEmpty(Parameter.phone) ? "-" : Parameter.phone,
                fax                     = String.IsNullOrEmpty(Parameter.fax) ? "-" : Parameter.fax,
                email                   = String.IsNullOrEmpty(Parameter.email) ? "-" : Parameter.email,
                comment                 = String.IsNullOrEmpty(Parameter.comment) ? "-" : Parameter.comment,
                doc_app_url             = prefix + HttpContext.Current.Request.Url.Authority + "/" + appName,
                medios_connect_logo_url = imageUrl
            }, "{{", "}}");

            try
            {
                // string mailFrom = cls_Get_Company_Settings.Invoke(Connection, Transaction, securityTicket).Result.Email;
                string mailFrom      = WebConfigurationManager.AppSettings["mailFrom"];
                var    mailsDistinct = mailToL.Distinct().ToList();
                foreach (var mailTo in mailsDistinct)
                {
                    EmailNotificationSenderUtil.SendEmail(mailFrom, mailTo, subjectMail, email_template);
                }
            }
            catch (Exception ex)
            {
                LogUtils.Logger.LogDocAppInfo(new LogUtils.LogEntry(System.Reflection.MethodInfo.GetCurrentMethod(), ex, null, "Temporary doctor creation: Email sending failed."), "EmailExceptions");
            }
            #endregion

            #region PERSON INFO
            ORM_CMN_PER_PersonInfo temporary_doctor_person_info = new ORM_CMN_PER_PersonInfo();
            temporary_doctor_person_info.LastName = Parameter.name;
            temporary_doctor_person_info.Modification_Timestamp = DateTime.Now;
            temporary_doctor_person_info.Tenant_RefID           = securityTicket.TenantID;

            temporary_doctor_person_info.Save(Connection, Transaction);

            ORM_CMN_PER_CommunicationContact_Type temporary_doctor_communication_contact_type_email = new ORM_CMN_PER_CommunicationContact_Type();
            temporary_doctor_communication_contact_type_email.Tenant_RefID = securityTicket.TenantID;
            temporary_doctor_communication_contact_type_email.Type         = "Email";

            temporary_doctor_communication_contact_type_email.Save(Connection, Transaction);

            ORM_CMN_PER_CommunicationContact temporary_doctor_communication_contact_email = new ORM_CMN_PER_CommunicationContact();
            temporary_doctor_communication_contact_email.Tenant_RefID           = securityTicket.TenantID;
            temporary_doctor_communication_contact_email.Modification_Timestamp = DateTime.Now;
            temporary_doctor_communication_contact_email.PersonInfo_RefID       = temporary_doctor_person_info.CMN_PER_PersonInfoID;
            temporary_doctor_communication_contact_email.Content      = Parameter.email;
            temporary_doctor_communication_contact_email.Contact_Type = temporary_doctor_communication_contact_type_email.CMN_PER_CommunicationContact_TypeID;

            temporary_doctor_communication_contact_email.Save(Connection, Transaction);

            ORM_CMN_PER_CommunicationContact_Type temporary_doctor_communication_contact_type_phone = new ORM_CMN_PER_CommunicationContact_Type();
            temporary_doctor_communication_contact_type_phone.Tenant_RefID = securityTicket.TenantID;
            temporary_doctor_communication_contact_type_phone.Type         = "Phone";

            temporary_doctor_communication_contact_type_phone.Save(Connection, Transaction);

            ORM_CMN_PER_CommunicationContact temporary_doctor_communication_contact_phone = new ORM_CMN_PER_CommunicationContact();
            temporary_doctor_communication_contact_phone.Tenant_RefID           = securityTicket.TenantID;
            temporary_doctor_communication_contact_phone.Modification_Timestamp = DateTime.Now;
            temporary_doctor_communication_contact_phone.PersonInfo_RefID       = temporary_doctor_person_info.CMN_PER_PersonInfoID;
            temporary_doctor_communication_contact_phone.Content      = Parameter.phone;
            temporary_doctor_communication_contact_phone.Contact_Type = temporary_doctor_communication_contact_type_phone.CMN_PER_CommunicationContact_TypeID;

            temporary_doctor_communication_contact_phone.Save(Connection, Transaction);
            #endregion

            #region BPT
            ORM_CMN_BPT_BusinessParticipant temporary_doctor_bpt = new ORM_CMN_BPT_BusinessParticipant();
            temporary_doctor_bpt.IfNaturalPerson_CMN_PER_PersonInfo_RefID = temporary_doctor_person_info.CMN_PER_PersonInfoID;
            temporary_doctor_bpt.IsNaturalPerson        = true;
            temporary_doctor_bpt.Modification_Timestamp = DateTime.Now;
            temporary_doctor_bpt.Tenant_RefID           = securityTicket.TenantID;

            temporary_doctor_bpt.Save(Connection, Transaction);
            #endregion

            #region DOCTOR
            ORM_HEC_Doctor temporary_doctor = new ORM_HEC_Doctor();
            // account ref. id = guid empty means that this is a temporary doctor with no account
            temporary_doctor.Account_RefID                     = Guid.Empty;
            temporary_doctor.BusinessParticipant_RefID         = temporary_doctor_bpt.CMN_BPT_BusinessParticipantID;
            temporary_doctor.DoctorIDNumber                    = "";
            temporary_doctor.IsDoctorForFollowupTreatmentsOnly = true;
            temporary_doctor.Tenant_RefID = securityTicket.TenantID;

            temporary_doctor.Save(Connection, Transaction);

            ORM_HEC_Doctor_UniversalProperty temporary_doctor_universal_property = new ORM_HEC_Doctor_UniversalProperty();
            temporary_doctor_universal_property.GlobalPropertyMatchingID = "mm.docconnect.temporary.aftercare.doctor.comment";
            temporary_doctor_universal_property.IsValue_String           = true;
            temporary_doctor_universal_property.Modification_Timestamp   = DateTime.Now;
            temporary_doctor_universal_property.PropertyName             = "Comment";
            temporary_doctor_universal_property.Tenant_RefID             = securityTicket.TenantID;

            temporary_doctor_universal_property.Save(Connection, Transaction);

            ORM_HEC_Doctor_UniversalPropertyValue temporary_doctor_universal_property_value = new ORM_HEC_Doctor_UniversalPropertyValue();
            temporary_doctor_universal_property_value.HEC_Doctor_RefID        = temporary_doctor.HEC_DoctorID;
            temporary_doctor_universal_property_value.Modification_Timestamp  = DateTime.Now;
            temporary_doctor_universal_property_value.Value_String            = Parameter.comment;
            temporary_doctor_universal_property_value.UniversalProperty_RefID = temporary_doctor_universal_property.HEC_Doctor_UniversalPropertyID;
            temporary_doctor_universal_property_value.Tenant_RefID            = securityTicket.TenantID;

            temporary_doctor_universal_property_value.Save(Connection, Transaction);
            #endregion

            #region IMPORT TO ELASTIC

            Practice_Doctors_Model temporary_doctor_elastic_model = new Practice_Doctors_Model();
            temporary_doctor_elastic_model.autocomplete_name = Parameter.name;
            temporary_doctor_elastic_model.name                   = Parameter.name;
            temporary_doctor_elastic_model.name_untouched         = Parameter.name;
            temporary_doctor_elastic_model.address                = Parameter.street + " " + Parameter.house_number;
            temporary_doctor_elastic_model.zip                    = Parameter.zip;
            temporary_doctor_elastic_model.city                   = Parameter.city;
            temporary_doctor_elastic_model.email                  = Parameter.email;
            temporary_doctor_elastic_model.phone                  = Parameter.phone;
            temporary_doctor_elastic_model.account_status         = "temp";
            temporary_doctor_elastic_model.id                     = temporary_doctor.HEC_DoctorID.ToString();
            temporary_doctor_elastic_model.practice_for_doctor_id = Parameter.practice_id.ToString();
            temporary_doctor_elastic_model.tenantid               = securityTicket.TenantID.ToString();
            temporary_doctor_elastic_model.type                   = "Doctor";

            Add_New_Practice.Import_Practice_Data_to_ElasticDB(new List <Practice_Doctors_Model>()
            {
                temporary_doctor_elastic_model
            }, securityTicket.TenantID.ToString());
            #endregion

            returnValue.Result = temporary_doctor.HEC_DoctorID;
            return(returnValue);

            #endregion UserCode
        }
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_MD_SCPD_1341 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_Guid();
            //Put your code here
            var contractDetails = ORM_CMN_CTR_Contract.Query.Search(Connection, Transaction, new ORM_CMN_CTR_Contract.Query()
            {
                CMN_CTR_ContractID = Parameter.ContractID,
                IsDeleted          = false,
                Tenant_RefID       = securityTicket.TenantID
            }).SingleOrDefault();

            var insuranceToBrokerContract = ORM_HEC_CRT_InsuranceToBrokerContract.Query.Search(Connection, Transaction, new ORM_HEC_CRT_InsuranceToBrokerContract.Query()
            {
                Ext_CMN_CTR_Contract_RefID = Parameter.ContractID,
                IsDeleted    = false,
                Tenant_RefID = securityTicket.TenantID
            }).SingleOrDefault();

            if (contractDetails != null && insuranceToBrokerContract != null)
            {
                var currentParticipatingDoctors = ORM_HEC_CRT_InsuranceToBrokerContract_ParticipatingDoctor.Query.Search(Connection, Transaction, new ORM_HEC_CRT_InsuranceToBrokerContract_ParticipatingDoctor.Query()
                {
                    InsuranceToBrokerContract_RefID = insuranceToBrokerContract.HEC_CRT_InsuranceToBrokerContractID,
                    Tenant_RefID = securityTicket.TenantID,
                    IsDeleted    = false
                }).Where(cpd => Parameter.ParticipatingDoctors.Any(pd => !pd.IsParticipating && pd.AssignmentID == cpd.HEC_CRT_InsuranceToBrokerContract_ParticipatingDoctorID));

                foreach (var participatingDoctor in currentParticipatingDoctors)
                {
                    var doctor = Parameter.ParticipatingDoctors.Where(pd => pd.AssignmentID == participatingDoctor.HEC_CRT_InsuranceToBrokerContract_ParticipatingDoctorID && !pd.IsParticipating).SingleOrDefault();

                    if (doctor != null)
                    {
                        participatingDoctor.ValidThrough           = doctor.ConsentDate;
                        participatingDoctor.Modification_Timestamp = DateTime.Now;

                        participatingDoctor.Save(Connection, Transaction);
                    }
                }

                List <Practice_Doctors_Model> practiceL    = new List <Practice_Doctors_Model>();
                List <Practice_Doctors_Model> DoctorFoundL = new List <Practice_Doctors_Model>();

                foreach (var doctor in Parameter.ParticipatingDoctors.Where(d => d.IsParticipating && d.AssignmentID == Guid.Empty))
                {
                    var participatingDoctor = new ORM_HEC_CRT_InsuranceToBrokerContract_ParticipatingDoctor();
                    participatingDoctor.Creation_Timestamp = DateTime.Now;
                    participatingDoctor.Doctor_RefID       = doctor.DoctorID;
                    participatingDoctor.HEC_CRT_InsuranceToBrokerContract_ParticipatingDoctorID = Guid.NewGuid();
                    participatingDoctor.ValidFrom    = doctor.ConsentDate;
                    participatingDoctor.ValidThrough = contractDetails.ValidThrough;
                    participatingDoctor.InsuranceToBrokerContract_RefID = insuranceToBrokerContract.HEC_CRT_InsuranceToBrokerContractID;
                    participatingDoctor.Modification_Timestamp          = DateTime.Now;
                    participatingDoctor.Tenant_RefID = securityTicket.TenantID;

                    participatingDoctor.Save(Connection, Transaction);

                    var data = cls_Check_Doctor_Contracts_Dates.Invoke(Connection, Transaction, new P_DO_CDCD_1505()
                    {
                        DoctorID = doctor.DoctorID
                    }, securityTicket).Result;

                    int DoctorContracts = data.Count();

                    Practice_Doctors_Model DoctorFound = Get_Doctors_for_PracticeID.Set_Contract_Number_for_DoctorID(new Doctor_Contracts()
                    {
                        DocID = doctor.DoctorID
                    }, securityTicket);

                    if (DoctorFound != null)
                    {
                        DoctorFound.contract = DoctorContracts;
                        DoctorFoundL.Add(DoctorFound);
                    }

                    var data2 = cls_Get_PracticeID_for_DoctorID.Invoke(Connection, Transaction, new P_DO_GPIDfDID_1353()
                    {
                        DoctorID = doctor.DoctorID
                    }, securityTicket).Result;
                    var Contracts = cls_Get_all_Doctors_Contract_Assignment_for_PracticeID.Invoke(Connection, Transaction, new P_DO_GCfPID_1507()
                    {
                        PracticeID = data2.First().HEC_MedicalPractiseID
                    }, securityTicket).Result;

                    int NumberOfContractsForPractice = Contracts.Count();

                    Practice_Doctors_Model PracticeFound = Get_Doctors_for_PracticeID.Get_Practice_for_PracticeID(new Practice_Doctors_Model()
                    {
                        id = data2.First().HEC_MedicalPractiseID.ToString()
                    }, securityTicket);
                    PracticeFound.contract = NumberOfContractsForPractice;
                    practiceL.Add(PracticeFound);
                }

                if (practiceL.Count != 0)
                {
                    Add_New_Practice.Import_Practice_Data_to_ElasticDB(practiceL, securityTicket.TenantID.ToString());
                }

                if (DoctorFoundL.Count != 0)
                {
                    Add_New_Practice.Import_Practice_Data_to_ElasticDB(DoctorFoundL, securityTicket.TenantID.ToString());
                }
            }

            return(returnValue);

            #endregion UserCode
        }
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_DO_SDtC_1228 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var  returnValue  = new FR_Guid();
            Guid AssignmentID = Guid.Empty;

            if (Parameter.DoctorAssignment == Guid.Empty)
            {
                var insuranceTobrokerContract = ORM_HEC_CRT_InsuranceToBrokerContract.Query.Search(Connection, Transaction, new ORM_HEC_CRT_InsuranceToBrokerContract.Query()
                {
                    Ext_CMN_CTR_Contract_RefID = Parameter.ContractID,
                    Tenant_RefID = securityTicket.TenantID,
                    IsDeleted    = false
                }).SingleOrDefault();

                if (insuranceTobrokerContract != null)
                {
                    AssignmentID = insuranceTobrokerContract.HEC_CRT_InsuranceToBrokerContractID;
                }
                else
                {
                    insuranceTobrokerContract = new ORM_HEC_CRT_InsuranceToBrokerContract();
                    insuranceTobrokerContract.HEC_CRT_InsuranceToBrokerContractID = Guid.NewGuid();
                    insuranceTobrokerContract.Creation_Timestamp         = DateTime.Now;
                    insuranceTobrokerContract.IsDeleted                  = false;
                    insuranceTobrokerContract.Tenant_RefID               = securityTicket.TenantID;
                    insuranceTobrokerContract.Ext_CMN_CTR_Contract_RefID = Parameter.ContractID;
                    insuranceTobrokerContract.Save(Connection, Transaction);

                    AssignmentID = insuranceTobrokerContract.HEC_CRT_InsuranceToBrokerContractID;
                }

                var insuranceTobrokerContract2doctor = new ORM_HEC_CRT_InsuranceToBrokerContract_ParticipatingDoctor();
                insuranceTobrokerContract2doctor.Creation_Timestamp = DateTime.Now;
                insuranceTobrokerContract2doctor.HEC_CRT_InsuranceToBrokerContract_ParticipatingDoctorID = Guid.NewGuid();
                insuranceTobrokerContract2doctor.InsuranceToBrokerContract_RefID = AssignmentID;
                insuranceTobrokerContract2doctor.Tenant_RefID = securityTicket.TenantID;
                insuranceTobrokerContract2doctor.IsDeleted    = false;
                insuranceTobrokerContract2doctor.Doctor_RefID = Parameter.DoctorID;
                insuranceTobrokerContract2doctor.ValidFrom    = Parameter.ValidFrom;
                insuranceTobrokerContract2doctor.ValidThrough = Parameter.ValidThrough;

                insuranceTobrokerContract2doctor.Save(Connection, Transaction);
            }
            else
            {
                var insuranceTobrokerContract = ORM_HEC_CRT_InsuranceToBrokerContract.Query.Search(Connection, Transaction, new ORM_HEC_CRT_InsuranceToBrokerContract.Query()
                {
                    Ext_CMN_CTR_Contract_RefID = Parameter.ContractID,
                    Tenant_RefID = securityTicket.TenantID,
                    IsDeleted    = false
                }).Single();

                AssignmentID = insuranceTobrokerContract.HEC_CRT_InsuranceToBrokerContractID;

                var insuranceTobrokerContract2doctor = ORM_HEC_CRT_InsuranceToBrokerContract_ParticipatingDoctor.Query.Search(Connection, Transaction, new ORM_HEC_CRT_InsuranceToBrokerContract_ParticipatingDoctor.Query()
                {
                    HEC_CRT_InsuranceToBrokerContract_ParticipatingDoctorID = Parameter.DoctorAssignment,
                    InsuranceToBrokerContract_RefID = AssignmentID,
                    Tenant_RefID = securityTicket.TenantID,
                    IsDeleted    = false,
                    Doctor_RefID = Parameter.DoctorID
                }).Single();

                insuranceTobrokerContract2doctor.Modification_Timestamp = DateTime.Now;
                insuranceTobrokerContract2doctor.Doctor_RefID           = Parameter.DoctorID;
                insuranceTobrokerContract2doctor.ValidFrom    = Parameter.ValidFrom;
                insuranceTobrokerContract2doctor.ValidThrough = Parameter.ValidThrough;

                insuranceTobrokerContract2doctor.Save(Connection, Transaction);
            }

            var data = cls_Check_Doctor_Contracts_Dates.Invoke(Connection, Transaction, new P_DO_CDCD_1505()
            {
                DoctorID = Parameter.DoctorID
            }, securityTicket).Result;

            int DoctorContracts = data.Length;

            Practice_Doctors_Model DoctorFound = Get_Doctors_for_PracticeID.Set_Contract_Number_for_DoctorID(new Doctor_Contracts()
            {
                DocID = Parameter.DoctorID
            }, securityTicket);

            if (DoctorFound != null)
            {
                List <Practice_Doctors_Model> DoctorFoundL = new List <Practice_Doctors_Model>();
                DoctorFound.contract = DoctorContracts;
                DoctorFoundL.Add(DoctorFound);
                Add_New_Practice.Import_Practice_Data_to_ElasticDB(DoctorFoundL, securityTicket.TenantID.ToString());
            }

            P_DO_GPIDfDID_1353 ParametarDocID = new P_DO_GPIDfDID_1353();
            ParametarDocID.DoctorID = Parameter.DoctorID;
            DO_GPIDfDID_1353[] data2 = cls_Get_PracticeID_for_DoctorID.Invoke(Connection, Transaction, ParametarDocID, securityTicket).Result;

            P_DO_GCfPID_1507 ParametarPractice = new P_DO_GCfPID_1507();

            ParametarPractice.PracticeID = data2.First().HEC_MedicalPractiseID;
            DO_GCfPID_1507[] Contracts       = cls_Get_all_Doctors_Contract_Assignment_for_PracticeID.Invoke(Connection, Transaction, ParametarPractice, securityTicket).Result;
            int NumberOfContractsForPractice = Contracts.Count();
            Practice_Doctors_Model practice  = new Practice_Doctors_Model();
            practice.id = ParametarPractice.PracticeID.ToString();

            Practice_Doctors_Model        PracticeFound = Get_Doctors_for_PracticeID.Get_Practice_for_PracticeID(practice, securityTicket);
            List <Practice_Doctors_Model> practiceL     = new List <Practice_Doctors_Model>();
            PracticeFound.contract = NumberOfContractsForPractice;
            practiceL.Add(PracticeFound);
            Add_New_Practice.Import_Practice_Data_to_ElasticDB(practiceL, securityTicket.TenantID.ToString());

            return(returnValue);

            #endregion UserCode
        }
Exemple #4
0
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_DO_SD_1517 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            //Leave UserCode region to enable user code saving
            #region UserCode
            var returnValue = new FR_Guid();
            //Put your code here
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("de-DE");
            Guid   BusinessParticipantID         = Guid.NewGuid();
            Guid   personInfoID                  = Guid.NewGuid();
            Guid   PracticeCustomerID            = Guid.NewGuid();
            Guid   PracticeBusinessParticipantID = Guid.NewGuid();
            Guid   PracticeCompanyInfoID         = Guid.NewGuid();
            String PracticeName                  = "";
            Guid   BankAccountID                 = Guid.Empty;
            Guid   doctor_id    = Parameter.DoctorID;
            var    isOpPractice = false;

            //if (!String.IsNullOrEmpty(Parameter.Login_Email))
            //uncomment this if you've created account
            if (false)
            {
                string[] stringUser  = Parameter.Login_Email.Split('@');
                string   usernameStr = stringUser[0];

                try
                {
                    var doctorAccountInfo = ORM_USR_Account.Query.Search(Connection, Transaction, new ORM_USR_Account.Query()
                    {
                        IsDeleted    = false,
                        Tenant_RefID = securityTicket.TenantID,
                        AccountSignInEmailAddress = Parameter.Login_Email,
                    }).Single();

                    var accountGroupQuery = new ORM_USR_Account_FunctionLevelRights_Group.Query();
                    accountGroupQuery.Tenant_RefID             = securityTicket.TenantID;
                    accountGroupQuery.IsDeleted                = false;
                    accountGroupQuery.GlobalPropertyMatchingID = "mm.docconect.doc.app.group";

                    var accountGroup = ORM_USR_Account_FunctionLevelRights_Group.Query.Search(Connection, Transaction, accountGroupQuery).SingleOrDefault();

                    if (accountGroup == null)
                    {
                        accountGroup = new ORM_USR_Account_FunctionLevelRights_Group();
                        accountGroup.Tenant_RefID             = securityTicket.TenantID;
                        accountGroup.Label                    = "mm.docconect.doc.app.group";
                        accountGroup.GlobalPropertyMatchingID = "mm.docconect.doc.app.group";
                        accountGroup.Creation_Timestamp       = DateTime.Now;
                        accountGroup.USR_Account_FunctionLevelRights_GroupID = Guid.NewGuid();
                        accountGroup.Save(Connection, Transaction);
                    }

                    var PracticeAccount2UniversalProperty = ORM_HEC_MedicalPractice_2_UniversalProperty.Query.Search(Connection, Transaction, new ORM_HEC_MedicalPractice_2_UniversalProperty.Query()
                    {
                        IsDeleted    = false,
                        Tenant_RefID = securityTicket.TenantID,
                        HEC_MedicalPractice_RefID = Parameter.PracticeID
                    }).ToList();

                    List <ORM_HEC_MedicalPractice_UniversalProperty> PracticeUniversalPropertyList = new List <ORM_HEC_MedicalPractice_UniversalProperty>();
                    foreach (var item in PracticeAccount2UniversalProperty)
                    {
                        var PracticeUniversalProperty = ORM_HEC_MedicalPractice_UniversalProperty.Query.Search(Connection, Transaction, new ORM_HEC_MedicalPractice_UniversalProperty.Query()
                        {
                            IsDeleted    = false,
                            Tenant_RefID = securityTicket.TenantID,
                            HEC_MedicalPractice_UniversalPropertyID = item.HEC_MedicalPractice_UniversalProperty_RefID
                        }).Single();
                        PracticeUniversalPropertyList.Add(PracticeUniversalProperty);
                    }
                    ;

                    P_DO_GPAIDfPID_1522 practiceAccountIDParameter = new P_DO_GPAIDfPID_1522();
                    practiceAccountIDParameter.PracticeID = Parameter.PracticeID;

                    var practiceAccountToFunctionLevelRightQ = new ORM_USR_Account_2_FunctionLevelRight.Query();
                    practiceAccountToFunctionLevelRightQ.Account_RefID = cls_Get_Practice_AccountID_for_PracticeID.Invoke(Connection, Transaction, practiceAccountIDParameter, securityTicket).Result.accountID;
                    practiceAccountToFunctionLevelRightQ.Tenant_RefID  = securityTicket.TenantID;
                    practiceAccountToFunctionLevelRightQ.IsDeleted     = false;

                    var practiceAccountToFunctionLevelRight = ORM_USR_Account_2_FunctionLevelRight.Query.Search(Connection, Transaction, practiceAccountToFunctionLevelRightQ).SingleOrDefault();

                    if (practiceAccountToFunctionLevelRight != null)
                    {
                        var practiceAccountFunctionLevelRightQ = new ORM_USR_Account_FunctionLevelRight.Query();
                        practiceAccountFunctionLevelRightQ.Tenant_RefID = securityTicket.TenantID;
                        practiceAccountFunctionLevelRightQ.IsDeleted    = false;
                        practiceAccountFunctionLevelRightQ.USR_Account_FunctionLevelRightID = practiceAccountToFunctionLevelRight.FunctionLevelRight_RefID;

                        var practiceAccountFunctionLevelRight = ORM_USR_Account_FunctionLevelRight.Query.Search(Connection, Transaction, practiceAccountFunctionLevelRightQ).SingleOrDefault();

                        if (practiceAccountFunctionLevelRight != null)
                        {
                            isOpPractice = practiceAccountFunctionLevelRight.GlobalPropertyMatchingID.Equals("mm.docconect.doc.app.op.practice");
                        }
                    }

                    var functionLevelRightQ = new ORM_USR_Account_FunctionLevelRight.Query();
                    functionLevelRightQ.Tenant_RefID             = securityTicket.TenantID;
                    functionLevelRightQ.IsDeleted                = false;
                    functionLevelRightQ.GlobalPropertyMatchingID = isOpPractice ? "mm.docconect.doc.app.op.doctor" : "mm.docconect.doc.app.ac.doctor";

                    var existingFunctionLevelRight = ORM_USR_Account_FunctionLevelRight.Query.Search(Connection, Transaction, functionLevelRightQ).SingleOrDefault();

                    var tempFunctionLevelRightID = Guid.Empty;

                    if (existingFunctionLevelRight == null)
                    {
                        ORM_USR_Account_FunctionLevelRight functionLevelRight = new ORM_USR_Account_FunctionLevelRight();
                        functionLevelRight.USR_Account_FunctionLevelRightID = Guid.NewGuid();
                        functionLevelRight.FunctionLevelRights_Group_RefID  = accountGroup.USR_Account_FunctionLevelRights_GroupID;
                        functionLevelRight.Tenant_RefID       = securityTicket.TenantID;
                        functionLevelRight.Creation_Timestamp = DateTime.Now;

                        functionLevelRight.RightName = isOpPractice ? "mm.docconect.doc.app.op.doctor" : "mm.docconect.doc.app.ac.doctor";
                        functionLevelRight.GlobalPropertyMatchingID = isOpPractice ? "mm.docconect.doc.app.op.doctor" : "mm.docconect.doc.app.ac.doctor";

                        functionLevelRight.Save(Connection, Transaction);

                        tempFunctionLevelRightID = functionLevelRight.USR_Account_FunctionLevelRightID;
                    }
                    else
                    {
                        tempFunctionLevelRightID = existingFunctionLevelRight.USR_Account_FunctionLevelRightID;
                    }

                    var accountToFunctionLevelRight = new ORM_USR_Account_2_FunctionLevelRight();
                    accountToFunctionLevelRight.Tenant_RefID             = securityTicket.TenantID;
                    accountToFunctionLevelRight.Creation_Timestamp       = DateTime.Now;
                    accountToFunctionLevelRight.AssignmentID             = Guid.NewGuid();
                    accountToFunctionLevelRight.Account_RefID            = doctorAccountInfo.USR_AccountID;
                    accountToFunctionLevelRight.FunctionLevelRight_RefID = tempFunctionLevelRightID;

                    accountToFunctionLevelRight.Save(Connection, Transaction);

                    var businessParticipantQ = new ORM_CMN_BPT_BusinessParticipant.Query();

                    businessParticipantQ.IsDeleted    = false;
                    businessParticipantQ.Tenant_RefID = securityTicket.TenantID;
                    businessParticipantQ.CMN_BPT_BusinessParticipantID = doctorAccountInfo.BusinessParticipant_RefID;
                    BusinessParticipantID = doctorAccountInfo.BusinessParticipant_RefID;

                    var DoctorBusinessParticipant = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, businessParticipantQ).Single();
                    DoctorBusinessParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID = Guid.NewGuid();
                    DoctorBusinessParticipant.DisplayName            = Parameter.First_Name + " " + Parameter.Last_Name;
                    DoctorBusinessParticipant.IsNaturalPerson        = true;
                    DoctorBusinessParticipant.Modification_Timestamp = DateTime.Now;
                    DoctorBusinessParticipant.Save(Connection, Transaction);
                    personInfoID = DoctorBusinessParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID;


                    var companyInfoPractice = new ORM_CMN_PER_PersonInfo();
                    companyInfoPractice.IsDeleted            = false;
                    companyInfoPractice.Tenant_RefID         = securityTicket.TenantID;
                    companyInfoPractice.CMN_PER_PersonInfoID = personInfoID;
                    companyInfoPractice.FirstName            = Parameter.First_Name;
                    companyInfoPractice.LastName             = Parameter.Last_Name;
                    companyInfoPractice.Salutation_General   = Parameter.Salutation;
                    companyInfoPractice.Title = Parameter.Title;
                    companyInfoPractice.Save(Connection, Transaction);

                    var communicationContact = new ORM_CMN_PER_CommunicationContact();
                    communicationContact.IsDeleted              = false;
                    communicationContact.Contact_Type           = Guid.NewGuid();
                    communicationContact.Tenant_RefID           = securityTicket.TenantID;
                    communicationContact.Modification_Timestamp = DateTime.Now;
                    communicationContact.PersonInfo_RefID       = personInfoID;
                    communicationContact.Content = Parameter.Email;
                    communicationContact.Save(Connection, Transaction);
                    Guid CommunicationContactTypeID = communicationContact.Contact_Type;


                    var communicationContactType = new ORM_CMN_PER_CommunicationContact_Type();
                    communicationContactType.IsDeleted    = false;
                    communicationContactType.Tenant_RefID = securityTicket.TenantID;
                    communicationContactType.CMN_PER_CommunicationContact_TypeID = CommunicationContactTypeID;
                    communicationContactType.Type = "Email";
                    communicationContactType.Save(Connection, Transaction);

                    var communicationContact2 = new ORM_CMN_PER_CommunicationContact();
                    communicationContact2.IsDeleted              = false;
                    communicationContact2.Contact_Type           = Guid.NewGuid();
                    communicationContact2.Tenant_RefID           = securityTicket.TenantID;
                    communicationContact2.Modification_Timestamp = DateTime.Now;
                    communicationContact2.PersonInfo_RefID       = personInfoID;
                    communicationContact2.Content = Parameter.Phone;
                    communicationContact2.Save(Connection, Transaction);
                    Guid CommunicationContactTypeID2 = communicationContact2.Contact_Type;


                    var communicationContactType2 = new ORM_CMN_PER_CommunicationContact_Type();
                    communicationContactType2.IsDeleted    = false;
                    communicationContactType2.Tenant_RefID = securityTicket.TenantID;
                    communicationContactType2.CMN_PER_CommunicationContact_TypeID = CommunicationContactTypeID2;
                    communicationContactType2.Type = "Phone";
                    communicationContactType2.Save(Connection, Transaction);

                    var doctor = new ORM_HEC_Doctor();
                    doctor.HEC_DoctorID = Guid.NewGuid();
                    doctor.IsDeleted    = false;
                    doctor.Tenant_RefID = securityTicket.TenantID;
                    doctor.BusinessParticipant_RefID = BusinessParticipantID;
                    doctor.DoctorIDNumber            = Parameter.LANR.ToString();
                    doctor.Save(Connection, Transaction);

                    doctor_id = doctor.HEC_DoctorID;

                    var ogranizationUnitPractice = ORM_CMN_BPT_CTM_OrganizationalUnit.Query.Search(Connection, Transaction, new ORM_CMN_BPT_CTM_OrganizationalUnit.Query()
                    {
                        IsDeleted    = false,
                        Tenant_RefID = securityTicket.TenantID,
                        IfMedicalPractise_HEC_MedicalPractice_RefID = Parameter.PracticeID
                    }).Single();


                    var organizationalunit_Staff = new ORM_CMN_BPT_CTM_OrganizationalUnit_Staff();
                    organizationalunit_Staff.IsDeleted    = false;
                    organizationalunit_Staff.Tenant_RefID = securityTicket.TenantID;
                    organizationalunit_Staff.BusinessParticipant_RefID = BusinessParticipantID;
                    organizationalunit_Staff.OrganizationalUnit_RefID  = ogranizationUnitPractice.CMN_BPT_CTM_OrganizationalUnitID;
                    organizationalunit_Staff.Save(Connection, Transaction);

                    var CustomerPRactice = ORM_CMN_BPT_CTM_Customer.Query.Search(Connection, Transaction, new ORM_CMN_BPT_CTM_Customer.Query()
                    {
                        IsDeleted              = false,
                        Tenant_RefID           = securityTicket.TenantID,
                        CMN_BPT_CTM_CustomerID = ogranizationUnitPractice.Customer_RefID
                    }).Single();
                    var PracticeBusinessParticipant = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, new ORM_CMN_BPT_BusinessParticipant.Query()
                    {
                        IsDeleted    = false,
                        Tenant_RefID = securityTicket.TenantID,
                        CMN_BPT_BusinessParticipantID = CustomerPRactice.Ext_BusinessParticipant_RefID
                    }).Single();

                    PracticeName = PracticeBusinessParticipant.DisplayName;
                    PracticeBusinessParticipantID = PracticeBusinessParticipant.CMN_BPT_BusinessParticipantID;
                    PracticeCompanyInfoID         = PracticeBusinessParticipant.IfCompany_CMN_COM_CompanyInfo_RefID;

                    if (Parameter.From_Practice_Bank)
                    {
                        var PracticeBusinessParticipant2bankaccount = ORM_CMN_BPT_BusinessParticipant_2_BankAccount.Query.Search(Connection, Transaction, new ORM_CMN_BPT_BusinessParticipant_2_BankAccount.Query()
                        {
                            IsDeleted    = false,
                            Tenant_RefID = securityTicket.TenantID,
                            CMN_BPT_BusinessParticipant_RefID = PracticeBusinessParticipant.CMN_BPT_BusinessParticipantID,
                        }).Single();


                        var business2bankAccount = new ORM_CMN_BPT_BusinessParticipant_2_BankAccount();
                        business2bankAccount.IsDeleted    = false;
                        business2bankAccount.Tenant_RefID = securityTicket.TenantID;
                        business2bankAccount.CMN_BPT_BusinessParticipant_RefID = BusinessParticipantID;
                        business2bankAccount.ACC_BNK_BankAccount_RefID         = PracticeBusinessParticipant2bankaccount.ACC_BNK_BankAccount_RefID;
                        business2bankAccount.Creation_Timestamp = DateTime.Now;
                        business2bankAccount.Save(Connection, Transaction);

                        BankAccountID = PracticeBusinessParticipant2bankaccount.ACC_BNK_BankAccount_RefID;
                        //end of save bank data if inherited from practice
                    }
                    else
                    {
                        if (!String.IsNullOrEmpty(Parameter.IBAN) || !String.IsNullOrEmpty(Parameter.Bank))
                        {
                            var business2bankAccount = new ORM_CMN_BPT_BusinessParticipant_2_BankAccount();
                            business2bankAccount.IsDeleted    = false;
                            business2bankAccount.Tenant_RefID = securityTicket.TenantID;
                            business2bankAccount.CMN_BPT_BusinessParticipant_RefID = BusinessParticipantID;
                            business2bankAccount.ACC_BNK_BankAccount_RefID         = Guid.NewGuid();
                            business2bankAccount.Creation_Timestamp = DateTime.Now;
                            business2bankAccount.Save(Connection, Transaction);

                            var bankAccountDoctor = new ORM_ACC_BNK_BankAccount();
                            bankAccountDoctor.IsDeleted             = false;
                            bankAccountDoctor.Tenant_RefID          = securityTicket.TenantID;
                            bankAccountDoctor.ACC_BNK_BankAccountID = business2bankAccount.ACC_BNK_BankAccount_RefID;
                            bankAccountDoctor.OwnerText             = Parameter.Account_Holder;
                            bankAccountDoctor.IBAN               = Parameter.IBAN;
                            bankAccountDoctor.Bank_RefID         = Guid.NewGuid();
                            bankAccountDoctor.Creation_Timestamp = DateTime.Now;
                            bankAccountDoctor.Save(Connection, Transaction);

                            if (!String.IsNullOrEmpty(Parameter.Bank))
                            {
                                var bank = new ORM_ACC_BNK_Bank();
                                bank.IsDeleted          = false;
                                bank.Tenant_RefID       = securityTicket.TenantID;
                                bank.ACC_BNK_BankID     = bankAccountDoctor.Bank_RefID;
                                bank.BICCode            = Parameter.BIC;
                                bank.BankName           = Parameter.Bank;
                                bank.Creation_Timestamp = DateTime.Now;
                                bank.Save(Connection, Transaction);
                            }

                            BankAccountID = business2bankAccount.ACC_BNK_BankAccount_RefID;
                            // end save bank data
                        }
                    }
                }
                catch
                {
                    throw new Exception();
                }
            }
            else
            {
                var DoctorBusinessParticipant = new ORM_CMN_BPT_BusinessParticipant();

                DoctorBusinessParticipant.IsDeleted    = false;
                DoctorBusinessParticipant.Tenant_RefID = securityTicket.TenantID;
                DoctorBusinessParticipant.CMN_BPT_BusinessParticipantID            = Guid.NewGuid();
                DoctorBusinessParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID = Guid.NewGuid();
                DoctorBusinessParticipant.DisplayName            = Parameter.First_Name + " " + Parameter.Last_Name;
                DoctorBusinessParticipant.IsNaturalPerson        = true;
                DoctorBusinessParticipant.Modification_Timestamp = DateTime.Now;
                DoctorBusinessParticipant.Save(Connection, Transaction);
                personInfoID          = DoctorBusinessParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID;
                BusinessParticipantID = DoctorBusinessParticipant.CMN_BPT_BusinessParticipantID;

                var companyInfoPractice = new ORM_CMN_PER_PersonInfo();
                companyInfoPractice.IsDeleted            = false;
                companyInfoPractice.Tenant_RefID         = securityTicket.TenantID;
                companyInfoPractice.CMN_PER_PersonInfoID = personInfoID;
                companyInfoPractice.FirstName            = Parameter.First_Name;
                companyInfoPractice.LastName             = Parameter.Last_Name;
                companyInfoPractice.Salutation_General   = Parameter.Salutation;
                companyInfoPractice.Title = Parameter.Title;
                companyInfoPractice.Save(Connection, Transaction);

                var communicationContact = new ORM_CMN_PER_CommunicationContact();
                communicationContact.IsDeleted              = false;
                communicationContact.Contact_Type           = Guid.NewGuid();
                communicationContact.Tenant_RefID           = securityTicket.TenantID;
                communicationContact.Modification_Timestamp = DateTime.Now;
                communicationContact.PersonInfo_RefID       = personInfoID;
                communicationContact.Content = Parameter.Email;
                communicationContact.Save(Connection, Transaction);
                Guid CommunicationContactTypeID = communicationContact.Contact_Type;

                var communicationContactType = new ORM_CMN_PER_CommunicationContact_Type();
                communicationContactType.IsDeleted    = false;
                communicationContactType.Tenant_RefID = securityTicket.TenantID;
                communicationContactType.CMN_PER_CommunicationContact_TypeID = CommunicationContactTypeID;
                communicationContactType.Type = "Email";
                communicationContactType.Save(Connection, Transaction);

                var communicationContact2 = new ORM_CMN_PER_CommunicationContact();
                communicationContact2.IsDeleted              = false;
                communicationContact2.Contact_Type           = Guid.NewGuid();
                communicationContact2.Tenant_RefID           = securityTicket.TenantID;
                communicationContact2.Modification_Timestamp = DateTime.Now;
                communicationContact2.PersonInfo_RefID       = personInfoID;
                communicationContact2.Content = Parameter.Phone;
                communicationContact2.Save(Connection, Transaction);
                Guid CommunicationContactTypeID2 = communicationContact2.Contact_Type;


                var communicationContactType2 = new ORM_CMN_PER_CommunicationContact_Type();
                communicationContactType2.IsDeleted    = false;
                communicationContactType2.Tenant_RefID = securityTicket.TenantID;
                communicationContactType2.CMN_PER_CommunicationContact_TypeID = CommunicationContactTypeID2;
                communicationContactType2.Type = "Phone";
                communicationContactType2.Save(Connection, Transaction);

                var doctor = new ORM_HEC_Doctor();
                doctor.HEC_DoctorID = Guid.NewGuid();
                doctor.IsDeleted    = false;
                doctor.Tenant_RefID = securityTicket.TenantID;
                doctor.BusinessParticipant_RefID = BusinessParticipantID;
                doctor.DoctorIDNumber            = Parameter.LANR.ToString();
                doctor.Save(Connection, Transaction);

                doctor_id = doctor.HEC_DoctorID;

                var ogranizationUnitPractice = ORM_CMN_BPT_CTM_OrganizationalUnit.Query.Search(Connection, Transaction, new ORM_CMN_BPT_CTM_OrganizationalUnit.Query()
                {
                    IsDeleted    = false,
                    Tenant_RefID = securityTicket.TenantID,
                    IfMedicalPractise_HEC_MedicalPractice_RefID = Parameter.PracticeID
                }).Single();


                var organizationalunit_Staff = new ORM_CMN_BPT_CTM_OrganizationalUnit_Staff();
                organizationalunit_Staff.IsDeleted    = false;
                organizationalunit_Staff.Tenant_RefID = securityTicket.TenantID;
                organizationalunit_Staff.BusinessParticipant_RefID = BusinessParticipantID;
                organizationalunit_Staff.OrganizationalUnit_RefID  = ogranizationUnitPractice.CMN_BPT_CTM_OrganizationalUnitID;
                organizationalunit_Staff.Save(Connection, Transaction);


                var CustomerPRactice = ORM_CMN_BPT_CTM_Customer.Query.Search(Connection, Transaction, new ORM_CMN_BPT_CTM_Customer.Query()
                {
                    IsDeleted              = false,
                    Tenant_RefID           = securityTicket.TenantID,
                    CMN_BPT_CTM_CustomerID = ogranizationUnitPractice.Customer_RefID
                }).Single();
                var PracticeBusinessParticipant = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, new ORM_CMN_BPT_BusinessParticipant.Query()
                {
                    IsDeleted    = false,
                    Tenant_RefID = securityTicket.TenantID,
                    CMN_BPT_BusinessParticipantID = CustomerPRactice.Ext_BusinessParticipant_RefID
                }).Single();
                PracticeName = PracticeBusinessParticipant.DisplayName;
                PracticeBusinessParticipantID = PracticeBusinessParticipant.CMN_BPT_BusinessParticipantID;
                PracticeCompanyInfoID         = PracticeBusinessParticipant.IfCompany_CMN_COM_CompanyInfo_RefID;
                if (Parameter.From_Practice_Bank == true)
                {
                    var PracticeBusinessParticipant2bankaccount = ORM_CMN_BPT_BusinessParticipant_2_BankAccount.Query.Search(Connection, Transaction, new ORM_CMN_BPT_BusinessParticipant_2_BankAccount.Query()
                    {
                        IsDeleted    = false,
                        Tenant_RefID = securityTicket.TenantID,
                        CMN_BPT_BusinessParticipant_RefID = PracticeBusinessParticipant.CMN_BPT_BusinessParticipantID,
                    }).Single();


                    var business2bankAccount = new ORM_CMN_BPT_BusinessParticipant_2_BankAccount();
                    business2bankAccount.IsDeleted    = false;
                    business2bankAccount.Tenant_RefID = securityTicket.TenantID;
                    business2bankAccount.CMN_BPT_BusinessParticipant_RefID = BusinessParticipantID;
                    business2bankAccount.ACC_BNK_BankAccount_RefID         = PracticeBusinessParticipant2bankaccount.ACC_BNK_BankAccount_RefID;
                    business2bankAccount.Creation_Timestamp = DateTime.Now;
                    business2bankAccount.Save(Connection, Transaction);

                    BankAccountID = PracticeBusinessParticipant2bankaccount.ACC_BNK_BankAccount_RefID;
                    //end of save bank data if inherited from practice
                }
                else
                {
                    if (!String.IsNullOrEmpty(Parameter.IBAN) || !String.IsNullOrEmpty(Parameter.Bank))
                    {
                        var business2bankAccount = new ORM_CMN_BPT_BusinessParticipant_2_BankAccount();
                        business2bankAccount.IsDeleted    = false;
                        business2bankAccount.Tenant_RefID = securityTicket.TenantID;
                        business2bankAccount.CMN_BPT_BusinessParticipant_RefID = BusinessParticipantID;
                        business2bankAccount.ACC_BNK_BankAccount_RefID         = Guid.NewGuid();
                        business2bankAccount.Creation_Timestamp = DateTime.Now;
                        business2bankAccount.Save(Connection, Transaction);

                        var bankAccountDoctor = new ORM_ACC_BNK_BankAccount();
                        bankAccountDoctor.IsDeleted             = false;
                        bankAccountDoctor.Tenant_RefID          = securityTicket.TenantID;
                        bankAccountDoctor.ACC_BNK_BankAccountID = business2bankAccount.ACC_BNK_BankAccount_RefID;
                        bankAccountDoctor.OwnerText             = Parameter.Account_Holder;
                        bankAccountDoctor.IBAN               = Parameter.IBAN;
                        bankAccountDoctor.Bank_RefID         = Guid.NewGuid();
                        bankAccountDoctor.Creation_Timestamp = DateTime.Now;
                        bankAccountDoctor.Save(Connection, Transaction);

                        if (!String.IsNullOrEmpty(Parameter.Bank))
                        {
                            var bank = new ORM_ACC_BNK_Bank();
                            bank.IsDeleted          = false;
                            bank.Tenant_RefID       = securityTicket.TenantID;
                            bank.ACC_BNK_BankID     = bankAccountDoctor.Bank_RefID;
                            bank.BICCode            = Parameter.BIC;
                            bank.BankName           = Parameter.Bank;
                            bank.Creation_Timestamp = DateTime.Now;
                            bank.Save(Connection, Transaction);
                        }

                        BankAccountID = business2bankAccount.ACC_BNK_BankAccount_RefID;
                        // end save bank data
                    }
                }
            }
            var PracticeCompanyInfoAddress = ORM_CMN_COM_CompanyInfo_Address.Query.Search(Connection, Transaction, new ORM_CMN_COM_CompanyInfo_Address.Query()
            {
                IsDeleted           = false,
                Tenant_RefID        = securityTicket.TenantID,
                CompanyInfo_RefID   = PracticeCompanyInfoID,
                Address_Description = "Standard address for billing, shipping",
            }).SingleOrDefault();

            Practice_Doctors_Model DPModel = new Practice_Doctors_Model();

            if (PracticeCompanyInfoAddress != null)
            {
                var PracticeUCD = ORM_CMN_UniversalContactDetail.Query.Search(Connection, Transaction, new ORM_CMN_UniversalContactDetail.Query()
                {
                    IsDeleted    = false,
                    Tenant_RefID = securityTicket.TenantID,
                    CMN_UniversalContactDetailID = PracticeCompanyInfoAddress.Address_UCD_RefID,
                }).SingleOrDefault();
                DPModel.address = PracticeUCD.Street_Name + " " + PracticeUCD.Street_Number;
                DPModel.zip     = PracticeUCD.ZIP;
                DPModel.city    = PracticeUCD.Town;
            }

            DPModel.account_status = "aktiv";
            DPModel.id             = doctor_id.ToString();

            var title = string.IsNullOrEmpty(Parameter.Title) ? "" : Parameter.Title.Trim();

            DPModel.name           = title + " " + Parameter.Last_Name + " " + Parameter.First_Name;
            DPModel.name_untouched = Parameter.Last_Name + " " + Parameter.First_Name;
            DPModel.bsnr_lanr      = Parameter.LANR.ToString();

            DPModel.salutation = title;

            DPModel.type = "Doctor";

            DPModel.bank = string.IsNullOrEmpty(Parameter.Bank) ? "" : Parameter.Bank;

            DPModel.bank_untouched = string.IsNullOrEmpty(Parameter.Bank) ? "" : Parameter.Bank;


            DPModel.phone = Parameter.Phone;

            DPModel.email = string.IsNullOrEmpty(Parameter.Email) ? "" : Parameter.Email;

            DPModel.iban = string.IsNullOrEmpty(Parameter.IBAN) ? "" : Parameter.IBAN;

            DPModel.bic = string.IsNullOrEmpty(Parameter.BIC) ? "" : Parameter.BIC;

            DPModel.bank_id             = BankAccountID.ToString();
            DPModel.bank_info_inherited = Parameter.From_Practice_Bank;
            DPModel.aditional_info      = "";
            DPModel.contract            = 0;


            DPModel.tenantid = securityTicket.TenantID.ToString();
            DPModel.practice_name_for_doctor = PracticeName;
            DPModel.practice_for_doctor_id   = Parameter.PracticeID.ToString();

            DPModel.role = isOpPractice ? "op" : "ac";

            List <Practice_Doctors_Model> DPModelL = new List <Practice_Doctors_Model>();
            DPModelL.Add(DPModel);

            Add_New_Practice.Import_Practice_Data_to_ElasticDB(DPModelL, securityTicket.TenantID.ToString());
            return(returnValue);

            #endregion UserCode
        }
Exemple #5
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
        }