示例#1
0
        protected static FR_L5MHC_SDIwDC_1040 Execute(DbConnection Connection, DbTransaction Transaction, P_L5MHC_SDIwDC_1040 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_L5MHC_SDIwDC_1040();
            //Put your code here

            returnValue.Result = new L5MHC_SDIwDC_1040();

            Guid DeviceInstanceId = Guid.Empty;
            if (!Parameter.BaseData.IsDelete)
            {
                DeviceInstanceId = cls_Save_DeviceInstance.Invoke(Connection, Transaction, Parameter.BaseData, securityTicket).Result;
            }
            else
            {
                List <ORM_PPS_TSK_Task_DeviceBooking> existingAppointment = ORM_PPS_TSK_Task_DeviceBooking.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task_DeviceBooking.Query
                {
                    IsDeleted    = false,
                    Tenant_RefID = securityTicket.TenantID,
                    PPS_DEV_Device_Instance_RefID = Parameter.BaseData.PPS_DEV_Device_InstanceID
                }).ToList();

                if (existingAppointment.Count > 0) //cannot delete
                {
                    List <L5MHC_SDIwDC_1040_UsedInAppointment> usedAppointmentList = new List <L5MHC_SDIwDC_1040_UsedInAppointment>();

                    foreach (var appointment in existingAppointment)
                    {
                        ORM_PPS_TSK_Task appointmentName = ORM_PPS_TSK_Task.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task.Query
                        {
                            IsDeleted      = false,
                            Tenant_RefID   = securityTicket.TenantID,
                            PPS_TSK_TaskID = appointment.PPS_TSK_Task_RefID
                        }).Single();
                        usedAppointmentList.Add(new L5MHC_SDIwDC_1040_UsedInAppointment {
                            AppointmentName = appointmentName.DisplayName
                        });
                    }
                    returnValue.Result.UsedInAppointment = usedAppointmentList.ToArray();
                }

                if (existingAppointment.Count == 0)
                {
                    DeviceInstanceId = cls_Save_DeviceInstance.Invoke(Connection, Transaction, Parameter.BaseData, securityTicket).Result;
                }
            }
            returnValue.Result.ID = DeviceInstanceId;

            return(returnValue);

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

            returnValue.Result = new L6PA_SPBDwDC_1323();

            Guid PatientId = Guid.Empty;
            if (!Parameter.BaseData.isDeleted)
            {
                PatientId = cls_Save_PatientBaseData.Invoke(Connection, Transaction, Parameter.BaseData, securityTicket).Result;

                cls_Create_EMRCreationRequest.Invoke(Connection, Transaction, new P_L5ME_CECRfPID_1520()
                {
                    PatientID = PatientId
                }, securityTicket);
            }
            else
            {
                List <ORM_HEC_ACT_PlannedAction> existingAppointment = ORM_HEC_ACT_PlannedAction.Query.Search(Connection, Transaction, new ORM_HEC_ACT_PlannedAction.Query
                {
                    IsDeleted     = false,
                    Tenant_RefID  = securityTicket.TenantID,
                    Patient_RefID = Parameter.BaseData.ID
                }).ToList();

                if (existingAppointment.Count > 0) //cannot delete
                {
                    List <L6PA_SPBDwDC_1323_UsedInAppointment> usedAppointmentList = new List <L6PA_SPBDwDC_1323_UsedInAppointment>();

                    foreach (var appointment in existingAppointment)
                    {
                        ORM_HEC_APP_Appointment patientAppointment = ORM_HEC_APP_Appointment.Query.Search(Connection, Transaction, new ORM_HEC_APP_Appointment.Query
                        {
                            IsDeleted             = false,
                            Tenant_RefID          = securityTicket.TenantID,
                            HEC_APP_AppointmentID = appointment.Appointment_RefID
                        }).Single();

                        ORM_PPS_TSK_Task appointmentName = ORM_PPS_TSK_Task.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task.Query
                        {
                            IsDeleted      = false,
                            Tenant_RefID   = securityTicket.TenantID,
                            PPS_TSK_TaskID = patientAppointment.Ext_PPS_TSK_Task_RefID
                        }).Single();

                        usedAppointmentList.Add(new L6PA_SPBDwDC_1323_UsedInAppointment {
                            AppointmentName = appointmentName.DisplayName
                        });
                    }
                    returnValue.Result.UsedInAppointment = usedAppointmentList.ToArray();
                }

                if (existingAppointment.Count == 0)
                {
                    PatientId = cls_Save_PatientBaseData.Invoke(Connection, Transaction, Parameter.BaseData, securityTicket).Result;
                }
            }
            returnValue.Result.ID = PatientId;

            return(returnValue);

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

            returnValue.Result = new L6DO_SDaSwDC_1224();

            Guid EmployeeId = Guid.Empty;
            if (!Parameter.BaseData.IsDeleted)
            {
                EmployeeId = cls_Save_Doctors_and_Staff.Invoke(Connection, Transaction, Parameter.BaseData, securityTicket).Result;

                if(Parameter.UpdateSlots)
                    foreach (var unit in Parameter.BaseData.OrgUnits)
                        cls_CreateUpdate_Slots_for_Practice.Invoke(Connection, Transaction, new P_L5S_SUSfP_1708() { PracticeID = unit.CMN_STR_Office_RefID }, securityTicket);
            }
            else
            {
                ORM_CMN_BPT_EMP_Employee employee = ORM_CMN_BPT_EMP_Employee.Query.Search(Connection, Transaction, new ORM_CMN_BPT_EMP_Employee.Query
                {
                    IsDeleted = false,
                    Tenant_RefID = securityTicket.TenantID,
                    BusinessParticipant_RefID = Parameter.BaseData.CMN_BPT_BusinessParticipantID
                }).Single();

                List<ORM_PPS_TSK_Task_RequiredStaff> existingAppointmentType = ORM_PPS_TSK_Task_RequiredStaff.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task_RequiredStaff.Query
                {
                    IsDeleted = false,
                    Tenant_RefID = securityTicket.TenantID,
                    Required_Employee_RefID = employee.CMN_BPT_EMP_EmployeeID
                }).ToList();

                if (existingAppointmentType.Count > 0) //cannot delete
                {
                    List<L6DO_SDaSwDC_1224_UsedInAppointmentType> usedAppointmentTypeList = new List<L6DO_SDaSwDC_1224_UsedInAppointmentType>();

                    foreach (var appointmentType in existingAppointmentType)
                    {
                        ORM_PPS_TSK_Task_Template appointmentTypeName = ORM_PPS_TSK_Task_Template.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task_Template.Query{
                            IsDeleted = false,
                            Tenant_RefID = securityTicket.TenantID,
                            PPS_TSK_Task_TemplateID = appointmentType.TaskTemplate_RefID
                        }).Single();

                        usedAppointmentTypeList.Add(new L6DO_SDaSwDC_1224_UsedInAppointmentType { AppointmentTypeName = appointmentTypeName.TaskTemplateName });
                    }
                    returnValue.Result.UsedInAppointmentType = usedAppointmentTypeList.ToArray();
                }

                List<ORM_PPS_TSK_Task_StaffBooking> existingAppointment = ORM_PPS_TSK_Task_StaffBooking.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task_StaffBooking.Query
                {
                    IsDeleted = false,
                    Tenant_RefID = securityTicket.TenantID,
                    CMN_BPT_EMP_Employee_RefID = employee.CMN_BPT_EMP_EmployeeID
                }).ToList();

                if (existingAppointment.Count > 0) //cannot delete
                {
                    List<L6DO_SDaSwDC_1224_UsedInAppointment> usedAppointmentList = new List<L6DO_SDaSwDC_1224_UsedInAppointment>();

                    foreach (var appointment in existingAppointment)
                    {
                        ORM_PPS_TSK_Task appointmentName = ORM_PPS_TSK_Task.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task.Query
                        {
                            IsDeleted = false,
                            Tenant_RefID = securityTicket.TenantID,
                            PPS_TSK_TaskID = appointment.PPS_TSK_Task_RefID
                        }).Single();

                        usedAppointmentList.Add(new L6DO_SDaSwDC_1224_UsedInAppointment { AppointmentName = appointmentName.DisplayName });
                    }
                    returnValue.Result.UsedInAppointment = usedAppointmentList.ToArray();
                }

                if (existingAppointment.Count == 0 && existingAppointmentType.Count == 0)
                {
                    EmployeeId = cls_Save_Doctors_and_Staff.Invoke(Connection, Transaction, Parameter.BaseData, securityTicket).Result;
                    var emp = ORM_CMN_BPT_EMP_Employee.Query.Search(Connection, Transaction, new ORM_CMN_BPT_EMP_Employee.Query { BusinessParticipant_RefID = Parameter.BaseData.CMN_BPT_BusinessParticipantID, Tenant_RefID = securityTicket.TenantID }).FirstOrDefault();
                    var orgUnits = ORM_CMN_BPT_EMP_Employee_2_Office.Query.Search(Connection, Transaction, new ORM_CMN_BPT_EMP_Employee_2_Office.Query { CMN_BPT_EMP_Employee_RefID = emp.CMN_BPT_EMP_EmployeeID, Tenant_RefID = securityTicket.TenantID });
                    if(Parameter.UpdateSlots)
                        foreach (var unit in orgUnits)
                            cls_CreateUpdate_Slots_for_Practice.Invoke(Connection, Transaction, new P_L5S_SUSfP_1708() { PracticeID = unit.CMN_STR_Office_RefID }, securityTicket);
                }
            }
            returnValue.Result.ID = EmployeeId;

			return returnValue;
			#endregion UserCode
		}
示例#4
0
        protected static FR_L5TA_STI_1037 Execute(DbConnection Connection, DbTransaction Transaction, P_L5TA_STI_1037 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_L5TA_STI_1037()
            {
                Result = new L5TA_STI_1037()
            };

            //Put your code here
            #region Save

            var webAvailabilityType = ORM_CMN_CAL_AVA_Availability_Type.Query.Search(Connection, Transaction, new ORM_CMN_CAL_AVA_Availability_Type.Query
            {
                IsDeleted                = false,
                Tenant_RefID             = securityTicket.TenantID,
                GlobalPropertyMatchingID = EnumUtils.GetEnumDescription(AvailabilityType.WebBooking)
            }).First();


            var standardAvailabilityType = ORM_CMN_CAL_AVA_Availability_Type.Query.Search(Connection, Transaction, new ORM_CMN_CAL_AVA_Availability_Type.Query
            {
                IsDeleted                = false,
                Tenant_RefID             = securityTicket.TenantID,
                GlobalPropertyMatchingID = EnumUtils.GetEnumDescription(AvailabilityType.Standard)
            }).First();

            if (Parameter.PPS_TSK_TaskID == Guid.Empty)
            {
                //=====================New Task=====================
                ORM_PPS_TSK_Task task = new ORM_PPS_TSK_Task();
                task.PPS_TSK_TaskID = Guid.NewGuid();
                var newTaskIdentifierNumber = ORM_PPS_TSK_Task.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task.Query
                {
                    Tenant_RefID = securityTicket.TenantID
                }).Count;

                task.TaskIdentifier = (++newTaskIdentifierNumber).ToString();
                task.DisplayName    = "AT" + task.TaskIdentifier;
                task.InstantiatedFrom_TaskTemplate_RefID = Parameter.TaskTemplate_RefID;
                task.PlannedStartDate       = Parameter.PlannedStartDate;
                task.PlannedDuration_in_sec = Parameter.PlannedDuration_in_sec;
                task.IsDeleted    = false;
                task.Tenant_RefID = securityTicket.TenantID;
                //=====================Selected availability types=====================

                ORM_PPRS_TSK_Task_SelectedAvailabilityType selectedTypes = new ORM_PPRS_TSK_Task_SelectedAvailabilityType();
                selectedTypes.PPRS_TSK_Task_SelectedAvailabilityTypeID = Guid.NewGuid();
                selectedTypes.PPS_TSK_Task_RefID = task.PPS_TSK_TaskID;
                selectedTypes.CMN_CAL_AVA_Availability_Type_RefID = Parameter.IsWebBooking ? webAvailabilityType.CMN_CAL_AVA_Availability_TypeID : standardAvailabilityType.CMN_CAL_AVA_Availability_TypeID;
                selectedTypes.IsDeleted    = false;
                selectedTypes.Tenant_RefID = securityTicket.TenantID;
                selectedTypes.Save(Connection, Transaction);
                //=====================New Employee=====================
                if (Parameter.Employee != null && Parameter.Employee.Count() > 0)
                {
                    foreach (var employeeParam in Parameter.Employee)
                    {
                        ORM_PPS_TSK_Task_StaffBooking staff = new ORM_PPS_TSK_Task_StaffBooking();
                        staff.PPS_TSK_Task_StaffBookingsID = Guid.NewGuid();
                        staff.PPS_TSK_Task_RefID           = task.PPS_TSK_TaskID;
                        staff.CMN_BPT_EMP_Employee_RefID   = employeeParam.CMN_BPT_EMP_Employee_RefID;
                        staff.CreatedFrom_TaskTemplate_RequiredStaff_RefID = employeeParam.CreatedFrom_TaskTemplate_RequiredStaff_RefID;
                        staff.IsDeleted    = false;
                        staff.Tenant_RefID = securityTicket.TenantID;
                        staff.Save(Connection, Transaction);
                    }
                }
                //=====================New Device=====================
                if (Parameter.Devices != null && Parameter.Devices.Count() > 0)
                {
                    foreach (var deviceParam in Parameter.Devices)
                    {
                        ORM_PPS_TSK_Task_DeviceBooking device = new ORM_PPS_TSK_Task_DeviceBooking();
                        device.PPS_TSK_Task_DeviceBookingID  = Guid.NewGuid();
                        device.PPS_TSK_Task_RefID            = task.PPS_TSK_TaskID;
                        device.PPS_DEV_Device_Instance_RefID = deviceParam.PPS_DEV_Device_Instance_RefID;
                        device.IsDeleted    = false;
                        device.Tenant_RefID = securityTicket.TenantID;
                        device.Save(Connection, Transaction);
                    }
                }
                //=====================New Office=====================
                if (Parameter.Office != null)
                {
                    ORM_PPS_TSK_Task_OfficeBooking office = new ORM_PPS_TSK_Task_OfficeBooking();
                    office.PPS_TSK_Task_OfficeBookingID = Guid.NewGuid();
                    office.PPS_TSK_Task_RefID           = task.PPS_TSK_TaskID;
                    office.CMN_STR_Office_RefID         = Parameter.Office.CMN_STR_Office_RefID;
                    office.IsDeleted    = false;
                    office.Tenant_RefID = securityTicket.TenantID;
                    office.Save(Connection, Transaction);
                }
                //=====================New Patient=====================
                if (Parameter.Patient != null)
                {
                    ORM_HEC_APP_Appointment appointmnet = new ORM_HEC_APP_Appointment();
                    appointmnet.HEC_APP_AppointmentID  = Guid.NewGuid();
                    appointmnet.Ext_PPS_TSK_Task_RefID = task.PPS_TSK_TaskID;
                    appointmnet.IsDeleted    = false;
                    appointmnet.Tenant_RefID = securityTicket.TenantID;
                    appointmnet.Save(Connection, Transaction);

                    ORM_HEC_ACT_PlannedAction plannedAppointment = new ORM_HEC_ACT_PlannedAction();
                    plannedAppointment.HEC_ACT_PlannedActionID = Guid.NewGuid();
                    plannedAppointment.Appointment_RefID       = appointmnet.HEC_APP_AppointmentID;
                    plannedAppointment.Patient_RefID           = Parameter.Patient.Patient_RefID;
                    plannedAppointment.IsDeleted    = false;
                    plannedAppointment.Tenant_RefID = securityTicket.TenantID;
                    plannedAppointment.Save(Connection, Transaction);
                }
                task.Save(Connection, Transaction);
                returnValue.Result.ID = task.PPS_TSK_TaskID;
            }
            #endregion
            //=====================Edit or Delete=====================
            else
            {
                var task = ORM_PPS_TSK_Task.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task.Query
                {
                    PPS_TSK_TaskID = Parameter.PPS_TSK_TaskID,
                    IsDeleted      = false,
                    Tenant_RefID   = securityTicket.TenantID
                }).Single();
                #region Edit
                if (Parameter.IsDeleted == false)
                {
                    if (task.InstantiatedFrom_TaskTemplate_RefID != Parameter.TaskTemplate_RefID) //changed task template
                    {
                        //=====================First delete old employee and device=====================
                        var employeeForDelete = ORM_PPS_TSK_Task_StaffBooking.Query.SoftDelete(Connection, Transaction, new ORM_PPS_TSK_Task_StaffBooking.Query
                        {
                            PPS_TSK_Task_RefID = Parameter.PPS_TSK_TaskID,
                            IsDeleted          = false,
                            Tenant_RefID       = securityTicket.TenantID
                        });
                        var deviceForDelete = ORM_PPS_TSK_Task_DeviceBooking.Query.SoftDelete(Connection, Transaction, new ORM_PPS_TSK_Task_DeviceBooking.Query
                        {
                            PPS_TSK_Task_RefID = Parameter.PPS_TSK_TaskID,
                            IsDeleted          = false,
                            Tenant_RefID       = securityTicket.TenantID
                        });
                        task.InstantiatedFrom_TaskTemplate_RefID = Parameter.TaskTemplate_RefID;
                        task.PlannedDuration_in_sec = Parameter.PlannedDuration_in_sec;

                        //=====================New Employee=====================
                        if (Parameter.Employee != null && Parameter.Employee.Count() > 0)
                        {
                            foreach (var employeeParam in Parameter.Employee)
                            {
                                ORM_PPS_TSK_Task_StaffBooking staff = new ORM_PPS_TSK_Task_StaffBooking();
                                staff.PPS_TSK_Task_StaffBookingsID = Guid.NewGuid();
                                staff.PPS_TSK_Task_RefID           = task.PPS_TSK_TaskID;
                                staff.CMN_BPT_EMP_Employee_RefID   = employeeParam.CMN_BPT_EMP_Employee_RefID;
                                staff.CreatedFrom_TaskTemplate_RequiredStaff_RefID = employeeParam.CreatedFrom_TaskTemplate_RequiredStaff_RefID;
                                staff.IsDeleted    = false;
                                staff.Tenant_RefID = securityTicket.TenantID;
                                staff.Save(Connection, Transaction);
                            }
                        }
                        //=====================New Device=====================
                        if (Parameter.Devices != null && Parameter.Devices.Count() > 0)
                        {
                            foreach (var deviceParam in Parameter.Devices)
                            {
                                ORM_PPS_TSK_Task_DeviceBooking device = new ORM_PPS_TSK_Task_DeviceBooking();
                                device.PPS_TSK_Task_DeviceBookingID  = Guid.NewGuid();
                                device.PPS_TSK_Task_RefID            = task.PPS_TSK_TaskID;
                                device.PPS_DEV_Device_Instance_RefID = deviceParam.PPS_DEV_Device_Instance_RefID;
                                device.IsDeleted    = false;
                                device.Tenant_RefID = securityTicket.TenantID;
                                device.Save(Connection, Transaction);
                            }
                        }
                    }
                    else //only change existing employee and device
                    {
                        //=====================Edit employee=====================
                        if (Parameter.Employee != null)
                        {
                            foreach (var employeeParam in Parameter.Employee)
                            {
                                var existingEmployee = ORM_PPS_TSK_Task_StaffBooking.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task_StaffBooking.Query
                                {
                                    PPS_TSK_Task_StaffBookingsID = employeeParam.PPS_TSK_Task_StaffBookingsID,
                                    IsDeleted    = false,
                                    Tenant_RefID = securityTicket.TenantID
                                }).SingleOrDefault();

                                if (existingEmployee == null) //if employee dont exist (if deleted from other page), create new
                                {
                                    ORM_PPS_TSK_Task_StaffBooking staff = new ORM_PPS_TSK_Task_StaffBooking();
                                    staff.PPS_TSK_Task_StaffBookingsID = Guid.NewGuid();
                                    staff.PPS_TSK_Task_RefID           = task.PPS_TSK_TaskID;
                                    staff.CMN_BPT_EMP_Employee_RefID   = employeeParam.CMN_BPT_EMP_Employee_RefID;
                                    staff.CreatedFrom_TaskTemplate_RequiredStaff_RefID = employeeParam.CreatedFrom_TaskTemplate_RequiredStaff_RefID;
                                    staff.IsDeleted    = false;
                                    staff.Tenant_RefID = securityTicket.TenantID;
                                    staff.Save(Connection, Transaction);
                                }
                                else
                                {
                                    existingEmployee.CMN_BPT_EMP_Employee_RefID = employeeParam.CMN_BPT_EMP_Employee_RefID;
                                    existingEmployee.CreatedFrom_TaskTemplate_RequiredStaff_RefID = employeeParam.CreatedFrom_TaskTemplate_RequiredStaff_RefID;
                                    existingEmployee.Tenant_RefID = securityTicket.TenantID;
                                    existingEmployee.Save(Connection, Transaction);
                                }
                            }
                        }
                        //=====================Edit device=====================
                        if (Parameter.Devices != null)
                        {
                            foreach (var deviceParam in Parameter.Devices)
                            {
                                var existingDevice = ORM_PPS_TSK_Task_DeviceBooking.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task_DeviceBooking.Query
                                {
                                    PPS_TSK_Task_DeviceBookingID = deviceParam.PPS_TSK_Task_DeviceBookingID,
                                    IsDeleted    = false,
                                    Tenant_RefID = securityTicket.TenantID
                                }).SingleOrDefault();
                                if (existingDevice == null)
                                {
                                    ORM_PPS_TSK_Task_DeviceBooking device = new ORM_PPS_TSK_Task_DeviceBooking();
                                    device.PPS_TSK_Task_DeviceBookingID  = Guid.NewGuid();
                                    device.PPS_TSK_Task_RefID            = task.PPS_TSK_TaskID;
                                    device.PPS_DEV_Device_Instance_RefID = deviceParam.PPS_DEV_Device_Instance_RefID;
                                    device.IsDeleted    = false;
                                    device.Tenant_RefID = securityTicket.TenantID;
                                    device.Save(Connection, Transaction);
                                }
                                else
                                {
                                    existingDevice.PPS_DEV_Device_Instance_RefID = deviceParam.PPS_DEV_Device_Instance_RefID;
                                    existingDevice.Tenant_RefID = securityTicket.TenantID;
                                    existingDevice.Save(Connection, Transaction);
                                }
                            }
                        }
                    }
                    //=====================Edit other data=====================
                    task.PlannedStartDate = Parameter.PlannedStartDate;

                    //=====================Edit selected availability types=====================
                    var existingAvailabilityType = ORM_PPRS_TSK_Task_SelectedAvailabilityType.Query.Search(Connection, Transaction, new ORM_PPRS_TSK_Task_SelectedAvailabilityType.Query
                    {
                        IsDeleted          = false,
                        Tenant_RefID       = securityTicket.TenantID,
                        PPS_TSK_Task_RefID = task.PPS_TSK_TaskID
                    }).Single();

                    existingAvailabilityType.CMN_CAL_AVA_Availability_Type_RefID = Parameter.IsWebBooking ? webAvailabilityType.CMN_CAL_AVA_Availability_TypeID : standardAvailabilityType.CMN_CAL_AVA_Availability_TypeID;
                    existingAvailabilityType.Save(Connection, Transaction);

                    //=====================Edit Office=====================
                    if (Parameter.Office != null)
                    {
                        var existingOffice = ORM_PPS_TSK_Task_OfficeBooking.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task_OfficeBooking.Query
                        {
                            PPS_TSK_Task_OfficeBookingID = Parameter.Office.PPS_TSK_Task_OfficeBookingID,
                            IsDeleted    = false,
                            Tenant_RefID = securityTicket.TenantID
                        }).Single();
                        existingOffice.CMN_STR_Office_RefID = Parameter.Office.CMN_STR_Office_RefID;
                        existingOffice.Tenant_RefID         = securityTicket.TenantID;
                        existingOffice.Save(Connection, Transaction);
                    }
                    //=====================Edit Patient=====================
                    //when inter tenant communication is implemented, this part should be changed.
                    if (Parameter.Patient != null && Parameter.Patient.Patient_RefID != Guid.Empty)
                    {
                        var appointmentForEdit = ORM_HEC_APP_Appointment.Query.Search(Connection, Transaction, new ORM_HEC_APP_Appointment.Query
                        {
                            Ext_PPS_TSK_Task_RefID = task.PPS_TSK_TaskID,
                            IsDeleted    = false,
                            Tenant_RefID = securityTicket.TenantID
                        }).Single();

                        var patientPlannedAppointmentForEdit = ORM_HEC_ACT_PlannedAction.Query.Search(Connection, Transaction, new ORM_HEC_ACT_PlannedAction.Query()
                        {
                            Appointment_RefID = appointmentForEdit.HEC_APP_AppointmentID,
                            Tenant_RefID      = securityTicket.TenantID,
                            IsDeleted         = false
                        }).Single();

                        returnValue.Result.ReplacedPatient = new L5TA_STI_1037_ReplacedPatient()
                        {
                            ID = patientPlannedAppointmentForEdit.Patient_RefID
                        };

                        patientPlannedAppointmentForEdit.Patient_RefID = Parameter.Patient.Patient_RefID;
                        patientPlannedAppointmentForEdit.Save(Connection, Transaction);
                    }
                }
                #endregion
                #region Delete
                else
                {
                    //=====================Delete employee=====================
                    var employeeForDelete = ORM_PPS_TSK_Task_StaffBooking.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task_StaffBooking.Query
                    {
                        PPS_TSK_Task_RefID = Parameter.PPS_TSK_TaskID,
                        IsDeleted          = false,
                        Tenant_RefID       = securityTicket.TenantID
                    }).ToArray();
                    foreach (var empForDel in employeeForDelete)
                    {
                        empForDel.IsDeleted = true;
                        empForDel.Save(Connection, Transaction);
                    }
                    //=====================Delete devices=====================
                    var deviceForDelete = ORM_PPS_TSK_Task_DeviceBooking.Query.SoftDelete(Connection, Transaction, new ORM_PPS_TSK_Task_DeviceBooking.Query
                    {
                        PPS_TSK_Task_RefID = Parameter.PPS_TSK_TaskID,
                        IsDeleted          = false,
                        Tenant_RefID       = securityTicket.TenantID
                    });

                    //=====================Delete office=====================
                    var officeForDelete = ORM_PPS_TSK_Task_OfficeBooking.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task_OfficeBooking.Query
                    {
                        PPS_TSK_Task_RefID = Parameter.PPS_TSK_TaskID,
                        IsDeleted          = false,
                        Tenant_RefID       = securityTicket.TenantID
                    }).Single();

                    //=====================Delete patient=====================
                    var appointmentForDelete = ORM_HEC_APP_Appointment.Query.Search(Connection, Transaction, new ORM_HEC_APP_Appointment.Query
                    {
                        Ext_PPS_TSK_Task_RefID = task.PPS_TSK_TaskID,
                        IsDeleted    = false,
                        Tenant_RefID = securityTicket.TenantID
                    }).Single();

                    var plannedAction = ORM_HEC_ACT_PlannedAction.Query.Search(Connection, Transaction, new ORM_HEC_ACT_PlannedAction.Query()
                    {
                        Tenant_RefID      = securityTicket.TenantID,
                        Appointment_RefID = appointmentForDelete.HEC_APP_AppointmentID,
                    }).Single();

                    var patientPlannedAppointmentToDelete = ORM_HEC_ACT_PlannedAction.Query.Search(Connection, Transaction, new ORM_HEC_ACT_PlannedAction.Query()
                    {
                        Appointment_RefID = appointmentForDelete.HEC_APP_AppointmentID,
                        Patient_RefID     = plannedAction.Patient_RefID,
                        Tenant_RefID      = securityTicket.TenantID,
                        IsDeleted         = false
                    }).Single();

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

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

                    task.IsDeleted = true;

                    var firstBookedEmp = employeeForDelete.First();
                    var reqStaff       = ORM_PPS_TSK_Task_RequiredStaff.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task_RequiredStaff.Query()
                    {
                        Tenant_RefID = securityTicket.TenantID,
                        IsDeleted    = false,
                        PPS_TSK_Task_RequiredStaffID = firstBookedEmp.CreatedFrom_TaskTemplate_RequiredStaff_RefID
                    }).Single();

                    var combsSraff = ORM_PPS_TSK_BOK_StaffResource.Query.Search(Connection, Transaction, new ORM_PPS_TSK_BOK_StaffResource.Query()
                    {
                        Tenant_RefID = securityTicket.TenantID,
                        CreatedFor_TaskTemplateRequiredStaff_RefID = reqStaff.PPS_TSK_Task_RequiredStaffID
                    }).ToArray();

                    var comb = ORM_PPS_TSK_BOK_AvailableResourceCombination.Query.Search(Connection, Transaction, new ORM_PPS_TSK_BOK_AvailableResourceCombination.Query()
                    {
                        Tenant_RefID = securityTicket.TenantID,
                        PPS_TSK_BOK_AvailableResourceCombinationID = combsSraff[0].AvailableResourceCombination_RefID,
                    }).Single();

                    if (!comb.IsDeleted)
                    {
                        comb.IsAvailable = true;
                        comb.Save(Connection, Transaction);
                    }

                    officeForDelete.IsDeleted = true;
                    officeForDelete.Save(Connection, Transaction);
                }
                #endregion
                task.Save(Connection, Transaction);
                returnValue.Result.ID = task.PPS_TSK_TaskID;
            }

            return(returnValue);

            #endregion UserCode
        }
示例#5
0
        protected static FR_L5BTS_STI_1331 Execute(DbConnection Connection, DbTransaction Transaction, P_L5BTS_STI_1331 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            //Leave UserCode region to enable user code saving
            #region UserCode
            var returnValue = new FR_L5BTS_STI_1331()
            {
                Result = new L5BTS_STI_1331()
            };

            //Put your code here
            #region Save

            var webAvailabilityType = ORM_CMN_CAL_AVA_Availability_Type.Query.Search(Connection, Transaction, new ORM_CMN_CAL_AVA_Availability_Type.Query
            {
                IsDeleted                = false,
                Tenant_RefID             = securityTicket.TenantID,
                GlobalPropertyMatchingID = EnumUtils.GetEnumDescription(AvailabilityType.WebBooking)
            }).First();


            var standardAvailabilityType = ORM_CMN_CAL_AVA_Availability_Type.Query.Search(Connection, Transaction, new ORM_CMN_CAL_AVA_Availability_Type.Query
            {
                IsDeleted                = false,
                Tenant_RefID             = securityTicket.TenantID,
                GlobalPropertyMatchingID = EnumUtils.GetEnumDescription(AvailabilityType.Standard)
            }).First();

            if (Parameter.PPS_TSK_TaskID == Guid.Empty)
            {
                //=====================New Task=====================
                ORM_PPS_TSK_Task task = new ORM_PPS_TSK_Task();
                task.PPS_TSK_TaskID = Guid.NewGuid();
                var newTaskIdentifierNumber = ORM_PPS_TSK_Task.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task.Query
                {
                    Tenant_RefID = securityTicket.TenantID
                }).Count;

                task.TaskIdentifier = (++newTaskIdentifierNumber).ToString();
                task.DisplayName    = "AT" + task.TaskIdentifier;
                task.InstantiatedFrom_TaskTemplate_RefID = Parameter.TaskTemplate_RefID;
                task.PlannedStartDate       = Parameter.PlannedStartDate;
                task.PlannedDuration_in_sec = Parameter.PlannedDuration_in_sec;
                task.IsDeleted    = false;
                task.Tenant_RefID = securityTicket.TenantID;
                //=====================Selected availability types=====================

                ORM_PPRS_TSK_Task_SelectedAvailabilityType selectedTypes = new ORM_PPRS_TSK_Task_SelectedAvailabilityType();
                selectedTypes.PPRS_TSK_Task_SelectedAvailabilityTypeID = Guid.NewGuid();
                selectedTypes.PPS_TSK_Task_RefID = task.PPS_TSK_TaskID;
                selectedTypes.CMN_CAL_AVA_Availability_Type_RefID = Parameter.IsWebBooking ? webAvailabilityType.CMN_CAL_AVA_Availability_TypeID : standardAvailabilityType.CMN_CAL_AVA_Availability_TypeID;
                selectedTypes.IsDeleted    = false;
                selectedTypes.Tenant_RefID = securityTicket.TenantID;
                selectedTypes.Save(Connection, Transaction);
                //=====================New Employee=====================
                if (Parameter.Employee != null && Parameter.Employee.Count() > 0)
                {
                    foreach (var employeeParam in Parameter.Employee)
                    {
                        ORM_PPS_TSK_Task_StaffBooking staff = new ORM_PPS_TSK_Task_StaffBooking();
                        staff.PPS_TSK_Task_StaffBookingsID = Guid.NewGuid();
                        staff.PPS_TSK_Task_RefID           = task.PPS_TSK_TaskID;
                        staff.CMN_BPT_EMP_Employee_RefID   = employeeParam.CMN_BPT_EMP_Employee_RefID;
                        staff.CreatedFrom_TaskTemplate_RequiredStaff_RefID = employeeParam.CreatedFrom_TaskTemplate_RequiredStaff_RefID;
                        staff.IsDeleted    = false;
                        staff.Tenant_RefID = securityTicket.TenantID;
                        staff.Save(Connection, Transaction);
                    }
                }
                //=====================New Device=====================
                if (Parameter.Devices != null && Parameter.Devices.Count() > 0)
                {
                    foreach (var deviceParam in Parameter.Devices)
                    {
                        ORM_PPS_TSK_Task_DeviceBooking device = new ORM_PPS_TSK_Task_DeviceBooking();
                        device.PPS_TSK_Task_DeviceBookingID  = Guid.NewGuid();
                        device.PPS_TSK_Task_RefID            = task.PPS_TSK_TaskID;
                        device.PPS_DEV_Device_Instance_RefID = deviceParam.PPS_DEV_Device_Instance_RefID;
                        device.IsDeleted    = false;
                        device.Tenant_RefID = securityTicket.TenantID;
                        device.Save(Connection, Transaction);
                    }
                }
                //=====================New Office=====================
                if (Parameter.Office != null)
                {
                    ORM_PPS_TSK_Task_OfficeBooking office = new ORM_PPS_TSK_Task_OfficeBooking();
                    office.PPS_TSK_Task_OfficeBookingID = Guid.NewGuid();
                    office.PPS_TSK_Task_RefID           = task.PPS_TSK_TaskID;
                    office.CMN_STR_Office_RefID         = Parameter.Office.CMN_STR_Office_RefID;
                    office.IsDeleted    = false;
                    office.Tenant_RefID = securityTicket.TenantID;
                    office.Save(Connection, Transaction);
                }
                //=====================New Patient=====================
                if (Parameter.Patient != null)
                {
                    ORM_HEC_APP_Appointment appointmnet = new ORM_HEC_APP_Appointment();
                    appointmnet.HEC_APP_AppointmentID  = Guid.NewGuid();
                    appointmnet.Ext_PPS_TSK_Task_RefID = task.PPS_TSK_TaskID;
                    appointmnet.IsDeleted    = false;
                    appointmnet.Tenant_RefID = securityTicket.TenantID;
                    appointmnet.Save(Connection, Transaction);

                    ORM_HEC_ACT_PlannedAction plannedAppointment = new ORM_HEC_ACT_PlannedAction();
                    plannedAppointment.HEC_ACT_PlannedActionID = Guid.NewGuid();
                    plannedAppointment.Appointment_RefID       = appointmnet.HEC_APP_AppointmentID;
                    plannedAppointment.Patient_RefID           = Parameter.Patient.Patient_RefID;
                    plannedAppointment.IsDeleted    = false;
                    plannedAppointment.Tenant_RefID = securityTicket.TenantID;
                    plannedAppointment.Save(Connection, Transaction);
                }
                task.Save(Connection, Transaction);
                returnValue.Result.ID = task.PPS_TSK_TaskID;
            }
            #endregion
            //=====================Edit or Delete=====================
            else
            {
                var task = ORM_PPS_TSK_Task.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task.Query
                {
                    PPS_TSK_TaskID = Parameter.PPS_TSK_TaskID,
                    IsDeleted      = false,
                    Tenant_RefID   = securityTicket.TenantID
                }).Single();
                #region Edit
                if (Parameter.IsDeleted == false)
                {
                    if (task.InstantiatedFrom_TaskTemplate_RefID != Parameter.TaskTemplate_RefID) //changed task template
                    {
                        //=====================First delete old employee and device=====================
                        var employeeForDelete = ORM_PPS_TSK_Task_StaffBooking.Query.SoftDelete(Connection, Transaction, new ORM_PPS_TSK_Task_StaffBooking.Query
                        {
                            PPS_TSK_Task_RefID = Parameter.PPS_TSK_TaskID,
                            IsDeleted          = false,
                            Tenant_RefID       = securityTicket.TenantID
                        });
                        var deviceForDelete = ORM_PPS_TSK_Task_DeviceBooking.Query.SoftDelete(Connection, Transaction, new ORM_PPS_TSK_Task_DeviceBooking.Query
                        {
                            PPS_TSK_Task_RefID = Parameter.PPS_TSK_TaskID,
                            IsDeleted          = false,
                            Tenant_RefID       = securityTicket.TenantID
                        });
                        task.InstantiatedFrom_TaskTemplate_RefID = Parameter.TaskTemplate_RefID;
                        task.PlannedDuration_in_sec = Parameter.PlannedDuration_in_sec;

                        //=====================New Employee=====================
                        if (Parameter.Employee != null && Parameter.Employee.Count() > 0)
                        {
                            foreach (var employeeParam in Parameter.Employee)
                            {
                                ORM_PPS_TSK_Task_StaffBooking staff = new ORM_PPS_TSK_Task_StaffBooking();
                                staff.PPS_TSK_Task_StaffBookingsID = Guid.NewGuid();
                                staff.PPS_TSK_Task_RefID           = task.PPS_TSK_TaskID;
                                staff.CMN_BPT_EMP_Employee_RefID   = employeeParam.CMN_BPT_EMP_Employee_RefID;
                                staff.CreatedFrom_TaskTemplate_RequiredStaff_RefID = employeeParam.CreatedFrom_TaskTemplate_RequiredStaff_RefID;
                                staff.IsDeleted    = false;
                                staff.Tenant_RefID = securityTicket.TenantID;
                                staff.Save(Connection, Transaction);
                            }
                        }
                        //=====================New Device=====================
                        if (Parameter.Devices != null && Parameter.Devices.Count() > 0)
                        {
                            foreach (var deviceParam in Parameter.Devices)
                            {
                                ORM_PPS_TSK_Task_DeviceBooking device = new ORM_PPS_TSK_Task_DeviceBooking();
                                device.PPS_TSK_Task_DeviceBookingID  = Guid.NewGuid();
                                device.PPS_TSK_Task_RefID            = task.PPS_TSK_TaskID;
                                device.PPS_DEV_Device_Instance_RefID = deviceParam.PPS_DEV_Device_Instance_RefID;
                                device.IsDeleted    = false;
                                device.Tenant_RefID = securityTicket.TenantID;
                                device.Save(Connection, Transaction);
                            }
                        }
                    }
                    else //only change existing employee and device
                    {
                        //=====================Edit employee=====================
                        if (Parameter.Employee != null)
                        {
                            foreach (var employeeParam in Parameter.Employee)
                            {
                                var existingEmployee = ORM_PPS_TSK_Task_StaffBooking.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task_StaffBooking.Query
                                {
                                    PPS_TSK_Task_StaffBookingsID = employeeParam.PPS_TSK_Task_StaffBookingsID,
                                    IsDeleted    = false,
                                    Tenant_RefID = securityTicket.TenantID
                                }).SingleOrDefault();

                                if (existingEmployee == null) //if employee dont exist (if deleted from other page), create new
                                {
                                    ORM_PPS_TSK_Task_StaffBooking staff = new ORM_PPS_TSK_Task_StaffBooking();
                                    staff.PPS_TSK_Task_StaffBookingsID = Guid.NewGuid();
                                    staff.PPS_TSK_Task_RefID           = task.PPS_TSK_TaskID;
                                    staff.CMN_BPT_EMP_Employee_RefID   = employeeParam.CMN_BPT_EMP_Employee_RefID;
                                    staff.CreatedFrom_TaskTemplate_RequiredStaff_RefID = employeeParam.CreatedFrom_TaskTemplate_RequiredStaff_RefID;
                                    staff.IsDeleted    = false;
                                    staff.Tenant_RefID = securityTicket.TenantID;
                                    staff.Save(Connection, Transaction);
                                }
                                else
                                {
                                    existingEmployee.CMN_BPT_EMP_Employee_RefID = employeeParam.CMN_BPT_EMP_Employee_RefID;
                                    existingEmployee.CreatedFrom_TaskTemplate_RequiredStaff_RefID = employeeParam.CreatedFrom_TaskTemplate_RequiredStaff_RefID;
                                    existingEmployee.Tenant_RefID = securityTicket.TenantID;
                                    existingEmployee.Save(Connection, Transaction);
                                }
                            }
                        }
                        //=====================Edit device=====================
                        if (Parameter.Devices != null)
                        {
                            foreach (var deviceParam in Parameter.Devices)
                            {
                                var existingDevice = ORM_PPS_TSK_Task_DeviceBooking.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task_DeviceBooking.Query
                                {
                                    PPS_TSK_Task_DeviceBookingID = deviceParam.PPS_TSK_Task_DeviceBookingID,
                                    IsDeleted    = false,
                                    Tenant_RefID = securityTicket.TenantID
                                }).SingleOrDefault();
                                if (existingDevice == null)
                                {
                                    ORM_PPS_TSK_Task_DeviceBooking device = new ORM_PPS_TSK_Task_DeviceBooking();
                                    device.PPS_TSK_Task_DeviceBookingID  = Guid.NewGuid();
                                    device.PPS_TSK_Task_RefID            = task.PPS_TSK_TaskID;
                                    device.PPS_DEV_Device_Instance_RefID = deviceParam.PPS_DEV_Device_Instance_RefID;
                                    device.IsDeleted    = false;
                                    device.Tenant_RefID = securityTicket.TenantID;
                                    device.Save(Connection, Transaction);
                                }
                                else
                                {
                                    existingDevice.PPS_DEV_Device_Instance_RefID = deviceParam.PPS_DEV_Device_Instance_RefID;
                                    existingDevice.Tenant_RefID = securityTicket.TenantID;
                                    existingDevice.Save(Connection, Transaction);
                                }
                            }
                        }
                    }
                    //=====================Edit other data=====================
                    task.PlannedStartDate = Parameter.PlannedStartDate;

                    //=====================Edit selected availability types=====================
                    var existingAvailabilityType = ORM_PPRS_TSK_Task_SelectedAvailabilityType.Query.Search(Connection, Transaction, new ORM_PPRS_TSK_Task_SelectedAvailabilityType.Query
                    {
                        IsDeleted          = false,
                        Tenant_RefID       = securityTicket.TenantID,
                        PPS_TSK_Task_RefID = task.PPS_TSK_TaskID
                    }).Single();

                    existingAvailabilityType.CMN_CAL_AVA_Availability_Type_RefID = Parameter.IsWebBooking ? webAvailabilityType.CMN_CAL_AVA_Availability_TypeID : standardAvailabilityType.CMN_CAL_AVA_Availability_TypeID;
                    existingAvailabilityType.Save(Connection, Transaction);

                    //=====================Edit Office=====================
                    if (Parameter.Office != null)
                    {
                        var existingOffice = ORM_PPS_TSK_Task_OfficeBooking.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task_OfficeBooking.Query
                        {
                            PPS_TSK_Task_OfficeBookingID = Parameter.Office.PPS_TSK_Task_OfficeBookingID,
                            IsDeleted    = false,
                            Tenant_RefID = securityTicket.TenantID
                        }).Single();
                        existingOffice.CMN_STR_Office_RefID = Parameter.Office.CMN_STR_Office_RefID;
                        existingOffice.Tenant_RefID         = securityTicket.TenantID;
                        existingOffice.Save(Connection, Transaction);
                    }
                    //=====================Edit Patient=====================
                    //when inter tenant communication is implemented, this part should be changed.
                    if (Parameter.Patient != null && Parameter.Patient.Patient_RefID != Guid.Empty)
                    {
                        var appointmentForEdit = ORM_HEC_APP_Appointment.Query.Search(Connection, Transaction, new ORM_HEC_APP_Appointment.Query
                        {
                            Ext_PPS_TSK_Task_RefID = task.PPS_TSK_TaskID,
                            IsDeleted    = false,
                            Tenant_RefID = securityTicket.TenantID
                        }).Single();

                        var patientPlannedAppointmentForEdit = ORM_HEC_ACT_PlannedAction.Query.Search(Connection, Transaction, new ORM_HEC_ACT_PlannedAction.Query()
                        {
                            Appointment_RefID = appointmentForEdit.HEC_APP_AppointmentID,
                            Tenant_RefID      = securityTicket.TenantID,
                            IsDeleted         = false
                        }).Single();

                        returnValue.Result.ReplacedPatient = new L5TA_STI_1037_ReplacedPatient()
                        {
                            ID = patientPlannedAppointmentForEdit.Patient_RefID
                        };

                        patientPlannedAppointmentForEdit.Patient_RefID = Parameter.Patient.Patient_RefID;
                        patientPlannedAppointmentForEdit.Save(Connection, Transaction);
                    }
                }
                #endregion
                #region Delete
                else
                {
                    //=====================Delete employee=====================
                    var employeeForDelete = ORM_PPS_TSK_Task_StaffBooking.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task_StaffBooking.Query
                    {
                        PPS_TSK_Task_RefID = Parameter.PPS_TSK_TaskID,
                        IsDeleted          = false,
                        Tenant_RefID       = securityTicket.TenantID
                    }).ToArray();
                    foreach (var empForDel in employeeForDelete)
                    {
                        empForDel.IsDeleted = true;
                        empForDel.Save(Connection, Transaction);
                    }
                    //=====================Delete devices=====================
                    var deviceForDelete = ORM_PPS_TSK_Task_DeviceBooking.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task_DeviceBooking.Query
                    {
                        PPS_TSK_Task_RefID = Parameter.PPS_TSK_TaskID,
                        IsDeleted          = false,
                        Tenant_RefID       = securityTicket.TenantID
                    }).ToArray();
                    foreach (var devForDel in deviceForDelete)
                    {
                        devForDel.IsDeleted = true;
                        devForDel.Save(Connection, Transaction);
                    }

                    //=====================Delete office=====================
                    var officeForDelete = ORM_PPS_TSK_Task_OfficeBooking.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task_OfficeBooking.Query
                    {
                        PPS_TSK_Task_RefID = Parameter.PPS_TSK_TaskID,
                        IsDeleted          = false,
                        Tenant_RefID       = securityTicket.TenantID
                    }).Single();

                    //=====================Delete patient=====================
                    var appointmentForDelete = ORM_HEC_APP_Appointment.Query.Search(Connection, Transaction, new ORM_HEC_APP_Appointment.Query
                    {
                        Ext_PPS_TSK_Task_RefID = task.PPS_TSK_TaskID,
                        IsDeleted    = false,
                        Tenant_RefID = securityTicket.TenantID
                    }).Single();

                    var plannedAction = ORM_HEC_ACT_PlannedAction.Query.Search(Connection, Transaction, new ORM_HEC_ACT_PlannedAction.Query()
                    {
                        Tenant_RefID      = securityTicket.TenantID,
                        Appointment_RefID = appointmentForDelete.HEC_APP_AppointmentID,
                    }).Single();

                    var patientPlannedAppointmentToDelete = ORM_HEC_ACT_PlannedAction.Query.Search(Connection, Transaction, new ORM_HEC_ACT_PlannedAction.Query()
                    {
                        Appointment_RefID = appointmentForDelete.HEC_APP_AppointmentID,
                        Patient_RefID     = plannedAction.Patient_RefID,
                        Tenant_RefID      = securityTicket.TenantID,
                        IsDeleted         = false
                    }).Single();

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

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

                    task.IsDeleted = true;

                    var firstBookedEmp = employeeForDelete;


                    var slot = ORM_PPS_TSK_BOK_BookableTimeSlot.Query.Search(Connection, Transaction, new ORM_PPS_TSK_BOK_BookableTimeSlot.Query()
                    {
                        Tenant_RefID       = securityTicket.TenantID,
                        FreeInterval_Start = task.PlannedStartDate,
                        TaskTemplate_RefID = task.InstantiatedFrom_TaskTemplate_RefID,
                        Office_RefID       = officeForDelete.CMN_STR_Office_RefID,
                        IsDeleted          = false
                    }).Single();

                    var allCombs = cls_Get_Slot_NotAvaCombiantions_for_SlotID.Invoke(Connection, Transaction, new P_L5BTS_GSNACfSID_1456()
                    {
                        SlotID = slot.PPS_TSK_BOK_BookableTimeSlotID
                    }, securityTicket).Result;

                    L5BTS_GSNACfSID_1456 selecetedComb = null;
                    foreach (var comb in allCombs)
                    {
                        if (!comb.Devices.Select(s => s.PPS_DEV_Device_Instance_RefID).Except(deviceForDelete.Select(s => s.PPS_DEV_Device_Instance_RefID)).Any()
                            &&
                            !comb.Staff.Select(s => s.CMN_BPT_EMP_Employee_RefID).Except(employeeForDelete.Select(s => s.CMN_BPT_EMP_Employee_RefID)).Any())
                        {
                            selecetedComb = comb;
                        }
                    }

                    if (selecetedComb != null)
                    {
                        var comb = ORM_PPS_TSK_BOK_AvailableResourceCombination.Query.Search(Connection, Transaction, new ORM_PPS_TSK_BOK_AvailableResourceCombination.Query()
                        {
                            Tenant_RefID = securityTicket.TenantID,
                            PPS_TSK_BOK_AvailableResourceCombinationID = selecetedComb.PPS_TSK_BOK_AvailableResourceCombinationID,
                        }).Single();

                        if (!comb.IsDeleted)
                        {
                            comb.IsAvailable = true;
                            comb.Save(Connection, Transaction);


                            L5TE_GSAfT_1645[] allEmployeesDB = cls_Get_Staff_with_Availability_for_TenantID.Invoke(Connection, Transaction, securityTicket).Result;
                            ORM_HEC_Doctor[]  hecDoctorsDB   = ORM_HEC_Doctor.Query.Search(Connection, Transaction, new ORM_HEC_Doctor.Query()
                            {
                                Tenant_RefID = securityTicket.TenantID, IsDeleted = false
                            }).ToArray();
                            ORM_HEC_Doctor_AssignableAppointmentType[] hecDoctor2ATDB = ORM_HEC_Doctor_AssignableAppointmentType.Query.Search(Connection, Transaction, new ORM_HEC_Doctor_AssignableAppointmentType.Query()
                            {
                                Tenant_RefID = securityTicket.TenantID, IsDeleted = false
                            }).ToArray();
                            L5TE_GTEFAS_1440[] allStaffExceptions      = cls_Get_TimeExceptionsForAllStaff.Invoke(Connection, Transaction, securityTicket).Result;
                            L3P_GPfT_1537[]    professionsForTenant    = cls_Get_Professions_for_TenantID.Invoke(Connection, Transaction, securityTicket).Result;
                            L5TE_GSAfT_1645[]  employeesFromPracticeDB = allEmployeesDB.Where(e => employeeForDelete.Select(s => s.CMN_BPT_EMP_Employee_RefID).Contains(e.CMN_BPT_EMP_EmployeeID)).ToArray();
                            var staff = ModelConvertor.ConvertStaffDBData(officeForDelete.CMN_STR_Office_RefID, employeesFromPracticeDB, hecDoctorsDB, hecDoctor2ATDB, allStaffExceptions, professionsForTenant);

                            TimeSlot ts = new TimeSlot()
                            {
                                PeriodStart = slot.FreeInterval_Start, PeriodEnd = slot.FreeInterval_End
                            };
                            bool isComboWebBookable = true;
                            foreach (var item in selecetedComb.Staff)
                            {
                                var employee = staff.Single(s => s.ID == item.CMN_BPT_EMP_Employee_RefID);
                                if (!StaffAvailabiltyCalculations.IsStaffWebBookableInThisTameRange(employee, ts))
                                {
                                    isComboWebBookable = false;
                                    break;
                                }
                            }

                            if (isComboWebBookable)
                            {
                                var slot2ATs = ORM_PPS_TSK_BOK_BookableTimeSlots_2_AvailabilityType.Query.Search(Connection, Transaction, new ORM_PPS_TSK_BOK_BookableTimeSlots_2_AvailabilityType.Query()
                                {
                                    Tenant_RefID = securityTicket.TenantID,
                                    IsDeleted    = false,
                                    PPS_TSK_BOK_BookableTimeSlot_RefID = slot.PPS_TSK_BOK_BookableTimeSlotID
                                }).Single();

                                if (slot2ATs.CMN_CAL_AVA_Availability_TypeID != webAvailabilityType.CMN_CAL_AVA_Availability_TypeID)
                                {
                                    slot2ATs.CMN_CAL_AVA_Availability_TypeID = webAvailabilityType.CMN_CAL_AVA_Availability_TypeID;
                                    slot2ATs.Save(Connection, Transaction);
                                }
                            }
                        }
                    }

                    officeForDelete.IsDeleted = true;
                    officeForDelete.Save(Connection, Transaction);
                }
                #endregion
                task.Save(Connection, Transaction);
                returnValue.Result.ID = task.PPS_TSK_TaskID;
            }

            return(returnValue);

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

            returnValue.Result = new L6OU_SOUGDwDC_1541();

            Guid OrgUnitId = Guid.Empty;
            if (!Parameter.BaseData.IsDeleted)
            {
                OrgUnitId = cls_Save_OrgsUnitsGeneralData.Invoke(Connection, Transaction, Parameter.BaseData, securityTicket).Result;

                if (Parameter.UpdateSlots)
                {
                    cls_CreateUpdate_Slots_for_Practice.Invoke(Connection, Transaction, new P_L5S_SUSfP_1708()
                    {
                        PracticeID = OrgUnitId
                    }, securityTicket);
                }
            }
            else
            {
                List <ORM_PPS_TSK_Task_OfficeBooking> existingAppointment = ORM_PPS_TSK_Task_OfficeBooking.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task_OfficeBooking.Query
                {
                    IsDeleted            = false,
                    Tenant_RefID         = securityTicket.TenantID,
                    CMN_STR_Office_RefID = Parameter.BaseData.OrgUnitID
                }).ToList();

                if (existingAppointment.Count > 0) //cannot delete
                {
                    List <L6OU_SOUGDwDC_1541_UsedInAppointment> usedAppointmentList = new List <L6OU_SOUGDwDC_1541_UsedInAppointment>();

                    foreach (var appointment in existingAppointment)
                    {
                        ORM_PPS_TSK_Task appointmentName = ORM_PPS_TSK_Task.Query.Search(Connection, Transaction, new ORM_PPS_TSK_Task.Query
                        {
                            IsDeleted      = false,
                            Tenant_RefID   = securityTicket.TenantID,
                            PPS_TSK_TaskID = appointment.PPS_TSK_Task_RefID
                        }).Single();
                        usedAppointmentList.Add(new L6OU_SOUGDwDC_1541_UsedInAppointment {
                            AppointmentName = appointmentName.DisplayName
                        });
                    }
                    returnValue.Result.UsedInAppointment = usedAppointmentList.ToArray();
                }

                List <ORM_CMN_STR_Office> existingParentOrgUnit = ORM_CMN_STR_Office.Query.Search(Connection, Transaction, new ORM_CMN_STR_Office.Query
                {
                    IsDeleted    = false,
                    Tenant_RefID = securityTicket.TenantID,
                    Parent_RefID = Parameter.BaseData.OrgUnitID
                }).ToList();

                if (existingParentOrgUnit.Count > 0) //cannot delete
                {
                    List <L6OU_SOUGDwDC_1541_UsedInOrgUnit> usedOrgUnitList = new List <L6OU_SOUGDwDC_1541_UsedInOrgUnit>();

                    foreach (var orgUnit in existingParentOrgUnit)
                    {
                        usedOrgUnitList.Add(new L6OU_SOUGDwDC_1541_UsedInOrgUnit {
                            OrgUnitName = orgUnit.Office_Name
                        });
                    }

                    returnValue.Result.UsedInOrgUnit = usedOrgUnitList.ToArray();
                }

                List <ORM_CMN_BPT_EMP_Employee_2_Office> existingEmployee = ORM_CMN_BPT_EMP_Employee_2_Office.Query.Search(Connection, Transaction, new ORM_CMN_BPT_EMP_Employee_2_Office.Query
                {
                    IsDeleted            = false,
                    Tenant_RefID         = securityTicket.TenantID,
                    CMN_STR_Office_RefID = Parameter.BaseData.OrgUnitID
                }).ToList();

                if (existingEmployee.Count > 0) //cannot delete
                {
                    List <L6OU_SOUGDwDC_1541_UsedInEmployee> usedEmployeeList = new List <L6OU_SOUGDwDC_1541_UsedInEmployee>();

                    foreach (var employee in existingEmployee)
                    {
                        L5DO_GSNfEID_1107 employeeName = cls_Get_Staff_Name_for_EmployeeID.Invoke(Connection, Transaction, new P_L5DO_GSNfEID_1107 {
                            EmployeeID = employee.CMN_BPT_EMP_Employee_RefID
                        }, securityTicket).Result;
                        string fullName = employeeName.FirstName + " " + employeeName.LastName;
                        usedEmployeeList.Add(new L6OU_SOUGDwDC_1541_UsedInEmployee {
                            EmployeeName = fullName
                        });
                    }
                    returnValue.Result.UsedInEmployee = usedEmployeeList.ToArray();
                }

                if (existingAppointment.Count == 0 && existingParentOrgUnit.Count == 0 && existingEmployee.Count == 0)
                {
                    OrgUnitId = cls_Save_OrgsUnitsGeneralData.Invoke(Connection, Transaction, Parameter.BaseData, securityTicket).Result;

                    if (Parameter.UpdateSlots)
                    {
                        cls_Delete_Slots_for_PracticeID.Invoke(Connection, Transaction, new P_L5BTS_DSfPID_1103()
                        {
                            OfficeID = Parameter.BaseData.OrgUnitID
                        }, securityTicket);
                    }
                }
            }
            returnValue.Result.ID = OrgUnitId;

            return(returnValue);

            #endregion UserCode
        }