private int GenerateHistoryFromPattern(ShiftProfileDto profile) { TryFillCache(profile); var shiftsToInsert = new List <ShiftHistoryRecord>(); var shiftsRecords = cacheShifts[profile.Name]; var crewRecords = cacheCrews[profile.Name]; foreach (var pattern in profile.CrewPatterns) { var shiftRecord = shiftsRecords.FirstOrDefault(s => s.Name == pattern.Shift); var crewRecord = crewRecords.FirstOrDefault(c => c.Name == pattern.Crew); var start = profile.Start.Date.AddDays(pattern.Day - 1) + shiftRecord.Start; var _end = profile.Start.Date.AddDays(pattern.Day - 1) + shiftRecord.End; if (_end < start) { _end = _end.AddDays(1); } shiftsToInsert.Add(new ShiftHistoryRecord { Start = start, End = _end, IdCrew = crewRecord?.IdCrew, IdShift = shiftRecord.IdShift, IdShiftProfile = shiftRecord.IdShiftProfile }); } cacheHistoryPatterns.Remove(profile.Name); return(repository.InsertShiftHistory(shiftsToInsert)); }
public void Should_Map_ShiftProfileDto_to_ShiftProfile() { // Arrange var _mShiftProfile = new Mock <ShiftProfile>(); var shiftProfileDto = new ShiftProfileDto { IsActive = true, StartDateTime = _testDateTime, EndDateTime = _testDateTime, EmployeeId = 1, //HoursWorked = 12, ShiftId = 1 }; // Act CommonHelperAppService.MapDtoToEntityForUpdating(shiftProfileDto, _mShiftProfile.Object); // Assert Assert.AreEqual(shiftProfileDto.IsActive, _mShiftProfile.Object.IsActive); Assert.AreEqual(shiftProfileDto.StartDateTime, _mShiftProfile.Object.StartDateTime); Assert.AreEqual(shiftProfileDto.EndDateTime, _mShiftProfile.Object.EndDateTime); Assert.AreEqual(shiftProfileDto.HoursWorked, _mShiftProfile.Object.HoursWorked); Assert.AreEqual(shiftProfileDto.EmployeeId, _mShiftProfile.Object.EmployeeId); Assert.AreEqual(shiftProfileDto.ShiftId, _mShiftProfile.Object.ShiftId); }
public int InsertShiftProfile(ShiftProfileDto profile) { var rows = 0; // Insert Profile var idShiftProfile = InsertAsync <ShiftProfileRecord, int>(new ShiftProfileRecord { Name = profile.Name, CycleDays = profile.CycleDays, Start = profile.Start.LocalDateTime, End = profile.End.LocalDateTime, Enable = true, }).Result; rows += 1; // Insert Shifts var shifts = profile.Shifts.Select(s => new ShiftRecord { IdShiftProfile = idShiftProfile, Name = s.Name, Start = s.Start, End = s.End, Enable = true }).ToList(); var sql = queryFactory.GetInsertQuery <ShiftRecord>(); var affected = Conn.Execute(sql, shifts, Tran); rows += affected; // Insert Crews var crews = profile.Crews.Select(s => new CrewRecord { IdShiftProfile = idShiftProfile, Name = s.Name, Enable = true }).ToList(); sql = queryFactory.GetInsertQuery <CrewRecord>(); affected = Conn.Execute(sql, crews, Tran); rows += affected; // Insert Holidays var holidays = profile.Holidays.Select(h => new HolidayRecord { IdShiftProfile = idShiftProfile, Name = h.Name, Start = h.Start.LocalDateTime, End = h.End.LocalDateTime, Enable = true }).ToList(); sql = queryFactory.GetInsertQuery <HolidayRecord>(); affected = Conn.Execute(sql, holidays, Tran); rows += affected; return(rows); }
public void Update(ShiftProfileDto shiftProfileDto, long userId) { var shiftProfile = _unitOfWork.ShiftProfileRepository.GetById(shiftProfileDto.Id); CommonHelperAppService.MapDtoToEntityForUpdating(shiftProfileDto, shiftProfile); _unitOfWork.ShiftProfileRepository.Update(shiftProfile); _unitOfWork.Save(); // Audit _auditLogAppService.Audit( AppConstants.ActionTypeUpdate, AppConstants.ShiftProfileTableName, userId, shiftProfile.Id); }
private int GenerateHistory( ShiftProfileDto profile, DateTime?begin, DateTime?end = null) { TryFillCache(profile); begin ??= DateTime.Now; end ??= DateTime.Now; var shiftsToInsert = new List <ShiftHistoryRecord>(); var historyRecords = cacheHistoryPatterns[profile.Name]; var shiftsRecords = cacheShifts[profile.Name]; var date = begin.Value; while (date < end) { // El Turno (sifht) está dado por comparacion de horas en el día var shiftRecord = shiftsRecords.FirstOrDefault(s => GetShiftDate(s, date) != null); if (shiftRecord == null) { date = date.AddMinutes(10); continue; } // La escuadra (crew) se detecta mediante el patternHistory, que sería una parte // del historico que fue cargada en base a la configuracion var patternDay = (date - profile.Start).TotalDays % profile.CycleDays; var historyDate = profile.Start.LocalDateTime.AddDays(patternDay); var pattern = historyRecords?.FirstOrDefault(h => h.Start <= historyDate && h.End > historyDate); var shiftDate = GetShiftDate(shiftRecord, date).Value; shiftsToInsert.Add(new ShiftHistoryRecord { Start = shiftDate.start, End = shiftDate.end, IdCrew = pattern?.IdCrew, IdShift = shiftRecord.IdShift, IdShiftProfile = shiftRecord.IdShiftProfile }); date = shiftDate.end; } return(repository.InsertShiftHistory(shiftsToInsert)); }
private void InsertShiftProfile(ShiftProfileDto profile) { var profileRecord = repository.GetShiftProfile(profile.Name); if (profileRecord == null || profileRecord.Start != profile.Start) { repository.InsertShiftProfile(profile); cacheHistoryPatterns.Remove(profile.Name); cacheShifts.Remove(profile.Name); cacheCrews.Remove(profile.Name); } if (profile.CrewPatterns?.Any() == true) { GenerateHistoryFromPattern(profile); } }
private void TryFillCache(ShiftProfileDto profile) { var profileName = profile.Name; var begin = profile.Start.LocalDateTime; var end = profile.Start.LocalDateTime.AddDays(profile.CycleDays); if (!cacheHistoryPatterns.ContainsKey(profileName)) { cacheHistoryPatterns[profileName] = repository.GetShiftHistory(profileName, begin, end); } if (!cacheShifts.ContainsKey(profileName)) { cacheShifts[profileName] = repository.GetShifts(profileName, true); } if (!cacheCrews.ContainsKey(profileName)) { cacheCrews[profileName] = repository.GetSCrews(profileName, true); } }
public static void MapDtoToEntityForUpdating(ShiftProfileDto shiftProfileDto, ShiftProfile shiftProfile) { shiftProfile.IsActive = shiftProfileDto.IsActive; shiftProfile.StartDateTime = shiftProfileDto.StartDateTime; shiftProfile.EndDateTime = shiftProfileDto.EndDateTime; shiftProfile.ActualStartDateTime = shiftProfileDto.ActualStartDateTime; shiftProfile.ActualEndDateTime = shiftProfileDto.ActualEndDateTime; shiftProfile.ZktStartDateTime = shiftProfileDto.ZktStartDateTime; shiftProfile.ZktEndDateTime = shiftProfileDto.ZktEndDateTime; shiftProfile.HoursWorked = shiftProfileDto.HoursWorked; shiftProfile.EmployeeId = shiftProfileDto.EmployeeId; shiftProfile.ShiftId = shiftProfileDto.ShiftId; shiftProfile.IsApproved = shiftProfileDto.IsApproved; shiftProfile.Status = shiftProfileDto.Status; shiftProfile.Reason = shiftProfileDto.Reason; shiftProfile.Notes = shiftProfileDto.Notes; shiftProfile.IsModified = shiftProfileDto.IsModified; }
// CRUD public void Create(ShiftProfileDto shiftProfileDto, long userId) { shiftProfileDto.HoursWorked = CommonHelperAppService.ReturnCalculatedTimespanBetweenTwoDateTimeObjects( shiftProfileDto.ActualStartDateTime, shiftProfileDto.ActualEndDateTime, shiftProfileDto.Status); var shiftProfile = Mapper.Map <ShiftProfile>(shiftProfileDto); _unitOfWork.ShiftProfileRepository.Create(shiftProfile); _unitOfWork.Save(); // Audit _auditLogAppService.Audit( AppConstants.ActionTypeCreate, AppConstants.ShiftProfileTableName, userId, shiftProfile.Id); UpdateAndDeleteNoShow(shiftProfileDto.ShiftId, userId); }