///<summary>
		/// Method Invocation of wrapper classes
		///<summary>
		protected static FR_L6DO_SDaSwDC_1224 Invoke(DbConnection Connection, DbTransaction Transaction, string ConnectionString,P_L6DO_SDaSwDC_1224 Parameter,CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
		{
			bool cleanupConnection = Connection == null;
			bool cleanupTransaction = Transaction == null;

			FR_L6DO_SDaSwDC_1224 functionReturn = new FR_L6DO_SDaSwDC_1224();
			try
			{

				if (cleanupConnection == true) 
				{
					Connection = CSV2Core_MySQL.Support.DBSQLSupport.CreateConnection(ConnectionString);
					Connection.Open();
				}
				if (cleanupTransaction == true)
				{
					Transaction = Connection.BeginTransaction();
				}

				functionReturn = Execute(Connection, Transaction,Parameter,securityTicket);

				#region Cleanup Connection/Transaction
				//Commit the transaction 
				if (cleanupTransaction == true)
				{
					Transaction.Commit();
				}
				//Close the connection
				if (cleanupConnection == true)
				{
					Connection.Close();
				}
				#endregion
			}
			catch (Exception ex)
			{
				try
				{
					if (cleanupTransaction == true && Transaction!=null)
					{
						Transaction.Rollback();
					}
				}
				catch { }

				try
				{
					if (cleanupConnection == true && Connection != null)
					{
						Connection.Close();
					}
				}
				catch { }

				throw new Exception("Exception occured in method cls_Save_Doctors_and_Staff_withDeleteCheck",ex);
			}
			return functionReturn;
		}
		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
		}