private IEnumerable <TimeSheetRecord> InsertEmployee(FactStaffWithHistory employee, IEnumerable <Exception> exeptions, IEnumerable <OK_Otpusk> otpuskList) { List <TimeSheetRecord> timeSheetRecordLList; // Генерируем табель if (employee.PlanStaff.Post.Category.id == IdPps) //если категория работника ППС //if (employee.PlanStaff.Post.Category.IsPPS.Value) //если категория работника ППС { //PPS timeSheetRecordLList = PpsTimeSheetGenerate(employee); } else { timeSheetRecordLList = employee.PlanStaff.WorkShedule.id == Week5Days //5 days week ? FiveDayesTimeSheetGenerate(employee) //6 days week : SixDayesTimeSheetGenerate(employee); } // Добавляем дни исключения timeSheetRecordLList = AddExceptoinDaysToTimeSheetRecords(employee, timeSheetRecordLList, exeptions); // Добавляем информацию о отпусе timeSheetRecordLList = AddHolidaysToTimeSheetRecords(employee, timeSheetRecordLList, otpuskList); return(timeSheetRecordLList); }
//Генерация табеля для гибкой недели private List <TimeSheetRecord> FlexibleTimeSheetGenerate(FactStaffWithHistory employee) { var timeSheetRecordLList = new List <TimeSheetRecord>(); for (int i = _timeSheet.DateBeginPeriod.Day - 1; i < _timeSheet.DateEndPeriod.Day; i++) { TimeSheetRecord timeSheetRecord; var date = _timeSheet.DateBeginPeriod.AddDays(i); if ((employee.DateEnd != null && employee.DateEnd < date) || employee.DateBegin > date) { timeSheetRecord = NewTimeSheetRecord(date, employee.idFactStaffHistory, IdX, _timeSheet.id, 0); timeSheetRecordLList.Add(timeSheetRecord); continue; } switch (date.DayOfWeek) { default: { timeSheetRecord = GenerageTimeSheetRecord(employee, date, IdVihodnoy); break; } } timeSheetRecordLList.Add(timeSheetRecord); } return(timeSheetRecordLList); }
//Вносит в табель информацию о отпуске и возвращает обновлённые записи табеля на работника List <TimeSheetRecord> AddHolidaysToTimeSheetRecords(FactStaffWithHistory employee, IEnumerable <TimeSheetRecord> timeSheetRecords, IEnumerable <OK_Otpusk> otpusk) { var timeSheetRecordLList = new List <TimeSheetRecord>(timeSheetRecords); var okOtpusks = otpusk as OK_Otpusk[] ?? otpusk.ToArray(); if (okOtpusks.Any()) { foreach (var okOtpusk in okOtpusks) { int beginDay; if (_timeSheet.DateBeginPeriod.Year == okOtpusk.DateBegin.Value.Year && _timeSheet.DateBeginPeriod.Month == okOtpusk.DateBegin.Value.Month) { beginDay = okOtpusk.DateBegin.Value.Day; } else { beginDay = _timeSheet.DateBeginPeriod.Day; } int endDay; if (okOtpusk.DateEnd.HasValue && _timeSheet.DateEndPeriod.Year == okOtpusk.DateEnd.Value.Year && _timeSheet.DateEndPeriod.Month == okOtpusk.DateEnd.Value.Month) { endDay = okOtpusk.DateEnd.Value.Day; } else { endDay = _timeSheet.DateEndPeriod.Day; } for (int i = beginDay; i <= endDay; i++) { var otpuskDay = new DateTime(_timeSheet.DateBeginPeriod.Year, _timeSheet.DateBeginPeriod.Month, i); var record = timeSheetRecordLList.FirstOrDefault( f => f.RecordDate == otpuskDay && f.idFactStaffHistory == employee.idFactStaffHistory); if (record == null || record.idDayStatus == IdX) { continue; } if (okOtpusk.OK_Otpuskvid.idDayStatus != null) { record.idDayStatus = (int)okOtpusk.OK_Otpuskvid.idDayStatus; record.JobTimeCount = 0; } else { record.idDayStatus = 6; record.JobTimeCount = 0; } } } } return(timeSheetRecordLList); }
//Генерирукет запись в табеле private TimeSheetRecord GenerageTimeSheetRecord(FactStaffWithHistory employee, DateTime date, int idDayStatus, double manFullDay = 0, double manNotFullDay = 0, double womanFullDay = 0, double womanNotFullDay = 0) { if (employee.StaffCount == 1) { return(NewTimeSheetRecord(date, employee.idFactStaffHistory, idDayStatus, _timeSheet.id, employee.Employee.SexBit ? manFullDay : womanFullDay)); } return(NewTimeSheetRecord(date, employee.idFactStaffHistory, idDayStatus, _timeSheet.id, (double)employee.StaffCount * (employee.Employee.SexBit ? manNotFullDay : womanNotFullDay))); }
//Вносит в табель информацию о больничном и возвращает обновлённые записи табеля на работника List <TimeSheetRecord> AddHospitalsToTimeSheetRecords(FactStaffWithHistory employee, IEnumerable <TimeSheetRecord> timeSheetRecords, IEnumerable <OK_Inkapacity> inkapacities , IEnumerable <OK_Otpusk> otpusk) { var OtpuskPoRodams = otpusk.Where(w => w.idOtpuskVid == IdOtpusPoRodam).ToList(); //вытаскиваем отпуск по родам. Опуск может быть взят частями и подряд var timeSheetRecordLList = new List <TimeSheetRecord>(timeSheetRecords); var okInkapacities = inkapacities as OK_Inkapacity[] ?? inkapacities.ToArray(); if (okInkapacities.Any()) { //если имеем отпуск по родам и даты начала и конца отпуска совпадают с больничным то это вовсе не болничный, а Отпуск по беременности и родам (9), больничный в табеле не отображаем, отображаем Отпуск. okInkapacities = okInkapacities.Where(x => !OtpuskPoRodams.Any(y => y.DateBegin == x.DateBegin && y.DateEnd == x.DateEnd)).ToArray(); foreach (var inkapacity in okInkapacities) { int beginDay; if (_timeSheet.DateBeginPeriod.Year == inkapacity.DateBegin.Year && _timeSheet.DateBeginPeriod.Month == inkapacity.DateBegin.Month) { beginDay = inkapacity.DateBegin.Day; } else { beginDay = _timeSheet.DateBeginPeriod.Day; } int endDay; if (inkapacity.DateEnd.HasValue && _timeSheet.DateEndPeriod.Year == inkapacity.DateEnd.Value.Year && _timeSheet.DateEndPeriod.Month == inkapacity.DateEnd.Value.Month) { endDay = inkapacity.DateEnd.Value.Day; } else { endDay = _timeSheet.DateEndPeriod.Day; } for (int i = beginDay; i <= endDay; i++) { var day = new DateTime(_timeSheet.DateBeginPeriod.Year, _timeSheet.DateBeginPeriod.Month, i); var record = timeSheetRecordLList.FirstOrDefault(f => f.RecordDate.Date == day.Date && f.idFactStaffHistory == employee.idFactStaffHistory); if (record == null || record.idDayStatus == IdX) { continue; } record.idDayStatus = (int)Models.DayStatus.Б; record.JobTimeCount = 0; } } } return(timeSheetRecordLList); }
//Вносит в табель дни исключения и возвращает обновлённые записи табеля на работника List <TimeSheetRecord> AddExceptoinDaysToTimeSheetRecords(FactStaffWithHistory employee, IEnumerable <TimeSheetRecord> timeSheetRecords, IEnumerable <Exception> exeptions, IEnumerable <OK_Otpusk> otpusk) { var timeSheetRecordLList = new List <TimeSheetRecord>(timeSheetRecords); foreach (var exception in exeptions.Where(w => w.idWorkShedule == employee.PlanStaff.IdWorkShedule)) { var day = timeSheetRecordLList.FirstOrDefault(f => f.RecordDate.Date == exception.DateException.Date); if (day == null || day.idDayStatus == IdX) { continue; } var list = otpusk.Select(s => s.OK_Otpuskvid.idDayStatus); if ((list.Any(x => !x.HasValue || x.Value == day.idDayStatus) && exception.idDayStatus == IdTransferVihodnoy) || (list.Contains(IdOtpuskFemale) && exception.idDayStatus == IdVihodnoy)) { // если у человека отпуск и это перенесенный выходной (праздник) -> то оставляем отпуск // если женский отпуск и это выходной(праздник) -> то оставляем отпуск continue; } if (exception.idDayStatus == IdPp) { day.JobTimeCount = day.JobTimeCount > 1 ? day.JobTimeCount - 1 : 0; } else { timeSheetRecordLList.Remove(day); if (employee.StaffCount == 1) { day = NewTimeSheetRecord(exception.DateException, employee.idFactStaffHistory, (exception.idDayStatus == IdTransferVihodnoy) ? IdVihodnoy: exception.idDayStatus, // перенесенный выходной менякм на обычный выходной _timeSheet.id, employee.Employee.SexBit && employee.PlanStaff.Post.Category.id != IdPps ? (double)exception.KolHourMPS : (double)exception.KolHourGPS); } else { day = NewTimeSheetRecord(exception.DateException, employee.idFactStaffHistory, (exception.idDayStatus == IdTransferVihodnoy) ? IdVihodnoy : exception.idDayStatus, // перенесенный выходной менякм на обычный выходной _timeSheet.id, (double)employee.StaffCount * (employee.Employee.SexBit && employee.PlanStaff.Post.Category.id != IdPps ? (double)exception.KolHourMNS : (double)exception.KolHourGNS)); } timeSheetRecordLList.Add(day); } } return(timeSheetRecordLList); }
private IEnumerable <TimeSheetRecord> InsertEmployee(FactStaffWithHistory employee, IEnumerable <Exception> exeptions, IEnumerable <OK_Otpusk> otpuskList, IEnumerable <OK_Inkapacity> inkapacities, IEnumerable <Event> businesstrips) { List <TimeSheetRecord> timeSheetRecordLList; // Генерируем табель if (employee.PlanStaff.Post.Category.id == IdPps || employee.PlanStaff.Post.Category.id == 0) //if (employee.PlanStaff.Post.Category.IsPPS.Value) { var FullEmployee = employee.WorkHoursInWeek == FullPpsHours; //PPS //timeSheetRecordLList = PpsTimeSheetGenerate(employee, FullEmployee); timeSheetRecordLList = employee.PlanStaff.WorkShedule.id == Week5Days ? FiveDayesPpsTimeSheetGenerate(employee, FullEmployee) : PpsTimeSheetGenerate(employee, FullEmployee) ; } else { var fullEmployee = (employee.Employee.SexBit && employee.WorkHoursInWeek == FullManHours) || (!employee.Employee.SexBit && employee.WorkHoursInWeek == FullWomanHours) ; timeSheetRecordLList = employee.PlanStaff.WorkShedule.id == Week5Days //5 days week ? FiveDayesTimeSheetGenerate(employee, fullEmployee) //6 days week : (employee.PlanStaff.WorkShedule.id == Week6Days ? SixDayesTimeSheetGenerate(employee, fullEmployee) //flexible week : FlexibleTimeSheetGenerate(employee) ); } // Добавляем информацию о отпусе timeSheetRecordLList = AddHolidaysToTimeSheetRecords(employee, timeSheetRecordLList, otpuskList); // Добавляем дни исключения timeSheetRecordLList = AddExceptoinDaysToTimeSheetRecords(employee, timeSheetRecordLList, exeptions, otpuskList); // Добавляем информацию о больничных timeSheetRecordLList = AddHospitalsToTimeSheetRecords(employee, timeSheetRecordLList, inkapacities, otpuskList); //Добавляем информацию о командировках timeSheetRecordLList = AddBusinessTripToTimeSheetRecords(employee, timeSheetRecordLList, businesstrips); return(timeSheetRecordLList); }
List <TimeSheetRecord> AddBusinessTripToTimeSheetRecords(FactStaffWithHistory employee, IEnumerable <TimeSheetRecord> timeSheetRecords, IEnumerable <Event> businesstrips) { var timeSheetRecordList = new List <TimeSheetRecord>(timeSheetRecords); var okBusinesstrips = businesstrips as Event[] ?? businesstrips.ToArray(); if (okBusinesstrips.Any()) { foreach (var businesstrip in okBusinesstrips) { int beginDay; if (_timeSheet.DateBeginPeriod.Year == businesstrip.DateBegin.Value.Year && _timeSheet.DateBeginPeriod.Month == businesstrip.DateBegin.Value.Month) { beginDay = businesstrip.DateBegin.Value.Day; } else { beginDay = _timeSheet.DateBeginPeriod.Day; } int endDay; if (businesstrip.DateEnd.HasValue && _timeSheet.DateEndPeriod.Year == businesstrip.DateEnd.Value.Year && _timeSheet.DateEndPeriod.Month == businesstrip.DateEnd.Value.Month) { endDay = businesstrip.DateEnd.Value.Day; } else { endDay = _timeSheet.DateEndPeriod.Day; } for (int i = beginDay; i <= endDay; i++) { var day = new DateTime(_timeSheet.DateBeginPeriod.Year, _timeSheet.DateBeginPeriod.Month, i); var record = timeSheetRecordList.FirstOrDefault(f => f.RecordDate.Date == day.Date && f.idFactStaffHistory == employee.idFactStaffHistory); if (record == null || record.idDayStatus == IdX) { continue; } record.idDayStatus = (int)Models.DayStatus.К; record.JobTimeCount = 0; } } } return(timeSheetRecordList); }
//Генерация табеля для шестидневной рабочей недели private List <TimeSheetRecord> SixDayesTimeSheetGenerate(FactStaffWithHistory employee) { const double man6 = 7; const double manSaturday6 = 5; const double manNotFullDay6 = 6.6; const double womanFullDay6 = 6.25; const double womanSaturday6 = 4.75; const double womanNotFullDay6 = 6; var timeSheetRecordLList = new List <TimeSheetRecord>(); for (int i = _timeSheet.DateBeginPeriod.Day - 1; i < _timeSheet.DateEndPeriod.Day; i++) { TimeSheetRecord timeSheetRecord; var date = _timeSheet.DateBeginPeriod.AddDays(i); if ((employee.DateEnd != null && employee.DateEnd < date) || employee.DateBegin > date) { timeSheetRecord = NewTimeSheetRecord(date, employee.idFactStaffHistory, IdX, _timeSheet.id, 0); timeSheetRecordLList.Add(timeSheetRecord); continue; } switch (date.DayOfWeek) { case DayOfWeek.Saturday: { timeSheetRecord = GenerageTimeSheetRecord(employee, date, IdYavka, manSaturday6, manNotFullDay6, womanSaturday6, womanNotFullDay6); break; } case DayOfWeek.Sunday: { timeSheetRecord = GenerageTimeSheetRecord(employee, date, IdVihodnoy); break; } default: { timeSheetRecord = GenerageTimeSheetRecord(employee, date, IdYavka, man6, manNotFullDay6, womanFullDay6, womanNotFullDay6); break; } } timeSheetRecordLList.Add(timeSheetRecord); } return(timeSheetRecordLList); }
//Генерация табеля для шестидневной рабочей недели private List <TimeSheetRecord> SixDayesTimeSheetGenerate(FactStaffWithHistory employee, bool fullEmployee) { const double man6 = 7; const double woman6 = 6.25; const double manSaturday = 5; const double womanSaturday = 4.75; var employee6 = (double)(employee.WorkHoursInWeek / 6); var timeSheetRecordLList = new List <TimeSheetRecord>(); for (int i = _timeSheet.DateBeginPeriod.Day - 1; i < _timeSheet.DateEndPeriod.Day; i++) { TimeSheetRecord timeSheetRecord; var date = _timeSheet.DateBeginPeriod.AddDays(i); if ((employee.DateEnd != null && employee.DateEnd < date) || employee.DateBegin > date) { timeSheetRecord = NewTimeSheetRecord(date, employee.idFactStaffHistory, IdX, _timeSheet.id, 0); timeSheetRecordLList.Add(timeSheetRecord); continue; } switch (date.DayOfWeek) { case DayOfWeek.Saturday: { timeSheetRecord = GenerageTimeSheetRecord(employee, date, IdYavka, fullEmployee ? manSaturday : employee6, //мужчины fullEmployee ? womanSaturday : employee6); //женщины break; } case DayOfWeek.Sunday: { timeSheetRecord = GenerageTimeSheetRecord(employee, date, IdVihodnoy); break; } default: { timeSheetRecord = GenerageTimeSheetRecord(employee, date, IdYavka, fullEmployee ? man6 : employee6, fullEmployee ? woman6 : employee6); break; } } timeSheetRecordLList.Add(timeSheetRecord); } return(timeSheetRecordLList); }
//Генерация табеля для ППС private List <TimeSheetRecord> PpsTimeSheetGenerate(FactStaffWithHistory employee, bool fullEmployee) { var employeePps = (double)(employee.WorkHoursInWeek / 6); const double fullPps = 6.25; const double saturdayPps = 4.75; var timeSheetRecordLList = new List <TimeSheetRecord>(); for (int i = _timeSheet.DateBeginPeriod.Day - 1; i < _timeSheet.DateEndPeriod.Day; i++) { TimeSheetRecord timeSheetRecord; var date = _timeSheet.DateBeginPeriod.AddDays(i); if ((employee.DateEnd != null && employee.DateEnd < date) || employee.DateBegin > date) { timeSheetRecord = NewTimeSheetRecord(date, employee.idFactStaffHistory, IdX, _timeSheet.id, 0); timeSheetRecordLList.Add(timeSheetRecord); continue; } switch (date.DayOfWeek) { case DayOfWeek.Saturday: { timeSheetRecord = GenerageTimeSheetRecord(employee, date, IdYavka, (employee.StaffCount == 1) ? saturdayPps : employeePps, (employee.StaffCount == 1) ? saturdayPps : employeePps); break; } case DayOfWeek.Sunday: { timeSheetRecord = GenerageTimeSheetRecord(employee, date, IdVihodnoy); break; } default: { timeSheetRecord = GenerageTimeSheetRecord(employee, date, IdYavka, (employee.StaffCount == 1) ? fullPps : employeePps, (employee.StaffCount == 1) ? fullPps : employeePps); break; } } timeSheetRecordLList.Add(timeSheetRecord); } return(timeSheetRecordLList); }
//Генерация табеля для пятидневной рабочей недели private List <TimeSheetRecord> FiveDayesTimeSheetGenerate(FactStaffWithHistory employee, bool fullEmployee) { var employee5 = (double)(employee.WorkHoursInWeek / 5); const int womanFriday = 6; const double womanDaily = 7.5; var timeSheetRecordLList = new List <TimeSheetRecord>(); for (int i = _timeSheet.DateBeginPeriod.Day - 1; i < _timeSheet.DateEndPeriod.Day; i++) { TimeSheetRecord timeSheetRecord; var date = _timeSheet.DateBeginPeriod.AddDays(i); if ((employee.DateEnd != null && employee.DateEnd < date) || employee.DateBegin > date) { timeSheetRecord = NewTimeSheetRecord(date, employee.idFactStaffHistory, IdX, _timeSheet.id, 0); timeSheetRecordLList.Add(timeSheetRecord); continue; } switch (date.DayOfWeek) { case DayOfWeek.Friday: { timeSheetRecord = GenerageTimeSheetRecord(employee, date, IdYavka, employee5, fullEmployee ? womanFriday : employee5); break; } case DayOfWeek.Saturday: case DayOfWeek.Sunday: { timeSheetRecord = GenerageTimeSheetRecord(employee, date, IdVihodnoy); break; } default: { timeSheetRecord = GenerageTimeSheetRecord(employee, date, IdYavka, employee5, fullEmployee ? womanDaily : employee5); break; } } timeSheetRecordLList.Add(timeSheetRecord); } return(timeSheetRecordLList); }
//Вносит в табель дни исключения и возвращает обновлённые записи табеля на работника List <TimeSheetRecord> AddExceptoinDaysToTimeSheetRecords(FactStaffWithHistory employee, IEnumerable <TimeSheetRecord> timeSheetRecords, IEnumerable <Exception> exeptions) { var timeSheetRecordLList = new List <TimeSheetRecord>(timeSheetRecords); foreach (var exception in exeptions.Where(w => w.idWorkShedule == employee.PlanStaff.IdWorkShedule)) { var day = timeSheetRecordLList.FirstOrDefault(f => f.RecordDate.Date == exception.DateException.Date); if (day == null || day.idDayStatus == IdX) { continue; } if (exception.idDayStatus == IdPp) { day.JobTimeCount = day.JobTimeCount > 1 ? day.JobTimeCount - 1 : 0; } else { timeSheetRecordLList.Remove(day); if (employee.StaffCount == 1) { day = NewTimeSheetRecord(exception.DateException, employee.idFactStaffHistory, exception.idDayStatus, _timeSheet.id, employee.Employee.SexBit && employee.PlanStaff.Post.Category.id != IdPps ? (double)exception.KolHourMPS : (double)exception.KolHourGPS); } else { day = NewTimeSheetRecord(exception.DateException, employee.idFactStaffHistory, exception.idDayStatus, _timeSheet.id, (double)employee.StaffCount * (employee.Employee.SexBit && employee.PlanStaff.Post.Category.id != IdPps ? (double)exception.KolHourMNS : (double)exception.KolHourGNS)); } timeSheetRecordLList.Add(day); } } return(timeSheetRecordLList); }
//Генерирует запись в табеле /* private TimeSheetRecord GenerageTimeSheetRecord(FactStaffWithHistory employee, DateTime date, int idDayStatus, * double manFullDay=0, double manNotFullDay=0, double womanFullDay=0, double womanNotFullDay=0) * { * if (employee.StaffCount == 1) * { * return NewTimeSheetRecord(date, employee.idFactStaffHistory, idDayStatus, * _timeSheet.id, employee.Employee.SexBit ? manFullDay : womanFullDay); * } * return NewTimeSheetRecord(date, employee.idFactStaffHistory, idDayStatus, * _timeSheet.id, * (double) employee.StaffCount* * (employee.Employee.SexBit ? manNotFullDay : womanNotFullDay)); * }*/ private TimeSheetRecord GenerageTimeSheetRecord(FactStaffWithHistory employee, DateTime date, int idDayStatus, double manDay = 0, double womanDay = 0) { return(NewTimeSheetRecord(date, employee.idFactStaffHistory, idDayStatus, _timeSheet.id, employee.Employee.SexBit ? manDay: womanDay)); }
//Генерация табеля для работников private void InsertEmployees(FactStaffWithHistory[] employees) { try { FactStaffWithHistory previousEmpl = null; // переменная для хранения предыдущей FactStaffWithHistory var exeptions = _db.Exception.Where(w => w.DateException >= _timeSheet.DateBeginPeriod && w.DateException <= _timeSheet.DateEndPeriod).ToArray(); var factStaffIds = employees.Select(s => s.id).Distinct(); var otpusk = _db.OK_Otpusks.Where(w => factStaffIds.Contains(w.idFactStaff) && w.DateBegin <= _timeSheet.DateEndPeriod && w.DateEnd >= _timeSheet.DateBeginPeriod).ToArray(); var holspitals = _db.OK_Inkapacities.Where(w => employees.Select(s => s.idEmployee).Distinct().Contains(w.idEmployee) && w.DateBegin <= _timeSheet.DateEndPeriod && w.DateEnd >= _timeSheet.DateBeginPeriod); var trips = _db.Events.Where(w => employees.Select(x => x.idFactStaffHistory).Contains(w.idFactStaffHistory) && w.DateBegin <= _timeSheet.DateEndPeriod && w.idPrikazEnd == null && w.DateEnd >= _timeSheet.DateBeginPeriod && w.idEventKind == IdBusinessTripKind && w.idEventType == BeginEvent); foreach (var employee in employees) { var employeeOtpusk = otpusk.Where(w => w.DateBegin.HasValue && w.idFactStaff == employee.id && w.DateBegin <= _timeSheet.DateEndPeriod && w.DateEnd >= _timeSheet.DateBeginPeriod).ToArray(); var employeeHosp = holspitals.Where(w => w.idEmployee == employee.idEmployee && w.DateBegin <= _timeSheet.DateEndPeriod && w.DateEnd >= _timeSheet.DateBeginPeriod).ToArray(); var employeeTrips = trips.Where(w => w.FactStaffHistory.idFactStaff == employee.id && w.DateBegin <= _timeSheet.DateEndPeriod && w.DateEnd >= _timeSheet.DateBeginPeriod).ToArray(); _timeSheetRecordLList.AddRange(InsertEmployee(employee, exeptions, employeeOtpusk, employeeHosp, employeeTrips)); if (previousEmpl != null && //если имеются FactStaffWithHistory с одинаковыми idEmployee, StaffCount, idPlanStaff, id(idFactStaff) previousEmpl.idEmployee == employee.idEmployee && previousEmpl.StaffCount == employee.StaffCount && previousEmpl.idPlanStaff == employee.idPlanStaff && previousEmpl.id == employee.id) { var previousItems = _timeSheetRecordLList.Where(p => p.idFactStaffHistory == previousEmpl.idFactStaffHistory && p.idTimeSheet == _timeSheet.id).ToList(); // берем записи табеля с 1ым FactStaffWithHistory var currentItems = _timeSheetRecordLList.Where(p => p.idFactStaffHistory == employee.idFactStaffHistory && p.idTimeSheet == _timeSheet.id).ToList(); // берем записи табеля с 2ым FactStaffWithHistory foreach (var deletepreviousItem in previousItems) //удаляем первые FactStaffWithHistory из общей колекции записей _timeSheetRecordLList { _timeSheetRecordLList.Remove(deletepreviousItem); } foreach (var deletecurrentItem in currentItems) //удаляем вторые FactStaffWithHistory из общей колекции записей _timeSheetRecordLList { _timeSheetRecordLList.Remove(deletecurrentItem); } foreach (var currentItem in currentItems) //проходим по всем записям табеля с 2ым FactStaffWithHistory //и делаем слияние 1 и 2 FactStaffWithHistory записей табеля в общую { //также меняем idFactStaffHistory у записей табеля 1 на dFactStaffHistory записей табеля 2 //"чтобы не было 2ух строк с одинаковыми человеками"(актуально при продлении трудового договора) if (currentItem.RecordDate <= previousEmpl.DateEnd) { var previousItemWithDay = previousItems.Where(m => m.RecordDate == currentItem.RecordDate).SingleOrDefault(); previousItemWithDay.idFactStaffHistory = currentItem.idFactStaffHistory; _timeSheetRecordLList.Add(previousItemWithDay); } if (currentItem.RecordDate > previousEmpl.DateEnd) { _timeSheetRecordLList.Add(currentItem); } } } previousEmpl = employee; //сохраняем текущий элемент как предыдущую } } catch (System.Exception ex) { _db.TimeSheet.DeleteAllOnSubmit(_db.TimeSheet.Where(w => w.id == IdTimeSheet)); _db.SubmitChanges(); var sb = new StringBuilder(); sb.AppendLine("Формирование табеля завершилось неудачей"); if (employees.Any(a => a.PlanStaff.IdWorkShedule == null)) { foreach (var employee in employees.Where(w => w.PlanStaff.IdWorkShedule == null)) { sb.AppendLine(string.Format(" {0} {1} {2}", employee.Employee.LastName, employee.Employee.FirstName, employee.Employee.Otch)); } sb.AppendLine(); sb.AppendLine("Для устранения проблемы обратитесь в отдел управления кадрами."); } sb.AppendLine(ex.Message); throw new System.Exception(sb.ToString()); } }