protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L5US_SSU_1847 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            //Leave UserCode region to enable user code saving
            #region UserCode
            var returnValue = new FR_Guid();

            var userAccount = new ORM_USR_Account();
            var result      = userAccount.Load(Connection, Transaction, Parameter.USR_AccountID);

            if (result.Status == FR_Status.Success)
            {
                //Get business participant via userAccount
                var businessParticipant = new ORM_CMN_BPT_BusinessParticipant();
                businessParticipant.Load(Connection, Transaction, userAccount.BusinessParticipant_RefID);

                //Load person
                var personInfo = new ORM_CMN_PER_PersonInfo();
                personInfo.Load(Connection, Transaction, businessParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID);

                //Load communication contacts for person
                P_L2CN_GCCfPI_1222 contactsParam = new P_L2CN_GCCfPI_1222();
                contactsParam.PersonInfoID = personInfo.CMN_PER_PersonInfoID;
                var contactsForPersonInfo = cls_Get_ComunicationContacts_for_PersonInfoID.Invoke(Connection, Transaction, contactsParam, securityTicket).Result.ToList();

                if (!Parameter.IsInitialSave)
                {
                    if (Parameter.IsDeleted)
                    {
                        #region Delete

                        var queryApplicationSubscription = new ORM_CMN_Account_ApplicationSubscription.Query();
                        queryApplicationSubscription.Application_RefID = Parameter.ApplicationID;
                        queryApplicationSubscription.Account_RefID     = userAccount.USR_AccountID;
                        queryApplicationSubscription.Tenant_RefID      = securityTicket.TenantID;

                        var foundApplicationSubscription = ORM_CMN_Account_ApplicationSubscription.Query.SoftDelete(Connection, Transaction, queryApplicationSubscription);

                        return(new FR_Guid(FR_Base.Status_OK, userAccount.USR_AccountID));

                        #endregion
                    }

                    #region Edit


                    personInfo.FirstName = Parameter.FirstName_ContactPerson;
                    personInfo.LastName  = Parameter.LastName_ContactPerson;
                    personInfo.Save(Connection, Transaction);

                    var employeeTemp = ORM_CMN_BPT_EMP_Employee.Query.Search(Connection, Transaction, new ORM_CMN_BPT_EMP_Employee.Query()
                    {
                        BusinessParticipant_RefID = businessParticipant.CMN_BPT_BusinessParticipantID,
                        Tenant_RefID = securityTicket.TenantID,
                        IsDeleted    = false
                    }).SingleOrDefault();

                    if (employeeTemp == null)
                    {
                        var newEmployee = new ORM_CMN_BPT_EMP_Employee();
                        newEmployee.CMN_BPT_EMP_EmployeeID    = Guid.NewGuid();
                        newEmployee.BusinessParticipant_RefID = businessParticipant.CMN_BPT_BusinessParticipantID;
                        newEmployee.StandardFunction          = "APOAdminEmployee";
                        newEmployee.Tenant_RefID = securityTicket.TenantID;
                        newEmployee.Save(Connection, Transaction);
                    }


                    var address = new ORM_CMN_Address();
                    address.Load(Connection, Transaction, personInfo.Address_RefID);
                    address.Street_Name     = Parameter.Street_Name;
                    address.Street_Number   = Parameter.Street_Number;
                    address.City_Name       = Parameter.Town;
                    address.City_PostalCode = Parameter.ZIP;
                    address.Save(Connection, Transaction);


                    try
                    {
                        //telephone
                        var telephone = contactsForPersonInfo.Where(i => i.Type == EnumUtils.GetEnumDescription(EComunactionContactType.Phone))
                                        .First().Contacts;

                        if (telephone.Count() == 1)
                        {
                            var contactID = telephone[0].CMN_PER_CommunicationContactID;

                            var contactTelephone = new ORM_CMN_PER_CommunicationContact();
                            contactTelephone.Load(Connection, Transaction, contactID);
                            contactTelephone.Content = Parameter.Contact_Telephone;
                            contactTelephone.Save(Connection, Transaction);
                        }
                    }
                    catch
                    {
                        //Log this
                    }

                    #region CommentedUsefull-PreviousWayOfContactEmailHandling

                    //try
                    //{
                    //    //email
                    //    var email = contactsForPersonInfo.Where(i => i.Type == EnumUtils.GetEnumDescription(EComunactionContactType.Email))
                    //        .First().Contacts;

                    //    if (email.Count() == 1)
                    //    {
                    //        var contactID = email[0].CMN_PER_CommunicationContactID;

                    //        var contactEmail = new ORM_CMN_PER_CommunicationContact();
                    //        contactEmail.Load(Connection, Transaction, contactID);
                    //        contactEmail.Content = Parameter.Contact_Email;
                    //        contactEmail.Save(Connection, Transaction);
                    //    }

                    //}
                    //catch
                    //{

                    //    //Log this
                    //}

                    #endregion

                    #endregion
                }

                #region SaveGroup

                var userAccountToGroup = ORM_USR_Account_2_Group.Query.Search(Connection, Transaction, new ORM_USR_Account_2_Group.Query()
                {
                    USR_Account_RefID = userAccount.USR_AccountID,
                    Tenant_RefID      = securityTicket.TenantID,
                    IsDeleted         = false
                }).SingleOrDefault();


                if (userAccountToGroup == null)
                {
                    var newGroup = new ORM_USR_Account_2_Group();
                    newGroup.AssignmentID       = Guid.NewGuid();
                    newGroup.USR_Account_RefID  = userAccount.USR_AccountID;
                    newGroup.USR_Group_RefID    = Parameter.USR_GroupID;
                    newGroup.Creation_Timestamp = DateTime.Now;
                    newGroup.Tenant_RefID       = securityTicket.TenantID;
                    newGroup.Save(Connection, Transaction);
                }
                else
                {
                    userAccountToGroup.USR_Group_RefID = Parameter.USR_GroupID;
                    userAccountToGroup.Save(Connection, Transaction);
                }

                #endregion

                #region SaveEmployee

                var employee = ORM_CMN_BPT_EMP_Employee.Query.Search(Connection, Transaction, new ORM_CMN_BPT_EMP_Employee.Query()
                {
                    BusinessParticipant_RefID = businessParticipant.CMN_BPT_BusinessParticipantID,
                    Tenant_RefID = securityTicket.TenantID,
                    IsDeleted    = false
                }).SingleOrDefault();

                if (employee == null)
                {
                    var newEmployee = new ORM_CMN_BPT_EMP_Employee();
                    newEmployee.CMN_BPT_EMP_EmployeeID    = Guid.NewGuid();
                    newEmployee.BusinessParticipant_RefID = businessParticipant.CMN_BPT_BusinessParticipantID;
                    newEmployee.StandardFunction          = "APOAdminEmployee";
                    newEmployee.Tenant_RefID = securityTicket.TenantID;
                    newEmployee.Save(Connection, Transaction);
                }

                #endregion

                #region CreateOrUpdateContactEmail

                var emailTypeProperty     = EnumUtils.GetEnumDescription(EComunactionContactType.Email);
                var contactEmailTypeQuery = new ORM_CMN_PER_CommunicationContact_Type.Query();
                contactEmailTypeQuery.Type         = emailTypeProperty;
                contactEmailTypeQuery.Tenant_RefID = securityTicket.TenantID;
                var contactEmailType = ORM_CMN_PER_CommunicationContact_Type.Query.Search(Connection, Transaction, contactEmailTypeQuery).FirstOrDefault();

                //Search for default contact email and create it if don't exist

                var defaultContactEmailQuery = new ORM_CMN_PER_CommunicationContact.Query();
                defaultContactEmailQuery.PersonInfo_RefID = personInfo.CMN_PER_PersonInfoID;
                defaultContactEmailQuery.Contact_Type     = contactEmailType.CMN_PER_CommunicationContact_TypeID;
                defaultContactEmailQuery.Tenant_RefID     = securityTicket.TenantID;
                var defaultContactEmail = ORM_CMN_PER_CommunicationContact.Query.Search(Connection, Transaction, defaultContactEmailQuery).FirstOrDefault();

                if (defaultContactEmail == null)
                {
                    defaultContactEmail = new ORM_CMN_PER_CommunicationContact();
                    defaultContactEmail.PersonInfo_RefID        = personInfo.CMN_PER_PersonInfoID;
                    defaultContactEmail.Contact_Type            = contactEmailType.CMN_PER_CommunicationContact_TypeID;
                    defaultContactEmail.IsDefaultForContactType = true;
                    defaultContactEmail.Tenant_RefID            = securityTicket.TenantID;
                }

                defaultContactEmail.Content = Parameter.Contact_Email;
                defaultContactEmail.IsDefaultForContactType = true;
                defaultContactEmail.Save(Connection, Transaction);

                #endregion
            }
            else
            {
                FR_Guid error = new FR_Guid();
                error.ErrorMessage = "No Such ID.";
                error.Status       = FR_Status.Error_Internal;
                return(error);
            }

            return(new FR_Guid(FR_Base.Status_OK, userAccount.USR_AccountID));

            #endregion UserCode
        }
        protected static FR_L5EM_GEFT_0959_Array Execute(DbConnection Connection, DbTransaction Transaction, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_L5EM_GEFT_0959_Array();
            List <L5EM_GEFT_0959>          employeeResultList = new List <L5EM_GEFT_0959>();
            ORM_CMN_BPT_EMP_Employee.Query employeeQuery      = new ORM_CMN_BPT_EMP_Employee.Query();
            employeeQuery.IsDeleted    = false;
            employeeQuery.Tenant_RefID = securityTicket.TenantID;
            List <ORM_CMN_BPT_EMP_Employee> employeeList = ORM_CMN_BPT_EMP_Employee.Query.Search(Connection, Transaction, employeeQuery);

            foreach (var employeeItem in employeeList)
            {
                L5EM_GEFT_0959           result   = new L5EM_GEFT_0959();
                ORM_CMN_BPT_EMP_Employee employee = new ORM_CMN_BPT_EMP_Employee();
                employee.Load(Connection, Transaction, employeeItem.CMN_BPT_EMP_EmployeeID);
                result.CMN_BPT_EMP_EmployeeID = employee.CMN_BPT_EMP_EmployeeID;
                result.Staff_Number           = employee.Staff_Number;
                result.StandardFunction       = employee.StandardFunction;

                ORM_USR_Account.Query accountQuery = new ORM_USR_Account.Query();
                accountQuery.BusinessParticipant_RefID = employee.BusinessParticipant_RefID;
                accountQuery.Tenant_RefID = securityTicket.TenantID;
                accountQuery.IsDeleted    = false;
                ORM_USR_Account account = ORM_USR_Account.Query.Search(Connection, Transaction, accountQuery).FirstOrDefault();
                if (account != null)
                {
                    result.USR_AccountID = account.USR_AccountID;
                }

                ORM_CMN_BPT_BusinessParticipant businessParticipant = new ORM_CMN_BPT_BusinessParticipant();
                businessParticipant.Load(Connection, Transaction, employee.BusinessParticipant_RefID);

                result.CMN_BPT_BusinessParticipantID = businessParticipant.CMN_BPT_BusinessParticipantID;
                result.DisplayName = businessParticipant.DisplayName;

                ORM_CMN_PER_PersonInfo personInfo = new ORM_CMN_PER_PersonInfo();
                personInfo.Load(Connection, Transaction, businessParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID);
                result.CMN_PER_PersonInfoID = personInfo.CMN_PER_PersonInfoID;
                result.FirstName            = personInfo.FirstName;
                result.LastName             = personInfo.LastName;
                result.PrimaryEmail         = personInfo.PrimaryEmail;
                result.Title = personInfo.Title;
                result.ProfileImage_Document_RefID = personInfo.ProfileImage_Document_RefID;
                result.BirthDate = personInfo.BirthDate;

                ORM_CMN_BPT_EMP_EmploymentRelationship.Query workingContractQuery = new ORM_CMN_BPT_EMP_EmploymentRelationship.Query();
                workingContractQuery.Employee_RefID = employee.CMN_BPT_EMP_EmployeeID;
                workingContractQuery.IsDeleted      = false;
                workingContractQuery.Tenant_RefID   = securityTicket.TenantID;
                ORM_CMN_BPT_EMP_EmploymentRelationship employmentRelationship = ORM_CMN_BPT_EMP_EmploymentRelationship.Query.Search(Connection, Transaction, workingContractQuery).FirstOrDefault();

                result.CMN_BPT_EMP_EmploymentRelationshipID = employmentRelationship.CMN_BPT_EMP_EmploymentRelationshipID;
                result.Work_StartDate = employmentRelationship.Work_StartDate;
                result.Work_EndDate   = employmentRelationship.Work_EndDate;

                //Address
                if (personInfo.Address_RefID != Guid.Empty)
                {
                    ORM_CMN_Address address = new ORM_CMN_Address();
                    address.Load(Connection, Transaction, personInfo.Address_RefID);
                    result.CMN_AddressID = address.CMN_AddressID;
                    result.Street_Name   = address.Street_Name;
                    result.Street_Number = address.Street_Number;
                    result.City_AdministrativeDistrict = address.City_AdministrativeDistrict;
                    result.City_Region     = address.City_Region;
                    result.City_Name       = address.City_Name;
                    result.City_PostalCode = address.City_PostalCode;
                    result.Province_Name   = address.Province_Name;
                    result.Country_Name    = address.Country_Name;
                    result.Country_ISOCode = address.Country_ISOCode;
                }

                //Contacts
                ORM_CMN_PER_CommunicationContact.Query comunicationContactQuery = new ORM_CMN_PER_CommunicationContact.Query();
                comunicationContactQuery.PersonInfo_RefID = personInfo.CMN_PER_PersonInfoID;
                comunicationContactQuery.Tenant_RefID     = securityTicket.TenantID;
                comunicationContactQuery.IsDeleted        = false;
                List <ORM_CMN_PER_CommunicationContact> comunicationContacts       = ORM_CMN_PER_CommunicationContact.Query.Search(Connection, Transaction, comunicationContactQuery);
                List <L5EM_GEFT_0959_Contacts>          resultComunicationContacts = new List <L5EM_GEFT_0959_Contacts>();
                foreach (var comunicationContact in comunicationContacts)
                {
                    ORM_CMN_PER_CommunicationContact_Type contactType = new ORM_CMN_PER_CommunicationContact_Type();
                    contactType.Load(Connection, Transaction, comunicationContact.CMN_PER_CommunicationContactID);
                    L5EM_GEFT_0959_Contacts resultComunicationContact = new L5EM_GEFT_0959_Contacts();
                    resultComunicationContact.CMN_PER_CommunicationContact_TypeID = contactType.CMN_PER_CommunicationContact_TypeID;
                    resultComunicationContact.CMN_PER_CommunicationContactID      = comunicationContact.CMN_PER_CommunicationContactID;
                    resultComunicationContact.Content = comunicationContact.Content;
                    resultComunicationContact.Type    = contactType.Type;
                    resultComunicationContacts.Add(resultComunicationContact);
                }
                result.Contacts = resultComunicationContacts.ToArray();


                //Employee workplace history
                ORM_CMN_BPT_EMP_Employee_WorkplaceAssignment.Query employeeWorkplaceAssignmentsQuery = new ORM_CMN_BPT_EMP_Employee_WorkplaceAssignment.Query();
                employeeWorkplaceAssignmentsQuery.CMN_BPT_EMP_Employee_RefID = employee.CMN_BPT_EMP_EmployeeID;
                employeeWorkplaceAssignmentsQuery.Tenant_RefID = securityTicket.TenantID;
                employeeWorkplaceAssignmentsQuery.IsDeleted    = false;
                List <ORM_CMN_BPT_EMP_Employee_WorkplaceAssignment> employeeWorkplaceAssignemntsList = ORM_CMN_BPT_EMP_Employee_WorkplaceAssignment.Query.Search(Connection, Transaction, employeeWorkplaceAssignmentsQuery);
                List <L5EM_GEFT_0959_EmployeeWorkplaceHistory>      employeeWorkplaceAssignments     = new List <L5EM_GEFT_0959_EmployeeWorkplaceHistory>();

                foreach (var workplaceAssignemns in employeeWorkplaceAssignemntsList)
                {
                    L5EM_GEFT_0959_EmployeeWorkplaceHistory item = new L5EM_GEFT_0959_EmployeeWorkplaceHistory();
                    item.BoundTo_Workplace_RefID = workplaceAssignemns.BoundTo_Workplace_RefID;
                    item.CMN_BPT_EMP_Employee_PlanGroup_RefID       = workplaceAssignemns.CMN_BPT_EMP_Employee_PlanGroup_RefID;
                    item.CMN_BPT_EMP_Employee_WorkplaceAssignmentID = workplaceAssignemns.CMN_BPT_EMP_Employee_WorkplaceAssignment;
                    item.Default_BreakTime_Template_RefID           = workplaceAssignemns.Default_BreakTime_Template_RefID;
                    item.IsBreakTimeCalculated_Actual   = workplaceAssignemns.IsBreakTimeCalculated_Actual;
                    item.IsBreakTimeCalculated_Planning = workplaceAssignemns.IsBreakTimeCalculated_Planning;
                    item.SequenceNumber = workplaceAssignemns.SequenceNumber;
                    item.WorkplaceAssignment_StartDate = workplaceAssignemns.WorkplaceAssignment_StartDate;

                    employeeWorkplaceAssignments.Add(item);
                }

                result.EmployeeWorkplaceHistory = employeeWorkplaceAssignments.ToArray();


                //Contracts

                ORM_CMN_BPT_EMP_EmploymentRelationship_2_WorkingContract.Query contractTermQuery = new ORM_CMN_BPT_EMP_EmploymentRelationship_2_WorkingContract.Query();
                contractTermQuery.EmploymentRelationship_RefID = employmentRelationship.CMN_BPT_EMP_EmploymentRelationshipID;
                contractTermQuery.IsDeleted    = false;
                contractTermQuery.Tenant_RefID = securityTicket.TenantID;
                List <ORM_CMN_BPT_EMP_EmploymentRelationship_2_WorkingContract> contracts = ORM_CMN_BPT_EMP_EmploymentRelationship_2_WorkingContract.Query.Search(Connection, Transaction, contractTermQuery);
                List <L5EM_GEFT_0959_WorkingContracts> resultContracts = new List <L5EM_GEFT_0959_WorkingContracts>();
                foreach (var contractToRelationship in contracts)
                {
                    L5EM_GEFT_0959_WorkingContracts resultContract = new L5EM_GEFT_0959_WorkingContracts();

                    ORM_CMN_BPT_EMP_WorkingContract workingContract = new ORM_CMN_BPT_EMP_WorkingContract();
                    workingContract.Load(Connection, Transaction, contractToRelationship.WorkingContract_RefID);
                    if (!workingContract.IsDeleted)
                    {
                        resultContract.CMN_BPT_EMP_WorkingContractID = workingContract.CMN_BPT_EMP_WorkingContractID;
                        resultContract.EmploymentRelationshipToWorkingContractAssignmentID = contractToRelationship.AssignmentID;
                        resultContract.IsWorkingContract_Active     = contractToRelationship.IsContract_Active;
                        resultContract.Contract_StartDate           = workingContract.Contract_StartDate;
                        resultContract.Contract_EndDate             = workingContract.Contract_EndDate;
                        resultContract.IsContractEndDateDefined     = workingContract.IsContractEndDateDefined;
                        resultContract.IsWorkTimeCalculated_InDays  = workingContract.IsWorkTimeCalculated_InDays;
                        resultContract.IsWorkTimeCalculated_InHours = workingContract.IsWorkTimeCalculated_InHours;
                        resultContract.R_WorkTime_DaysPerWeek       = workingContract.R_WorkTime_DaysPerWeek;
                        resultContract.R_WorkTime_HoursPerWeek      = workingContract.R_WorkTime_HoursPerWeek;

                        //Office hours
                        ORM_CMN_BPT_EMP_WorkingContract_2_WorkingDay.Query workingContractTermToWorkingDayQuery = new ORM_CMN_BPT_EMP_WorkingContract_2_WorkingDay.Query();
                        workingContractTermToWorkingDayQuery.CMN_BPT_EMP_WorkingContract_RefID = workingContract.CMN_BPT_EMP_WorkingContractID;
                        workingContractTermToWorkingDayQuery.Tenant_RefID = securityTicket.TenantID;
                        workingContractTermToWorkingDayQuery.IsDeleted    = false;
                        List <ORM_CMN_BPT_EMP_WorkingContract_2_WorkingDay> workingDayAssigments    = ORM_CMN_BPT_EMP_WorkingContract_2_WorkingDay.Query.Search(Connection, Transaction, workingContractTermToWorkingDayQuery);
                        List <L5EM_GEFT_0959_WeeklyOfficeHours>             resultWeeklyOfficeHours = new List <L5EM_GEFT_0959_WeeklyOfficeHours>();
                        foreach (var workingDayAssigment in workingDayAssigments)
                        {
                            ORM_CMN_CAL_WeeklyOfficeHours_Interval interval = new ORM_CMN_CAL_WeeklyOfficeHours_Interval();
                            interval.Load(Connection, Transaction, workingDayAssigment.CMN_CAL_WeeklyOfficeHours_Interval_RefID);

                            L5EM_GEFT_0959_WeeklyOfficeHours resultOfficeHour = new L5EM_GEFT_0959_WeeklyOfficeHours();
                            resultOfficeHour.CMN_CAL_WeeklyOfficeHours_IntervalID = interval.CMN_CAL_WeeklyOfficeHours_IntervalID;
                            resultOfficeHour.IsFriday           = interval.IsFriday;
                            resultOfficeHour.IsMonday           = interval.IsMonday;
                            resultOfficeHour.IsSaturday         = interval.IsSaturday;
                            resultOfficeHour.IsSunday           = interval.IsSunday;
                            resultOfficeHour.IsThursday         = interval.IsThursday;
                            resultOfficeHour.IsTuesday          = interval.IsTuesday;
                            resultOfficeHour.IsWednesday        = interval.IsWednesday;
                            resultOfficeHour.IsWholeDay         = interval.IsWholeDay;
                            resultOfficeHour.TimeFrom_InMinutes = interval.TimeFrom_InMinutes;
                            resultOfficeHour.TimeTo_InMinutes   = interval.TimeTo_InMinutes;
                            resultWeeklyOfficeHours.Add(resultOfficeHour);
                        }
                        resultContract.WeeklyOfficeHours = resultWeeklyOfficeHours.ToArray();


                        //Allowed absence reasons
                        ORM_CMN_BPT_EMP_WorkingContract_AllowedAbsenceReason.Query AllowedAbsenceReasonQuery = new ORM_CMN_BPT_EMP_WorkingContract_AllowedAbsenceReason.Query();
                        AllowedAbsenceReasonQuery.WorkingContract_RefID = resultContract.CMN_BPT_EMP_WorkingContractID;
                        AllowedAbsenceReasonQuery.Tenant_RefID          = securityTicket.TenantID;
                        AllowedAbsenceReasonQuery.IsDeleted             = false;
                        List <ORM_CMN_BPT_EMP_WorkingContract_AllowedAbsenceReason> allowedAbsenceReasons       = ORM_CMN_BPT_EMP_WorkingContract_AllowedAbsenceReason.Query.Search(Connection, Transaction, AllowedAbsenceReasonQuery);
                        List <L5EM_GEFT_0959_WorkingContractToLeaveRequest>         resultAllowedAbsenceReasons = new List <L5EM_GEFT_0959_WorkingContractToLeaveRequest>();
                        foreach (var allowedAbsenceReason in allowedAbsenceReasons)
                        {
                            ORM_CMN_BPT_STA_AbsenceReason absenceReason = new ORM_CMN_BPT_STA_AbsenceReason();
                            absenceReason.Load(Connection, Transaction, allowedAbsenceReason.STA_AbsenceReason_RefID);

                            L5EM_GEFT_0959_WorkingContractToLeaveRequest resultReasonresultReason = new L5EM_GEFT_0959_WorkingContractToLeaveRequest();
                            resultReasonresultReason.CMN_BPT_EMP_Employee_WorkingContract_AllowedAbsenceReasonID = allowedAbsenceReason.CMN_BPT_EMP_WorkingContract_AllowedAbsenceReasonID;
                            resultReasonresultReason.STA_AbsenceReason_RefID          = absenceReason.CMN_BPT_STA_AbsenceReasonID;
                            resultReasonresultReason.IsAbsenceCalculated_InDays       = allowedAbsenceReason.IsAbsenceCalculated_InDays;
                            resultReasonresultReason.IsAbsenceCalculated_InHours      = allowedAbsenceReason.IsAbsenceCalculated_InHours;
                            resultReasonresultReason.ContractAllowedAbsence_per_Month = allowedAbsenceReason.ContractAllowedAbsence_per_Month;
                            resultAllowedAbsenceReasons.Add(resultReasonresultReason);
                        }
                        resultContract.WorkingContractToLeaveRequest = resultAllowedAbsenceReasons.ToArray();

                        resultContracts.Add(resultContract);
                    }
                }
                result.WorkingContracts = resultContracts.ToArray();
                employeeResultList.Add(result);
            }
            returnValue.Result = employeeResultList.ToArray();
            //Put your code here
            return(returnValue);

            #endregion UserCode
        }
Beispiel #3
0
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L6DR_SD_1537 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
            if (Parameter.CMN_BPT_BusinessParticipantID == Guid.Empty)
            {
                #region Create Mode

                #region BusinessParticipant


                ORM_CMN_BPT_BusinessParticipant bParticipant = new ORM_CMN_BPT_BusinessParticipant();
                bParticipant.CMN_BPT_BusinessParticipantID            = Guid.NewGuid();
                bParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID = Guid.NewGuid();
                bParticipant.IsNaturalPerson    = true;
                bParticipant.Tenant_RefID       = securityTicket.TenantID;
                bParticipant.Creation_Timestamp = DateTime.Now;
                bParticipant.Save(Connection, Transaction);

                #endregion

                #region Account

                ORM_USR_Account account = new ORM_USR_Account();

                account.Username    = Parameter.FirstName + "_" + Parameter.LastName;
                account.AccountType = 3;
                account.BusinessParticipant_RefID = bParticipant.CMN_BPT_BusinessParticipantID;
                account.Tenant_RefID       = securityTicket.TenantID;
                account.Creation_Timestamp = DateTime.Now;
                account.Save(Connection, Transaction);

                P_L2DC_GUDCfT_1505 codeParam = new P_L2DC_GUDCfT_1505();
                codeParam.codeLength = 8;
                var checkCodeValue = cls_GetUniqueDeviceCodeForTenant.Invoke(Connection, Transaction, codeParam, securityTicket).Result;

                ORM_USR_Device_AccountCode accountCode = new ORM_USR_Device_AccountCode();
                accountCode.Account_RefID                   = account.USR_AccountID;
                accountCode.AccountCode_Value               = checkCodeValue.CodeValue;
                accountCode.AccountCode_ValidFrom           = DateTime.Now;
                accountCode.AccountCode_CurrentStatus_RefID = Guid.NewGuid();
                accountCode.IsAccountCode_Expirable         = false;
                accountCode.Tenant_RefID       = securityTicket.TenantID;
                accountCode.Creation_Timestamp = DateTime.Now;
                accountCode.Save(Connection, Transaction);

                ORM_USR_Device_AccountCode_StatusHistory Device_AccountCode_StatusHistory = new ORM_USR_Device_AccountCode_StatusHistory();
                Device_AccountCode_StatusHistory.USR_Device_AccountCode_StatusHistoryID = accountCode.AccountCode_CurrentStatus_RefID;
                Device_AccountCode_StatusHistory.IsAccountCode_Active     = true;
                Device_AccountCode_StatusHistory.Device_AccountCode_RefID = accountCode.USR_Device_AccountCodeID;
                Device_AccountCode_StatusHistory.Tenant_RefID             = securityTicket.TenantID;
                Device_AccountCode_StatusHistory.Creation_Timestamp       = DateTime.Now;
                Device_AccountCode_StatusHistory.Save(Connection, Transaction);


                ORM_USR_Device_AccountCode_UsageHistory USR_Device_AccountCode_UsageHistory = new ORM_USR_Device_AccountCode_UsageHistory();
                USR_Device_AccountCode_UsageHistory.USR_Device_AccountCode_UsageHistoryID = Guid.NewGuid();
                USR_Device_AccountCode_UsageHistory.Tenant_RefID             = securityTicket.TenantID;
                USR_Device_AccountCode_UsageHistory.Device_AccountCode_RefID = accountCode.USR_Device_AccountCodeID;
                USR_Device_AccountCode_UsageHistory.Creation_Timestamp       = DateTime.Now;
                USR_Device_AccountCode_UsageHistory.Save(Connection, Transaction);


                #endregion

                #region PersonInfo and Adresses

                ORM_CMN_PER_PersonInfo personInfo = new ORM_CMN_PER_PersonInfo();
                personInfo.CMN_PER_PersonInfoID = bParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID;
                personInfo.Salutation_General   = Parameter.Salutation_General;
                personInfo.FirstName            = Parameter.FirstName;
                personInfo.LastName             = Parameter.LastName;
                personInfo.PrimaryEmail         = Parameter.PrimaryMail;
                personInfo.Address_RefID        = Guid.NewGuid();
                personInfo.Tenant_RefID         = securityTicket.TenantID;
                personInfo.Save(Connection, Transaction);

                ORM_CMN_PER_PersonInfo_2_Address personAdress = new ORM_CMN_PER_PersonInfo_2_Address();
                personAdress.AssignmentID             = Guid.NewGuid();
                personAdress.CMN_PER_PersonInfo_RefID = personInfo.CMN_PER_PersonInfoID;
                personAdress.CMN_Address_RefID        = personInfo.Address_RefID;
                personAdress.IsPrimary    = true;
                personAdress.Tenant_RefID = securityTicket.TenantID;
                personAdress.Save(Connection, Transaction);

                ORM_CMN_Address adress = new ORM_CMN_Address();
                adress.CMN_AddressID   = personInfo.Address_RefID;
                adress.City_Name       = Parameter.City_Name;
                adress.Province_Name   = Parameter.Province_Name;
                adress.Street_Name     = Parameter.Street_Name;
                adress.Street_Number   = Parameter.Street_Number;
                adress.City_PostalCode = Parameter.City_PostalCode;
                adress.Tenant_RefID    = securityTicket.TenantID;
                adress.Save(Connection, Transaction);

                #endregion

                #region Contacts

                foreach (var parContact in Parameter.Contacts)
                {
                    ORM_CMN_PER_CommunicationContact contact = new ORM_CMN_PER_CommunicationContact();
                    contact.Content          = parContact.Content;
                    contact.Contact_Type     = parContact.CMN_PER_CommunicationContact_TypeID;
                    contact.Tenant_RefID     = securityTicket.TenantID;
                    contact.PersonInfo_RefID = personInfo.CMN_PER_PersonInfoID;
                    contact.Save(Connection, Transaction);
                }

                #endregion

                #region Driver To Employer

                ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant bpt_asBP = new ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant();
                bpt_asBP.CMN_BPT_BusinessParticipant_AssociatedBusinessParticipantID = Guid.NewGuid();
                bpt_asBP.BusinessParticipant_RefID          = bParticipant.CMN_BPT_BusinessParticipantID;
                bpt_asBP.AssociatedParticipant_FunctionName = "Driver";
                bpt_asBP.Tenant_RefID = securityTicket.TenantID;
                bpt_asBP.AssociatedBusinessParticipant_RefID = Parameter.CMN_BPT_BusinessParticipantID_Of_Employer;
                bpt_asBP.Save(Connection, Transaction);

                #endregion

                #endregion
            }
            else
            {
                ORM_CMN_BPT_BusinessParticipant bParticipant = new ORM_CMN_BPT_BusinessParticipant();
                bParticipant.Load(Connection, Transaction, Parameter.CMN_BPT_BusinessParticipantID);

                var personInfo = new ORM_CMN_PER_PersonInfo();
                personInfo.Load(Connection, Transaction, bParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID);
                personInfo.Salutation_General = Parameter.Salutation_General;
                personInfo.FirstName          = Parameter.FirstName;
                personInfo.LastName           = Parameter.LastName;
                personInfo.PrimaryEmail       = Parameter.PrimaryMail;
                personInfo.Save(Connection, Transaction);

                ORM_CMN_Address adress = new ORM_CMN_Address();
                adress.Load(Connection, Transaction, personInfo.Address_RefID);
                adress.City_Name     = Parameter.City_Name;
                adress.Province_Name = Parameter.Province_Name;
                adress.Street_Name   = Parameter.Street_Name;
                adress.Street_Number = Parameter.Street_Number;
                adress.Save(Connection, Transaction);

                foreach (var parContact in Parameter.Contacts)
                {
                    var query = new ORM_CMN_PER_CommunicationContact.Query();
                    query.Contact_Type     = parContact.CMN_PER_CommunicationContact_TypeID;
                    query.IsDeleted        = false;
                    query.PersonInfo_RefID = personInfo.CMN_PER_PersonInfoID;

                    var contact = ORM_CMN_PER_CommunicationContact.Query.Search(Connection, Transaction, query).First();
                    contact.Content      = parContact.Content;
                    contact.Contact_Type = parContact.CMN_PER_CommunicationContact_TypeID;
                    contact.Save(Connection, Transaction);
                }

                var associationQuery = new ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant.Query();
                associationQuery.BusinessParticipant_RefID = Parameter.CMN_BPT_BusinessParticipantID;
                associationQuery.IsDeleted = false;

                var bpt_asBP = ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant.Query.Search(Connection, Transaction, associationQuery).First();
                bpt_asBP.AssociatedBusinessParticipant_RefID = Parameter.CMN_BPT_BusinessParticipantID_Of_Employer;
                bpt_asBP.Save(Connection, Transaction);
            }
            return(returnValue);

            #endregion UserCode
        }
Beispiel #4
0
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L3MD_SDBI_1349 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_Guid();

            var doctor = new ORM_HEC_Doctor();

            if (Parameter.DoctorID != Guid.Empty)
            {
                var result = doctor.Load(Connection, Transaction, Parameter.DoctorID);
                if (result.Status != FR_Status.Success || doctor.HEC_DoctorID == Guid.Empty)
                {
                    var error = new FR_Guid();
                    error.ErrorMessage = "No Such ID";
                    error.Status       = FR_Status.Error_Internal;
                    return(error);
                }
                #region Edit

                bool bopAccIsChenged = (doctor.Account_RefID == Parameter.Account_RefID) ? false : true;

                doctor.Account_RefID = Parameter.Account_RefID;
                if (Parameter.isLucentisSave)
                {
                    doctor.DoctorIDNumber = Parameter.ifLucentis_LANR;
                }
                doctor.Save(Connection, Transaction);

                //bussinessParticipant
                var query1 = new ORM_CMN_BPT_BusinessParticipant.Query();
                query1.CMN_BPT_BusinessParticipantID = doctor.BusinessParticipant_RefID;

                var bussinessParticipant = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, query1).First();

                if (!bopAccIsChenged)
                {
                    //personInfo
                    var query2 = new ORM_CMN_PER_PersonInfo.Query();
                    query2.CMN_PER_PersonInfoID = bussinessParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID;

                    var personInfo = ORM_CMN_PER_PersonInfo.Query.Search(Connection, Transaction, query2).First();
                    personInfo.FirstName = Parameter.FirstName;
                    personInfo.LastName  = Parameter.LastName;
                    personInfo.Title     = Parameter.Title;
                    if (Parameter.isOphthalSave)
                    {
                        personInfo.Salutation_General = Parameter.ifOphthal_Salutation_General;
                        personInfo.Salutation_Letter  = Parameter.ifOphthal_Salutation_Letter;
                    }
                    if (Parameter.isLucentisSave)
                    {
                        personInfo.PrimaryEmail = Parameter.ifLucentis_LoginEmail;
                    }
                    personInfo.Save(Connection, Transaction);

                    var query4 = new ORM_CMN_PER_CommunicationContact.Query();
                    query4.PersonInfo_RefID = personInfo.CMN_PER_PersonInfoID;

                    var communicationContactsList = ORM_CMN_PER_CommunicationContact.Query.Search(Connection, Transaction, query4).ToList();

                    if (Parameter.Contacts != null)
                    {
                        foreach (var parContact in Parameter.Contacts)
                        {
                            ORM_CMN_PER_CommunicationContact communicationContacts = communicationContactsList.FirstOrDefault(c => c.Contact_Type == parContact.CMN_PER_CommunicationContact_TypeID);
                            if (communicationContacts != null)
                            {
                                communicationContacts.Tenant_RefID = securityTicket.TenantID;
                                communicationContacts.Content      = parContact.Content;
                                communicationContacts.Save(Connection, Transaction);
                            }
                            else
                            {
                                communicationContacts = new ORM_CMN_PER_CommunicationContact();
                                communicationContacts.CMN_PER_CommunicationContactID = Guid.NewGuid();
                                communicationContacts.PersonInfo_RefID   = personInfo.CMN_PER_PersonInfoID;
                                communicationContacts.Contact_Type       = parContact.CMN_PER_CommunicationContact_TypeID;
                                communicationContacts.Content            = parContact.Content;
                                communicationContacts.Creation_Timestamp = DateTime.Now;
                                communicationContacts.Tenant_RefID       = securityTicket.TenantID;
                                communicationContacts.Save(Connection, Transaction);
                            }
                        }
                    }
                }
                else
                {
                    var account2personInfoQuery = new ORM_CMN_PER_PersonInfo_2_Account.Query();
                    account2personInfoQuery.USR_Account_RefID = Parameter.Account_RefID;
                    account2personInfoQuery.Tenant_RefID      = securityTicket.TenantID;
                    account2personInfoQuery.IsDeleted         = false;
                    var account2personInfo = ORM_CMN_PER_PersonInfo_2_Account.Query.Search(Connection, Transaction, account2personInfoQuery).First();

                    var query2 = new ORM_CMN_PER_PersonInfo.Query();
                    query2.CMN_PER_PersonInfoID = account2personInfo.CMN_PER_PersonInfo_RefID;

                    var personInfo = ORM_CMN_PER_PersonInfo.Query.Search(Connection, Transaction, query2).First();
                    personInfo.FirstName = Parameter.FirstName;
                    personInfo.LastName  = Parameter.LastName;
                    personInfo.Title     = Parameter.Title;
                    personInfo.Save(Connection, Transaction);

                    var query4 = new ORM_CMN_PER_CommunicationContact.Query();
                    query4.PersonInfo_RefID = bussinessParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID;
                    var communicationContactsList = ORM_CMN_PER_CommunicationContact.Query.Search(Connection, Transaction, query4).ToList();

                    if (communicationContactsList != null)
                    {
                        foreach (var c in communicationContactsList)
                        {
                            c.PersonInfo_RefID = personInfo.CMN_PER_PersonInfoID;
                            c.Save(Connection, Transaction);
                        }
                    }

                    if (Parameter.Contacts != null)
                    {
                        foreach (var parContact in Parameter.Contacts)
                        {
                            ORM_CMN_PER_CommunicationContact communicationContacts = communicationContactsList.FirstOrDefault(c => c.Contact_Type == parContact.CMN_PER_CommunicationContact_TypeID);
                            if (communicationContacts != null)
                            {
                                communicationContacts.Tenant_RefID = securityTicket.TenantID;
                                communicationContacts.Content      = parContact.Content;
                                communicationContacts.Save(Connection, Transaction);
                            }
                            else
                            {
                                communicationContacts = new ORM_CMN_PER_CommunicationContact();
                                communicationContacts.CMN_PER_CommunicationContactID = Guid.NewGuid();
                                communicationContacts.PersonInfo_RefID   = personInfo.CMN_PER_PersonInfoID;
                                communicationContacts.Contact_Type       = parContact.CMN_PER_CommunicationContact_TypeID;
                                communicationContacts.Content            = parContact.Content;
                                communicationContacts.Creation_Timestamp = DateTime.Now;
                                communicationContacts.Tenant_RefID       = securityTicket.TenantID;
                                communicationContacts.Save(Connection, Transaction);
                            }
                        }
                    }

                    bussinessParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID = personInfo.CMN_PER_PersonInfoID;
                    bussinessParticipant.Save(Connection, Transaction);
                }
                if (Parameter.isLucentisSave)
                {
                    foreach (var practice in Parameter.Practices)
                    {
                        //if (practice.PracticeID != Guid.Empty) {

                        var medPract = new ORM_HEC_MedicalPractis.Query();
                        medPract.HEC_MedicalPractiseID = practice.PracticeID;

                        var medicalPractice = ORM_HEC_MedicalPractis.Query.Search(Connection, Transaction, medPract).First();

                        var queryCompanyInfo = new ORM_CMN_COM_CompanyInfo.Query();
                        queryCompanyInfo.CMN_COM_CompanyInfoID = medicalPractice.Ext_CompanyInfo_RefID;

                        var companyInfo = ORM_CMN_COM_CompanyInfo.Query.Search(Connection, Transaction, queryCompanyInfo).First();

                        var practiceQuery = new ORM_CMN_BPT_BusinessParticipant.Query();
                        practiceQuery.IfCompany_CMN_COM_CompanyInfo_RefID = companyInfo.CMN_COM_CompanyInfoID;

                        var practiceBPT = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, practiceQuery).First();

                        var query3 = new ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant.Query();
                        query3.BusinessParticipant_RefID           = bussinessParticipant.CMN_BPT_BusinessParticipantID;
                        query3.AssociatedBusinessParticipant_RefID = practiceBPT.CMN_BPT_BusinessParticipantID;
                        query3.IsDeleted = false;

                        var associatedbusinessparticipantsOriginal = ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant.Query.Search(Connection, Transaction, query3).FirstOrDefault();
                        if (associatedbusinessparticipantsOriginal != null)
                        {
                            associatedbusinessparticipantsOriginal.IsDeleted = practice.isDeleted;
                            associatedbusinessparticipantsOriginal.Save(Connection, Transaction);
                        }
                        else
                        {
                            var associatedbusinessparticipants = new ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant();
                            associatedbusinessparticipants.BusinessParticipant_RefID           = bussinessParticipant.CMN_BPT_BusinessParticipantID;
                            associatedbusinessparticipants.AssociatedBusinessParticipant_RefID = practiceBPT.CMN_BPT_BusinessParticipantID;
                            associatedbusinessparticipants.AssociatedParticipant_FunctionName  = practice.AssociatedParticipant_FunctionName;
                            associatedbusinessparticipants.Creation_Timestamp = DateTime.Now;
                            associatedbusinessparticipants.Tenant_RefID       = securityTicket.TenantID;
                            associatedbusinessparticipants.Save(Connection, Transaction);


                            // }
                        }
                    }
                }
                if (Parameter.isOphthalSave && Parameter.Practices.Length == 1)
                {
                    var query3 = new ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant.Query();
                    query3.BusinessParticipant_RefID = bussinessParticipant.CMN_BPT_BusinessParticipantID;
                    query3.IsDeleted = false;
                    var associatedbusinessparticipantsRes = ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant.Query.Search(Connection, Transaction, query3);
                    ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant firstPractice;

                    var medPract = new ORM_HEC_MedicalPractis.Query();
                    medPract.HEC_MedicalPractiseID = Parameter.Practices[0].PracticeID;
                    var medicalPractice  = ORM_HEC_MedicalPractis.Query.Search(Connection, Transaction, medPract).First();
                    var queryCompanyInfo = new ORM_CMN_COM_CompanyInfo.Query();
                    queryCompanyInfo.CMN_COM_CompanyInfoID = medicalPractice.Ext_CompanyInfo_RefID;
                    var companyInfo   = ORM_CMN_COM_CompanyInfo.Query.Search(Connection, Transaction, queryCompanyInfo).First();
                    var practiceQuery = new ORM_CMN_BPT_BusinessParticipant.Query();
                    practiceQuery.IfCompany_CMN_COM_CompanyInfo_RefID = companyInfo.CMN_COM_CompanyInfoID;
                    var practiceBPT = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, practiceQuery).First();

                    if (associatedbusinessparticipantsRes.Count > 0)
                    {
                        associatedbusinessparticipantsRes = associatedbusinessparticipantsRes.OrderBy(a => a.Creation_Timestamp).ToList();
                        firstPractice = associatedbusinessparticipantsRes.First();
                        foreach (var item in associatedbusinessparticipantsRes)
                        {
                            if (item != firstPractice && item.CMN_BPT_BusinessParticipant_AssociatedBusinessParticipantID == practiceBPT.CMN_BPT_BusinessParticipantID)
                            {
                                item.IsDeleted = true;
                                item.Save(Connection, Transaction);
                            }
                        }
                    }
                    else
                    {
                        firstPractice = new ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant();
                        firstPractice.CMN_BPT_BusinessParticipant_AssociatedBusinessParticipantID = Guid.NewGuid();
                        firstPractice.Tenant_RefID = securityTicket.TenantID;
                        firstPractice.BusinessParticipant_RefID = bussinessParticipant.CMN_BPT_BusinessParticipantID;
                    }
                    firstPractice.AssociatedParticipant_FunctionName  = Parameter.Practices[0].AssociatedParticipant_FunctionName;
                    firstPractice.AssociatedBusinessParticipant_RefID = practiceBPT.CMN_BPT_BusinessParticipantID;
                    firstPractice.Save(Connection, Transaction);
                }
                #endregion
            }
            else
            {
                #region Save

                //personInfo
                Guid personInfoID;
                if (Parameter.Account_RefID == Guid.Empty)
                {
                    var personInfo = new ORM_CMN_PER_PersonInfo();
                    personInfo.CMN_PER_PersonInfoID = Guid.NewGuid();
                    personInfo.FirstName            = Parameter.FirstName;
                    personInfo.LastName             = Parameter.LastName;
                    if (Parameter.isLucentisSave)
                    {
                        personInfo.PrimaryEmail = Parameter.ifLucentis_LoginEmail;
                    }
                    personInfo.Title = Parameter.Title;
                    personInfo.Creation_Timestamp = DateTime.Now;
                    personInfo.Tenant_RefID       = securityTicket.TenantID;
                    if (Parameter.isOphthalSave)
                    {
                        personInfo.Salutation_Letter  = Parameter.ifOphthal_Salutation_Letter;
                        personInfo.Salutation_General = Parameter.ifOphthal_Salutation_General;
                    }
                    personInfo.Save(Connection, Transaction);
                    personInfoID = personInfo.CMN_PER_PersonInfoID;
                }
                else
                {
                    var account2personInfoQuery = new ORM_CMN_PER_PersonInfo_2_Account.Query();
                    account2personInfoQuery.USR_Account_RefID = Parameter.Account_RefID;
                    account2personInfoQuery.Tenant_RefID      = securityTicket.TenantID;
                    account2personInfoQuery.IsDeleted         = false;
                    var account2personInfo = ORM_CMN_PER_PersonInfo_2_Account.Query.Search(Connection, Transaction, account2personInfoQuery).First();

                    var query2 = new ORM_CMN_PER_PersonInfo.Query();
                    query2.CMN_PER_PersonInfoID = account2personInfo.CMN_PER_PersonInfo_RefID;

                    var personInfo = ORM_CMN_PER_PersonInfo.Query.Search(Connection, Transaction, query2).First();
                    personInfo.FirstName = Parameter.FirstName;
                    personInfo.LastName  = Parameter.LastName;
                    personInfo.Title     = Parameter.Title;
                    personInfo.Save(Connection, Transaction);

                    personInfoID = personInfo.CMN_PER_PersonInfoID;
                }
                //bussinessParticipants
                var bussinessParticipantTable = new ORM_CMN_BPT_BusinessParticipant();
                bussinessParticipantTable.CMN_BPT_BusinessParticipantID = Guid.NewGuid();
                bussinessParticipantTable.IsNaturalPerson    = true;
                bussinessParticipantTable.IsTenant           = false;
                bussinessParticipantTable.IsCompany          = false;
                bussinessParticipantTable.IsDeleted          = false;
                bussinessParticipantTable.Creation_Timestamp = DateTime.Now;
                bussinessParticipantTable.Tenant_RefID       = securityTicket.TenantID;
                bussinessParticipantTable.IfNaturalPerson_CMN_PER_PersonInfo_RefID = personInfoID;
                bussinessParticipantTable.Save(Connection, Transaction);

                doctor.HEC_DoctorID = Guid.NewGuid();
                if (Parameter.isLucentisSave)
                {
                    doctor.DoctorIDNumber = Parameter.ifLucentis_LANR;
                }
                doctor.Creation_Timestamp        = DateTime.Now;
                doctor.Tenant_RefID              = securityTicket.TenantID;
                doctor.BusinessParticipant_RefID = bussinessParticipantTable.CMN_BPT_BusinessParticipantID;
                doctor.Account_RefID             = Parameter.Account_RefID;
                doctor.Save(Connection, Transaction);

                if (Parameter.Contacts != null)
                {
                    foreach (var contact in Parameter.Contacts)
                    {
                        ORM_CMN_PER_CommunicationContact communicationContacts = new ORM_CMN_PER_CommunicationContact();
                        communicationContacts.CMN_PER_CommunicationContactID = Guid.NewGuid();
                        communicationContacts.PersonInfo_RefID   = personInfoID;
                        communicationContacts.Contact_Type       = contact.CMN_PER_CommunicationContact_TypeID;
                        communicationContacts.Content            = contact.Content;
                        communicationContacts.Creation_Timestamp = DateTime.Now;
                        communicationContacts.Tenant_RefID       = securityTicket.TenantID;
                        communicationContacts.Save(Connection, Transaction);
                    }
                }

                foreach (var practice in Parameter.Practices)
                {
                    if (practice.isDeleted == true)
                    {
                        continue;
                    }

                    var medPract = new ORM_HEC_MedicalPractis.Query();
                    medPract.HEC_MedicalPractiseID = practice.PracticeID;
                    var medicalPractice = ORM_HEC_MedicalPractis.Query.Search(Connection, Transaction, medPract).First();

                    var queryCompanyInfo = new ORM_CMN_COM_CompanyInfo.Query();
                    queryCompanyInfo.CMN_COM_CompanyInfoID = medicalPractice.Ext_CompanyInfo_RefID;
                    var companyInfo = ORM_CMN_COM_CompanyInfo.Query.Search(Connection, Transaction, queryCompanyInfo).First();

                    var practiceQuery = new ORM_CMN_BPT_BusinessParticipant.Query();
                    practiceQuery.IfCompany_CMN_COM_CompanyInfo_RefID = companyInfo.CMN_COM_CompanyInfoID;
                    var practiceBPT = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, practiceQuery).First();

                    //associatedbusinessparticipants
                    var associatedbusinessparticipants = new ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant();
                    associatedbusinessparticipants.BusinessParticipant_RefID           = bussinessParticipantTable.CMN_BPT_BusinessParticipantID;
                    associatedbusinessparticipants.AssociatedBusinessParticipant_RefID = practiceBPT.CMN_BPT_BusinessParticipantID;
                    associatedbusinessparticipants.AssociatedParticipant_FunctionName  = practice.AssociatedParticipant_FunctionName;
                    associatedbusinessparticipants.Creation_Timestamp = DateTime.Now;
                    associatedbusinessparticipants.Tenant_RefID       = securityTicket.TenantID;
                    associatedbusinessparticipants.Save(Connection, Transaction);
                }
                #endregion
            }

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

            #endregion UserCode
        }
Beispiel #5
0
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L5PR_SP__1122 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_Guid();

            ORM_HEC_MedicalPractis item = new ORM_HEC_MedicalPractis();

            if (Parameter.PracticeID != Guid.Empty)
            {
                var result = item.Load(Connection, Transaction, Parameter.PracticeID);
                if (result.Status != FR_Status.Success || item.HEC_MedicalPractiseID == Guid.Empty)
                {
                    var error = new FR_Guid();
                    error.ErrorMessage = "No Such ID";
                    error.Status       = FR_Status.Error_Internal;
                    return(error);
                }

                #region Delete

                if (Parameter.isDeleted == true)
                {
                    var query_BP_ContactPerson_del = new ORM_CMN_BPT_BusinessParticipant.Query();
                    query_BP_ContactPerson_del.CMN_BPT_BusinessParticipantID = item.ContactPerson_RefID;
                    query_BP_ContactPerson_del.Tenant_RefID = securityTicket.TenantID;

                    var found_BP_ContactPerson_del = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, query_BP_ContactPerson_del).First();
                    found_BP_ContactPerson_del.IsDeleted = true;

                    var query_PersonInfo_del = new ORM_CMN_PER_PersonInfo.Query();
                    query_PersonInfo_del.CMN_PER_PersonInfoID = found_BP_ContactPerson_del.IfNaturalPerson_CMN_PER_PersonInfo_RefID;

                    var found_PersonInfo_del = ORM_CMN_PER_PersonInfo.Query.Search(Connection, Transaction, query_PersonInfo_del).First();
                    found_PersonInfo_del.IsDeleted = true;
                    found_PersonInfo_del.Save(Connection, Transaction);


                    var query_CommunicationContact_del = new ORM_CMN_PER_CommunicationContact.Query();
                    query_CommunicationContact_del.PersonInfo_RefID = found_PersonInfo_del.CMN_PER_PersonInfoID;


                    var found_CommunicationContact_del = ORM_CMN_PER_CommunicationContact.Query.Search(Connection, Transaction, query_CommunicationContact_del).First();
                    found_CommunicationContact_del.IsDeleted = true;

                    found_CommunicationContact_del.Save(Connection, Transaction);


                    var query_CompanyInfo_del = new ORM_CMN_COM_CompanyInfo.Query();
                    query_CompanyInfo_del.CMN_COM_CompanyInfoID = item.Ext_CompanyInfo_RefID;

                    var found_CompanyInfo_del = ORM_CMN_COM_CompanyInfo.Query.Search(Connection, Transaction, query_CompanyInfo_del).First();
                    found_CompanyInfo_del.IsDeleted = true;

                    found_CompanyInfo_del.Save(Connection, Transaction);

                    var query_BP_Company_del = new ORM_CMN_BPT_BusinessParticipant.Query();
                    query_BP_Company_del.IfCompany_CMN_COM_CompanyInfo_RefID = found_CompanyInfo_del.CMN_COM_CompanyInfoID;

                    var found_BP_Company_del = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, query_BP_Company_del).First();
                    found_BP_Company_del.IsDeleted = true;
                    found_BP_Company_del.Save(Connection, Transaction);

                    var query_UniversalContactDetails_del = new ORM_CMN_UniversalContactDetail.Query();
                    query_UniversalContactDetails_del.CMN_UniversalContactDetailID = found_CompanyInfo_del.Contact_UCD_RefID;

                    var found_UniversalCompanyDetails_del = ORM_CMN_UniversalContactDetail.Query.Search(Connection, Transaction, query_UniversalContactDetails_del).First();

                    found_UniversalCompanyDetails_del.IsDeleted = true;
                    found_UniversalCompanyDetails_del.Save(Connection, Transaction);

                    item.IsDeleted = true;
                    return(new FR_Guid(item.Save(Connection, Transaction), item.HEC_MedicalPractiseID));
                }

                #endregion
                #region Edit

                var query1 = new ORM_CMN_BPT_BusinessParticipant.Query();
                query1.CMN_BPT_BusinessParticipantID = item.ContactPerson_RefID;
                query1.Tenant_RefID    = securityTicket.TenantID;
                query1.IsCompany       = false;
                query1.IsNaturalPerson = true;
                query1.IsTenant        = false;

                var bussinessParticipant = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, query1).First();


                var query2 = new ORM_CMN_PER_PersonInfo.Query();
                query2.CMN_PER_PersonInfoID = bussinessParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID;

                var personInfo = ORM_CMN_PER_PersonInfo.Query.Search(Connection, Transaction, query2).First();

                personInfo.FirstName    = Parameter.ContactPersonFirstName;
                personInfo.LastName     = Parameter.ContactPersonLastName;
                personInfo.PrimaryEmail = Parameter.ContactPersonEmail;
                personInfo.Save(Connection, Transaction);


                var query3 = new ORM_CMN_PER_CommunicationContact.Query();
                query3.PersonInfo_RefID = personInfo.CMN_PER_PersonInfoID;


                var communicationContacts = ORM_CMN_PER_CommunicationContact.Query.Search(Connection, Transaction, query3).First();
                communicationContacts.Contact_Type = Parameter.ContactTypePhone;
                communicationContacts.Content      = Parameter.ContactPersonPhoneNumber;

                communicationContacts.Save(Connection, Transaction);


                var query4 = new ORM_CMN_COM_CompanyInfo.Query();
                query4.CMN_COM_CompanyInfoID = item.Ext_CompanyInfo_RefID;

                var companyInfo = ORM_CMN_COM_CompanyInfo.Query.Search(Connection, Transaction, query4).First();
                companyInfo.CompanyInfo_EstablishmentNumber = Parameter.BSNR;

                companyInfo.Save(Connection, Transaction);

                var query5 = new ORM_CMN_BPT_BusinessParticipant.Query();
                query5.IfCompany_CMN_COM_CompanyInfo_RefID = companyInfo.CMN_COM_CompanyInfoID;

                var extCompany = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, query5).First();
                extCompany.DisplayName = Parameter.PracticeName;
                extCompany.Save(Connection, Transaction);

                var query6 = new ORM_CMN_UniversalContactDetail.Query();
                query6.CMN_UniversalContactDetailID = companyInfo.Contact_UCD_RefID;

                var companyDetails = ORM_CMN_UniversalContactDetail.Query.Search(Connection, Transaction, query6).First();

                companyDetails.ZIP               = Parameter.ZIP;
                companyDetails.Town              = Parameter.Town;
                companyDetails.Street_Name       = Parameter.PracticeStreet;
                companyDetails.Street_Number     = Parameter.PracticeNumber;
                companyDetails.Contact_Email     = Parameter.PracitceEmail;
                companyDetails.Street_Name_Line2 = Parameter.PracticeStreet2;

                companyDetails.Save(Connection, Transaction);
                #endregion
            }
            else
            {
                #region Save

                item.HEC_MedicalPractiseID = Guid.NewGuid();

                item.Creation_Timestamp = DateTime.Now;
                item.Tenant_RefID       = securityTicket.TenantID;


                //business Participants
                ORM_CMN_BPT_BusinessParticipant contactPerson = new ORM_CMN_BPT_BusinessParticipant();
                Guid businessParticipantsID = Guid.NewGuid();

                contactPerson.CMN_BPT_BusinessParticipantID = businessParticipantsID;
                contactPerson.IsCompany          = false;
                contactPerson.IsNaturalPerson    = true;
                contactPerson.IsTenant           = false;
                contactPerson.Creation_Timestamp = DateTime.Now;
                contactPerson.Tenant_RefID       = securityTicket.TenantID;

                item.ContactPerson_RefID = businessParticipantsID;

                //person info
                ORM_CMN_PER_PersonInfo personInfo = new ORM_CMN_PER_PersonInfo();
                Guid personInfoID = Guid.NewGuid();

                contactPerson.IfNaturalPerson_CMN_PER_PersonInfo_RefID = personInfoID;
                contactPerson.Save(Connection, Transaction);

                personInfo.CMN_PER_PersonInfoID = personInfoID;
                personInfo.FirstName            = Parameter.ContactPersonFirstName;
                personInfo.LastName             = Parameter.ContactPersonLastName;
                personInfo.PrimaryEmail         = Parameter.ContactPersonEmail;
                personInfo.Creation_Timestamp   = DateTime.Now;
                personInfo.Tenant_RefID         = securityTicket.TenantID;

                personInfo.Save(Connection, Transaction);

                //Communication Contact
                ORM_CMN_PER_CommunicationContact communicationContacts = new ORM_CMN_PER_CommunicationContact();

                communicationContacts.CMN_PER_CommunicationContactID = Guid.NewGuid();
                communicationContacts.PersonInfo_RefID   = personInfoID;
                communicationContacts.Contact_Type       = Parameter.ContactTypePhone;
                communicationContacts.Content            = Parameter.ContactPersonPhoneNumber;
                communicationContacts.Creation_Timestamp = DateTime.Now;
                communicationContacts.Tenant_RefID       = securityTicket.TenantID;

                communicationContacts.Save(Connection, Transaction);


                ORM_CMN_COM_CompanyInfo companyInfo = new ORM_CMN_COM_CompanyInfo();
                Guid companyInfoID = Guid.NewGuid();
                companyInfo.CMN_COM_CompanyInfoID           = companyInfoID;
                companyInfo.Creation_Timestamp              = DateTime.Now;
                companyInfo.Tenant_RefID                    = securityTicket.TenantID;
                companyInfo.CompanyInfo_EstablishmentNumber = Parameter.BSNR;

                item.Ext_CompanyInfo_RefID = companyInfoID;


                ORM_CMN_BPT_BusinessParticipant extCompany = new ORM_CMN_BPT_BusinessParticipant();
                extCompany.CMN_BPT_BusinessParticipantID = Guid.NewGuid();
                extCompany.Creation_Timestamp            = DateTime.Now;
                extCompany.Tenant_RefID    = securityTicket.TenantID;
                extCompany.DisplayName     = Parameter.PracticeName;
                extCompany.IsCompany       = true;
                extCompany.IsNaturalPerson = false;
                extCompany.IsTenant        = false;
                extCompany.IsDeleted       = false;
                extCompany.IfCompany_CMN_COM_CompanyInfo_RefID = companyInfoID;

                extCompany.Save(Connection, Transaction);

                ORM_CMN_UniversalContactDetail companyDetails = new ORM_CMN_UniversalContactDetail();
                Guid companyDetailsID = Guid.NewGuid();

                companyInfo.Contact_UCD_RefID = companyDetailsID;
                companyInfo.Save(Connection, Transaction);

                companyDetails.CMN_UniversalContactDetailID = companyDetailsID;
                companyDetails.Tenant_RefID       = securityTicket.TenantID;
                companyDetails.Creation_Timestamp = DateTime.Now;
                companyDetails.IsCompany          = true;
                companyDetails.ZIP               = Parameter.ZIP;
                companyDetails.Town              = Parameter.Town;
                companyDetails.Street_Name       = Parameter.PracticeStreet;
                companyDetails.Street_Number     = Parameter.PracticeNumber;
                companyDetails.Contact_Email     = Parameter.PracitceEmail;
                companyDetails.Street_Name_Line2 = Parameter.PracticeStreet2;
                companyDetails.IsDeleted         = false;
                companyDetails.Save(Connection, Transaction);

                item.Save(Connection, Transaction);


                #endregion
            }

            returnValue.Result = item.HEC_MedicalPractiseID;

            return(returnValue);

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

            ORM_CMN_Address address = new ORM_CMN_Address();
            if (Parameter.CMN_AddressID != Guid.Empty)
            {
                var result = address.Load(Connection, Transaction, Parameter.CMN_AddressID);
                if (result.Status != FR_Status.Success || address.CMN_AddressID == Guid.Empty)
                {
                    var error = new FR_Guid();
                    error.ErrorMessage = "No Such ID";
                    error.Status       = FR_Status.Error_Internal;
                    return(error);
                }
            }
            address.City_Name       = Parameter.City_Name;
            address.Street_Name     = Parameter.Street_Name;
            address.Street_Number   = Parameter.Street_Number;
            address.Country_Name    = Parameter.Country_Name;
            address.Province_Name   = Parameter.Province_Name;
            address.Tenant_RefID    = securityTicket.TenantID;
            address.City_PostalCode = Parameter.City_PostalCode;
            address.Save(Connection, Transaction);
            CSV2Core.DlTrace.Trace("success address");
            ORM_CMN_PER_PersonInfo person = new ORM_CMN_PER_PersonInfo();
            if (Parameter.CMN_PER_PersonInfoID != Guid.Empty)
            {
                var result = person.Load(Connection, Transaction, Parameter.CMN_PER_PersonInfoID);
                if (result.Status != FR_Status.Success || person.CMN_PER_PersonInfoID == Guid.Empty)
                {
                    var error = new FR_Guid();
                    error.ErrorMessage = "No Such ID";
                    error.Status       = FR_Status.Error_Internal;
                    return(error);
                }
            }

            //person.AccountImage_URL = Parameter.ProfileImage_Document_RefID;
            person.FirstName     = Parameter.FirstName;
            person.LastName      = Parameter.LastName;
            person.PrimaryEmail  = Parameter.PrimaryEmail;
            person.Tenant_RefID  = securityTicket.TenantID;
            person.Title         = Parameter.Title;
            person.Address_RefID = address.CMN_AddressID;
            person.ProfileImage_Document_RefID = Parameter.ImageID;
            person.BirthDate        = Parameter.BirthDate;
            person.NumberOfChildren = Parameter.TaxInfoParameter != null ? Parameter.TaxInfoParameter.NumberOfChildren : 0;
            person.Save(Connection, Transaction);
            CSV2Core.DlTrace.Trace("success persopm");

            var contactQuery = new ORM_CMN_PER_CommunicationContact.Query();
            contactQuery.Tenant_RefID     = securityTicket.TenantID;
            contactQuery.PersonInfo_RefID = person.CMN_PER_PersonInfoID;
            contactQuery.IsDeleted        = false;
            var deleteContacts = ORM_CMN_PER_CommunicationContact.Query.SoftDelete(Connection, Transaction, contactQuery);
            foreach (var parContact in Parameter.Contacts)
            {
                ORM_CMN_PER_CommunicationContact contact = new ORM_CMN_PER_CommunicationContact();
                if (parContact.CMN_PER_CommunicationContactID != Guid.Empty)
                {
                    var result = contact.Load(Connection, Transaction, parContact.CMN_PER_CommunicationContactID);
                    if (result.Status != FR_Status.Success || contact.CMN_PER_CommunicationContactID == Guid.Empty)
                    {
                        var error = new FR_Guid();
                        error.ErrorMessage = "No Such ID";
                        error.Status       = FR_Status.Error_Internal;
                        return(error);
                    }
                }
                contact.Content          = parContact.Content;
                contact.Contact_Type     = parContact.CMN_PER_CommunicationContact_TypeID;
                contact.Tenant_RefID     = securityTicket.TenantID;
                contact.PersonInfo_RefID = person.CMN_PER_PersonInfoID;
                contact.Save(Connection, Transaction);
                CSV2Core.DlTrace.Trace("success contact");
            }

            ORM_CMN_BPT_BusinessParticipant bParticipant = new ORM_CMN_BPT_BusinessParticipant();
            if (Parameter.CMN_BPT_BusinessParticipantID != Guid.Empty)
            {
                var result = bParticipant.Load(Connection, Transaction, Parameter.CMN_BPT_BusinessParticipantID);
                if (result.Status != FR_Status.Success || bParticipant.CMN_BPT_BusinessParticipantID == Guid.Empty)
                {
                    var error = new FR_Guid();
                    error.ErrorMessage = "No Such ID";
                    error.Status       = FR_Status.Error_Internal;
                    return(error);
                }
            }
            bParticipant.DisplayName = Parameter.DisplayName;
            bParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID = person.CMN_PER_PersonInfoID;
            bParticipant.IsNaturalPerson = true;
            bParticipant.Tenant_RefID    = securityTicket.TenantID;
            bParticipant.Save(Connection, Transaction);
            ORM_CMN_BPT_EMP_Employee employee = new ORM_CMN_BPT_EMP_Employee();
            CSV2Core.DlTrace.Trace("success bpart");


            if (Parameter.CMN_BPT_EMP_EmployeeID != Guid.Empty)
            {
                var result = employee.Load(Connection, Transaction, Parameter.CMN_BPT_EMP_EmployeeID);
                if (result.Status != FR_Status.Success || employee.CMN_BPT_EMP_EmployeeID == Guid.Empty)
                {
                    var error = new FR_Guid();
                    error.ErrorMessage = "No Such ID";
                    error.Status       = FR_Status.Error_Internal;
                    return(error);
                }
            }
            employee.BusinessParticipant_RefID = bParticipant.CMN_BPT_BusinessParticipantID;
            employee.Staff_Number     = Parameter.Staff_Number;
            employee.StandardFunction = Parameter.StandardFunction;
            employee.Tenant_RefID     = securityTicket.TenantID;
            employee.Save(Connection, Transaction);
            CSV2Core.DlTrace.Trace("success employee");


            ORM_CMN_BPT_EMP_EmploymentRelationship employmentRelationship = new ORM_CMN_BPT_EMP_EmploymentRelationship();
            if (Parameter.CMN_BPT_EMP_Employee_EmploymentRelationshipID != Guid.Empty)
            {
                var result = employmentRelationship.Load(Connection, Transaction, Parameter.CMN_BPT_EMP_Employee_EmploymentRelationshipID);
                if (result.Status != FR_Status.Success || employmentRelationship.CMN_BPT_EMP_EmploymentRelationshipID == Guid.Empty)
                {
                    var error = new FR_Guid();
                    error.ErrorMessage = "No Such ID";
                    error.Status       = FR_Status.Error_Internal;
                    return(error);
                }
            }
            employmentRelationship.Work_StartDate = Parameter.Work_StartDate;

            bool resignationDateChanged = false;
            if (employmentRelationship.Work_EndDate != Parameter.Work_EndDate)
            {
                resignationDateChanged = true;
            }

            employmentRelationship.Work_EndDate   = Parameter.Work_EndDate;
            employmentRelationship.Tenant_RefID   = securityTicket.TenantID;
            employmentRelationship.Employee_RefID = employee.CMN_BPT_EMP_EmployeeID;
            employmentRelationship.Save(Connection, Transaction);
            CSV2Core.DlTrace.Trace("success employmentRelationship");

            if (Parameter.Work_EndDate.Ticks != 0)
            {
                ORM_CMN_BPT_EMP_EmploymentRelationship_2_WorkingContract.Query empRelationShipToWorkingContractQuery = new ORM_CMN_BPT_EMP_EmploymentRelationship_2_WorkingContract.Query();
                empRelationShipToWorkingContractQuery.EmploymentRelationship_RefID = employmentRelationship.CMN_BPT_EMP_EmploymentRelationshipID;
                empRelationShipToWorkingContractQuery.Tenant_RefID = securityTicket.TenantID;
                empRelationShipToWorkingContractQuery.IsDeleted    = false;
                List <ORM_CMN_BPT_EMP_EmploymentRelationship_2_WorkingContract> workingContractAssignments = ORM_CMN_BPT_EMP_EmploymentRelationship_2_WorkingContract.Query.Search(Connection, Transaction, empRelationShipToWorkingContractQuery);
                foreach (var workingContractAssignment in workingContractAssignments)
                {
                    ORM_CMN_BPT_EMP_WorkingContract workingContract = new ORM_CMN_BPT_EMP_WorkingContract();
                    if (workingContractAssignment.WorkingContract_RefID != Guid.Empty)
                    {
                        var result = workingContract.Load(Connection, Transaction, workingContractAssignment.WorkingContract_RefID);
                        if (result.Status != FR_Status.Success || workingContract.CMN_BPT_EMP_WorkingContractID == Guid.Empty)
                        {
                            var error = new FR_Guid();
                            error.ErrorMessage = "No Such ID";
                            error.Status       = FR_Status.Error_Internal;
                            return(error);
                        }
                    }
                    if (workingContract.Contract_EndDate.Ticks == 0 || workingContract.Contract_EndDate.Ticks > Parameter.Work_EndDate.Ticks)
                    {
                        workingContract.Contract_EndDate         = Parameter.Work_EndDate;
                        workingContract.IsContractEndDateDefined = true;
                        workingContract.Save(Connection, Transaction);
                    }
                }
            }

            var activeTimeFrame = cls_Get_Active_CalculationTimeFrame.Invoke(Connection, Transaction, securityTicket).Result;

            var timeframes   = cls_Get_CalculationTimeFramesForTenant.Invoke(Connection, Transaction, securityTicket).Result.ToList();
            var resultFrames = timeframes.Where(i => i.CalculationTimeframe_StartDate.Year == employmentRelationship.Work_StartDate.Year).ToList();
            if (resultFrames.Count == 0)
            {
                var newFrame    = new ORM_CMN_CAL_CalculationTimeframe();
                int currentYear = Parameter.Work_StartDate.Year;
                while (currentYear < activeTimeFrame.CalculationTimeframe_StartDate.Year)
                {
                    if (!timeframes.Any(i => i.CalculationTimeframe_StartDate.Year == currentYear))
                    {
                        newFrame.CalculationTimeframe_StartDate        = new DateTime(currentYear, 1, 1);
                        newFrame.CalculationTimeframe_EstimatedEndDate = new DateTime(currentYear, 12, 31);
                        newFrame.Tenant_RefID = securityTicket.TenantID;
                        newFrame.Save(Connection, Transaction);
                        newFrame = new ORM_CMN_CAL_CalculationTimeframe();
                    }
                    currentYear++;

                    ORM_CMN_BPT_EMP_EmploymentRelationship_Timeframe relationshipFrame = new ORM_CMN_BPT_EMP_EmploymentRelationship_Timeframe();
                    relationshipFrame.CalculationTimeframe_RefID   = newFrame.CMN_CAL_CalculationTimeframeID;
                    relationshipFrame.EmploymentRelationship_RefID = employmentRelationship.CMN_BPT_EMP_EmploymentRelationshipID;
                    relationshipFrame.Tenant_RefID = securityTicket.TenantID;
                    relationshipFrame.Save(Connection, Transaction);
                }
            }
            else
            {
                timeframes = timeframes.Where(i => i.CalculationTimeframe_StartDate.Year < activeTimeFrame.CalculationTimeframe_StartDate.Year && i.CalculationTimeframe_StartDate.Year >= employmentRelationship.Work_StartDate.Year).ToList();
                foreach (var timeframe in timeframes)
                {
                    ORM_CMN_BPT_EMP_EmploymentRelationship_Timeframe.Query relationshipFrameQuery = new ORM_CMN_BPT_EMP_EmploymentRelationship_Timeframe.Query();
                    relationshipFrameQuery.CalculationTimeframe_RefID   = timeframe.CMN_CAL_CalculationTimeframeID;
                    relationshipFrameQuery.EmploymentRelationship_RefID = employmentRelationship.CMN_BPT_EMP_EmploymentRelationshipID;
                    relationshipFrameQuery.Tenant_RefID = securityTicket.TenantID;
                    List <ORM_CMN_BPT_EMP_EmploymentRelationship_Timeframe> oldContractFrames = ORM_CMN_BPT_EMP_EmploymentRelationship_Timeframe.Query.Search(Connection, Transaction, relationshipFrameQuery);
                    if (oldContractFrames.Count == 0)
                    {
                        ORM_CMN_BPT_EMP_EmploymentRelationship_Timeframe relationshipFrame = new ORM_CMN_BPT_EMP_EmploymentRelationship_Timeframe();
                        relationshipFrame.CalculationTimeframe_RefID   = timeframe.CMN_CAL_CalculationTimeframeID;
                        relationshipFrame.EmploymentRelationship_RefID = employmentRelationship.CMN_BPT_EMP_EmploymentRelationshipID;
                        relationshipFrame.Tenant_RefID = securityTicket.TenantID;
                        relationshipFrame.Save(Connection, Transaction);
                    }
                }
            }



            P_L5EM_GAERCTFFE_1405 timeFrameParam = new P_L5EM_GAERCTFFE_1405();
            timeFrameParam.EmployeeID = employee.CMN_BPT_EMP_EmployeeID;
            L5EM_GAERCTFFE_1405 employeeTimeFrame = cls_Get_Active_EmployeeRelationshipTimeFrame_For_EmployeeID.Invoke(Connection, Transaction, timeFrameParam, securityTicket).Result;
            if (employeeTimeFrame == null)
            {
                ORM_CMN_BPT_EMP_EmploymentRelationship_Timeframe timeFrame = new ORM_CMN_BPT_EMP_EmploymentRelationship_Timeframe();
                timeFrame.CalculationTimeframe_RefID   = activeTimeFrame.CMN_CAL_CalculationTimeframeID;
                timeFrame.EmploymentRelationship_RefID = employmentRelationship.CMN_BPT_EMP_EmploymentRelationshipID;
                timeFrame.Tenant_RefID = securityTicket.TenantID;
                timeFrame.Save(Connection, Transaction);
                CSV2Core.DlTrace.Trace("success timeFrame");
            }

            //save employee professions
            P_L5EM_SEP_1447 saveProfessionsPar = new P_L5EM_SEP_1447();
            saveProfessionsPar.Employee_RefID    = employee.CMN_BPT_EMP_EmployeeID;
            saveProfessionsPar.FunctionHistories = Parameter.FunctionHistories;
            cls_Save_EmployeeFunctionHistory.Invoke(Connection, Transaction, saveProfessionsPar, securityTicket);
            CSV2Core.DlTrace.Trace("success employee function history");

            //save workplace histories
            P_L5EM_SWPH_1625 saveWorkplaceHistoryPar = new P_L5EM_SWPH_1625();
            saveWorkplaceHistoryPar.Employee_RefID     = employee.CMN_BPT_EMP_EmployeeID;
            saveWorkplaceHistoryPar.WorkplaceHistories = Parameter.WorkplaceHistories;
            cls_Save_WorkplaceHistories.Invoke(Connection, Transaction, saveWorkplaceHistoryPar, securityTicket);
            CSV2Core.DlTrace.Trace("success workplaceHistories");

            P_L5EM_SUED_1648 saveDocuments = new P_L5EM_SUED_1648();
            saveDocuments.Employee_RefID = employee.CMN_BPT_EMP_EmployeeID;
            saveDocuments.Documents      = Parameter.Documents;
            cls_Save_Uploaded_Employee_Document.Invoke(Connection, Transaction, saveDocuments, securityTicket);
            CSV2Core.DlTrace.Trace("success documents");

            P_L5EM_SEQS_0959 saveSkillsPar = new P_L5EM_SEQS_0959();
            saveSkillsPar.Employee_RefID = employee.CMN_BPT_EMP_EmployeeID;
            saveSkillsPar.Skills         = Parameter.Skills;
            cls_Save_Employee_QualificationSkills.Invoke(Connection, Transaction, saveSkillsPar, securityTicket);
            CSV2Core.DlTrace.Trace("success skills");

            CSV2Core.DlTrace.Trace("AccountID :" + Parameter.USR_AccountID);

            if (Parameter.USR_AccountID != null && Parameter.USR_AccountID != Guid.Empty)
            {
                CSV2Core.DlTrace.Trace("success param");

                ORM_USR_Account account = new ORM_USR_Account();
                if (Parameter.USR_AccountID != Guid.Empty)
                {
                    var result = account.Load(Connection, Transaction, Parameter.USR_AccountID);
                    if (account.USR_AccountID == Guid.Empty)
                    {
                        account.USR_AccountID = Guid.NewGuid();
                        account.AccountType   = 2;
                    }
                    account.Username = Parameter.username;
                    account.DefaultLanguage_RefID     = Parameter.LanguageID;
                    account.BusinessParticipant_RefID = bParticipant.CMN_BPT_BusinessParticipantID;
                    account.Tenant_RefID = securityTicket.TenantID;
                    account.Save(Connection, Transaction);


                    var personToAccountQuery = new ORM_CMN_PER_PersonInfo_2_Account.Query();
                    personToAccountQuery.Tenant_RefID      = securityTicket.TenantID;
                    personToAccountQuery.USR_Account_RefID = account.USR_AccountID;
                    personToAccountQuery.IsDeleted         = false;
                    var personToAccounts = ORM_CMN_PER_PersonInfo_2_Account.Query.Search(Connection, Transaction, personToAccountQuery);
                    if (personToAccounts.Count != 0)
                    {
                        ORM_CMN_PER_PersonInfo_2_Account personToAccount = personToAccounts[0];
                        personToAccount.CMN_PER_PersonInfo_RefID = person.CMN_PER_PersonInfoID;
                        personToAccount.USR_Account_RefID        = account.USR_AccountID;
                        personToAccount.Tenant_RefID             = securityTicket.TenantID;
                        personToAccount.Save(Connection, Transaction);
                    }
                    else
                    {
                        ORM_CMN_PER_PersonInfo_2_Account personToAccount = new ORM_CMN_PER_PersonInfo_2_Account();
                        personToAccount.CMN_PER_PersonInfo_RefID = person.CMN_PER_PersonInfoID;
                        personToAccount.USR_Account_RefID        = account.USR_AccountID;
                        personToAccount.Tenant_RefID             = securityTicket.TenantID;
                        personToAccount.Save(Connection, Transaction);
                    }
                }



                if (Parameter.Rights != null)
                {
                    foreach (var rightsParam in Parameter.Rights)
                    {
                        if (rightsParam.RightAssinmentID == Guid.Empty && rightsParam.RightID != Guid.Empty)
                        {
                            var right2account = new ORM_USR_Account_2_FunctionLevelRight();
                            right2account.Account_RefID            = Parameter.USR_AccountID;
                            right2account.FunctionLevelRight_RefID = rightsParam.RightID;
                            right2account.Tenant_RefID             = securityTicket.TenantID;
                            right2account.Save(Connection, Transaction);
                            CSV2Core.DlTrace.Trace("success save right: " + right2account.FunctionLevelRight_RefID);
                        }

                        if (rightsParam.RightAssinmentID != Guid.Empty && rightsParam.RightID == Guid.Empty)
                        {
                            var right2account = new ORM_USR_Account_2_FunctionLevelRight();
                            if (rightsParam.RightAssinmentID != Guid.Empty)
                            {
                                var result = right2account.Load(Connection, Transaction, rightsParam.RightAssinmentID);
                                if (result.Status != FR_Status.Success || right2account.AssignmentID == Guid.Empty)
                                {
                                    var error = new FR_Guid();
                                    error.ErrorMessage = "No Such ID";
                                    error.Status       = FR_Status.Error_Internal;
                                    return(error);
                                }
                            }
                            right2account.IsDeleted = true;
                            right2account.Save(Connection, Transaction);
                            CSV2Core.DlTrace.Trace("success save right2Acc: " + right2account.FunctionLevelRight_RefID);
                        }
                    }
                }
            }

            CSV2Core.DlTrace.Trace("emp id wtf " + Parameter.CMN_BPT_EMP_EmployeeID);

            if (Parameter.CMN_BPT_EMP_EmployeeID == Guid.Empty || resignationDateChanged)
            {
                var enterpriseService          = InfrastructureFactory.CreateEnterpriseService();
                KeyPerformanceIndicator action = new KeyPerformanceIndicator();
                action.PerformedByAccountID     = securityTicket.AccountID;
                action.PerformedByApplicationID = Parameter.ApplicationID;
                action.PerformedOn               = DateTime.Now;
                action.PerformedByTenantID       = securityTicket.TenantID;
                action.KeyPerformanceIndicatorID = Guid.Parse("4dda967a-5399-4929-afae-7af64699895b");
                action.Value = cls_Get_Employees_For_Tenant.Invoke(Connection, Transaction, securityTicket).Result.Where(i => i.Work_EndDate.Ticks == 0 || i.Work_EndDate.Ticks > DateTime.Now.Ticks).ToArray().Length;

                var result = enterpriseService.SendMessage(action.ToPayload(), KeyPerformanceIndicator.MESSAGE_TYPE, Parameter.ApplicationID, EMessageRecipient.CUSTOMER_MANAGEMENT_PLATFORM);
                // ServerLog.Instance.Info("Enterprise message sending " + (result.Code == 200 ? "successful" : "failed"));
                CSV2Core.DlTrace.Trace("success send kpi");
            }

            Parameter.TaxInfoParameter.EmployeeID = employee.CMN_BPT_EMP_EmployeeID;
            Parameter.TaxInfoParameter.CMN_BPT_BusinessParticipantID = bParticipant.CMN_BPT_BusinessParticipantID;
            cls_Save_Employee_TaxInformation.Invoke(Connection, Transaction, Parameter.TaxInfoParameter, securityTicket);
            cls_Save_Employee_BankAccount.Invoke(Connection, Transaction, Parameter.BankAccountParameter, securityTicket);
            Parameter.SocialSecurity.CMN_PER_PersonInfoID          = person.CMN_PER_PersonInfoID;
            Parameter.SocialSecurity.CMN_BPT_EMP_EmployeeID        = employee.CMN_BPT_EMP_EmployeeID;
            Parameter.SocialSecurity.CMN_BPT_BusinessParticipantID = bParticipant.CMN_BPT_BusinessParticipantID;
            cls_Save_Employee_SocialSecurity.Invoke(Connection, Transaction, Parameter.SocialSecurity, securityTicket);

            returnValue.Result = employee.CMN_BPT_EMP_EmployeeID;
            return(returnValue);

            #endregion UserCode
        }
Beispiel #7
0
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L5PH_SP_1226 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_Guid();

            ORM_HEC_Pharmacy item = new ORM_HEC_Pharmacy();


            if (Parameter.PharmacyID != Guid.Empty)
            {
                var result = item.Load(Connection, Transaction, Parameter.PharmacyID);
                if (result.Status != FR_Status.Success)
                {
                    var error = new FR_Guid();
                    error.ErrorMessage = "No Such ID";
                    error.Status       = FR_Status.Error_Internal;
                    return(error);
                }

                #region Delete

                if (Parameter.IsDeleted)
                {
                    //Contact person data finding and deleting
                    var query_BusinessParticipant_ContactPerson_del = new ORM_CMN_BPT_BusinessParticipant.Query();
                    query_BusinessParticipant_ContactPerson_del.CMN_BPT_BusinessParticipantID = item.ContactPerson_BusinessParticipant_RefID;
                    query_BusinessParticipant_ContactPerson_del.Tenant_RefID = item.Tenant_RefID;

                    ORM_CMN_BPT_BusinessParticipant found_BusinessParticipant_ContactPerson_del = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, query_BusinessParticipant_ContactPerson_del).First();
                    found_BusinessParticipant_ContactPerson_del.IsDeleted = true;

                    var query_PersonInfo_del = new ORM_CMN_PER_PersonInfo.Query();
                    query_PersonInfo_del.CMN_PER_PersonInfoID = found_BusinessParticipant_ContactPerson_del.IfNaturalPerson_CMN_PER_PersonInfo_RefID;

                    ORM_CMN_PER_PersonInfo found_PersonInfo_del = ORM_CMN_PER_PersonInfo.Query.Search(Connection, Transaction, query_PersonInfo_del).First();
                    found_PersonInfo_del.IsDeleted = true;

                    var query_CommunicationContact_del = new ORM_CMN_PER_CommunicationContact.Query();
                    query_CommunicationContact_del.PersonInfo_RefID = found_PersonInfo_del.CMN_PER_PersonInfoID;

                    ORM_CMN_PER_CommunicationContact found_CommunicationContact_del = ORM_CMN_PER_CommunicationContact.Query.Search(Connection, Transaction, query_CommunicationContact_del).First();
                    found_CommunicationContact_del.IsDeleted = true;

                    found_BusinessParticipant_ContactPerson_del.Save(Connection, Transaction);
                    found_CommunicationContact_del.Save(Connection, Transaction);
                    found_PersonInfo_del.Save(Connection, Transaction);


                    //Company (pharmacy) finding and deleting

                    var query_CompanyInfo_del = new ORM_CMN_COM_CompanyInfo.Query();
                    query_CompanyInfo_del.CMN_COM_CompanyInfoID = item.Ext_CompanyInfo_RefID;

                    ORM_CMN_COM_CompanyInfo found_CompanyInfo_del = ORM_CMN_COM_CompanyInfo.Query.Search(Connection, Transaction, query_CompanyInfo_del).First();
                    found_CompanyInfo_del.IsDeleted = true;

                    var query_CompanyContactDetails_del = new ORM_CMN_UniversalContactDetail.Query();
                    query_CompanyContactDetails_del.CMN_UniversalContactDetailID = found_CompanyInfo_del.Contact_UCD_RefID;

                    ORM_CMN_UniversalContactDetail found_CompanyContactDetails_del = ORM_CMN_UniversalContactDetail.Query.Search(Connection, Transaction, query_CompanyContactDetails_del).First();
                    found_CompanyContactDetails_del.IsDeleted = true;

                    var query_BusinessParticipant_Company_del = new ORM_CMN_BPT_BusinessParticipant.Query();
                    query_BusinessParticipant_Company_del.IfCompany_CMN_COM_CompanyInfo_RefID = found_CompanyInfo_del.CMN_COM_CompanyInfoID;

                    ORM_CMN_BPT_BusinessParticipant found_BusinessParticipant_Company_del = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, query_BusinessParticipant_Company_del).First();
                    found_BusinessParticipant_Company_del.IsDeleted = true;

                    found_CompanyInfo_del.Save(Connection, Transaction);
                    found_CompanyContactDetails_del.Save(Connection, Transaction);
                    found_BusinessParticipant_Company_del.Save(Connection, Transaction);

                    item.IsDeleted = true;
                    return(new FR_Guid(item.Save(Connection, Transaction), item.HEC_PharmacyID));
                }

                #endregion

                #region Edit

                //Contact person data finding and edit

                var query_BusinessParticipant_ContactPerson = new ORM_CMN_BPT_BusinessParticipant.Query();
                query_BusinessParticipant_ContactPerson.CMN_BPT_BusinessParticipantID = item.ContactPerson_BusinessParticipant_RefID;
                query_BusinessParticipant_ContactPerson.Tenant_RefID = item.Tenant_RefID;

                ORM_CMN_BPT_BusinessParticipant found_BusinessParticipant_ContactPerson = new ORM_CMN_BPT_BusinessParticipant();
                if (item.ContactPerson_BusinessParticipant_RefID != null && item.ContactPerson_BusinessParticipant_RefID != Guid.Empty)
                {
                    found_BusinessParticipant_ContactPerson = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, query_BusinessParticipant_ContactPerson).First();
                }
                else
                {
                    found_BusinessParticipant_ContactPerson = null;
                }
                if (found_BusinessParticipant_ContactPerson != null)
                {
                    var query_PersonInfo = new ORM_CMN_PER_PersonInfo.Query();
                    query_PersonInfo.CMN_PER_PersonInfoID = found_BusinessParticipant_ContactPerson.IfNaturalPerson_CMN_PER_PersonInfo_RefID;

                    ORM_CMN_PER_PersonInfo found_PersonInfo = ORM_CMN_PER_PersonInfo.Query.Search(Connection, Transaction, query_PersonInfo).First();
                    found_PersonInfo.FirstName    = Parameter.ContactFirstName;
                    found_PersonInfo.LastName     = Parameter.ContactLastName;
                    found_PersonInfo.PrimaryEmail = Parameter.ContactEmail;

                    var query_CommunicationContact = new ORM_CMN_PER_CommunicationContact.Query();
                    query_CommunicationContact.PersonInfo_RefID = found_PersonInfo.CMN_PER_PersonInfoID;

                    ORM_CMN_PER_CommunicationContact found_CommunicationContact = ORM_CMN_PER_CommunicationContact.Query.Search(Connection, Transaction, query_CommunicationContact).First();
                    found_CommunicationContact.Content = Parameter.ContactPhoneNumber;

                    found_CommunicationContact.Save(Connection, Transaction);
                    found_PersonInfo.Save(Connection, Transaction);
                }
                else
                {
                    ORM_CMN_BPT_BusinessParticipant contactPerson = new ORM_CMN_BPT_BusinessParticipant();
                    Guid businessParticipantID = Guid.NewGuid();

                    contactPerson.CMN_BPT_BusinessParticipantID = businessParticipantID;
                    contactPerson.IsCompany          = false;
                    contactPerson.IsNaturalPerson    = true;
                    contactPerson.IsTenant           = false;
                    contactPerson.Creation_Timestamp = DateTime.Now;
                    contactPerson.Tenant_RefID       = securityTicket.TenantID;

                    item.ContactPerson_BusinessParticipant_RefID = businessParticipantID;

                    //person info
                    ORM_CMN_PER_PersonInfo personInfo = new ORM_CMN_PER_PersonInfo();
                    Guid personInfoID = Guid.NewGuid();

                    contactPerson.IfNaturalPerson_CMN_PER_PersonInfo_RefID = personInfoID;
                    contactPerson.Save(Connection, Transaction);

                    personInfo.CMN_PER_PersonInfoID = personInfoID;
                    personInfo.FirstName            = Parameter.ContactFirstName;
                    personInfo.LastName             = Parameter.ContactLastName;
                    personInfo.PrimaryEmail         = Parameter.ContactEmail;
                    personInfo.Creation_Timestamp   = DateTime.Now;
                    personInfo.Tenant_RefID         = securityTicket.TenantID;

                    personInfo.Save(Connection, Transaction);

                    //Communication Contact
                    ORM_CMN_PER_CommunicationContact communicationContacts = new ORM_CMN_PER_CommunicationContact();

                    communicationContacts.CMN_PER_CommunicationContactID = Guid.NewGuid();
                    communicationContacts.PersonInfo_RefID   = personInfoID;
                    communicationContacts.Contact_Type       = Parameter.ContactTypePhone;
                    communicationContacts.Content            = Parameter.ContactPhoneNumber;
                    communicationContacts.Creation_Timestamp = DateTime.Now;
                    communicationContacts.Tenant_RefID       = securityTicket.TenantID;

                    communicationContacts.Save(Connection, Transaction);


                    item.ContactPerson_BusinessParticipant_RefID = businessParticipantID;
                    item.Save(Connection, Transaction);
                }

                //Company (pharmacy) finding and edit

                var query_CompanyInfo = new ORM_CMN_COM_CompanyInfo.Query();
                query_CompanyInfo.CMN_COM_CompanyInfoID = item.Ext_CompanyInfo_RefID;

                ORM_CMN_COM_CompanyInfo found_CompanyInfo = ORM_CMN_COM_CompanyInfo.Query.Search(Connection, Transaction, query_CompanyInfo).First();

                var query_CompanyContactDetails = new ORM_CMN_UniversalContactDetail.Query();
                query_CompanyContactDetails.CMN_UniversalContactDetailID = found_CompanyInfo.Contact_UCD_RefID;

                ORM_CMN_UniversalContactDetail found_CompanyContactDetails = ORM_CMN_UniversalContactDetail.Query.Search(Connection, Transaction, query_CompanyContactDetails).FirstOrDefault();
                if (found_CompanyContactDetails != null)
                {
                    found_CompanyContactDetails.CompanyName_Line1 = Parameter.PharmacyName;
                    found_CompanyContactDetails.Contact_Email     = Parameter.MainEmail;
                    found_CompanyContactDetails.Street_Name       = Parameter.Street;
                    found_CompanyContactDetails.Street_Number     = Parameter.Number;
                    found_CompanyContactDetails.Street_Name_Line2 = Parameter.Street2;
                    found_CompanyContactDetails.Town = Parameter.Town;
                    found_CompanyContactDetails.ZIP  = Parameter.ZIP;
                    found_CompanyContactDetails.Save(Connection, Transaction);
                }
                else
                {
                    ORM_CMN_UniversalContactDetail universalContactDetails = new ORM_CMN_UniversalContactDetail();
                    universalContactDetails.CMN_UniversalContactDetailID = Guid.NewGuid();
                    universalContactDetails.IsCompany         = true;
                    universalContactDetails.CompanyName_Line1 = Parameter.PharmacyName;
                    universalContactDetails.Street_Name       = Parameter.Street;
                    universalContactDetails.Street_Name_Line2 = Parameter.Street2;
                    universalContactDetails.Street_Number     = Parameter.Number;
                    universalContactDetails.Contact_Email     = Parameter.MainEmail;
                    universalContactDetails.ZIP          = Parameter.ZIP;
                    universalContactDetails.Town         = Parameter.Town;
                    universalContactDetails.Tenant_RefID = securityTicket.TenantID;


                    universalContactDetails.Save(Connection, Transaction);

                    found_CompanyInfo.Contact_UCD_RefID = universalContactDetails.CMN_UniversalContactDetailID;
                    found_CompanyInfo.Save(Connection, Transaction);
                }

                var query_BusinessParticipant_Company = new ORM_CMN_BPT_BusinessParticipant.Query();
                query_BusinessParticipant_Company.IfCompany_CMN_COM_CompanyInfo_RefID = found_CompanyInfo.CMN_COM_CompanyInfoID;

                ORM_CMN_BPT_BusinessParticipant found_BusinessParticipant_Company = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, query_BusinessParticipant_Company).First();
                found_BusinessParticipant_Company.DisplayName = Parameter.PharmacyName;


                found_BusinessParticipant_Company.Save(Connection, Transaction);

                #endregion
            }
            else
            {
                #region Save

                item.HEC_PharmacyID = Guid.NewGuid();

                item.Creation_Timestamp = DateTime.Now;
                item.Tenant_RefID       = securityTicket.TenantID;

                //business Participants
                ORM_CMN_BPT_BusinessParticipant contactPerson = new ORM_CMN_BPT_BusinessParticipant();
                Guid businessParticipantID = Guid.NewGuid();

                contactPerson.CMN_BPT_BusinessParticipantID = businessParticipantID;
                contactPerson.IsCompany          = false;
                contactPerson.IsNaturalPerson    = true;
                contactPerson.IsTenant           = false;
                contactPerson.Creation_Timestamp = DateTime.Now;
                contactPerson.Tenant_RefID       = securityTicket.TenantID;

                item.ContactPerson_BusinessParticipant_RefID = businessParticipantID;

                //person info
                ORM_CMN_PER_PersonInfo personInfo = new ORM_CMN_PER_PersonInfo();
                Guid personInfoID = Guid.NewGuid();

                contactPerson.IfNaturalPerson_CMN_PER_PersonInfo_RefID = personInfoID;
                contactPerson.Save(Connection, Transaction);

                personInfo.CMN_PER_PersonInfoID = personInfoID;
                personInfo.FirstName            = Parameter.ContactFirstName;
                personInfo.LastName             = Parameter.ContactLastName;
                personInfo.PrimaryEmail         = Parameter.ContactEmail;
                personInfo.Creation_Timestamp   = DateTime.Now;
                personInfo.Tenant_RefID         = securityTicket.TenantID;

                personInfo.Save(Connection, Transaction);

                //Communication Contact
                ORM_CMN_PER_CommunicationContact communicationContacts = new ORM_CMN_PER_CommunicationContact();

                communicationContacts.CMN_PER_CommunicationContactID = Guid.NewGuid();
                communicationContacts.PersonInfo_RefID   = personInfoID;
                communicationContacts.Contact_Type       = Parameter.ContactTypePhone;
                communicationContacts.Content            = Parameter.ContactPhoneNumber;
                communicationContacts.Creation_Timestamp = DateTime.Now;
                communicationContacts.Tenant_RefID       = securityTicket.TenantID;

                communicationContacts.Save(Connection, Transaction);

                //ORM_CMN_COM_CompanyInfo companyInfo = new ORM_CMN_COM_CompanyInfo();
                //Guid companyInfoID = Guid.NewGuid();

                ORM_CMN_COM_CompanyInfo extCompanyInfo = new ORM_CMN_COM_CompanyInfo();
                Guid extCompanyInfoID = Guid.NewGuid();
                extCompanyInfo.CMN_COM_CompanyInfoID = extCompanyInfoID;
                Guid contactUCDID = Guid.NewGuid();
                extCompanyInfo.Contact_UCD_RefID  = contactUCDID;
                extCompanyInfo.Creation_Timestamp = DateTime.Now;
                extCompanyInfo.Tenant_RefID       = securityTicket.TenantID;

                item.Ext_CompanyInfo_RefID = extCompanyInfoID;

                ORM_CMN_UniversalContactDetail universalContactDetails = new ORM_CMN_UniversalContactDetail();
                universalContactDetails.CMN_UniversalContactDetailID = contactUCDID;
                universalContactDetails.IsCompany         = true;
                universalContactDetails.CompanyName_Line1 = Parameter.PharmacyName;
                universalContactDetails.Street_Name       = Parameter.Street;
                universalContactDetails.Street_Name_Line2 = Parameter.Street2;
                universalContactDetails.Street_Number     = Parameter.Number;
                universalContactDetails.Contact_Email     = Parameter.MainEmail;
                universalContactDetails.ZIP          = Parameter.ZIP;
                universalContactDetails.Town         = Parameter.Town;
                universalContactDetails.Tenant_RefID = securityTicket.TenantID;

                ORM_CMN_BPT_BusinessParticipant extCompany = new ORM_CMN_BPT_BusinessParticipant();
                extCompany.CMN_BPT_BusinessParticipantID = Guid.NewGuid();
                extCompany.Creation_Timestamp            = DateTime.Now;
                extCompany.Tenant_RefID    = securityTicket.TenantID;
                extCompany.DisplayName     = Parameter.PharmacyName;
                extCompany.IsCompany       = true;
                extCompany.IsNaturalPerson = false;
                extCompany.IsTenant        = false;
                extCompany.IsDeleted       = false;
                extCompany.IfCompany_CMN_COM_CompanyInfo_RefID = extCompanyInfoID;

                extCompany.Save(Connection, Transaction);
                extCompanyInfo.Save(Connection, Transaction);
                universalContactDetails.Save(Connection, Transaction);
                item.Save(Connection, Transaction);

                returnValue.Result = item.HEC_PharmacyID;
                #endregion
            }

            return(returnValue);

            #endregion UserCode
        }
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L6PA_DMSP_1546 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            //Leave UserCode region to enable user code saving
            #region UserCode
            var returnValue = new FR_Guid();

            P_L6PA_GMSPfID_1538 getParam = new P_L6PA_GMSPfID_1538();
            getParam.HEC_PatientID = Parameter.HEC_PatientID;
            var patient = cls_Get_MS_Patients_For_ID.Invoke(Connection, Transaction, getParam, securityTicket).Result;
            if (patient != null)
            {
                if (patient.Addresses != null)
                {
                    foreach (var address in patient.Addresses)
                    {
                        P_L6PA_MSVAFP_1545 delAddressPar = new P_L6PA_MSVAFP_1545();
                        delAddressPar.CMN_AddressID = address.CMN_AddressID;
                        cls_Delete_MS_AddressForPatient.Invoke(Connection, Transaction, delAddressPar, securityTicket);
                    }
                }
                if (patient.Contacts != null)
                {
                    foreach (var contact in patient.Contacts)
                    {
                        ORM_CMN_PER_CommunicationContact.Query cQuery = new ORM_CMN_PER_CommunicationContact.Query();
                        cQuery.CMN_PER_CommunicationContactID = contact.CMN_PER_CommunicationContactID;
                        cQuery.IsDeleted    = false;
                        cQuery.Tenant_RefID = securityTicket.TenantID;
                        ORM_CMN_PER_CommunicationContact.Query.SoftDelete(Connection, Transaction, cQuery);
                    }
                }

                ORM_CMN_PER_PersonInfo.Query ORM_CMN_PER_PersonInfoQuery = new ORM_CMN_PER_PersonInfo.Query();
                ORM_CMN_PER_PersonInfoQuery.CMN_PER_PersonInfoID = patient.CMN_PER_PersonInfoID;
                ORM_CMN_PER_PersonInfoQuery.IsDeleted            = false;
                ORM_CMN_PER_PersonInfoQuery.Tenant_RefID         = securityTicket.TenantID;
                ORM_CMN_PER_PersonInfo.Query.SoftDelete(Connection, Transaction, ORM_CMN_PER_PersonInfoQuery);

                ORM_CMN_BPT_BusinessParticipant.Query ORM_CMN_BPT_BusinessParticipantoQuery = new ORM_CMN_BPT_BusinessParticipant.Query();
                ORM_CMN_BPT_BusinessParticipantoQuery.CMN_BPT_BusinessParticipantID = patient.CMN_BPT_BusinessParticipantID;
                ORM_CMN_BPT_BusinessParticipantoQuery.IsDeleted    = false;
                ORM_CMN_BPT_BusinessParticipantoQuery.Tenant_RefID = securityTicket.TenantID;
                ORM_CMN_BPT_BusinessParticipant.Query.SoftDelete(Connection, Transaction, ORM_CMN_BPT_BusinessParticipantoQuery);

                ORM_HEC_Patient.Query ORM_HEC_PatientQuery = new ORM_HEC_Patient.Query();
                ORM_HEC_PatientQuery.HEC_PatientID = patient.HEC_PatientID;
                ORM_HEC_PatientQuery.IsDeleted     = false;
                ORM_HEC_PatientQuery.Tenant_RefID  = securityTicket.TenantID;
                ORM_HEC_Patient.Query.SoftDelete(Connection, Transaction, ORM_HEC_PatientQuery);

                ORM_HEC_Patient_HealthInsurance.Query ORM_HEC_Patient_HealthInsuranceoQuery = new ORM_HEC_Patient_HealthInsurance.Query();
                ORM_HEC_Patient_HealthInsuranceoQuery.HEC_Patient_HealthInsurancesID = patient.HEC_Patient_HealthInsurancesID;
                ORM_HEC_Patient_HealthInsuranceoQuery.IsDeleted    = false;
                ORM_HEC_Patient_HealthInsuranceoQuery.Tenant_RefID = securityTicket.TenantID;
                ORM_HEC_Patient_HealthInsurance.Query.SoftDelete(Connection, Transaction, ORM_HEC_Patient_HealthInsuranceoQuery);

                ORM_HEC_STU_Study_ParticipatingPatient.Query ORM_HEC_STU_Study_ParticipatingPatientQuery = new ORM_HEC_STU_Study_ParticipatingPatient.Query();
                ORM_HEC_STU_Study_ParticipatingPatientQuery.HEC_STU_Study_ParticipatingPatientID = patient.HEC_STU_Study_ParticipatingPatientID;
                ORM_HEC_STU_Study_ParticipatingPatientQuery.IsDeleted    = false;
                ORM_HEC_STU_Study_ParticipatingPatientQuery.Tenant_RefID = securityTicket.TenantID;
                ORM_HEC_STU_Study_ParticipatingPatient.Query.SoftDelete(Connection, Transaction, ORM_HEC_STU_Study_ParticipatingPatientQuery);

                ORM_USR_Account.Query ORM_USR_AccountQuery = new ORM_USR_Account.Query();
                ORM_USR_AccountQuery.IsDeleted    = false;
                ORM_USR_AccountQuery.Tenant_RefID = securityTicket.TenantID;
                ORM_USR_AccountQuery.BusinessParticipant_RefID = patient.CMN_BPT_BusinessParticipantID;
                ORM_USR_Account.Query.SoftDelete(Connection, Transaction, ORM_USR_AccountQuery);

                var accountRes = ORM_USR_Account.Query.Search(Connection, Transaction, ORM_USR_AccountQuery);

                if (accountRes.Count == 1)
                {
                    ORM_USR_Device_AccountCode.Query ORM_USR_Device_AccountCodeQuery = new ORM_USR_Device_AccountCode.Query();
                    ORM_USR_Device_AccountCodeQuery.IsDeleted     = false;
                    ORM_USR_Device_AccountCodeQuery.Tenant_RefID  = securityTicket.TenantID;
                    ORM_USR_Device_AccountCodeQuery.Account_RefID = accountRes[0].USR_AccountID;
                }
            }

            return(returnValue);

            #endregion UserCode
        }
Beispiel #9
0
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L6PA_SMSP_1548 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            //Leave UserCode region to enable user code saving
            #region UserCode
            var             returnValue = new FR_Guid();
            ORM_HEC_Patient patient     = new ORM_HEC_Patient();
            if (Parameter.HEC_PatientID != Guid.Empty)
            {
                var result = patient.Load(Connection, Transaction, Parameter.HEC_PatientID);
                if (result.Status != FR_Status.Success || patient.HEC_PatientID == Guid.Empty)
                {
                    var error = new FR_Guid();
                    error.ErrorMessage = "No Such ID";
                    error.Status       = FR_Status.Error_Internal;
                    return(error);
                }
            }
            patient.IsPatientParticipationPolicyValidated = Parameter.HasFulfilledParticipationPolicyRequirements;
            patient.PatientComment = Parameter.Comment;
            patient.Tenant_RefID   = securityTicket.TenantID;

            ORM_CMN_BPT_BusinessParticipant bParticipant = new ORM_CMN_BPT_BusinessParticipant();
            if (patient.CMN_BPT_BusinessParticipant_RefID != Guid.Empty)
            {
                var result = bParticipant.Load(Connection, Transaction, patient.CMN_BPT_BusinessParticipant_RefID);
                if (result.Status != FR_Status.Success || bParticipant.CMN_BPT_BusinessParticipantID == Guid.Empty)
                {
                    var error = new FR_Guid();
                    error.ErrorMessage = "No Such ID";
                    error.Status       = FR_Status.Error_Internal;
                    return(error);
                }
            }
            bParticipant.IsNaturalPerson = true;
            bParticipant.Tenant_RefID    = securityTicket.TenantID;


            ORM_CMN_PER_PersonInfo person = new ORM_CMN_PER_PersonInfo();
            if (bParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID != Guid.Empty)
            {
                var result = person.Load(Connection, Transaction, bParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID);
                if (result.Status != FR_Status.Success || person.CMN_PER_PersonInfoID == Guid.Empty)
                {
                    var error = new FR_Guid();
                    error.ErrorMessage = "No Such ID";
                    error.Status       = FR_Status.Error_Internal;
                    return(error);
                }
            }
            person.FirstName          = Parameter.FirstName;
            person.LastName           = Parameter.LastName;
            person.PrimaryEmail       = Parameter.Mail;
            person.Tenant_RefID       = securityTicket.TenantID;
            person.Salutation_General = Parameter.Salutation;
            person.Save(Connection, Transaction);

            bParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID = person.CMN_PER_PersonInfoID;
            bParticipant.Save(Connection, Transaction);

            patient.CMN_BPT_BusinessParticipant_RefID = bParticipant.CMN_BPT_BusinessParticipantID;
            patient.Save(Connection, Transaction);

            ORM_CMN_PER_CommunicationContact.Query contactQuery = new ORM_CMN_PER_CommunicationContact.Query();
            contactQuery.IsDeleted        = false;
            contactQuery.Tenant_RefID     = securityTicket.TenantID;
            contactQuery.PersonInfo_RefID = person.CMN_PER_PersonInfoID;
            var contactQueryRes = ORM_CMN_PER_CommunicationContact.Query.Search(Connection, Transaction, contactQuery);
            ORM_CMN_PER_CommunicationContact contactPhone = contactQueryRes.FirstOrDefault(c => c.Contact_Type == STLD_ContactTypes.Phone);
            ORM_CMN_PER_CommunicationContact contactFax   = contactQueryRes.FirstOrDefault(c => c.Contact_Type == STLD_ContactTypes.Fax);

            if (contactPhone == null)
            {
                contactPhone = new ORM_CMN_PER_CommunicationContact();
                contactPhone.Contact_Type     = STLD_ContactTypes.Phone;
                contactPhone.Tenant_RefID     = securityTicket.TenantID;
                contactPhone.PersonInfo_RefID = person.CMN_PER_PersonInfoID;
            }
            contactPhone.Content = Parameter.Phone;
            contactPhone.Save(Connection, Transaction);

            if (contactFax == null)
            {
                contactFax                  = new ORM_CMN_PER_CommunicationContact();
                contactFax.Content          = Parameter.Fax;
                contactFax.Contact_Type     = STLD_ContactTypes.Fax;
                contactFax.Tenant_RefID     = securityTicket.TenantID;
                contactFax.PersonInfo_RefID = person.CMN_PER_PersonInfoID;
                contactFax.Save(Connection, Transaction);
            }
            contactFax.Content = Parameter.Fax;
            contactFax.Save(Connection, Transaction);

            ORM_HEC_Patient_HealthInsurance.Query Patient_HealthInsuranceQuery = new ORM_HEC_Patient_HealthInsurance.Query();
            Patient_HealthInsuranceQuery.Tenant_RefID  = securityTicket.TenantID;
            Patient_HealthInsuranceQuery.IsDeleted     = false;
            Patient_HealthInsuranceQuery.Patient_RefID = patient.HEC_PatientID;

            ORM_HEC_Patient_HealthInsurance Patient_HealthInsurance;
            var Patient_HealthInsuranceQueryRes = ORM_HEC_Patient_HealthInsurance.Query.Search(Connection, Transaction, Patient_HealthInsuranceQuery);
            if (Patient_HealthInsuranceQueryRes.Count == 1)
            {
                Patient_HealthInsurance = Patient_HealthInsuranceQueryRes[0];
            }
            else
            {
                Patient_HealthInsurance = new ORM_HEC_Patient_HealthInsurance();
                Patient_HealthInsurance.Patient_RefID = patient.HEC_PatientID;
                Patient_HealthInsurance.Tenant_RefID  = securityTicket.TenantID;
                Patient_HealthInsurance.IsPrimary     = true;
            }
            Patient_HealthInsurance.HealthInsurance_Number = Parameter.HealthcareNumber;
            Patient_HealthInsurance.Save(Connection, Transaction);

            ORM_HEC_STU_Study_ParticipatingPatient.Query Study_ParticipatingPatientsQuery = new ORM_HEC_STU_Study_ParticipatingPatient.Query();
            Study_ParticipatingPatientsQuery.Tenant_RefID  = securityTicket.TenantID;
            Study_ParticipatingPatientsQuery.IsDeleted     = false;
            Study_ParticipatingPatientsQuery.Patient_RefID = patient.HEC_PatientID;

            ORM_HEC_STU_Study_ParticipatingPatient Study_ParticipatingPatients;

            var Study_ParticipatingPatientsQueryRes = ORM_HEC_STU_Study_ParticipatingPatient.Query.Search(Connection, Transaction, Study_ParticipatingPatientsQuery);
            if (Study_ParticipatingPatientsQueryRes.Count == 1)
            {
                Study_ParticipatingPatients = Study_ParticipatingPatientsQueryRes[0];
            }
            else
            {
                Study_ParticipatingPatients = new ORM_HEC_STU_Study_ParticipatingPatient();
                Study_ParticipatingPatients.Patient_RefID = patient.HEC_PatientID;
                Study_ParticipatingPatients.Tenant_RefID  = securityTicket.TenantID;
            }
            Study_ParticipatingPatients.HasFulfilledParticipationPolicyRequirements = Parameter.HasFulfilledParticipationPolicyRequirements;
            Study_ParticipatingPatients.Save(Connection, Transaction);


            ORM_USR_Account       account;
            ORM_USR_Account.Query ORM_USR_AccountQuery = new ORM_USR_Account.Query();
            ORM_USR_AccountQuery.IsDeleted    = false;
            ORM_USR_AccountQuery.Tenant_RefID = securityTicket.TenantID;
            ORM_USR_AccountQuery.BusinessParticipant_RefID = bParticipant.CMN_BPT_BusinessParticipantID;
            var accountRes = ORM_USR_Account.Query.Search(Connection, Transaction, ORM_USR_AccountQuery);
            if (accountRes.Count == 1)
            {
                account = accountRes[0];
            }
            else
            {
                account = new ORM_USR_Account();
                account.Tenant_RefID = securityTicket.TenantID;
                account.BusinessParticipant_RefID = bParticipant.CMN_BPT_BusinessParticipantID;
                account.AccountType = 3;
                account.Save(Connection, Transaction);
            }

            ORM_USR_Device_AccountCode.Query ORM_USR_Device_AccountCodeQuery = new ORM_USR_Device_AccountCode.Query();
            ORM_USR_Device_AccountCodeQuery.IsDeleted     = false;
            ORM_USR_Device_AccountCodeQuery.Tenant_RefID  = securityTicket.TenantID;
            ORM_USR_Device_AccountCodeQuery.Account_RefID = account.USR_AccountID;
            var accountCodeRes = ORM_USR_Device_AccountCode.Query.Search(Connection, Transaction, ORM_USR_Device_AccountCodeQuery);
            if (accountCodeRes.Count == 0)
            {
                P_L2DC_GUDCfT_1505 codeParam = new P_L2DC_GUDCfT_1505();
                codeParam.codeLength = 8;
                var checkCodeValue = cls_GetUniqueDeviceCodeForTenant.Invoke(Connection, Transaction, codeParam, securityTicket).Result;

                ORM_USR_Device_AccountCode_StatusHistory AccountCode_StatusHistory = new ORM_USR_Device_AccountCode_StatusHistory();
                AccountCode_StatusHistory.Tenant_RefID         = securityTicket.TenantID;
                AccountCode_StatusHistory.IsAccountCode_Active = true;
                AccountCode_StatusHistory.Save(Connection, Transaction);

                ORM_USR_Device_AccountCode devoceAccpimtCpde = new ORM_USR_Device_AccountCode();
                devoceAccpimtCpde.Account_RefID                   = account.USR_AccountID;
                devoceAccpimtCpde.Tenant_RefID                    = securityTicket.TenantID;
                devoceAccpimtCpde.AccountCode_Value               = checkCodeValue.CodeValue;
                devoceAccpimtCpde.IsAccountCode_Expirable         = false;
                devoceAccpimtCpde.AccountCode_CurrentStatus_RefID = AccountCode_StatusHistory.USR_Device_AccountCode_StatusHistoryID;
                devoceAccpimtCpde.Save(Connection, Transaction);

                AccountCode_StatusHistory.Device_AccountCode_RefID = devoceAccpimtCpde.USR_Device_AccountCodeID;
                AccountCode_StatusHistory.Save(Connection, Transaction);
            }

            returnValue.Result = patient.HEC_PatientID;
            return(returnValue);

            #endregion UserCode
        }
Beispiel #10
0
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L6PA_SBBVP_1223 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_Guid();

            ORM_HEC_Patient patient = new ORM_HEC_Patient();
            if (Parameter.HEC_PatientID != Guid.Empty)
            {
                var result = patient.Load(Connection, Transaction, Parameter.HEC_PatientID);
                if (result.Status != FR_Status.Success || patient.HEC_PatientID == Guid.Empty)
                {
                    var error = new FR_Guid();
                    error.ErrorMessage = "No Such ID";
                    error.Status       = FR_Status.Error_Internal;
                    return(error);
                }
            }
            patient.IsPatientParticipationPolicyValidated = Parameter.HasFulfilledParticipationPolicyRequirements;
            patient.PatientComment = Parameter.Comment;
            patient.Tenant_RefID   = securityTicket.TenantID;

            ORM_CMN_BPT_BusinessParticipant bParticipant = new ORM_CMN_BPT_BusinessParticipant();
            if (patient.CMN_BPT_BusinessParticipant_RefID != Guid.Empty)
            {
                var result = bParticipant.Load(Connection, Transaction, patient.CMN_BPT_BusinessParticipant_RefID);
                if (result.Status != FR_Status.Success || bParticipant.CMN_BPT_BusinessParticipantID == Guid.Empty)
                {
                    var error = new FR_Guid();
                    error.ErrorMessage = "No Such ID";
                    error.Status       = FR_Status.Error_Internal;
                    return(error);
                }
            }
            bParticipant.IsNaturalPerson = true;
            bParticipant.Tenant_RefID    = securityTicket.TenantID;


            ORM_CMN_PER_PersonInfo person = new ORM_CMN_PER_PersonInfo();
            if (bParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID != Guid.Empty)
            {
                var result = person.Load(Connection, Transaction, bParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID);
                if (result.Status != FR_Status.Success || person.CMN_PER_PersonInfoID == Guid.Empty)
                {
                    var error = new FR_Guid();
                    error.ErrorMessage = "No Such ID";
                    error.Status       = FR_Status.Error_Internal;
                    return(error);
                }
            }
            person.FirstName          = Parameter.FirstName;
            person.LastName           = Parameter.LastName;
            person.PrimaryEmail       = Parameter.Mail;
            person.Tenant_RefID       = securityTicket.TenantID;
            person.Salutation_General = Parameter.Salutation;
            person.Save(Connection, Transaction);

            bParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID = person.CMN_PER_PersonInfoID;
            bParticipant.Save(Connection, Transaction);

            patient.CMN_BPT_BusinessParticipant_RefID = bParticipant.CMN_BPT_BusinessParticipantID;
            patient.Save(Connection, Transaction);

            ORM_CMN_PER_CommunicationContact.Query contactQuery = new ORM_CMN_PER_CommunicationContact.Query();
            contactQuery.IsDeleted        = false;
            contactQuery.Tenant_RefID     = securityTicket.TenantID;
            contactQuery.PersonInfo_RefID = person.CMN_PER_PersonInfoID;
            var contactQueryRes = ORM_CMN_PER_CommunicationContact.Query.Search(Connection, Transaction, contactQuery);
            ORM_CMN_PER_CommunicationContact contactPhone = contactQueryRes.FirstOrDefault(c => c.Contact_Type == STLD_ContactTypes.Phone);
            ORM_CMN_PER_CommunicationContact contactFax   = contactQueryRes.FirstOrDefault(c => c.Contact_Type == STLD_ContactTypes.Fax);

            if (contactPhone == null)
            {
                contactPhone = new ORM_CMN_PER_CommunicationContact();
                contactPhone.Contact_Type     = STLD_ContactTypes.Phone;
                contactPhone.Tenant_RefID     = securityTicket.TenantID;
                contactPhone.PersonInfo_RefID = person.CMN_PER_PersonInfoID;
            }
            contactPhone.Content = Parameter.Phone;
            contactPhone.Save(Connection, Transaction);

            if (contactFax == null)
            {
                contactFax                  = new ORM_CMN_PER_CommunicationContact();
                contactFax.Content          = Parameter.Fax;
                contactFax.Contact_Type     = STLD_ContactTypes.Fax;
                contactFax.Tenant_RefID     = securityTicket.TenantID;
                contactFax.PersonInfo_RefID = person.CMN_PER_PersonInfoID;
                contactFax.Save(Connection, Transaction);
            }
            contactFax.Content = Parameter.Fax;
            contactFax.Save(Connection, Transaction);

            ORM_HEC_Patient_HealthInsurance.Query Patient_HealthInsuranceQuery = new ORM_HEC_Patient_HealthInsurance.Query();
            Patient_HealthInsuranceQuery.Tenant_RefID  = securityTicket.TenantID;
            Patient_HealthInsuranceQuery.IsDeleted     = false;
            Patient_HealthInsuranceQuery.Patient_RefID = patient.HEC_PatientID;

            ORM_HEC_Patient_HealthInsurance Patient_HealthInsurance;
            var Patient_HealthInsuranceQueryRes = ORM_HEC_Patient_HealthInsurance.Query.Search(Connection, Transaction, Patient_HealthInsuranceQuery);
            if (Patient_HealthInsuranceQueryRes.Count == 1)
            {
                Patient_HealthInsurance = Patient_HealthInsuranceQueryRes[0];
            }
            else
            {
                Patient_HealthInsurance = new ORM_HEC_Patient_HealthInsurance();
                Patient_HealthInsurance.Patient_RefID = patient.HEC_PatientID;
                Patient_HealthInsurance.Tenant_RefID  = securityTicket.TenantID;
                Patient_HealthInsurance.IsPrimary     = true;
            }
            Patient_HealthInsurance.HealthInsurance_Number = Parameter.HealthcareNumber;
            Patient_HealthInsurance.Save(Connection, Transaction);

            ORM_HEC_STU_Study_ParticipatingPatient.Query Study_ParticipatingPatientsQuery = new ORM_HEC_STU_Study_ParticipatingPatient.Query();
            Study_ParticipatingPatientsQuery.Tenant_RefID  = securityTicket.TenantID;
            Study_ParticipatingPatientsQuery.IsDeleted     = false;
            Study_ParticipatingPatientsQuery.Patient_RefID = patient.HEC_PatientID;

            ORM_HEC_STU_Study_ParticipatingPatient Study_ParticipatingPatients;

            var Study_ParticipatingPatientsQueryRes = ORM_HEC_STU_Study_ParticipatingPatient.Query.Search(Connection, Transaction, Study_ParticipatingPatientsQuery);
            if (Study_ParticipatingPatientsQueryRes.Count == 1)
            {
                Study_ParticipatingPatients = Study_ParticipatingPatientsQueryRes[0];
            }
            else
            {
                Study_ParticipatingPatients = new ORM_HEC_STU_Study_ParticipatingPatient();
                Study_ParticipatingPatients.Patient_RefID = patient.HEC_PatientID;
                Study_ParticipatingPatients.Tenant_RefID  = securityTicket.TenantID;
            }
            Study_ParticipatingPatients.HasFulfilledParticipationPolicyRequirements = Parameter.HasFulfilledParticipationPolicyRequirements;
            Study_ParticipatingPatients.Save(Connection, Transaction);

            returnValue.Result = patient.HEC_PatientID;
            return(returnValue);

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

            if (Parameter.isDeleted == true)
            {
                P_L3MP_DPBID_1026 param = new P_L3MP_DPBID_1026();
                param.HEC_MedicalPractiseID = Parameter.PracticeID;

                cls_Delete_Practice_By_ID.Invoke(Connection, Transaction, param, securityTicket);
            }
            else
            {
                #region Save

                if (Parameter.PracticeID == Guid.Empty)
                {
                    /*********************************
                     *  Save ContactPerson
                     ********************************/
                    //business Participants
                    ORM_CMN_BPT_BusinessParticipant contactPerson = new ORM_CMN_BPT_BusinessParticipant();
                    Guid businessParticipantsID = Guid.NewGuid();

                    contactPerson.CMN_BPT_BusinessParticipantID = businessParticipantsID;
                    contactPerson.IsCompany          = false;
                    contactPerson.IsNaturalPerson    = true;
                    contactPerson.IsTenant           = false;
                    contactPerson.Creation_Timestamp = DateTime.Now;
                    contactPerson.Tenant_RefID       = securityTicket.TenantID;

                    //person info
                    ORM_CMN_PER_PersonInfo personInfo = new ORM_CMN_PER_PersonInfo();
                    Guid personInfoID = Guid.NewGuid();

                    contactPerson.IfNaturalPerson_CMN_PER_PersonInfo_RefID = personInfoID;
                    contactPerson.Save(Connection, Transaction);

                    personInfo.CMN_PER_PersonInfoID = personInfoID;
                    personInfo.FirstName            = Parameter.ContactPersonFirstName;
                    personInfo.LastName             = Parameter.ContactPersonLastName;
                    personInfo.PrimaryEmail         = Parameter.ContactPersonEmail;
                    personInfo.Creation_Timestamp   = DateTime.Now;
                    personInfo.Tenant_RefID         = securityTicket.TenantID;

                    personInfo.Save(Connection, Transaction);

                    //Communication Contact
                    ORM_CMN_PER_CommunicationContact communicationContacts = new ORM_CMN_PER_CommunicationContact();

                    communicationContacts.CMN_PER_CommunicationContactID = Guid.NewGuid();
                    communicationContacts.PersonInfo_RefID   = personInfoID;
                    communicationContacts.Contact_Type       = Parameter.ContactTypePhone;
                    communicationContacts.Content            = Parameter.ContactPersonPhoneNumber;
                    communicationContacts.Creation_Timestamp = DateTime.Now;
                    communicationContacts.Tenant_RefID       = securityTicket.TenantID;

                    communicationContacts.Save(Connection, Transaction);

                    /*********************************
                     * Save Practice
                     ********************************/
                    P_L3MP_SPBI_1602 param = new P_L3MP_SPBI_1602();
                    param.Street_Number     = Parameter.PracticeNumber;
                    param.Street_Name       = Parameter.PracticeStreet;
                    param.Street_Name_Line2 = Parameter.PracticeStreet2;
                    param.PracticeName      = Parameter.PracticeName;
                    param.isLucentis        = true;
                    param.ifLucentis_BSNR   = Parameter.BSNR;
                    param.PracticeEmail     = Parameter.PracitceEmail;
                    param.Town = Parameter.Town;
                    param.ZIP  = Parameter.ZIP;
                    param.HEC_MedicalPractiseID = Parameter.PracticeID;

                    Guid HEC_MedicalPractiseID = cls_Save_Practice_BaseInfo.Invoke(Connection, Transaction, param, securityTicket).Result;


                    /*********************************
                     * Save Cooperating Practices
                     ********************************/
                    var queryMedicalPractice = new ORM_HEC_MedicalPractis.Query();
                    queryMedicalPractice.HEC_MedicalPractiseID = HEC_MedicalPractiseID;
                    queryMedicalPractice.IsDeleted             = false;

                    var medicalPractice = ORM_HEC_MedicalPractis.Query.Search(Connection, Transaction, queryMedicalPractice).FirstOrDefault();

                    if (medicalPractice != null)
                    {
                        medicalPractice.ContactPerson_RefID = businessParticipantsID;
                        medicalPractice.Save(Connection, Transaction);

                        var queryCompanyInfo = new ORM_CMN_COM_CompanyInfo.Query();
                        queryCompanyInfo.CMN_COM_CompanyInfoID = medicalPractice.Ext_CompanyInfo_RefID;
                        queryCompanyInfo.IsDeleted             = false;

                        var CompanyInfo = ORM_CMN_COM_CompanyInfo.Query.Search(Connection, Transaction, queryCompanyInfo).FirstOrDefault();

                        if (CompanyInfo != null)
                        {
                            var queryBussinessParticipients = new ORM_CMN_BPT_BusinessParticipant.Query();
                            queryBussinessParticipients.IfCompany_CMN_COM_CompanyInfo_RefID = CompanyInfo.CMN_COM_CompanyInfoID;
                            queryBussinessParticipients.IsDeleted = false;

                            var bussinessParticipants = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, queryBussinessParticipients).FirstOrDefault();


                            if (bussinessParticipants != null)
                            {
                                foreach (var cooperatingPractice in Parameter.CooperatingPractices)
                                {
                                    ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant associatedBussinessParticipants = new ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant();

                                    associatedBussinessParticipants.CMN_BPT_BusinessParticipant_AssociatedBusinessParticipantID = Guid.NewGuid();
                                    associatedBussinessParticipants.IsDeleted = cooperatingPractice.isDeleted;
                                    associatedBussinessParticipants.BusinessParticipant_RefID           = bussinessParticipants.CMN_BPT_BusinessParticipantID;
                                    associatedBussinessParticipants.AssociatedBusinessParticipant_RefID = cooperatingPractice.PracticeID;
                                    associatedBussinessParticipants.Creation_Timestamp = DateTime.Now;
                                    associatedBussinessParticipants.Tenant_RefID       = bussinessParticipants.Tenant_RefID;

                                    associatedBussinessParticipants.Save(Connection, Transaction);
                                }
                            }
                        }

                        returnValue.Result = medicalPractice.HEC_MedicalPractiseID;
                    }
                }
                #endregion
                else
                {
                    /*********************************
                     * Edit Practice
                     ********************************/
                    P_L3MP_SPBI_1602 param = new P_L3MP_SPBI_1602();
                    param.Street_Number     = Parameter.PracticeNumber;
                    param.Street_Name       = Parameter.PracticeStreet;
                    param.Street_Name_Line2 = Parameter.PracticeStreet2;
                    param.PracticeName      = Parameter.PracticeName;
                    param.isLucentis        = true;
                    param.ifLucentis_BSNR   = Parameter.BSNR;
                    param.PracticeEmail     = Parameter.PracitceEmail;
                    param.Town = Parameter.Town;
                    param.ZIP  = Parameter.ZIP;
                    param.HEC_MedicalPractiseID = Parameter.PracticeID;

                    Guid HEC_MedicalPractiseID = cls_Save_Practice_BaseInfo.Invoke(Connection, Transaction, param, securityTicket).Result;

                    var medicalPracticeQuery = new ORM_HEC_MedicalPractis.Query();
                    medicalPracticeQuery.IsDeleted             = false;
                    medicalPracticeQuery.HEC_MedicalPractiseID = HEC_MedicalPractiseID;

                    var medicalPractice = ORM_HEC_MedicalPractis.Query.Search(Connection, Transaction, medicalPracticeQuery).FirstOrDefault();

                    if (medicalPractice != null)
                    {
                        /*********************************
                         *  Edit Contact Person
                         ********************************/

                        //contact person
                        var contactPerson = new ORM_CMN_BPT_BusinessParticipant();

                        var contactPersonQuery = new ORM_CMN_BPT_BusinessParticipant.Query();
                        contactPersonQuery.IsDeleted = false;

                        if (medicalPractice.ContactPerson_RefID != Guid.Empty)
                        {
                            contactPersonQuery.CMN_BPT_BusinessParticipantID = medicalPractice.ContactPerson_RefID;
                            contactPerson = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, contactPersonQuery).FirstOrDefault();
                        }
                        else
                        {
                            contactPerson = null;
                        }

                        Guid personInfoID = Guid.NewGuid();

                        if (contactPerson == null)
                        {
                            contactPerson = new ORM_CMN_BPT_BusinessParticipant();
                            contactPerson.CMN_BPT_BusinessParticipantID = Guid.NewGuid();
                            contactPerson.IsCompany          = false;
                            contactPerson.IsNaturalPerson    = true;
                            contactPerson.IsTenant           = false;
                            contactPerson.Creation_Timestamp = DateTime.Now;
                            contactPerson.Tenant_RefID       = securityTicket.TenantID;

                            contactPerson.IfNaturalPerson_CMN_PER_PersonInfo_RefID = personInfoID;
                            contactPerson.Save(Connection, Transaction);

                            medicalPractice.ContactPerson_RefID = contactPerson.CMN_BPT_BusinessParticipantID;
                            medicalPractice.Save(Connection, Transaction);
                        }

                        var personInfo = new ORM_CMN_PER_PersonInfo();

                        //person info
                        var personInfoQuery = new ORM_CMN_PER_PersonInfo.Query();
                        personInfoQuery.IsDeleted            = false;
                        personInfoQuery.CMN_PER_PersonInfoID = contactPerson.IfNaturalPerson_CMN_PER_PersonInfo_RefID;

                        personInfo = ORM_CMN_PER_PersonInfo.Query.Search(Connection, Transaction, personInfoQuery).FirstOrDefault();

                        if (personInfo == null)
                        {
                            personInfo = new ORM_CMN_PER_PersonInfo();
                            personInfo.CMN_PER_PersonInfoID = Guid.NewGuid();
                            personInfo.Creation_Timestamp   = DateTime.Now;
                            personInfo.Tenant_RefID         = securityTicket.TenantID;
                            personInfo.CMN_PER_PersonInfoID = personInfoID;

                            contactPerson.IfNaturalPerson_CMN_PER_PersonInfo_RefID = personInfo.CMN_PER_PersonInfoID;
                            contactPerson.Save(Connection, Transaction);
                        }

                        personInfo.FirstName    = Parameter.ContactPersonFirstName;
                        personInfo.LastName     = Parameter.ContactPersonLastName;
                        personInfo.PrimaryEmail = Parameter.ContactPersonEmail;

                        personInfo.Save(Connection, Transaction);

                        //Communication Contact

                        var communicationContacts = new ORM_CMN_PER_CommunicationContact();

                        var communicationContactsQuery = new ORM_CMN_PER_CommunicationContact.Query();
                        communicationContactsQuery.IsDeleted        = false;
                        communicationContactsQuery.PersonInfo_RefID = personInfo.CMN_PER_PersonInfoID;

                        communicationContacts = ORM_CMN_PER_CommunicationContact.Query.Search(Connection, Transaction, communicationContactsQuery).FirstOrDefault();

                        if (communicationContacts == null)
                        {
                            communicationContacts = new ORM_CMN_PER_CommunicationContact();
                            communicationContacts.CMN_PER_CommunicationContactID = Guid.NewGuid();
                            communicationContacts.PersonInfo_RefID   = personInfo.CMN_PER_PersonInfoID;
                            communicationContacts.Creation_Timestamp = DateTime.Now;
                            communicationContacts.Tenant_RefID       = securityTicket.TenantID;
                        }
                        communicationContacts.Contact_Type = Parameter.ContactTypePhone;
                        communicationContacts.Content      = Parameter.ContactPersonPhoneNumber;

                        communicationContacts.Save(Connection, Transaction);


                        /*********************************
                         *  Edit Cooperating Practices
                         ********************************/

                        var queryCompanyInfo = new ORM_CMN_COM_CompanyInfo.Query();
                        queryCompanyInfo.CMN_COM_CompanyInfoID = medicalPractice.Ext_CompanyInfo_RefID;
                        queryCompanyInfo.IsDeleted             = false;

                        var CompanyInfo = ORM_CMN_COM_CompanyInfo.Query.Search(Connection, Transaction, queryCompanyInfo).FirstOrDefault();

                        if (CompanyInfo != null)
                        {
                            var queryBussinessParticipients = new ORM_CMN_BPT_BusinessParticipant.Query();
                            queryBussinessParticipients.IfCompany_CMN_COM_CompanyInfo_RefID = CompanyInfo.CMN_COM_CompanyInfoID;
                            queryBussinessParticipients.IsDeleted = false;

                            var bussinessParticipants = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, queryBussinessParticipients).FirstOrDefault();


                            if (bussinessParticipants != null)
                            {
                                foreach (var cooperatingPractice in Parameter.CooperatingPractices)
                                {
                                    var associatedBussinessParticipantsQuery = new ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant.Query();
                                    associatedBussinessParticipantsQuery.AssociatedBusinessParticipant_RefID = cooperatingPractice.PracticeID;
                                    associatedBussinessParticipantsQuery.Tenant_RefID = bussinessParticipants.Tenant_RefID;
                                    associatedBussinessParticipantsQuery.BusinessParticipant_RefID = bussinessParticipants.CMN_BPT_BusinessParticipantID;

                                    var associtePractice = ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant.Query.Search(Connection, Transaction, associatedBussinessParticipantsQuery).FirstOrDefault();

                                    if (associtePractice != null)
                                    {
                                        associtePractice.IsDeleted = cooperatingPractice.isDeleted;
                                        associtePractice.AssociatedBusinessParticipant_RefID = cooperatingPractice.PracticeID;
                                        associtePractice.Save(Connection, Transaction);
                                    }
                                    else
                                    {
                                        ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant associatedBussinessParticipants = new ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant();

                                        associatedBussinessParticipants.CMN_BPT_BusinessParticipant_AssociatedBusinessParticipantID = Guid.NewGuid();
                                        associatedBussinessParticipants.IsDeleted = cooperatingPractice.isDeleted;
                                        associatedBussinessParticipants.BusinessParticipant_RefID           = bussinessParticipants.CMN_BPT_BusinessParticipantID;
                                        associatedBussinessParticipants.AssociatedBusinessParticipant_RefID = cooperatingPractice.PracticeID;
                                        associatedBussinessParticipants.Creation_Timestamp = DateTime.Now;
                                        associatedBussinessParticipants.Tenant_RefID       = bussinessParticipants.Tenant_RefID;

                                        associatedBussinessParticipants.Save(Connection, Transaction);
                                    }
                                }
                            }
                        }

                        returnValue.Result = medicalPractice.HEC_MedicalPractiseID;
                    }
                }
            }

            return(returnValue);

            #endregion UserCode
        }
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L5PA_GPBD_1613 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_Guid();
            returnValue.Result = new Guid();
            var patient = new ORM_HEC_Patient();
            //var serializer = new JsonNetSerializer();
            //var connection = new ElasticConnection((String)HttpContext.GetGlobalResourceObject("Global", "ElasticConnection"), 9200);

            #region Save

            if (Parameter.ID == null || Parameter.ID == Guid.Empty)
            {
                //ORM_HEC_Patient
                patient.HEC_PatientID      = Guid.NewGuid();
                patient.Tenant_RefID       = securityTicket.TenantID;
                patient.Creation_Timestamp = DateTime.Now;
                patient.CMN_BPT_BusinessParticipant_RefID = Guid.NewGuid();
                patient.Save(Connection, Transaction);

                //ORM_CMN_BPT_BusinessParticipant
                var businessParticipant = new ORM_CMN_BPT_BusinessParticipant();
                businessParticipant.CMN_BPT_BusinessParticipantID = patient.CMN_BPT_BusinessParticipant_RefID;
                businessParticipant.IsNaturalPerson        = true;
                businessParticipant.Tenant_RefID           = securityTicket.TenantID;
                businessParticipant.Creation_Timestamp     = DateTime.Now;
                businessParticipant.Modification_Timestamp = DateTime.Now;
                businessParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID = Guid.NewGuid();
                businessParticipant.Save(Connection, Transaction);

                //ORM_CMN_PER_PersonInfo
                var personInfo = new ORM_CMN_PER_PersonInfo();
                personInfo.CMN_PER_PersonInfoID = businessParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID;
                personInfo.FirstName            = Parameter.FirstName;
                personInfo.LastName             = Parameter.LastName;
                personInfo.PrimaryEmail         = Parameter.PrimaryEmail;
                personInfo.Title = Parameter.Title;
                personInfo.ProfileImage_Document_RefID = Parameter.ProfileImage_Document_RefID;
                personInfo.BirthDate                  = Parameter.BirthDate;
                personInfo.Gender                     = Int32.Parse(Parameter.Gender);
                personInfo.Salutation_General         = Parameter.AcademicTitle;
                personInfo.Address_RefID              = Guid.NewGuid();
                personInfo.Tenant_RefID               = securityTicket.TenantID;
                personInfo.Creation_Timestamp         = DateTime.Now;
                personInfo.Modification_Timestamp     = DateTime.Now;
                personInfo.AgeCalculation_YearOfBirth = DateTime.Now.Year - personInfo.BirthDate.Year;
                personInfo.Save(Connection, Transaction);

                //ORM_CMN_Address
                var address = new ORM_CMN_Address();
                address.CMN_AddressID      = personInfo.Address_RefID;
                address.Street_Name        = Parameter.Street_Name;
                address.Street_Number      = Parameter.Street_Number;
                address.City_Name          = Parameter.City_Name;
                address.City_PostalCode    = Parameter.City_PostalCode;
                address.Country_ISOCode    = Parameter.Country_ISOCode;
                address.Tenant_RefID       = securityTicket.TenantID;
                address.Creation_Timestamp = DateTime.Now;
                address.Save(Connection, Transaction);

                //ORM_CMN_PER_PersonInfo_SocialSecurityNumber
                var socialSecurityNumber = new ORM_CMN_PER_PersonInfo_SocialSecurityNumber();
                socialSecurityNumber.CMN_PER_PersonInfo_SocialSecurityNumberID = Guid.NewGuid();
                socialSecurityNumber.PersonInfo_RefID     = personInfo.CMN_PER_PersonInfoID;
                socialSecurityNumber.SocialSecurityNumber = Parameter.SocialSecurityNumber;
                socialSecurityNumber.Tenant_RefID         = securityTicket.TenantID;
                socialSecurityNumber.Creation_Timestamp   = DateTime.Now;
                socialSecurityNumber.Save(Connection, Transaction);

                var TelephoneCompanyType = CL1_CMN_PER.ORM_CMN_PER_CommunicationContact_Type.Query.Search(Connection, Transaction,
                                                                                                          new CL1_CMN_PER.ORM_CMN_PER_CommunicationContact_Type.Query()
                {
                    Type         = DLCore_DBCommons.Utils.EnumUtils.GetEnumDescription(CL2_Contact.DomainManagement.EComunactionContactType.Phone),
                    Tenant_RefID = securityTicket.TenantID,
                    IsDeleted    = false
                }).SingleOrDefault();

                var MobileCompanyType = CL1_CMN_PER.ORM_CMN_PER_CommunicationContact_Type.Query.Search(Connection, Transaction,
                                                                                                       new CL1_CMN_PER.ORM_CMN_PER_CommunicationContact_Type.Query()
                {
                    Type         = DLCore_DBCommons.Utils.EnumUtils.GetEnumDescription(CL2_Contact.DomainManagement.EComunactionContactType.Mobile),
                    Tenant_RefID = securityTicket.TenantID,
                    IsDeleted    = false
                }).Single();
                var EmailType = CL1_CMN_PER.ORM_CMN_PER_CommunicationContact_Type.Query.Search(Connection, Transaction,
                                                                                               new CL1_CMN_PER.ORM_CMN_PER_CommunicationContact_Type.Query()
                {
                    Type         = DLCore_DBCommons.Utils.EnumUtils.GetEnumDescription(CL2_Contact.DomainManagement.EComunactionContactType.Email),
                    Tenant_RefID = securityTicket.TenantID,
                    IsDeleted    = false
                }).Single();
                //add into database
                //var UrlType = CL1_CMN_PER.ORM_CMN_PER_CommunicationContact_Type.Query.Search(Connection, Transaction,
                //      new CL1_CMN_PER.ORM_CMN_PER_CommunicationContact_Type.Query()
                //      {
                //          Type = DLCore_DBCommons.Utils.EnumUtils.GetEnumDescription(CL2_Contact.DomainManagement.EComunactionContactType.URL),
                //          Tenant_RefID = securityTicket.TenantID,
                //          IsDeleted = false
                //      }).Single();
                var FaxType = CL1_CMN_PER.ORM_CMN_PER_CommunicationContact_Type.Query.Search(Connection, Transaction,
                                                                                             new CL1_CMN_PER.ORM_CMN_PER_CommunicationContact_Type.Query()
                {
                    Type         = DLCore_DBCommons.Utils.EnumUtils.GetEnumDescription(CL2_Contact.DomainManagement.EComunactionContactType.Fax),
                    Tenant_RefID = securityTicket.TenantID,
                    IsDeleted    = false
                }).Single();



                ORM_CMN_PER_CommunicationContact communicationContactsPhone = new ORM_CMN_PER_CommunicationContact();

                communicationContactsPhone.CMN_PER_CommunicationContactID = Guid.NewGuid();
                communicationContactsPhone.PersonInfo_RefID       = personInfo.CMN_PER_PersonInfoID;
                communicationContactsPhone.Contact_Type           = TelephoneCompanyType.CMN_PER_CommunicationContact_TypeID;
                communicationContactsPhone.Content                = Parameter.ContactTypes.Where(p => p.Type == DLCore_DBCommons.Utils.EnumUtils.GetEnumDescription(CL2_Contact.DomainManagement.EComunactionContactType.Phone)).Single().Content;
                communicationContactsPhone.Creation_Timestamp     = DateTime.Now;
                communicationContactsPhone.Modification_Timestamp = DateTime.Now;
                communicationContactsPhone.Tenant_RefID           = securityTicket.TenantID;
                communicationContactsPhone.Save(Connection, Transaction);

                ORM_CMN_PER_CommunicationContact communicationContactsMobile = new ORM_CMN_PER_CommunicationContact();

                communicationContactsMobile.CMN_PER_CommunicationContactID = Guid.NewGuid();
                communicationContactsMobile.PersonInfo_RefID       = personInfo.CMN_PER_PersonInfoID;
                communicationContactsMobile.Contact_Type           = MobileCompanyType.CMN_PER_CommunicationContact_TypeID;
                communicationContactsMobile.Content                = Parameter.ContactTypes.Where(p => p.Type == DLCore_DBCommons.Utils.EnumUtils.GetEnumDescription(CL2_Contact.DomainManagement.EComunactionContactType.Mobile)).Single().Content;
                communicationContactsMobile.Creation_Timestamp     = DateTime.Now;
                communicationContactsMobile.Modification_Timestamp = DateTime.Now;
                communicationContactsMobile.Tenant_RefID           = securityTicket.TenantID;

                communicationContactsMobile.Save(Connection, Transaction);

                ORM_CMN_PER_CommunicationContact communicationContactsEmail = new ORM_CMN_PER_CommunicationContact();

                communicationContactsEmail.CMN_PER_CommunicationContactID = Guid.NewGuid();
                communicationContactsEmail.PersonInfo_RefID       = personInfo.CMN_PER_PersonInfoID;
                communicationContactsEmail.Contact_Type           = EmailType.CMN_PER_CommunicationContact_TypeID;
                communicationContactsEmail.Content                = Parameter.ContactTypes.Where(p => p.Type == DLCore_DBCommons.Utils.EnumUtils.GetEnumDescription(CL2_Contact.DomainManagement.EComunactionContactType.Email)).Single().Content;
                communicationContactsEmail.Creation_Timestamp     = DateTime.Now;
                communicationContactsEmail.Modification_Timestamp = DateTime.Now;
                communicationContactsEmail.Tenant_RefID           = securityTicket.TenantID;

                communicationContactsEmail.Save(Connection, Transaction);

                //*******************Save languages to patient************************

                if (Parameter.Languages != null && Parameter.Languages.Count() != 0)
                {
                    foreach (var language in Parameter.Languages)
                    {
                        ORM_CMN_BPT_BusinessParticipant_SpokenLanguage bpLanguage = new ORM_CMN_BPT_BusinessParticipant_SpokenLanguage();
                        bpLanguage.CMN_BPT_BusinessParticipant_RefID            = businessParticipant.CMN_BPT_BusinessParticipantID;
                        bpLanguage.CMN_BPT_BusinessParticipant_SpokenLanguageID = Guid.NewGuid();
                        bpLanguage.CMN_Language_RefID = language.CMN_Language_RefID;
                        bpLanguage.IsDeleted          = false;
                        bpLanguage.Tenant_RefID       = securityTicket.TenantID;
                        bpLanguage.Save(Connection, Transaction);
                    }
                }

                //#region Upload To Elastic

                //bool indexExists = true;
                //#region set Mapping
                //string jsonPatientMapping = new MapBuilder<Patient>()
                //             .RootObject("patient", ro => ro
                //             .Properties(pr => pr
                //                 .MultiField("name", mfp => mfp.Fields(f => f
                //                     .String("name", sp => sp.IndexAnalyzer("autocomplete").SearchAnalyzer(DefaultAnalyzers.standard))
                //                     .String("lower_case_sort", sp => sp.Analyzer("caseinsensitive"))
                //                     )
                //                 )
                //                  .MultiField("last_name", mfp => mfp.Fields(f => f
                //                                     .String("last_name", sp => sp.IndexAnalyzer("autocomplete").SearchAnalyzer(DefaultAnalyzers.standard))
                //                                     .String("lower_case_sort", sp => sp.Analyzer("caseinsensitive"))
                //                                     )
                //                                  )
                //                    .MultiField("birthday", mfp => mfp.Fields(f => f
                //                                     .String("birthday", sp => sp.IndexAnalyzer("autocomplete").SearchAnalyzer(DefaultAnalyzers.standard))
                //                                     .String("lower_case_sort", sp => sp.Analyzer("caseinsensitive"))
                //                                     )
                //                                  )
                //                 .MultiField("age", mfp => mfp.Fields(f => f
                //                                     .String("age", sp => sp.IndexAnalyzer("autocomplete").SearchAnalyzer(DefaultAnalyzers.standard))
                //                                     .String("lower_case_sort", sp => sp.Analyzer("caseinsensitive"))
                //                                     )
                //                                  )
                //                 )).BuildBeautified();
                //#endregion


                //try
                //{
                //    connection.Head(new IndexExistsCommand(securityTicket.TenantID.ToString()));
                //}
                //catch (OperationException ex)
                //{
                //    if (ex.HttpStatusCode == 404)
                //        indexExists = false;
                //}

                //if (!indexExists)
                //{

                //    #region set index settings
                //    string settings = new IndexSettingsBuilder()
                //                          .Analysis(anl => anl
                //                              .Filter(fil => fil
                //                                  .EdgeNGram("autocomplete_filter", gr => gr.MinGram(1).MaxGram(20)))
                //                              .Analyzer(a => a
                //                                  .Custom("caseinsensitive", custom => custom
                //                                      .Tokenizer(DefaultTokenizers.keyword)
                //                                      .Filter("lowercase")
                //                                  )
                //                                  .Custom("autocomplete", custom => custom
                //                                      .Tokenizer(DefaultTokenizers.standard)
                //                                      .Filter("lowercase", "autocomplete_filter")
                //                                  )
                //                              )
                //                          )
                //                          .BuildBeautified();
                //    #endregion
                //    connection.Put(securityTicket.TenantID.ToString(), settings);

                //}


                //#region check if type exists

                //bool typeExists = true;

                //try
                //{
                //    connection.Head(new IndexExistsCommand(securityTicket.TenantID.ToString() + "/patient"));
                //}
                //catch (OperationException ex)
                //{
                //    if (ex.HttpStatusCode == 404)
                //        typeExists = false;
                //}
                //#endregion


                //if (!typeExists)
                //    connection.Put(new PutMappingCommand(securityTicket.TenantID.ToString(), "patient"), jsonPatientMapping);

                //string bulkCommand = new BulkCommand(index: securityTicket.TenantID.ToString(), type: "patient").Refresh();

                //List<Patient> patientList = new List<Patient>();
                //Patient patient_elastic = new Patient();
                //patient_elastic.id = patient.HEC_PatientID.ToString();
                //patient_elastic.age = (DateTime.Today.Year - Parameter.BirthDate.Year).ToString();
                //patient_elastic.birthday = Parameter.BirthDate.ToShortDateString();
                //patient_elastic.last_name = Parameter.LastName;
                //patient_elastic.name = Parameter.FirstName;
                //patientList.Add(patient_elastic);

                //string bulkJson = new BulkBuilder(serializer)
                //    .BuildCollection(patientList, (builder, pro) => builder.Index(data: pro, id: pro.id)
                //    );
                //connection.Post(bulkCommand, bulkJson);

                //#endregion
            }
            #endregion
            else
            {
                #region Delete
                if (Parameter.isDeleted)
                {
                    var patientQuery = new ORM_HEC_Patient.Query();
                    patientQuery.HEC_PatientID = Parameter.ID;
                    patientQuery.IsDeleted     = false;
                    patientQuery.Tenant_RefID  = securityTicket.TenantID;

                    patient           = ORM_HEC_Patient.Query.Search(Connection, Transaction, patientQuery).Single();
                    patient.IsDeleted = true;
                    patient.Save(Connection, Transaction);

                    var businessParticipantQuery = new ORM_CMN_BPT_BusinessParticipant.Query();
                    businessParticipantQuery.CMN_BPT_BusinessParticipantID = patient.CMN_BPT_BusinessParticipant_RefID;
                    businessParticipantQuery.IsDeleted    = false;
                    businessParticipantQuery.Tenant_RefID = securityTicket.TenantID;

                    var businessParticipant = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, businessParticipantQuery).Single();
                    businessParticipant.IsDeleted = true;
                    businessParticipant.Save(Connection, Transaction);

                    var personInfoQuery = new ORM_CMN_PER_PersonInfo.Query();
                    personInfoQuery.CMN_PER_PersonInfoID = businessParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID;
                    personInfoQuery.IsDeleted            = false;
                    personInfoQuery.Tenant_RefID         = securityTicket.TenantID;

                    var personInfo = ORM_CMN_PER_PersonInfo.Query.Search(Connection, Transaction, personInfoQuery).Single();
                    personInfo.IsDeleted = true;
                    personInfo.Save(Connection, Transaction);

                    var addressQuery = new ORM_CMN_Address.Query();
                    addressQuery.CMN_AddressID = personInfo.Address_RefID;
                    addressQuery.IsDeleted     = false;
                    addressQuery.Tenant_RefID  = securityTicket.TenantID;

                    var address = ORM_CMN_Address.Query.Search(Connection, Transaction, addressQuery).Single();
                    address.IsDeleted = true;
                    address.Save(Connection, Transaction);

                    var socialSecurityNumberQuery = new ORM_CMN_PER_PersonInfo_SocialSecurityNumber.Query();
                    socialSecurityNumberQuery.PersonInfo_RefID = personInfo.CMN_PER_PersonInfoID;
                    socialSecurityNumberQuery.IsDeleted        = false;
                    socialSecurityNumberQuery.Tenant_RefID     = securityTicket.TenantID;

                    var socialSecurityNumber = ORM_CMN_PER_PersonInfo_SocialSecurityNumber.Query.Search(Connection, Transaction, socialSecurityNumberQuery).Single();
                    socialSecurityNumber.IsDeleted = true;
                    socialSecurityNumber.Save(Connection, Transaction);

                    var communicationContactQuery = new ORM_CMN_PER_CommunicationContact.Query();
                    communicationContactQuery.PersonInfo_RefID = personInfo.CMN_PER_PersonInfoID;
                    communicationContactQuery.IsDeleted        = false;
                    communicationContactQuery.Tenant_RefID     = securityTicket.TenantID;

                    var communicationContactList = ORM_CMN_PER_CommunicationContact.Query.Search(Connection, Transaction, communicationContactQuery).ToList();

                    ORM_CMN_BPT_BusinessParticipant_SpokenLanguage.Query.SoftDelete(Connection, Transaction, new ORM_CMN_BPT_BusinessParticipant_SpokenLanguage.Query {
                        CMN_BPT_BusinessParticipant_RefID = businessParticipant.CMN_BPT_BusinessParticipantID, IsDeleted = false, Tenant_RefID = securityTicket.TenantID
                    });

                    //// delete on Elastic
                    //connection.Delete(securityTicket.TenantID.ToString() + "/patient/" + Parameter.ID.ToString());
                }
                #endregion
                #region Edit
                else
                {
                    var patientQuery = new ORM_HEC_Patient.Query();
                    patientQuery.HEC_PatientID = Parameter.ID;
                    patientQuery.IsDeleted     = false;
                    patientQuery.Tenant_RefID  = securityTicket.TenantID;

                    patient = ORM_HEC_Patient.Query.Search(Connection, Transaction, patientQuery).Single();

                    var businessParticipantQuery = new ORM_CMN_BPT_BusinessParticipant.Query();
                    businessParticipantQuery.CMN_BPT_BusinessParticipantID = patient.CMN_BPT_BusinessParticipant_RefID;
                    businessParticipantQuery.IsDeleted    = false;
                    businessParticipantQuery.Tenant_RefID = securityTicket.TenantID;

                    var businessParticipant = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, businessParticipantQuery).Single();

                    var personInfoQuery = new ORM_CMN_PER_PersonInfo.Query();
                    personInfoQuery.CMN_PER_PersonInfoID = businessParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID;
                    personInfoQuery.IsDeleted            = false;
                    personInfoQuery.Tenant_RefID         = securityTicket.TenantID;

                    var personInfo = ORM_CMN_PER_PersonInfo.Query.Search(Connection, Transaction, personInfoQuery).Single();
                    personInfo.FirstName    = Parameter.FirstName;
                    personInfo.LastName     = Parameter.LastName;
                    personInfo.PrimaryEmail = Parameter.PrimaryEmail;
                    personInfo.Title        = Parameter.Title;
                    personInfo.Gender       = Int32.Parse(Parameter.Gender);
                    personInfo.ProfileImage_Document_RefID = Parameter.ProfileImage_Document_RefID;
                    personInfo.BirthDate                  = Parameter.BirthDate;
                    personInfo.Salutation_General         = Parameter.AcademicTitle;
                    personInfo.Modification_Timestamp     = DateTime.Now;
                    personInfo.AgeCalculation_YearOfBirth = DateTime.Now.Year - personInfo.BirthDate.Year;
                    personInfo.Save(Connection, Transaction);

                    if (personInfo.Address_RefID != Guid.Empty)
                    {
                        var addressQuery = new ORM_CMN_Address.Query();
                        addressQuery.CMN_AddressID = personInfo.Address_RefID;
                        addressQuery.IsDeleted     = false;
                        addressQuery.Tenant_RefID  = securityTicket.TenantID;

                        var address = ORM_CMN_Address.Query.Search(Connection, Transaction, addressQuery).Single();
                        address.Street_Name     = Parameter.Street_Name;
                        address.Street_Number   = Parameter.Street_Number;
                        address.City_Name       = Parameter.City_Name;
                        address.City_PostalCode = Parameter.City_PostalCode;
                        address.Country_ISOCode = Parameter.Country_ISOCode;
                        address.Save(Connection, Transaction);
                    }
                    else
                    {
                        var address = new ORM_CMN_Address();
                        address.CMN_AddressID      = Guid.NewGuid();
                        address.Street_Name        = Parameter.Street_Name;
                        address.Street_Number      = Parameter.Street_Number;
                        address.City_Name          = Parameter.City_Name;
                        address.City_PostalCode    = Parameter.City_PostalCode;
                        address.Country_ISOCode    = Parameter.Country_ISOCode;
                        address.Tenant_RefID       = securityTicket.TenantID;
                        address.Creation_Timestamp = DateTime.Now;
                        address.Save(Connection, Transaction);


                        personInfo.Address_RefID = address.CMN_AddressID;
                        personInfo.Save(Connection, Transaction);
                    }


                    var socialSecurityNumberQuery = new ORM_CMN_PER_PersonInfo_SocialSecurityNumber.Query();
                    socialSecurityNumberQuery.PersonInfo_RefID = personInfo.CMN_PER_PersonInfoID;
                    socialSecurityNumberQuery.IsDeleted        = false;
                    socialSecurityNumberQuery.Tenant_RefID     = securityTicket.TenantID;

                    var socialSecurityNumber = ORM_CMN_PER_PersonInfo_SocialSecurityNumber.Query.Search(Connection, Transaction, socialSecurityNumberQuery).SingleOrDefault();

                    if (socialSecurityNumber == null)
                    {
                        socialSecurityNumber = new ORM_CMN_PER_PersonInfo_SocialSecurityNumber();
                        socialSecurityNumber.CMN_PER_PersonInfo_SocialSecurityNumberID = Guid.NewGuid();
                        socialSecurityNumber.PersonInfo_RefID = personInfo.CMN_PER_PersonInfoID;
                        socialSecurityNumber.Tenant_RefID     = securityTicket.TenantID;
                    }

                    socialSecurityNumber.SocialSecurityNumber = Parameter.SocialSecurityNumber;
                    socialSecurityNumber.Save(Connection, Transaction);

                    var communicationContactQuery = new ORM_CMN_PER_CommunicationContact.Query();
                    communicationContactQuery.PersonInfo_RefID = personInfo.CMN_PER_PersonInfoID;
                    communicationContactQuery.IsDeleted        = false;
                    communicationContactQuery.Tenant_RefID     = securityTicket.TenantID;

                    var communicationContactList = ORM_CMN_PER_CommunicationContact.Query.Search(Connection, Transaction, communicationContactQuery).ToList();

                    List <string> unusedTypes = new List <string>()
                    {
                        DLCore_DBCommons.Utils.EnumUtils.GetEnumDescription(CL2_Contact.DomainManagement.EComunactionContactType.URL),
                        DLCore_DBCommons.Utils.EnumUtils.GetEnumDescription(CL2_Contact.DomainManagement.EComunactionContactType.Mobile),
                        DLCore_DBCommons.Utils.EnumUtils.GetEnumDescription(CL2_Contact.DomainManagement.EComunactionContactType.Phone),
                        DLCore_DBCommons.Utils.EnumUtils.GetEnumDescription(CL2_Contact.DomainManagement.EComunactionContactType.Email)
                    };

                    foreach (var communicationContact in communicationContactList)
                    {
                        var communicationContact_TypeQuery = new ORM_CMN_PER_CommunicationContact_Type.Query();
                        communicationContact_TypeQuery.CMN_PER_CommunicationContact_TypeID = communicationContact.Contact_Type;
                        communicationContact_TypeQuery.IsDeleted    = false;
                        communicationContact_TypeQuery.Tenant_RefID = securityTicket.TenantID;
                        var communicationContact_Type = ORM_CMN_PER_CommunicationContact_Type.Query.Search(Connection, Transaction, communicationContact_TypeQuery).Single();

                        var newType = Parameter.ContactTypes.Where(i => i.Type == communicationContact_Type.Type).Single();
                        communicationContact.Content = newType.Content;
                        communicationContact.Modification_Timestamp = DateTime.Now;
                        communicationContact.Save(Connection, Transaction);

                        if (unusedTypes.Contains(communicationContact_Type.Type))
                        {
                            unusedTypes.Remove(communicationContact_Type.Type);
                        }
                    }

                    var contactTypes = cls_Get_AllComunicationContactTypes.Invoke(Connection, Transaction, securityTicket).Result.ToList();

                    foreach (var type in unusedTypes)
                    {
                        ORM_CMN_PER_CommunicationContact communicationContactsPhone = new ORM_CMN_PER_CommunicationContact();

                        communicationContactsPhone.CMN_PER_CommunicationContactID = Guid.NewGuid();
                        communicationContactsPhone.PersonInfo_RefID = personInfo.CMN_PER_PersonInfoID;
                        P_L2CN_GCTIDfGPMID_1359 contantTypeParam = new P_L2CN_GCTIDfGPMID_1359();
                        contantTypeParam.Type = type;
                        var contantTypeID = cls_Get_ContantTypeID_for_GlobalPropertyMatchingID.Invoke(Connection, Transaction, contantTypeParam, securityTicket).Result.ContactTypeID;
                        communicationContactsPhone.Contact_Type           = contantTypeID;
                        communicationContactsPhone.Content                = Parameter.ContactTypes.Where(p => p.Type == DLCore_DBCommons.Utils.EnumUtils.GetEnumDescription(CL2_Contact.DomainManagement.EComunactionContactType.Phone)).Single().Content;
                        communicationContactsPhone.Creation_Timestamp     = DateTime.Now;
                        communicationContactsPhone.Modification_Timestamp = DateTime.Now;
                        communicationContactsPhone.Tenant_RefID           = securityTicket.TenantID;
                        communicationContactsPhone.Save(Connection, Transaction);
                    }

                    //connection.Delete(securityTicket.TenantID.ToString() + "/patient/" + Parameter.ID.ToString());
                    //string bulkCommand = new BulkCommand(index: securityTicket.TenantID.ToString(), type: "patient").Refresh();

                    //List<Patient> patientList = new List<Patient>();
                    //Patient patient_elastic = new Patient();
                    //patient_elastic.id = patient.HEC_PatientID.ToString();
                    //patient_elastic.age = (DateTime.Today.Year - Parameter.BirthDate.Year).ToString();
                    //patient_elastic.birthday = Parameter.BirthDate.ToShortDateString();
                    //patient_elastic.last_name = Parameter.LastName;
                    //patient_elastic.name = Parameter.FirstName;
                    //patientList.Add(patient_elastic);

                    //string bulkJson = new BulkBuilder(serializer)
                    //    .BuildCollection(patientList, (builder, pro) => builder.Index(data: pro, id: pro.id)
                    //    );
                    //connection.Post(bulkCommand, bulkJson);


                    #region languages

                    if (Parameter.Languages == null || Parameter.Languages.Count() == 0)
                    {
                        P_L5PA_GPBD_1613_Languages Languages = new P_L5PA_GPBD_1613_Languages();
                        Languages.CMN_Language_RefID = Guid.Empty;
                    }
                    else if (Parameter.Languages != null || Parameter.Languages.Count() != 0)
                    {
                        foreach (var language in Parameter.Languages)
                        {
                            var languageToPatient = ORM_CMN_BPT_BusinessParticipant_SpokenLanguage.Query.Search(Connection, Transaction, new ORM_CMN_BPT_BusinessParticipant_SpokenLanguage.Query {
                                CMN_Language_RefID = language.CMN_Language_RefID, CMN_BPT_BusinessParticipant_RefID = patient.CMN_BPT_BusinessParticipant_RefID, IsDeleted = false, Tenant_RefID = securityTicket.TenantID
                            }).FirstOrDefault();
                            if (languageToPatient == null)
                            {
                                ORM_CMN_BPT_BusinessParticipant_SpokenLanguage bpLanguage = new ORM_CMN_BPT_BusinessParticipant_SpokenLanguage();
                                bpLanguage.CMN_BPT_BusinessParticipant_RefID            = patient.CMN_BPT_BusinessParticipant_RefID;
                                bpLanguage.CMN_BPT_BusinessParticipant_SpokenLanguageID = Guid.NewGuid();
                                bpLanguage.CMN_Language_RefID = language.CMN_Language_RefID;
                                bpLanguage.IsDeleted          = false;
                                bpLanguage.Tenant_RefID       = securityTicket.TenantID;
                                bpLanguage.Save(Connection, Transaction);
                            }
                        }

                        // deleting languages to patient that were deleted during edit

                        List <ORM_CMN_BPT_BusinessParticipant_SpokenLanguage> languageToDoctorList = ORM_CMN_BPT_BusinessParticipant_SpokenLanguage.Query.Search(Connection, Transaction, new ORM_CMN_BPT_BusinessParticipant_SpokenLanguage.Query {
                            CMN_BPT_BusinessParticipant_RefID = patient.CMN_BPT_BusinessParticipant_RefID, IsDeleted = false, Tenant_RefID = securityTicket.TenantID
                        });
                        foreach (var languageToDoctor in languageToDoctorList)
                        {
                            if (Parameter.Languages.FirstOrDefault(x => x.CMN_Language_RefID == languageToDoctor.CMN_Language_RefID) == null)
                            {
                                languageToDoctor.IsDeleted = true;
                                languageToDoctor.Save(Connection, Transaction);
                            }
                        }
                    }
                    else
                    {
                        List <ORM_CMN_BPT_BusinessParticipant_SpokenLanguage> languageToDoctorList = ORM_CMN_BPT_BusinessParticipant_SpokenLanguage.Query.Search(Connection, Transaction, new ORM_CMN_BPT_BusinessParticipant_SpokenLanguage.Query {
                            CMN_BPT_BusinessParticipant_RefID = patient.CMN_BPT_BusinessParticipant_RefID, IsDeleted = false, Tenant_RefID = securityTicket.TenantID
                        });
                        if (languageToDoctorList != null || languageToDoctorList.Count() != 0)
                        {
                            foreach (var language in languageToDoctorList)
                            {
                                language.IsDeleted = true;
                                language.Save(Connection, Transaction);
                            }
                        }
                    }

                    #endregion
                }
                #endregion
            }
            returnValue.Result = patient.HEC_PatientID;
            return(returnValue);

            #endregion UserCode
        }
Beispiel #13
0
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L3MD_DDbID_1031 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            //Leave UserCode region to enable user code saving
            #region UserCode
            var returnValue = new FR_Guid();
            var doctor      = new ORM_HEC_Doctor();

            if (Parameter.DoctorID != Guid.Empty)
            {
                var result = doctor.Load(Connection, Transaction, Parameter.DoctorID);
                if (result.Status != FR_Status.Success || doctor.HEC_DoctorID == Guid.Empty)
                {
                    var error = new FR_Guid();
                    error.ErrorMessage = "No Such ID";
                    error.Status       = FR_Status.Error_Internal;
                    return(error);
                }
                doctor.IsDeleted = true;
                doctor.Save(Connection, Transaction);

                //bussinessParticipant
                var query1 = new ORM_CMN_BPT_BusinessParticipant.Query();
                query1.CMN_BPT_BusinessParticipantID = doctor.BusinessParticipant_RefID;
                var bussinessParticipant = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, query1).First();
                bussinessParticipant.IsDeleted = true;
                bussinessParticipant.Save(Connection, Transaction);

                if (doctor.Account_RefID != Guid.Empty)
                {
                    var account2personInfoQuery = new ORM_CMN_PER_PersonInfo_2_Account.Query();
                    account2personInfoQuery.USR_Account_RefID = doctor.Account_RefID;
                    account2personInfoQuery.Tenant_RefID      = securityTicket.TenantID;
                    var account2personInfo = ORM_CMN_PER_PersonInfo_2_Account.Query.Search(Connection, Transaction, account2personInfoQuery).FirstOrDefault();
                    if (account2personInfo != null)
                    {
                        account2personInfo.IsDeleted = true;
                        account2personInfo.Save(Connection, Transaction);

                        var query2 = new ORM_CMN_PER_PersonInfo.Query();
                        query2.CMN_PER_PersonInfoID = account2personInfo.CMN_PER_PersonInfo_RefID;
                        var personInfo = ORM_CMN_PER_PersonInfo.Query.Search(Connection, Transaction, query2).First();
                        personInfo.IsDeleted = true;
                        personInfo.Save(Connection, Transaction);
                    }
                    var query4 = new ORM_CMN_PER_CommunicationContact.Query();
                    query4.PersonInfo_RefID = bussinessParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID;

                    var communicationContactsList = ORM_CMN_PER_CommunicationContact.Query.Search(Connection, Transaction, query4).ToList();

                    foreach (var contact in communicationContactsList)
                    {
                        contact.IsDeleted = true;
                        contact.Save(Connection, Transaction);
                    }
                }

                var query3 = new ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant.Query();
                query3.BusinessParticipant_RefID = bussinessParticipant.CMN_BPT_BusinessParticipantID;
                query3.IsDeleted = false;
                var abpRes = ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant.Query.Search(Connection, Transaction, query3);
                foreach (ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant assigned in abpRes)
                {
                    assigned.IsDeleted = true;
                    assigned.Save(Connection, Transaction);
                }

                #endregion

                ORM_CMN_BPT_CTM_Customer customer;
                ORM_CMN_BPT_CTM_Customer_2_SalesRepresentative SalesRepresentative;
                var customerQuery = new ORM_CMN_BPT_CTM_Customer.Query();
                customerQuery.Ext_BusinessParticipant_RefID = bussinessParticipant.CMN_BPT_BusinessParticipantID;
                var customerRes = ORM_CMN_BPT_CTM_Customer.Query.Search(Connection, Transaction, customerQuery);
                if (customerRes.Count != 0)
                {
                    customer = ORM_CMN_BPT_CTM_Customer.Query.Search(Connection, Transaction, customerQuery).First();
                    var SalesRepresentativeQuery = new ORM_CMN_BPT_CTM_Customer_2_SalesRepresentative.Query();
                    SalesRepresentativeQuery.Customer_RefID = customer.CMN_BPT_CTM_CustomerID;
                    SalesRepresentative = ORM_CMN_BPT_CTM_Customer_2_SalesRepresentative.Query.Search(Connection, Transaction, SalesRepresentativeQuery).First();

                    customer.IsDeleted = true;
                    customer.Save(Connection, Transaction);
                    SalesRepresentative.IsDeleted = true;
                    SalesRepresentative.Save(Connection, Transaction);
                }

                var accountQuery = new ORM_USR_Account.Query();
                accountQuery.BusinessParticipant_RefID = bussinessParticipant.CMN_BPT_BusinessParticipantID;
                accountQuery.AccountType = 3;
                var accountQueryRes = ORM_USR_Account.Query.Search(Connection, Transaction, accountQuery);
                if (accountQueryRes.Count != 0)
                {
                    var account = accountQueryRes.First();
                    account.IsDeleted = true;
                    account.Save(Connection, Transaction);

                    var codeQuery = new ORM_USR_Device_AccountCode.Query();
                    codeQuery.Account_RefID = account.USR_AccountID;
                    var code = ORM_USR_Device_AccountCode.Query.Search(Connection, Transaction, codeQuery).First();
                    code.IsDeleted = true;
                    code.Save(Connection, Transaction);

                    var codeStatusQuery = new ORM_USR_Device_AccountCode_StatusHistory.Query();
                    codeStatusQuery.Device_AccountCode_RefID = code.USR_Device_AccountCodeID;
                    var codeStatus = ORM_USR_Device_AccountCode_StatusHistory.Query.Search(Connection, Transaction, codeStatusQuery).First();
                    codeStatus.IsDeleted = true;
                    codeStatus.Save(Connection, Transaction);
                }
            }

            returnValue.Result = doctor.HEC_DoctorID;
            return(returnValue);
        }
        protected static FR_L2CN_GCCfPI_1222_Array Execute(DbConnection Connection, DbTransaction Transaction, P_L2CN_GCCfPI_1222 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            //Leave UserCode region to enable user code saving
            #region UserCode
            var returnValue = new FR_L2CN_GCCfPI_1222_Array();

            #region xmlContactTypes

            var xmlContactTypes = cls_Get_AllComunicationContactTypes.Invoke(Connection, Transaction, securityTicket).Result;

            #endregion

            #region peristedContactTypes
            var query = new ORM_CMN_PER_CommunicationContact_Type.Query();
            query.IsDeleted    = false;
            query.Tenant_RefID = securityTicket.TenantID;

            var peristedContactTypes = ORM_CMN_PER_CommunicationContact_Type.Query.Search(Connection, Transaction, query);
            #endregion

            #region contacts

            var query2 = new ORM_CMN_PER_CommunicationContact.Query();
            query2.IsDeleted        = false;
            query2.Tenant_RefID     = securityTicket.TenantID;
            query2.PersonInfo_RefID = Parameter.PersonInfoID;

            var contacts = ORM_CMN_PER_CommunicationContact.Query.Search(Connection, Transaction, query2);

            #endregion

            var result = new List <L2CN_GCCfPI_1222>();

            var differentTypes = contacts.GroupBy(i => i.Contact_Type).Select(g => g.First()).ToList();

            foreach (var contact in differentTypes)
            {
                var persitedContactType = peristedContactTypes.Where(i => i.CMN_PER_CommunicationContact_TypeID == contact.Contact_Type).SingleOrDefault();

                if (persitedContactType == null)
                {
                    continue;
                }

                var xmlContactType = xmlContactTypes.Where(i => i.Type == persitedContactType.Type).Single();

                var temp = new L2CN_GCCfPI_1222();
                temp.CMN_PER_CommunicationContact_TypeID = persitedContactType.CMN_PER_CommunicationContact_TypeID;
                temp.Type     = persitedContactType.Type;
                temp.TypeName = xmlContactType.Name;

                var relevantContacts = contacts.Where(i => i.Contact_Type == contact.Contact_Type);

                temp.Contacts = relevantContacts.Select(i => new L2CN_GCCfPI_1222a()
                {
                    CMN_PER_CommunicationContactID = i.CMN_PER_CommunicationContactID,
                    Content = i.Content
                }).ToArray();

                result.Add(temp);
            }

            returnValue.Result = result.ToArray();

            return(returnValue);

            #endregion UserCode
        }
Beispiel #15
0
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L6PA_DBBVP_1711 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            //Leave UserCode region to enable user code saving
            #region UserCode
            var returnValue = new FR_Guid();


            P_L6PA_GBBVPfID_1714 getParam = new P_L6PA_GBBVPfID_1714();
            getParam.HEC_PatientID = Parameter.HEC_PatientID;
            var patient = cls_Get_BBV_Patients_For_ID.Invoke(Connection, Transaction, getParam, securityTicket).Result;
            if (patient != null)
            {
                if (patient.Addresses != null)
                {
                    foreach (var address in patient.Addresses)
                    {
                        P_L6PA_DBBVAFP_1703 delAddressPar = new P_L6PA_DBBVAFP_1703();
                        delAddressPar.CMN_AddressID = address.CMN_AddressID;
                        cls_Delete_BBV_AddressForPatient.Invoke(Connection, Transaction, delAddressPar, securityTicket);
                    }
                }
                if (patient.Contacts != null)
                {
                    foreach (var contact in patient.Contacts)
                    {
                        ORM_CMN_PER_CommunicationContact.Query cQuery = new ORM_CMN_PER_CommunicationContact.Query();
                        cQuery.CMN_PER_CommunicationContactID = contact.CMN_PER_CommunicationContactID;
                        cQuery.IsDeleted    = false;
                        cQuery.Tenant_RefID = securityTicket.TenantID;
                        ORM_CMN_PER_CommunicationContact.Query.SoftDelete(Connection, Transaction, cQuery);
                    }
                }

                ORM_CMN_PER_PersonInfo.Query ORM_CMN_PER_PersonInfoQuery = new ORM_CMN_PER_PersonInfo.Query();
                ORM_CMN_PER_PersonInfoQuery.CMN_PER_PersonInfoID = patient.CMN_PER_PersonInfoID;
                ORM_CMN_PER_PersonInfoQuery.IsDeleted            = false;
                ORM_CMN_PER_PersonInfoQuery.Tenant_RefID         = securityTicket.TenantID;
                ORM_CMN_PER_PersonInfo.Query.SoftDelete(Connection, Transaction, ORM_CMN_PER_PersonInfoQuery);

                ORM_CMN_BPT_BusinessParticipant.Query ORM_CMN_BPT_BusinessParticipantoQuery = new ORM_CMN_BPT_BusinessParticipant.Query();
                ORM_CMN_BPT_BusinessParticipantoQuery.CMN_BPT_BusinessParticipantID = patient.CMN_BPT_BusinessParticipantID;
                ORM_CMN_BPT_BusinessParticipantoQuery.IsDeleted    = false;
                ORM_CMN_BPT_BusinessParticipantoQuery.Tenant_RefID = securityTicket.TenantID;
                ORM_CMN_BPT_BusinessParticipant.Query.SoftDelete(Connection, Transaction, ORM_CMN_BPT_BusinessParticipantoQuery);

                ORM_HEC_Patient.Query ORM_HEC_PatientQuery = new ORM_HEC_Patient.Query();
                ORM_HEC_PatientQuery.HEC_PatientID = patient.HEC_PatientID;
                ORM_HEC_PatientQuery.IsDeleted     = false;
                ORM_HEC_PatientQuery.Tenant_RefID  = securityTicket.TenantID;
                ORM_HEC_Patient.Query.SoftDelete(Connection, Transaction, ORM_HEC_PatientQuery);

                ORM_HEC_Patient_HealthInsurance.Query ORM_HEC_Patient_HealthInsuranceoQuery = new ORM_HEC_Patient_HealthInsurance.Query();
                ORM_HEC_Patient_HealthInsuranceoQuery.HEC_Patient_HealthInsurancesID = patient.HEC_Patient_HealthInsurancesID;
                ORM_HEC_Patient_HealthInsuranceoQuery.IsDeleted    = false;
                ORM_HEC_Patient_HealthInsuranceoQuery.Tenant_RefID = securityTicket.TenantID;
                ORM_HEC_Patient_HealthInsurance.Query.SoftDelete(Connection, Transaction, ORM_HEC_Patient_HealthInsuranceoQuery);

                ORM_HEC_STU_Study_ParticipatingPatient.Query ORM_HEC_STU_Study_ParticipatingPatientQuery = new ORM_HEC_STU_Study_ParticipatingPatient.Query();
                ORM_HEC_STU_Study_ParticipatingPatientQuery.HEC_STU_Study_ParticipatingPatientID = patient.HEC_STU_Study_ParticipatingPatientID;
                ORM_HEC_STU_Study_ParticipatingPatientQuery.IsDeleted    = false;
                ORM_HEC_STU_Study_ParticipatingPatientQuery.Tenant_RefID = securityTicket.TenantID;
                ORM_HEC_STU_Study_ParticipatingPatient.Query.SoftDelete(Connection, Transaction, ORM_HEC_STU_Study_ParticipatingPatientQuery);

                var transactionsQuery = new ORM_HEC_Patient_Prescription_Transaction.Query();
                transactionsQuery.PrescriptionTransaction_Patient_RefID = patient.HEC_PatientID;
                transactionsQuery.IsDeleted = false;
                ORM_HEC_Patient_Prescription_Transaction.Query.SoftDelete(Connection, Transaction, transactionsQuery);
            }
            return(returnValue);

            #endregion UserCode
        }