public async Task <List <Vacation> > BreakAsync(Vacation vacation, Guid identityWorkID, Guid instanceID, HolidayPaymentConfiguration config) { var initialVacationDate = vacation.InitialDate; var finalVacationDate = vacation.FinalDate; List <Vacation> result = new List <Vacation>(); MiddlewareManager <PeriodType> middlewareManager = new MiddlewareManager <PeriodType>(new BaseRecordManager <PeriodType>(), new PeriodTypeValidator()); MiddlewareManager <Employee> employeeMiddlewareManager = new MiddlewareManager <Employee>(new BaseRecordManager <Employee>(), new EmployeeValidator()); var employee = (await employeeMiddlewareManager.FindByExpressionAsync(p => p.ID == vacation.EmployeeID && p.Active == true && p.InstanceID == instanceID, identityWorkID, new string[] { "PeriodType", "PeriodType.Periods", "PeriodType.Periods.PeriodDetails" })).FirstOrDefault(); var periodTypes = employee.PeriodType; if (periodTypes != null) { var allDetails = periodTypes.Periods.SelectMany(x => x.PeriodDetails).ToList(); var periodDetails = allDetails.Where(p => (initialVacationDate >= p.InitialDate && initialVacationDate <= p.FinalDate) || (finalVacationDate >= p.FinalDate && p.InitialDate >= initialVacationDate) || finalVacationDate >= p.InitialDate && finalVacationDate <= p.FinalDate).OrderBy("InitialDate").ToList(); var totalCards = periodDetails.Count(); if (totalCards == 1) { result.Add(vacation); return(result); } int index = 0; periodDetails.ForEach(periodDetail => { Vacation clonedVacation = (Vacation)vacation.Clone(); clonedVacation.ID = Guid.NewGuid(); if (index == 0) { if (periodDetail.InitialDate != initialVacationDate) { clonedVacation.InitialDate = initialVacationDate; } else { clonedVacation.InitialDate = periodDetail.InitialDate; } clonedVacation.FinalDate = periodDetail.FinalDate; } else if (index == totalCards - 1) { if (periodDetail.FinalDate != finalVacationDate) { clonedVacation.FinalDate = finalVacationDate; } else { clonedVacation.FinalDate = periodDetail.FinalDate; } clonedVacation.InitialDate = periodDetail.InitialDate; } else { clonedVacation.InitialDate = periodDetail.InitialDate; clonedVacation.FinalDate = periodDetail.FinalDate; } index++; result.Add(clonedVacation); SetDaysOffAndVacationDays(clonedVacation, vacation, config); }); return(result); } return(result); }