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

            L6TN_GSFT_1017 settings = cls_Get_Settings_For_Tenant.Invoke(Connection, Transaction, securityTicket).Result;

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


            ORM_CMN_CAL_Event calEvent = new ORM_CMN_CAL_Event();
            calEvent.Load(Connection, Transaction, scheduleDetail.CMN_CAL_Event_RefID);

            if (scheduleDetail.CMN_BPT_EMP_Employee_LeaveRequest_RefID != Guid.Empty)
            {
                ORM_CMN_BPT_EMP_Employee_LeaveRequest leaveRequest = new ORM_CMN_BPT_EMP_Employee_LeaveRequest();
                leaveRequest.Load(Connection, Transaction, scheduleDetail.CMN_BPT_EMP_Employee_LeaveRequest_RefID);

                Guid oldLeaveRequestID = leaveRequest.CMN_BPT_EMP_Employee_LeaveRequestID;
                ORM_CMN_CAL_Event leaveRequestEvent = new ORM_CMN_CAL_Event();
                leaveRequestEvent.Load(Connection, Transaction, leaveRequest.CMN_CAL_Event_RefID);
                var calQuery = new ORM_CMN_CAL_Event.Query();
                calQuery.CMN_CAL_EventID = leaveRequest.CMN_CAL_Event_RefID;
                var calendarRes = ORM_CMN_CAL_Event.Query.Search(Connection, Transaction, calQuery);
                ORM_CMN_CAL_Event calendarEvent = calendarRes[0];
                var timeFrame = cls_Get_CalculationTimeFramesForTenant.Invoke(Connection, Transaction, securityTicket).Result.Where(x => x.CalculationTimeframe_StartDate.Year == calendarEvent.StartTime.Year).FirstOrDefault();


                if (leaveRequestEvent.StartTime.Date != leaveRequestEvent.EndTime.Date)
                {
                    DateTimeRange firstRange  = null;
                    DateTimeRange secondRange = null;
                    DateTimeRange thirdRange  = null;

                    ORM_CMN_STR_PPS_DailyWorkSchedule dailySchedule = new ORM_CMN_STR_PPS_DailyWorkSchedule();
                    dailySchedule.Load(Connection, Transaction, scheduleDetail.DailyWorkSchedule_RefID);

                    if (leaveRequestEvent.StartTime.Date == dailySchedule.WorkSheduleDate)
                    {
                        P_L5DWS_GDWSWDFT_0946 dailyScheduleDetailsPar = new P_L5DWS_GDWSWDFT_0946();
                        dailyScheduleDetailsPar.WorkSheduleDate = dailySchedule.WorkSheduleDate.AddDays(1).Date;
                        var schedule = cls_Get_DailyWorkSchedules_WithDetails_For_Date.Invoke(Connection, Transaction, dailyScheduleDetailsPar, securityTicket).Result.FirstOrDefault(i => i.Employee_RefID == dailySchedule.Employee_RefID);
                        secondRange = new DateTimeRange();
                        if (schedule != null && schedule.Details.Any(i => i.LeaveRequest_RefID == leaveRequest.CMN_BPT_EMP_Employee_LeaveRequestID))
                        {
                            secondRange.Start = schedule.Details.FirstOrDefault(i => i.LeaveRequest_RefID == leaveRequest.CMN_BPT_EMP_Employee_LeaveRequestID).FromTime_as_DateTime;
                            secondRange.End   = leaveRequestEvent.EndTime;
                        }
                        else
                        {
                            secondRange = null;
                        }
                    }
                    else if (leaveRequestEvent.EndTime.Date == dailySchedule.WorkSheduleDate)
                    {
                        firstRange       = new DateTimeRange();
                        firstRange.Start = leaveRequestEvent.StartTime;


                        P_L5DWS_GDWSWDFT_0946 dailyScheduleDetailsPar = new P_L5DWS_GDWSWDFT_0946();
                        dailyScheduleDetailsPar.WorkSheduleDate = dailySchedule.WorkSheduleDate.AddDays(-1).Date;
                        var schedule = cls_Get_DailyWorkSchedules_WithDetails_For_Date.Invoke(Connection, Transaction, dailyScheduleDetailsPar, securityTicket).Result.FirstOrDefault(i => i.Employee_RefID == dailySchedule.Employee_RefID);
                        if (schedule != null && schedule.Details.Any(i => i.LeaveRequest_RefID == leaveRequest.CMN_BPT_EMP_Employee_LeaveRequestID))
                        {
                            firstRange.End = schedule.Details.FirstOrDefault(i => i.LeaveRequest_RefID == leaveRequest.CMN_BPT_EMP_Employee_LeaveRequestID).ToTime_as_DateTime;
                        }
                        else
                        {
                            firstRange = null;
                        }
                    }
                    else
                    {
                        firstRange       = new DateTimeRange();
                        firstRange.Start = leaveRequestEvent.StartTime;

                        P_L5DWS_GDWSWDFT_0946 dailyScheduleDetailsPar = new P_L5DWS_GDWSWDFT_0946();
                        dailyScheduleDetailsPar.WorkSheduleDate = dailySchedule.WorkSheduleDate.AddDays(-1).Date;
                        var schedule = cls_Get_DailyWorkSchedules_WithDetails_For_Date.Invoke(Connection, Transaction, dailyScheduleDetailsPar, securityTicket).Result.FirstOrDefault(i => i.Employee_RefID == dailySchedule.Employee_RefID);
                        if (schedule != null && schedule.Details.Any(i => i.LeaveRequest_RefID == leaveRequest.CMN_BPT_EMP_Employee_LeaveRequestID))
                        {
                            firstRange.End = schedule.Details.FirstOrDefault(i => i.LeaveRequest_RefID == leaveRequest.CMN_BPT_EMP_Employee_LeaveRequestID).ToTime_as_DateTime;
                        }
                        else
                        {
                            firstRange = null;
                        }
                        P_L5DWS_GDWSWDFT_0946 dailyScheduleDetailsParSecond = new P_L5DWS_GDWSWDFT_0946();
                        dailyScheduleDetailsParSecond.WorkSheduleDate = dailySchedule.WorkSheduleDate.AddDays(1).Date;
                        var scheduleSecond = cls_Get_DailyWorkSchedules_WithDetails_For_Date.Invoke(Connection, Transaction, dailyScheduleDetailsParSecond, securityTicket).Result.FirstOrDefault(i => i.Employee_RefID == dailySchedule.Employee_RefID);
                        secondRange = new DateTimeRange();

                        if (scheduleSecond != null && scheduleSecond.Details.Any(i => i.LeaveRequest_RefID == leaveRequest.CMN_BPT_EMP_Employee_LeaveRequestID))
                        {
                            secondRange.End   = leaveRequestEvent.EndTime;
                            secondRange.Start = scheduleSecond.Details.FirstOrDefault(i => i.LeaveRequest_RefID == leaveRequest.CMN_BPT_EMP_Employee_LeaveRequestID).FromTime_as_DateTime;
                        }
                        else
                        {
                            secondRange = null;
                        }
                    }

                    #region cancelLeaveRequest

                    P_L5LR_SEAA_1351 ApprovalActionParam = new P_L5LR_SEAA_1351();
                    ApprovalActionParam.Approval_Action_Commnet         = "";
                    ApprovalActionParam.CMN_CAL_Event_Approval_ActionID = Guid.Empty;
                    ApprovalActionParam.EventApproval_RefID             = leaveRequest.CMN_CAL_Event_Approval_RefID;
                    ApprovalActionParam.IsApproval   = false;
                    ApprovalActionParam.IsDenial     = false;
                    ApprovalActionParam.IsRevocation = true;
                    cls_Save_Employee_ApprovalAction.Invoke(Connection, Transaction, ApprovalActionParam, securityTicket);



                    var approvalItem = new ORM_CMN_CAL_Event_Approval();
                    if (leaveRequest.CMN_CAL_Event_Approval_RefID != Guid.Empty)
                    {
                        var approvalResult = approvalItem.Load(Connection, Transaction, leaveRequest.CMN_CAL_Event_Approval_RefID);
                        if (approvalResult.Status != FR_Status.Success || approvalItem.CMN_CAL_Event_ApprovalID == Guid.Empty)
                        {
                            var error = new FR_Guid();
                            error.ErrorMessage = "No Such ID";
                            error.Status       = FR_Status.Error_Internal;
                            return(error);
                        }
                    }

                    bool isApprovedBefore = approvalItem.IsApproved;

                    approvalItem.IsApprovalProcessDenied         = false;
                    approvalItem.IsApprovalProcessOpened         = false;
                    approvalItem.IsApprovalProcessCanceledByUser = true;
                    approvalItem.IsApproved = false;

                    approvalItem.Save(Connection, Transaction);


                    P_L5EM_GEATFSbRTFE_1423 statParam = new P_L5EM_GEATFSbRTFE_1423();
                    statParam.absenceReasonID = leaveRequest.CMN_BPT_STA_AbsenceReason_RefID;
                    statParam.employeeID      = leaveRequest.RequestedFor_Employee_RefID;
                    statParam.timeFrameID     = timeFrame.CMN_CAL_CalculationTimeframeID;
                    var statistics = cls_Get_Employee_AbsenceReason_TimeframeStatistic_byReasonTimeFrameEmployee.Invoke(Connection, Transaction, statParam, securityTicket).Result;
                    if (statistics != null)
                    {
                        P_L5EM_SEARTFS_1356 updateStatisticsParam = new P_L5EM_SEARTFS_1356();
                        updateStatisticsParam.CMN_BPT_EMP_Employee_AbsenceReason_TimeframeStatisticsID = statistics.CMN_BPT_EMP_Employee_AbsenceReason_TimeframeStatisticsID;
                        updateStatisticsParam.Employee_RefID             = statistics.Employee_RefID;
                        updateStatisticsParam.CalculationTimeframe_RefID = statistics.CalculationTimeframe_RefID;
                        updateStatisticsParam.AbsenceReason_RefID        = statistics.AbsenceReason_RefID;

                        if (isApprovedBefore)
                        {
                            updateStatisticsParam.R_RequestReservedAbsence_InDays  = statistics.R_RequestReservedAbsence_InDays;
                            updateStatisticsParam.R_RequestReservedAbsence_InHours = statistics.R_RequestReservedAbsence_InHours;

                            updateStatisticsParam.R_AbsenceTimeUsed_InDays  = statistics.R_AbsenceTimeUsed_InDays - Parameter.durationInDays;
                            updateStatisticsParam.R_AbsenceTimeUsed_InHours = statistics.R_AbsenceTimeUsed_InHours - Parameter.durationInHours;

                            updateStatisticsParam.R_TotalAllowedAbsenceTime_InDays  = statistics.R_TotalAllowedAbsenceTime_InDays + Parameter.durationInDays;
                            updateStatisticsParam.R_TotalAllowedAbsenceTime_InHours = statistics.R_TotalAllowedAbsenceTime_InHours + Parameter.durationInHours;
                        }
                        else
                        {
                            updateStatisticsParam.R_RequestReservedAbsence_InDays  = statistics.R_RequestReservedAbsence_InDays - Parameter.durationInDays;
                            updateStatisticsParam.R_RequestReservedAbsence_InHours = statistics.R_RequestReservedAbsence_InHours - Parameter.durationInHours;

                            updateStatisticsParam.R_AbsenceTimeUsed_InDays  = statistics.R_AbsenceTimeUsed_InDays;
                            updateStatisticsParam.R_AbsenceTimeUsed_InHours = statistics.R_AbsenceTimeUsed_InHours;

                            updateStatisticsParam.R_TotalAllowedAbsenceTime_InDays  = statistics.R_TotalAllowedAbsenceTime_InDays;
                            updateStatisticsParam.R_TotalAllowedAbsenceTime_InHours = statistics.R_TotalAllowedAbsenceTime_InHours;
                        }

                        var res = cls_Save_Employee_AbsenceReason_TimeframeStatistic.Invoke(Connection, Transaction, updateStatisticsParam, securityTicket);
                    }
                    leaveRequest.Save(Connection, Transaction);

                    Guid absenceReasonID = leaveRequest.CMN_BPT_STA_AbsenceReason_RefID;

                    #endregion


                    #region FirstRange

                    var authRequired = true;

                    var absenceReasons = cls_get_Active_AbsenceReason_For_TenantID.Invoke(Connection, Transaction, securityTicket).Result;
                    if (absenceReasons != null)
                    {
                        if (absenceReasons.Any(r => r.CMN_BPT_STA_AbsenceReasonID == absenceReasonID))
                        {
                            var reason = absenceReasons.FirstOrDefault(r => r.CMN_BPT_STA_AbsenceReasonID == absenceReasonID);
                            if (reason != null)
                            {
                                authRequired = reason.IsAuthorizationRequired;
                            }
                        }
                    }

                    if (firstRange != null)
                    {
                        leaveRequestEvent = new ORM_CMN_CAL_Event();
                        leaveRequestEvent.CMN_CAL_EventID          = Guid.NewGuid();
                        leaveRequestEvent.StartTime                = firstRange.Start;
                        leaveRequestEvent.EndTime                  = firstRange.End;
                        leaveRequestEvent.R_EventDuration_sec      = (int)firstRange.End.Subtract(firstRange.Start).TotalSeconds;
                        leaveRequestEvent.IsRepetitive             = false;
                        leaveRequestEvent.IsWholeDayEvent          = false;
                        leaveRequestEvent.IsCalendarEvent_Editable = true;
                        leaveRequestEvent.Tenant_RefID             = securityTicket.TenantID;
                        leaveRequestEvent.Save(Connection, Transaction);

                        approvalItem = new ORM_CMN_CAL_Event_Approval();
                        approvalItem.CMN_CAL_Event_ApprovalID = Guid.NewGuid();
                        approvalItem.Event_RefID             = leaveRequestEvent.CMN_CAL_EventID;
                        approvalItem.IsApprovalProcessDenied = false;
                        approvalItem.IsApprovalProcessOpened = false;
                        if (settings.NumberOfResponsiblePersonsRequiredToApprove == 0 || !authRequired)
                        {
                            approvalItem.IsApproved = true;
                            approvalItem.IsApprovalProcessOpened = false;
                        }
                        else
                        {
                            approvalItem.IsApproved = false;
                            approvalItem.IsApprovalProcessOpened = true;
                        }
                        approvalItem.IsApprovalProcessCanceledByUser = false;
                        approvalItem.IsDeleted          = false;
                        approvalItem.Creation_Timestamp = DateTime.Now;
                        approvalItem.Tenant_RefID       = securityTicket.TenantID;
                        approvalItem.Save(Connection, Transaction);



                        leaveRequest = new ORM_CMN_BPT_EMP_Employee_LeaveRequest();
                        leaveRequest.CMN_BPT_EMP_Employee_LeaveRequestID = Guid.NewGuid();
                        leaveRequest.CMN_BPT_STA_AbsenceReason_RefID     = absenceReasonID;
                        leaveRequest.CMN_CAL_Event_Approval_RefID        = approvalItem.CMN_CAL_Event_ApprovalID;
                        leaveRequest.CMN_CAL_Event_RefID         = leaveRequestEvent.CMN_CAL_EventID;
                        leaveRequest.IsDeleted                   = false;
                        leaveRequest.RequestedFor_Employee_RefID = dailySchedule.Employee_RefID;
                        leaveRequest.Tenant_RefID                = securityTicket.TenantID;
                        leaveRequest.Creation_Timestamp          = DateTime.Now;
                        leaveRequest.LeaveRequestCreationSource  = "plannico.time";
                        leaveRequest.Save(Connection, Transaction);


                        P_L5DWS_GDWSWDFP_1225 par = new P_L5DWS_GDWSWDFP_1225();
                        par.StartDate = firstRange.Start;
                        par.EndDate   = firstRange.End;
                        var schedulesWithDetails = cls_Get_DailyWorkSchedules_WithDetails_For_Period.Invoke(Connection, Transaction, par, securityTicket).Result.DailyWorkSchedulesWithDetails;

                        foreach (var schedule in schedulesWithDetails)
                        {
                            if (schedule.Details.Any(i => i.LeaveRequest_RefID == oldLeaveRequestID))
                            {
                                foreach (var detailItem in schedule.Details.Where(i => i.LeaveRequest_RefID == oldLeaveRequestID).ToArray())
                                {
                                    ORM_CMN_STR_PPS_DailyWorkSchedule_Detail detail = new ORM_CMN_STR_PPS_DailyWorkSchedule_Detail();
                                    detail.Load(Connection, Transaction, detailItem.CMN_STR_PPS_DailyWorkSchedule_DetailID);
                                    detail.CMN_BPT_EMP_Employee_LeaveRequest_RefID = leaveRequest.CMN_BPT_EMP_Employee_LeaveRequestID;
                                    detail.Save(Connection, Transaction);
                                }
                            }
                        }
                    }
                    #endregion

                    #region secondRange
                    if (secondRange != null)
                    {
                        leaveRequestEvent = new ORM_CMN_CAL_Event();
                        leaveRequestEvent.CMN_CAL_EventID          = Guid.NewGuid();
                        leaveRequestEvent.StartTime                = secondRange.Start;
                        leaveRequestEvent.EndTime                  = secondRange.End;
                        leaveRequestEvent.R_EventDuration_sec      = (int)secondRange.End.Subtract(secondRange.Start).TotalSeconds;
                        leaveRequestEvent.IsRepetitive             = false;
                        leaveRequestEvent.IsWholeDayEvent          = false;
                        leaveRequestEvent.IsCalendarEvent_Editable = true;
                        leaveRequestEvent.Tenant_RefID             = securityTicket.TenantID;
                        leaveRequestEvent.Save(Connection, Transaction);

                        approvalItem = new ORM_CMN_CAL_Event_Approval();
                        approvalItem.CMN_CAL_Event_ApprovalID = Guid.NewGuid();
                        approvalItem.Event_RefID             = leaveRequestEvent.CMN_CAL_EventID;
                        approvalItem.IsApprovalProcessDenied = false;
                        approvalItem.IsApprovalProcessOpened = false;
                        if (settings.NumberOfResponsiblePersonsRequiredToApprove == 0 || !authRequired)
                        {
                            approvalItem.IsApproved = true;
                            approvalItem.IsApprovalProcessOpened = false;
                        }
                        else
                        {
                            approvalItem.IsApprovalProcessOpened = true;
                            approvalItem.IsApproved = false;
                        }
                        approvalItem.IsApprovalProcessCanceledByUser = false;
                        approvalItem.IsDeleted          = false;
                        approvalItem.Creation_Timestamp = DateTime.Now;
                        approvalItem.Tenant_RefID       = securityTicket.TenantID;
                        approvalItem.Save(Connection, Transaction);



                        leaveRequest = new ORM_CMN_BPT_EMP_Employee_LeaveRequest();
                        leaveRequest.CMN_BPT_EMP_Employee_LeaveRequestID = Guid.NewGuid();
                        leaveRequest.CMN_BPT_STA_AbsenceReason_RefID     = absenceReasonID;
                        leaveRequest.CMN_CAL_Event_Approval_RefID        = approvalItem.CMN_CAL_Event_ApprovalID;
                        leaveRequest.CMN_CAL_Event_RefID         = leaveRequestEvent.CMN_CAL_EventID;
                        leaveRequest.IsDeleted                   = false;
                        leaveRequest.RequestedFor_Employee_RefID = dailySchedule.Employee_RefID;
                        leaveRequest.Tenant_RefID                = securityTicket.TenantID;
                        leaveRequest.Creation_Timestamp          = DateTime.Now;
                        leaveRequest.LeaveRequestCreationSource  = "plannico.time";
                        leaveRequest.Save(Connection, Transaction);

                        P_L5DWS_GDWSWDFP_1225 par = new P_L5DWS_GDWSWDFP_1225();
                        par.StartDate = secondRange.Start;
                        par.EndDate   = secondRange.End;
                        var schedulesWithDetails = cls_Get_DailyWorkSchedules_WithDetails_For_Period.Invoke(Connection, Transaction, par, securityTicket).Result.DailyWorkSchedulesWithDetails;

                        foreach (var schedule in schedulesWithDetails)
                        {
                            if (schedule.Details.Any(i => i.LeaveRequest_RefID == oldLeaveRequestID))
                            {
                                foreach (var detailItem in schedule.Details.Where(i => i.LeaveRequest_RefID == oldLeaveRequestID).ToArray())
                                {
                                    ORM_CMN_STR_PPS_DailyWorkSchedule_Detail detail = new ORM_CMN_STR_PPS_DailyWorkSchedule_Detail();
                                    detail.Load(Connection, Transaction, detailItem.CMN_STR_PPS_DailyWorkSchedule_DetailID);
                                    detail.CMN_BPT_EMP_Employee_LeaveRequest_RefID = leaveRequest.CMN_BPT_EMP_Employee_LeaveRequestID;
                                    detail.Save(Connection, Transaction);
                                }
                            }
                        }
                    }

                    #endregion

                    #region thirdRange
                    if (thirdRange != null)
                    {
                        leaveRequestEvent = new ORM_CMN_CAL_Event();
                        leaveRequestEvent.CMN_CAL_EventID          = Guid.NewGuid();
                        leaveRequestEvent.StartTime                = thirdRange.Start;
                        leaveRequestEvent.EndTime                  = thirdRange.End;
                        leaveRequestEvent.R_EventDuration_sec      = (int)thirdRange.End.Subtract(thirdRange.Start).TotalSeconds;
                        leaveRequestEvent.IsRepetitive             = false;
                        leaveRequestEvent.IsWholeDayEvent          = false;
                        leaveRequestEvent.IsCalendarEvent_Editable = true;
                        leaveRequestEvent.Tenant_RefID             = securityTicket.TenantID;
                        leaveRequestEvent.Save(Connection, Transaction);

                        approvalItem = new ORM_CMN_CAL_Event_Approval();
                        approvalItem.CMN_CAL_Event_ApprovalID = Guid.NewGuid();
                        approvalItem.Event_RefID             = leaveRequestEvent.CMN_CAL_EventID;
                        approvalItem.IsApprovalProcessDenied = false;
                        approvalItem.IsApprovalProcessOpened = false;
                        if (settings.NumberOfResponsiblePersonsRequiredToApprove == 0 || !authRequired)
                        {
                            approvalItem.IsApprovalProcessOpened = false;
                            approvalItem.IsApproved = true;
                        }
                        else
                        {
                            approvalItem.IsApprovalProcessOpened = true;
                            approvalItem.IsApproved = false;
                        }
                        approvalItem.IsApprovalProcessCanceledByUser = false;
                        approvalItem.IsDeleted          = false;
                        approvalItem.Creation_Timestamp = DateTime.Now;
                        approvalItem.Tenant_RefID       = securityTicket.TenantID;
                        approvalItem.Save(Connection, Transaction);



                        leaveRequest = new ORM_CMN_BPT_EMP_Employee_LeaveRequest();
                        leaveRequest.CMN_BPT_EMP_Employee_LeaveRequestID = Guid.NewGuid();
                        leaveRequest.CMN_BPT_STA_AbsenceReason_RefID     = absenceReasonID;
                        leaveRequest.CMN_CAL_Event_Approval_RefID        = approvalItem.CMN_CAL_Event_ApprovalID;
                        leaveRequest.CMN_CAL_Event_RefID         = leaveRequestEvent.CMN_CAL_EventID;
                        leaveRequest.IsDeleted                   = false;
                        leaveRequest.RequestedFor_Employee_RefID = dailySchedule.Employee_RefID;
                        leaveRequest.Tenant_RefID                = securityTicket.TenantID;
                        leaveRequest.Creation_Timestamp          = DateTime.Now;
                        leaveRequest.LeaveRequestCreationSource  = "plannico.time";
                        leaveRequest.Save(Connection, Transaction);


                        P_L5DWS_GDWSWDFP_1225 par = new P_L5DWS_GDWSWDFP_1225();
                        par.StartDate = thirdRange.Start;
                        par.EndDate   = thirdRange.End;
                        var schedulesWithDetails = cls_Get_DailyWorkSchedules_WithDetails_For_Period.Invoke(Connection, Transaction, par, securityTicket).Result.DailyWorkSchedulesWithDetails;

                        foreach (var schedule in schedulesWithDetails)
                        {
                            if (schedule.Details.Any(i => i.LeaveRequest_RefID == oldLeaveRequestID))
                            {
                                foreach (var detailItem in schedule.Details.Where(i => i.LeaveRequest_RefID == oldLeaveRequestID).ToArray())
                                {
                                    ORM_CMN_STR_PPS_DailyWorkSchedule_Detail detail = new ORM_CMN_STR_PPS_DailyWorkSchedule_Detail();
                                    detail.Load(Connection, Transaction, detailItem.CMN_STR_PPS_DailyWorkSchedule_DetailID);
                                    detail.CMN_BPT_EMP_Employee_LeaveRequest_RefID = leaveRequest.CMN_BPT_EMP_Employee_LeaveRequestID;
                                    detail.Save(Connection, Transaction);
                                }
                            }
                        }
                    }

                    #endregion
                }
                else
                {
                    leaveRequestEvent.Remove(Connection, Transaction);
                    leaveRequest.Remove(Connection, Transaction);

                    var approvalItem = new ORM_CMN_CAL_Event_Approval();
                    if (leaveRequest.CMN_CAL_Event_Approval_RefID != Guid.Empty)
                    {
                        var approvalResult = approvalItem.Load(Connection, Transaction, leaveRequest.CMN_CAL_Event_Approval_RefID);
                        if (approvalResult.Status != FR_Status.Success || approvalItem.CMN_CAL_Event_ApprovalID == Guid.Empty)
                        {
                            var error = new FR_Guid();
                            error.ErrorMessage = "No Such ID";
                            error.Status       = FR_Status.Error_Internal;
                            return(error);
                        }
                    }

                    bool isApprovedBefore = approvalItem.IsApproved;

                    P_L5EM_GEATFSbRTFE_1423 statParam = new P_L5EM_GEATFSbRTFE_1423();
                    statParam.absenceReasonID = leaveRequest.CMN_BPT_STA_AbsenceReason_RefID;
                    statParam.employeeID      = leaveRequest.RequestedFor_Employee_RefID;
                    statParam.timeFrameID     = timeFrame.CMN_CAL_CalculationTimeframeID;
                    var statistics = cls_Get_Employee_AbsenceReason_TimeframeStatistic_byReasonTimeFrameEmployee.Invoke(Connection, Transaction, statParam, securityTicket).Result;
                    if (statistics != null)
                    {
                        P_L5EM_SEARTFS_1356 updateStatisticsParam = new P_L5EM_SEARTFS_1356();
                        updateStatisticsParam.CMN_BPT_EMP_Employee_AbsenceReason_TimeframeStatisticsID = statistics.CMN_BPT_EMP_Employee_AbsenceReason_TimeframeStatisticsID;
                        updateStatisticsParam.Employee_RefID             = statistics.Employee_RefID;
                        updateStatisticsParam.CalculationTimeframe_RefID = statistics.CalculationTimeframe_RefID;
                        updateStatisticsParam.AbsenceReason_RefID        = statistics.AbsenceReason_RefID;

                        if (isApprovedBefore)
                        {
                            updateStatisticsParam.R_RequestReservedAbsence_InDays  = statistics.R_RequestReservedAbsence_InDays;
                            updateStatisticsParam.R_RequestReservedAbsence_InHours = statistics.R_RequestReservedAbsence_InHours;

                            updateStatisticsParam.R_AbsenceTimeUsed_InDays  = statistics.R_AbsenceTimeUsed_InDays - Parameter.durationInDays;
                            updateStatisticsParam.R_AbsenceTimeUsed_InHours = statistics.R_AbsenceTimeUsed_InHours - Parameter.durationInHours;

                            updateStatisticsParam.R_TotalAllowedAbsenceTime_InDays  = statistics.R_TotalAllowedAbsenceTime_InDays + Parameter.durationInDays;
                            updateStatisticsParam.R_TotalAllowedAbsenceTime_InHours = statistics.R_TotalAllowedAbsenceTime_InHours + Parameter.durationInHours;
                        }
                        else
                        {
                            updateStatisticsParam.R_RequestReservedAbsence_InDays  = statistics.R_RequestReservedAbsence_InDays - Parameter.durationInDays;
                            updateStatisticsParam.R_RequestReservedAbsence_InHours = statistics.R_RequestReservedAbsence_InHours - Parameter.durationInHours;

                            updateStatisticsParam.R_AbsenceTimeUsed_InDays  = statistics.R_AbsenceTimeUsed_InDays;
                            updateStatisticsParam.R_AbsenceTimeUsed_InHours = statistics.R_AbsenceTimeUsed_InHours;

                            updateStatisticsParam.R_TotalAllowedAbsenceTime_InDays  = statistics.R_TotalAllowedAbsenceTime_InDays;
                            updateStatisticsParam.R_TotalAllowedAbsenceTime_InHours = statistics.R_TotalAllowedAbsenceTime_InHours;
                        }

                        var res = cls_Save_Employee_AbsenceReason_TimeframeStatistic.Invoke(Connection, Transaction, updateStatisticsParam, securityTicket);
                    }
                }
            }

            P_L5DWS_DDWSDFIDL_1014 param = new P_L5DWS_DDWSDFIDL_1014();
            param.DailyWorkSchedule_DetailID = Parameter.DailyWorkSchedule_DetailID;
            cls_Delete_Employee_DailyWorkSchedule_Detail.Invoke(Connection, Transaction, param, securityTicket);

            //Put your code here
            return(returnValue);

            #endregion UserCode
        }
Exemplo n.º 2
0
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L5DWS_SDWSD_1130 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

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

                scheduleDetail.AbsenceReason_RefID = Parameter.AbsenceReason_RefID;
                scheduleDetail.CMN_BPT_EMP_Employee_LeaveRequest_RefID = Parameter.CMN_BPT_EMP_Employee_LeaveRequest_RefID;
                scheduleDetail.CMN_CAL_Event_RefID       = Parameter.CMN_CAL_Event_RefID;
                scheduleDetail.DailyWorkSchedule_RefID   = Parameter.DailyWorkSchedule_RefID;
                scheduleDetail.IsWorkBreak               = Parameter.IsWorkBreak;
                scheduleDetail.SheduleForWorkplace_RefID = Parameter.SheduleForWorkplace_RefID;
                scheduleDetail.Tenant_RefID              = securityTicket.TenantID;
                scheduleDetail.Save(Connection, Transaction);

                ORM_CMN_CAL_Event.Query calEventQuery = new ORM_CMN_CAL_Event.Query();
                calEventQuery.CMN_CAL_EventID = Parameter.CMN_CAL_Event_RefID;
                calEventQuery.IsDeleted       = false;
                calEventQuery.Tenant_RefID    = securityTicket.TenantID;

                ORM_CMN_CAL_Event calEvent = ORM_CMN_CAL_Event.Query.Search(Connection, Transaction, calEventQuery).FirstOrDefault();

                calEvent.StartTime           = Parameter.WorkTime_Start;
                calEvent.EndTime             = Parameter.WorkTime_End;
                calEvent.R_EventDuration_sec = (int)Parameter.WorkTime_End.Subtract(Parameter.WorkTime_Start).TotalSeconds;
                calEvent.Save(Connection, Transaction);
            }
            else
            {
                scheduleDetail.AbsenceReason_RefID = Parameter.AbsenceReason_RefID;
                scheduleDetail.CMN_BPT_EMP_Employee_LeaveRequest_RefID = Parameter.CMN_BPT_EMP_Employee_LeaveRequest_RefID;
                scheduleDetail.CMN_CAL_Event_RefID       = Parameter.CMN_CAL_Event_RefID;
                scheduleDetail.DailyWorkSchedule_RefID   = Parameter.DailyWorkSchedule_RefID;
                scheduleDetail.IsWorkBreak               = Parameter.IsWorkBreak;
                scheduleDetail.SheduleForWorkplace_RefID = Parameter.SheduleForWorkplace_RefID;
                scheduleDetail.Tenant_RefID              = securityTicket.TenantID;


                ORM_CMN_CAL_Event calEvent = new ORM_CMN_CAL_Event();
                calEvent.StartTime           = Parameter.WorkTime_Start;
                calEvent.EndTime             = Parameter.WorkTime_End;
                calEvent.R_EventDuration_sec = (int)(Parameter.WorkTime_End - Parameter.WorkTime_Start).TotalSeconds;
                calEvent.Tenant_RefID        = securityTicket.TenantID;
                calEvent.Save(Connection, Transaction);

                scheduleDetail.CMN_CAL_Event_RefID = calEvent.CMN_CAL_EventID;
                scheduleDetail.Save(Connection, Transaction);
            }
            returnValue.Result = scheduleDetail.CMN_STR_PPS_DailyWorkSchedule_DetailID;

            return(returnValue);

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

            ORM_CMN_STR_PPS_DailyWorkSchedule.Query scheduleQuery = new ORM_CMN_STR_PPS_DailyWorkSchedule.Query();
            scheduleQuery.Employee_RefID  = Parameter.EmployeeID;
            scheduleQuery.WorkSheduleDate = Parameter.WorkscheduleDate;
            scheduleQuery.Tenant_RefID    = securityTicket.TenantID;
            scheduleQuery.IsDeleted       = false;
            List <ORM_CMN_STR_PPS_DailyWorkSchedule> schedules = ORM_CMN_STR_PPS_DailyWorkSchedule.Query.Search(Connection, Transaction, scheduleQuery);
            if (schedules.Count == 1)
            {
                ORM_CMN_STR_PPS_DailyWorkSchedule schedule = schedules[0];
                if (Parameter.SheduleBreakTemplate_RefID != Guid.Empty)
                {
                    ORM_CMN_PPS_BreakTime_Template breakTemplate = new ORM_CMN_PPS_BreakTime_Template();
                    var result = breakTemplate.Load(Connection, Transaction, Parameter.SheduleBreakTemplate_RefID);
                    if (result.Status != FR_Status.Success || breakTemplate.CMN_PPS_BreakTime_TemplateID == Guid.Empty)
                    {
                        var error = new FR_Guid();
                        error.ErrorMessage = "No Such ID.";
                        error.Status       = FR_Status.Error_Internal;
                        return(error);
                    }

                    int duration = 0;
                    ORM_CMN_PPS_BreakTime_Template_Assignment.Query breakeTimeAssigmentQuery = new ORM_CMN_PPS_BreakTime_Template_Assignment.Query();
                    breakeTimeAssigmentQuery.IsDeleted                = false;
                    breakeTimeAssigmentQuery.Tenant_RefID             = securityTicket.TenantID;
                    breakeTimeAssigmentQuery.BreakTime_Template_RefID = breakTemplate.CMN_PPS_BreakTime_TemplateID;
                    List <ORM_CMN_PPS_BreakTime_Template_Assignment> breakTimeAssignemnts = ORM_CMN_PPS_BreakTime_Template_Assignment.Query.Search(Connection, Transaction, breakeTimeAssigmentQuery);
                    foreach (var assignment in breakTimeAssignemnts)
                    {
                        ORM_CMN_PPS_BreakTime breakeTime = new ORM_CMN_PPS_BreakTime();
                        breakeTime.Load(Connection, Transaction, assignment.BreakTime_RefID);
                        duration += breakeTime.Default_Duration_in_sec;
                    }
                    schedule.SheduleBreakTemplate_RefID = breakTemplate.CMN_PPS_BreakTime_TemplateID;
                    schedule.BreakDurationTime_in_sec   = duration;
                    schedule.Save(Connection, Transaction);
                }
                else
                {
                    P_L5DWS_GDWSDFDWSID_1156 par = new P_L5DWS_GDWSDFDWSID_1156();
                    par.DailyWorkScheduleID = schedule.CMN_STR_PPS_DailyWorkScheduleID;
                    List <L5DWS_GDWSDFDWSID_1156> details = cls_Get_DailyWorkSchedule_Detail_For_DailyWorkScheduleID.Invoke(Connection, Transaction, par, securityTicket).Result.ToList();
                    foreach (var detail in details)
                    {
                        ORM_CMN_STR_PPS_DailyWorkSchedule_Detail scheduleDetail = new ORM_CMN_STR_PPS_DailyWorkSchedule_Detail();
                        var result = scheduleDetail.Load(Connection, Transaction, detail.CMN_STR_PPS_DailyWorkSchedule_DetailID);
                        if (result.Status != FR_Status.Success || scheduleDetail.CMN_STR_PPS_DailyWorkSchedule_DetailID == Guid.Empty)
                        {
                            var error = new FR_Guid();
                            error.ErrorMessage = "No Such ID.";
                            error.Status       = FR_Status.Error_Internal;
                            return(error);
                        }
                        scheduleDetail.SheduleForWorkplace_RefID = Parameter.WorkplaceID;
                        scheduleDetail.Save(Connection, Transaction);
                    }
                }
            }


            //Put your code here
            return(returnValue);

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


            LeaveRequestUtils leaveRequestUtil = new LeaveRequestUtils();

            ORM_CMN_BPT_EMP_EffectiveWorkTime_Position effectivePosition = new ORM_CMN_BPT_EMP_EffectiveWorkTime_Position();
            if (Parameter.CMN_BPT_EMP_EffectiveWorkTime_PositionID != Guid.Empty)
            {
                var result = effectivePosition.Load(Connection, Transaction, Parameter.CMN_BPT_EMP_EffectiveWorkTime_PositionID);
                if (result.Status != FR_Status.Success || effectivePosition.CMN_BPT_EMP_EffectiveWorkTime_PositionID == Guid.Empty)
                {
                    var error = new FR_Guid();
                    error.ErrorMessage = "No Such ID.";
                    error.Status       = FR_Status.Error_Internal;
                    return(error);
                }
                effectivePosition.EffectiveWorkTime_Header_RefID          = Parameter.EffectiveWorkTime_Header_RefID;
                effectivePosition.CMN_BPT_EMP_Employe_RefID               = Parameter.Employee_RefID;
                effectivePosition.CMN_BPT_EMP_Employee_LeaveRequest_RefID = Parameter.CMN_BPT_EMP_Employee_LeaveRequest_RefID;
                effectivePosition.Workplace_RefID          = Parameter.Workplace_RefID;
                effectivePosition.Tenant_RefID             = securityTicket.TenantID;
                effectivePosition.WorkTime_StartTime       = Parameter.WorkTime_Start;
                effectivePosition.WorkTime_Duration_in_sec = (int)Parameter.WorkTime_End.Subtract(Parameter.WorkTime_Start).TotalSeconds;
                effectivePosition.SourceOfEntry            = Parameter.SourceOfEntry;
                effectivePosition.Save(Connection, Transaction);

                if (Parameter.AbsenceReason_RefID != Guid.Empty)
                {
                    P_L5DWS_GDWSWDFPAE_1331 par = new P_L5DWS_GDWSWDFPAE_1331();
                    par.EmployeeID      = Parameter.Employee_RefID;
                    par.PeriodStartDate = Parameter.WorkTime_Start.Date;
                    par.PeriodEndDate   = Parameter.WorkTime_Start.Date;
                    var planningData = cls_Get_DailyWorkSchedules_WithDetails_For_Period_And_EmployeeID.Invoke(Connection, Transaction, par, securityTicket).Result;
                    if (planningData.DailyWorkSchedulesWithDetails.Length != 0)
                    {
                        var dailyWorkSchedule = planningData.DailyWorkSchedulesWithDetails[0];
                        foreach (var detail in dailyWorkSchedule.Details)
                        {
                            if (detail.LeaveRequest_RefID != Guid.Empty)
                            {
                                ORM_CMN_BPT_EMP_Employee_LeaveRequest leaveRequest = new ORM_CMN_BPT_EMP_Employee_LeaveRequest();
                                leaveRequest.Load(Connection, Transaction, detail.LeaveRequest_RefID);
                                leaveRequest.Remove(Connection, Transaction);

                                ORM_CMN_CAL_Event leaveRequestEvent = new ORM_CMN_CAL_Event();
                                leaveRequestEvent.Load(Connection, Transaction, leaveRequest.CMN_CAL_Event_RefID);
                                leaveRequestEvent.Remove(Connection, Transaction);

                                var calQuery = new ORM_CMN_CAL_Event.Query();
                                calQuery.CMN_CAL_EventID = leaveRequest.CMN_CAL_Event_RefID;
                                var calendarRes = ORM_CMN_CAL_Event.Query.Search(Connection, Transaction, calQuery);
                                ORM_CMN_CAL_Event calendarEvent = calendarRes[0];

                                P_L5TN_GCTFFTAY_1320 timeFrameParam = new P_L5TN_GCTFFTAY_1320();
                                timeFrameParam.Year = leaveRequestEvent.StartTime.Year;
                                var timeFrame = cls_Get_CalculationTimeFramesForTenant_And_Year.Invoke(Connection, Transaction, timeFrameParam, securityTicket).Result.CalculationTimeFrame;

                                var approvalItem = new ORM_CMN_CAL_Event_Approval();
                                if (leaveRequest.CMN_CAL_Event_Approval_RefID != Guid.Empty)
                                {
                                    var approvalResult = approvalItem.Load(Connection, Transaction, leaveRequest.CMN_CAL_Event_Approval_RefID);
                                    if (approvalResult.Status != FR_Status.Success || approvalItem.CMN_CAL_Event_ApprovalID == Guid.Empty)
                                    {
                                        var error = new FR_Guid();
                                        error.ErrorMessage = "No Such ID";
                                        error.Status       = FR_Status.Error_Internal;
                                        return(error);
                                    }
                                }
                                bool isApprovedBefore = approvalItem.IsApproved;

                                P_L5EM_GEATFSbRTFE_1423 statParam = new P_L5EM_GEATFSbRTFE_1423();
                                statParam.absenceReasonID = leaveRequest.CMN_BPT_STA_AbsenceReason_RefID;
                                statParam.employeeID      = leaveRequest.RequestedFor_Employee_RefID;
                                statParam.timeFrameID     = timeFrame.CMN_CAL_CalculationTimeframeID;
                                var statistics = cls_Get_Employee_AbsenceReason_TimeframeStatistic_byReasonTimeFrameEmployee.Invoke(Connection, Transaction, statParam, securityTicket).Result;
                                if (statistics != null)
                                {
                                    P_L5EM_SEARTFS_1356 updateStatisticsParam = new P_L5EM_SEARTFS_1356();
                                    updateStatisticsParam.CMN_BPT_EMP_Employee_AbsenceReason_TimeframeStatisticsID = statistics.CMN_BPT_EMP_Employee_AbsenceReason_TimeframeStatisticsID;
                                    updateStatisticsParam.Employee_RefID             = statistics.Employee_RefID;
                                    updateStatisticsParam.CalculationTimeframe_RefID = statistics.CalculationTimeframe_RefID;
                                    updateStatisticsParam.AbsenceReason_RefID        = statistics.AbsenceReason_RefID;

                                    updateStatisticsParam.R_AbsenceCarryOver_InDays  = statistics.R_AbsenceCarryOver_InDays;
                                    updateStatisticsParam.R_AbsenceCarryOver_InHours = statistics.R_AbsenceCarryOver_InHours;

                                    var durationInDays  = leaveRequestUtil.LeaveRequestDuration(leaveRequestEvent.StartTime, leaveRequestEvent.EndTime, Parameter.Events, Parameter.Employees.FirstOrDefault(x => x.CMN_BPT_EMP_EmployeeID == leaveRequest.RequestedFor_Employee_RefID), true);
                                    var durationInHours = leaveRequestUtil.LeaveRequestDuration(leaveRequestEvent.StartTime, leaveRequestEvent.EndTime, Parameter.Events, Parameter.Employees.FirstOrDefault(x => x.CMN_BPT_EMP_EmployeeID == leaveRequest.RequestedFor_Employee_RefID), false);


                                    if (!isApprovedBefore)
                                    {
                                        updateStatisticsParam.R_RequestReservedAbsence_InDays  = statistics.R_RequestReservedAbsence_InDays - durationInDays;
                                        updateStatisticsParam.R_RequestReservedAbsence_InHours = statistics.R_RequestReservedAbsence_InHours - durationInHours;

                                        updateStatisticsParam.R_AbsenceTimeUsed_InDays  = statistics.R_AbsenceTimeUsed_InDays;
                                        updateStatisticsParam.R_AbsenceTimeUsed_InHours = statistics.R_AbsenceTimeUsed_InHours;

                                        updateStatisticsParam.R_TotalAllowedAbsenceTime_InDays  = statistics.R_TotalAllowedAbsenceTime_InDays;
                                        updateStatisticsParam.R_TotalAllowedAbsenceTime_InHours = statistics.R_TotalAllowedAbsenceTime_InHours;
                                    }

                                    var res = cls_Save_Employee_AbsenceReason_TimeframeStatistic.Invoke(Connection, Transaction, updateStatisticsParam, securityTicket);
                                }

                                ORM_CMN_STR_PPS_DailyWorkSchedule_Detail scheduleDetail = new ORM_CMN_STR_PPS_DailyWorkSchedule_Detail();
                                result = scheduleDetail.Load(Connection, Transaction, detail.CMN_STR_PPS_DailyWorkSchedule_DetailID);
                                if (result.Status != FR_Status.Success || scheduleDetail.CMN_STR_PPS_DailyWorkSchedule_DetailID == Guid.Empty)
                                {
                                    var error = new FR_Guid();
                                    error.ErrorMessage = "No Such ID.";
                                    error.Status       = FR_Status.Error_Internal;
                                    return(error);
                                }

                                scheduleDetail.CMN_BPT_EMP_Employee_LeaveRequest_RefID = Guid.Empty;
                                scheduleDetail.Save(Connection, Transaction);
                            }
                        }
                    }

                    if (effectivePosition.CMN_BPT_EMP_Employee_LeaveRequest_RefID == Guid.Empty)
                    {
                        ORM_CMN_CAL_Event leaveRequestEvent = new ORM_CMN_CAL_Event();
                        leaveRequestEvent.StartTime                = Parameter.WorkTime_Start;
                        leaveRequestEvent.EndTime                  = Parameter.WorkTime_End;
                        leaveRequestEvent.R_EventDuration_sec      = (int)Parameter.WorkTime_End.Subtract(Parameter.WorkTime_Start).TotalSeconds;
                        leaveRequestEvent.IsRepetitive             = false;
                        leaveRequestEvent.IsWholeDayEvent          = false;
                        leaveRequestEvent.IsCalendarEvent_Editable = true;
                        leaveRequestEvent.Tenant_RefID             = securityTicket.TenantID;
                        leaveRequestEvent.Save(Connection, Transaction);

                        var approvalItem = new ORM_CMN_CAL_Event_Approval();
                        approvalItem.Event_RefID             = leaveRequestEvent.CMN_CAL_EventID;
                        approvalItem.IsApprovalProcessDenied = false;
                        approvalItem.IsApprovalProcessOpened = false;
                        approvalItem.IsApproved = true;
                        approvalItem.IsApprovalProcessCanceledByUser = false;
                        approvalItem.IsDeleted          = false;
                        approvalItem.Creation_Timestamp = DateTime.Now;
                        approvalItem.Tenant_RefID       = securityTicket.TenantID;
                        approvalItem.Save(Connection, Transaction);



                        var requestItem = new ORM_CMN_BPT_EMP_Employee_LeaveRequest();
                        requestItem.CMN_BPT_STA_AbsenceReason_RefID = Parameter.AbsenceReason_RefID;
                        requestItem.CMN_CAL_Event_Approval_RefID    = approvalItem.CMN_CAL_Event_ApprovalID;
                        requestItem.CMN_CAL_Event_RefID             = leaveRequestEvent.CMN_CAL_EventID;
                        requestItem.IsDeleted = false;
                        requestItem.RequestedBy_Employee_RefID  = Parameter.Employee_RefID;
                        requestItem.RequestedFor_Employee_RefID = Parameter.Employee_RefID;
                        requestItem.Tenant_RefID               = securityTicket.TenantID;
                        requestItem.Creation_Timestamp         = DateTime.Now;
                        requestItem.LeaveRequestCreationSource = "plannico.time";
                        requestItem.Save(Connection, Transaction);
                        effectivePosition.CMN_BPT_EMP_Employee_LeaveRequest_RefID = requestItem.CMN_BPT_EMP_Employee_LeaveRequestID;
                        effectivePosition.Save(Connection, Transaction);

                        P_L5TN_GCTFFTAY_1320 timeFrameParam = new P_L5TN_GCTFFTAY_1320();
                        timeFrameParam.Year = leaveRequestEvent.StartTime.Year;
                        var timeFrame = cls_Get_CalculationTimeFramesForTenant_And_Year.Invoke(Connection, Transaction, timeFrameParam, securityTicket).Result.CalculationTimeFrame;

                        P_L5EM_GEATFSbRTFE_1423 statParam = new P_L5EM_GEATFSbRTFE_1423();
                        statParam.absenceReasonID = requestItem.CMN_BPT_STA_AbsenceReason_RefID;
                        statParam.employeeID      = requestItem.RequestedFor_Employee_RefID;
                        statParam.timeFrameID     = timeFrame.CMN_CAL_CalculationTimeframeID;
                        var statistics = cls_Get_Employee_AbsenceReason_TimeframeStatistic_byReasonTimeFrameEmployee.Invoke(Connection, Transaction, statParam, securityTicket).Result;
                        if (statistics != null)
                        {
                            P_L5EM_SEARTFS_1356 updateStatisticsParam = new P_L5EM_SEARTFS_1356();
                            updateStatisticsParam.CMN_BPT_EMP_Employee_AbsenceReason_TimeframeStatisticsID = statistics.CMN_BPT_EMP_Employee_AbsenceReason_TimeframeStatisticsID;
                            updateStatisticsParam.Employee_RefID             = statistics.Employee_RefID;
                            updateStatisticsParam.CalculationTimeframe_RefID = statistics.CalculationTimeframe_RefID;
                            updateStatisticsParam.AbsenceReason_RefID        = statistics.AbsenceReason_RefID;

                            updateStatisticsParam.R_RequestReservedAbsence_InDays  = statistics.R_RequestReservedAbsence_InDays;
                            updateStatisticsParam.R_RequestReservedAbsence_InHours = statistics.R_RequestReservedAbsence_InHours;

                            updateStatisticsParam.R_AbsenceCarryOver_InDays  = statistics.R_AbsenceCarryOver_InDays;
                            updateStatisticsParam.R_AbsenceCarryOver_InHours = statistics.R_AbsenceCarryOver_InHours;

                            updateStatisticsParam.R_TotalAllowedAbsenceTime_InDays  = statistics.R_TotalAllowedAbsenceTime_InDays - Parameter.durationInDays;
                            updateStatisticsParam.R_TotalAllowedAbsenceTime_InHours = statistics.R_TotalAllowedAbsenceTime_InHours - Parameter.durationInHours;


                            updateStatisticsParam.R_AbsenceTimeUsed_InDays  = statistics.R_AbsenceTimeUsed_InDays + Parameter.durationInDays;
                            updateStatisticsParam.R_AbsenceTimeUsed_InHours = statistics.R_AbsenceTimeUsed_InHours + Parameter.durationInHours;

                            var res = cls_Save_Employee_AbsenceReason_TimeframeStatistic.Invoke(Connection, Transaction, updateStatisticsParam, securityTicket);
                        }
                    }
                    else
                    {
                        ORM_CMN_BPT_EMP_Employee_LeaveRequest leaveRequest = new ORM_CMN_BPT_EMP_Employee_LeaveRequest();
                        leaveRequest.Load(Connection, Transaction, effectivePosition.CMN_BPT_EMP_Employee_LeaveRequest_RefID);

                        ORM_CMN_CAL_Event leaveRequestEvent = new ORM_CMN_CAL_Event();
                        leaveRequestEvent.Load(Connection, Transaction, leaveRequest.CMN_CAL_Event_RefID);
                        if (leaveRequest.CMN_BPT_STA_AbsenceReason_RefID != Parameter.AbsenceReason_RefID || Parameter.OldDurationInHours != Parameter.durationInHours || Parameter.OldDurationInDays != Parameter.durationInDays)
                        {
                            var calQuery = new ORM_CMN_CAL_Event.Query();
                            calQuery.CMN_CAL_EventID = leaveRequest.CMN_CAL_Event_RefID;
                            var calendarRes = ORM_CMN_CAL_Event.Query.Search(Connection, Transaction, calQuery);
                            ORM_CMN_CAL_Event calendarEvent = calendarRes[0];

                            P_L5TN_GCTFFTAY_1320 timeFrameParam = new P_L5TN_GCTFFTAY_1320();
                            timeFrameParam.Year = leaveRequestEvent.StartTime.Year;
                            var timeFrame = cls_Get_CalculationTimeFramesForTenant_And_Year.Invoke(Connection, Transaction, timeFrameParam, securityTicket).Result.CalculationTimeFrame;

                            P_L5EM_GEATFSbRTFE_1423 statParam = new P_L5EM_GEATFSbRTFE_1423();
                            statParam.absenceReasonID = leaveRequest.CMN_BPT_STA_AbsenceReason_RefID;
                            statParam.employeeID      = leaveRequest.RequestedFor_Employee_RefID;
                            statParam.timeFrameID     = timeFrame.CMN_CAL_CalculationTimeframeID;
                            var statistics = cls_Get_Employee_AbsenceReason_TimeframeStatistic_byReasonTimeFrameEmployee.Invoke(Connection, Transaction, statParam, securityTicket).Result;
                            if (statistics != null)
                            {
                                P_L5EM_SEARTFS_1356 updateStatisticsParam = new P_L5EM_SEARTFS_1356();
                                updateStatisticsParam.CMN_BPT_EMP_Employee_AbsenceReason_TimeframeStatisticsID = statistics.CMN_BPT_EMP_Employee_AbsenceReason_TimeframeStatisticsID;
                                updateStatisticsParam.Employee_RefID             = statistics.Employee_RefID;
                                updateStatisticsParam.CalculationTimeframe_RefID = statistics.CalculationTimeframe_RefID;
                                updateStatisticsParam.AbsenceReason_RefID        = statistics.AbsenceReason_RefID;

                                updateStatisticsParam.R_AbsenceCarryOver_InDays  = statistics.R_AbsenceCarryOver_InDays;
                                updateStatisticsParam.R_AbsenceCarryOver_InHours = statistics.R_AbsenceCarryOver_InHours;

                                updateStatisticsParam.R_TotalAllowedAbsenceTime_InDays  = statistics.R_TotalAllowedAbsenceTime_InDays + Parameter.OldDurationInDays - Parameter.durationInDays;
                                updateStatisticsParam.R_TotalAllowedAbsenceTime_InHours = statistics.R_TotalAllowedAbsenceTime_InHours + Parameter.OldDurationInHours - Parameter.durationInHours;

                                updateStatisticsParam.R_RequestReservedAbsence_InDays  = statistics.R_RequestReservedAbsence_InDays;
                                updateStatisticsParam.R_RequestReservedAbsence_InHours = statistics.R_RequestReservedAbsence_InHours;

                                updateStatisticsParam.R_AbsenceTimeUsed_InDays  = statistics.R_AbsenceTimeUsed_InDays - Parameter.OldDurationInDays + Parameter.durationInDays;
                                updateStatisticsParam.R_AbsenceTimeUsed_InHours = statistics.R_AbsenceTimeUsed_InHours - Parameter.OldDurationInHours + Parameter.durationInHours;

                                var res = cls_Save_Employee_AbsenceReason_TimeframeStatistic.Invoke(Connection, Transaction, updateStatisticsParam, securityTicket);
                            }
                            leaveRequest.CMN_BPT_STA_AbsenceReason_RefID = Parameter.AbsenceReason_RefID;
                            leaveRequest.Save(Connection, Transaction);
                        }


                        leaveRequestEvent.StartTime           = Parameter.WorkTime_Start;
                        leaveRequestEvent.EndTime             = Parameter.WorkTime_End;
                        leaveRequestEvent.R_EventDuration_sec = (int)Parameter.WorkTime_End.Subtract(Parameter.WorkTime_Start).TotalSeconds;
                        leaveRequestEvent.Save(Connection, Transaction);
                    }
                }
                else
                {
                    if (effectivePosition.CMN_BPT_EMP_Employee_LeaveRequest_RefID != Guid.Empty)
                    {
                        ORM_CMN_BPT_EMP_Employee_LeaveRequest leaveRequest = new ORM_CMN_BPT_EMP_Employee_LeaveRequest();
                        leaveRequest.Load(Connection, Transaction, effectivePosition.CMN_BPT_EMP_Employee_LeaveRequest_RefID);
                        leaveRequest.Remove(Connection, Transaction);

                        ORM_CMN_CAL_Event_Approval.Query calEventApprovalQuery = new ORM_CMN_CAL_Event_Approval.Query();
                        calEventApprovalQuery.Event_RefID  = leaveRequest.CMN_CAL_Event_RefID;
                        calEventApprovalQuery.IsDeleted    = false;
                        calEventApprovalQuery.Tenant_RefID = securityTicket.TenantID;
                        ORM_CMN_CAL_Event_Approval calEventApproval = ORM_CMN_CAL_Event_Approval.Query.Search(Connection, Transaction, calEventApprovalQuery).FirstOrDefault();
                        calEventApproval.Remove(Connection, Transaction);

                        ORM_CMN_CAL_Event leaveRequestEvent = new ORM_CMN_CAL_Event();
                        leaveRequestEvent.Load(Connection, Transaction, leaveRequest.CMN_CAL_Event_RefID);
                        leaveRequestEvent.Remove(Connection, Transaction);
                        effectivePosition.CMN_BPT_EMP_Employee_LeaveRequest_RefID = Guid.Empty;
                        effectivePosition.Save(Connection, Transaction);

                        P_L5TN_GCTFFTAY_1320 timeFrameParam = new P_L5TN_GCTFFTAY_1320();
                        timeFrameParam.Year = leaveRequestEvent.StartTime.Year;
                        var timeFrame = cls_Get_CalculationTimeFramesForTenant_And_Year.Invoke(Connection, Transaction, timeFrameParam, securityTicket).Result.CalculationTimeFrame;


                        P_L5EM_GEATFSbRTFE_1423 statParam = new P_L5EM_GEATFSbRTFE_1423();
                        statParam.absenceReasonID = leaveRequest.CMN_BPT_STA_AbsenceReason_RefID;
                        statParam.employeeID      = leaveRequest.RequestedFor_Employee_RefID;
                        statParam.timeFrameID     = timeFrame.CMN_CAL_CalculationTimeframeID;
                        var statistics = cls_Get_Employee_AbsenceReason_TimeframeStatistic_byReasonTimeFrameEmployee.Invoke(Connection, Transaction, statParam, securityTicket).Result;
                        if (statistics != null)
                        {
                            P_L5EM_SEARTFS_1356 updateStatisticsParam = new P_L5EM_SEARTFS_1356();
                            updateStatisticsParam.CMN_BPT_EMP_Employee_AbsenceReason_TimeframeStatisticsID = statistics.CMN_BPT_EMP_Employee_AbsenceReason_TimeframeStatisticsID;
                            updateStatisticsParam.Employee_RefID             = statistics.Employee_RefID;
                            updateStatisticsParam.CalculationTimeframe_RefID = statistics.CalculationTimeframe_RefID;
                            updateStatisticsParam.AbsenceReason_RefID        = statistics.AbsenceReason_RefID;

                            updateStatisticsParam.R_AbsenceCarryOver_InDays  = statistics.R_AbsenceCarryOver_InDays;
                            updateStatisticsParam.R_AbsenceCarryOver_InHours = statistics.R_AbsenceCarryOver_InHours;

                            updateStatisticsParam.R_TotalAllowedAbsenceTime_InDays  = statistics.R_TotalAllowedAbsenceTime_InDays + Parameter.OldDurationInDays;
                            updateStatisticsParam.R_TotalAllowedAbsenceTime_InHours = statistics.R_TotalAllowedAbsenceTime_InHours + Parameter.OldDurationInHours;

                            updateStatisticsParam.R_RequestReservedAbsence_InDays  = statistics.R_RequestReservedAbsence_InDays;
                            updateStatisticsParam.R_RequestReservedAbsence_InHours = statistics.R_RequestReservedAbsence_InHours;

                            updateStatisticsParam.R_AbsenceTimeUsed_InDays  = statistics.R_AbsenceTimeUsed_InDays - Parameter.OldDurationInDays;
                            updateStatisticsParam.R_AbsenceTimeUsed_InHours = statistics.R_AbsenceTimeUsed_InHours - Parameter.OldDurationInHours;

                            var res = cls_Save_Employee_AbsenceReason_TimeframeStatistic.Invoke(Connection, Transaction, updateStatisticsParam, securityTicket);
                        }
                    }
                }
            }
            else
            {
                effectivePosition.EffectiveWorkTime_Header_RefID          = Parameter.EffectiveWorkTime_Header_RefID;
                effectivePosition.CMN_BPT_EMP_Employe_RefID               = Parameter.Employee_RefID;
                effectivePosition.CMN_BPT_EMP_Employee_LeaveRequest_RefID = Parameter.CMN_BPT_EMP_Employee_LeaveRequest_RefID;
                effectivePosition.Workplace_RefID          = Parameter.Workplace_RefID;
                effectivePosition.Tenant_RefID             = securityTicket.TenantID;
                effectivePosition.WorkTime_StartTime       = Parameter.WorkTime_Start;
                effectivePosition.WorkTime_Duration_in_sec = (int)Parameter.WorkTime_End.Subtract(Parameter.WorkTime_Start).TotalSeconds;
                effectivePosition.SourceOfEntry            = Parameter.SourceOfEntry;
                effectivePosition.Save(Connection, Transaction);

                if (Parameter.AbsenceReason_RefID != Guid.Empty)
                {
                    ORM_CMN_CAL_Event leaveRequestEvent = new ORM_CMN_CAL_Event();
                    leaveRequestEvent.StartTime                = Parameter.WorkTime_Start;
                    leaveRequestEvent.EndTime                  = Parameter.WorkTime_End;
                    leaveRequestEvent.R_EventDuration_sec      = (int)Parameter.WorkTime_End.Subtract(Parameter.WorkTime_Start).TotalSeconds;
                    leaveRequestEvent.IsRepetitive             = false;
                    leaveRequestEvent.IsWholeDayEvent          = false;
                    leaveRequestEvent.IsCalendarEvent_Editable = true;
                    leaveRequestEvent.Tenant_RefID             = securityTicket.TenantID;
                    leaveRequestEvent.Save(Connection, Transaction);

                    var approvalItem = new ORM_CMN_CAL_Event_Approval();
                    approvalItem.Event_RefID             = leaveRequestEvent.CMN_CAL_EventID;
                    approvalItem.IsApprovalProcessDenied = false;
                    approvalItem.IsApprovalProcessOpened = false;
                    approvalItem.IsApproved = true;
                    approvalItem.IsApprovalProcessCanceledByUser = false;
                    approvalItem.IsDeleted          = false;
                    approvalItem.Creation_Timestamp = DateTime.Now;
                    approvalItem.Tenant_RefID       = securityTicket.TenantID;
                    approvalItem.Save(Connection, Transaction);

                    var requestItem = new ORM_CMN_BPT_EMP_Employee_LeaveRequest();
                    requestItem.CMN_BPT_STA_AbsenceReason_RefID = Parameter.AbsenceReason_RefID;
                    requestItem.CMN_CAL_Event_Approval_RefID    = approvalItem.CMN_CAL_Event_ApprovalID;
                    requestItem.CMN_CAL_Event_RefID             = leaveRequestEvent.CMN_CAL_EventID;
                    requestItem.IsDeleted = false;
                    requestItem.RequestedBy_Employee_RefID  = Parameter.RequestedBy_Employee_RefID;
                    requestItem.RequestedFor_Employee_RefID = Parameter.RequestedFor_Employee_RefID;
                    requestItem.Tenant_RefID               = securityTicket.TenantID;
                    requestItem.Creation_Timestamp         = DateTime.Now;
                    requestItem.LeaveRequestCreationSource = "plannico.time";
                    requestItem.Save(Connection, Transaction);
                    effectivePosition.CMN_BPT_EMP_Employee_LeaveRequest_RefID = requestItem.CMN_BPT_EMP_Employee_LeaveRequestID;
                    effectivePosition.Save(Connection, Transaction);


                    P_L5TN_GCTFFTAY_1320 timeFrameParam = new P_L5TN_GCTFFTAY_1320();
                    timeFrameParam.Year = leaveRequestEvent.StartTime.Year;
                    var timeFrame = cls_Get_CalculationTimeFramesForTenant_And_Year.Invoke(Connection, Transaction, timeFrameParam, securityTicket).Result.CalculationTimeFrame;

                    P_L5EM_GEATFSbRTFE_1423 statParam = new P_L5EM_GEATFSbRTFE_1423();
                    statParam.absenceReasonID = requestItem.CMN_BPT_STA_AbsenceReason_RefID;
                    statParam.employeeID      = requestItem.RequestedFor_Employee_RefID;
                    statParam.timeFrameID     = timeFrame.CMN_CAL_CalculationTimeframeID;
                    var statistics = cls_Get_Employee_AbsenceReason_TimeframeStatistic_byReasonTimeFrameEmployee.Invoke(Connection, Transaction, statParam, securityTicket).Result;
                    if (statistics != null)
                    {
                        P_L5EM_SEARTFS_1356 updateStatisticsParam = new P_L5EM_SEARTFS_1356();
                        updateStatisticsParam.CMN_BPT_EMP_Employee_AbsenceReason_TimeframeStatisticsID = statistics.CMN_BPT_EMP_Employee_AbsenceReason_TimeframeStatisticsID;
                        updateStatisticsParam.Employee_RefID             = statistics.Employee_RefID;
                        updateStatisticsParam.CalculationTimeframe_RefID = statistics.CalculationTimeframe_RefID;
                        updateStatisticsParam.AbsenceReason_RefID        = statistics.AbsenceReason_RefID;

                        updateStatisticsParam.R_AbsenceCarryOver_InDays  = statistics.R_AbsenceCarryOver_InDays;
                        updateStatisticsParam.R_AbsenceCarryOver_InHours = statistics.R_AbsenceCarryOver_InHours;

                        updateStatisticsParam.R_TotalAllowedAbsenceTime_InDays  = statistics.R_TotalAllowedAbsenceTime_InDays - Parameter.durationInDays;
                        updateStatisticsParam.R_TotalAllowedAbsenceTime_InHours = statistics.R_TotalAllowedAbsenceTime_InHours - Parameter.durationInHours;


                        updateStatisticsParam.R_AbsenceTimeUsed_InDays  = statistics.R_AbsenceTimeUsed_InDays + Parameter.durationInDays;
                        updateStatisticsParam.R_AbsenceTimeUsed_InHours = statistics.R_AbsenceTimeUsed_InHours + Parameter.durationInHours;

                        var res = cls_Save_Employee_AbsenceReason_TimeframeStatistic.Invoke(Connection, Transaction, updateStatisticsParam, securityTicket);
                    }
                }
            }



            returnValue.Result = effectivePosition.CMN_BPT_EMP_EffectiveWorkTime_PositionID;

            return(returnValue);

            #endregion UserCode
        }