private void UpdateAttendance(MemberAttendanceForView memberAttendanceData, int projectId, DateTime weekRequested) { var weekSought = weekRequested.GetWeek(); var yearSought = weekRequested.Year; bool isInsert = memberAttendanceData.AttendanceId == -1; Attendance attendance; if (isInsert) //that means its an INSERT { attendance = new Attendance { ProjectId = projectId, CreatedBy = User.Identity.Name, CreatedOn = DateTime.UtcNow, Year = yearSought, Week = weekSought, WeekString = GetDisplayWeek(weekRequested.StartOfWeek(DayOfWeek.Monday)) }; using (var scope = new TransactionScope()) { try { _unitOfWork.AttendanceRepository.Insert(attendance); _unitOfWork.Save(); foreach (var item in memberAttendanceData.MemberAttendances) { var newMembAtt = GetNewMemberAttendance(attendance.AttendanceId, item); _unitOfWork.MemberAttendanceRepository.Insert(newMembAtt); } _unitOfWork.Save(); scope.Complete(); } catch (Exception ex) { scope.Dispose(); //Log the error (uncomment dex variable name after DataException and add a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); Utility.WriteToLog("AttendanceController.UpdateAttendance() : " + ex, "Error"); throw new Exception(ex.Message); } } } else { attendance = _unitOfWork.AttendanceRepository.GetById(memberAttendanceData.AttendanceId); attendance.ModifiedBy = User.Identity.Name; attendance.ModifiedOn = DateTime.UtcNow; try { _unitOfWork.AttendanceRepository.Update(attendance); _unitOfWork.Save(); foreach (var item in memberAttendanceData.MemberAttendances) { var item1 = item; var memberAttendance = _unitOfWork.MemberAttendanceRepository.Get(filter: x => x.MemberId == item1.Member.MemberId && x.AttendanceId == attendance.AttendanceId).ToList(); if (memberAttendance.Any()) { var dataToUpdate = memberAttendance[0]; UpdateMemberAttendanceData(item1, dataToUpdate); _unitOfWork.MemberAttendanceRepository.Update(dataToUpdate); } } _unitOfWork.Save(); } catch (Exception ex) { //scope.Dispose(); //Log the error (uncomment dex variable name after DataException and add a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); Utility.WriteToLog("AttendanceController.UpdateAttendance() : " + ex, "Error"); throw new Exception(ex.Message); } } }
private MemberAttendanceForView CreateAttendanceDataForWeekAndProject(int projectId, DateTime weekRequested) { var listOfMemberAttendance = new List<MemberAttendance>(); foreach (var item in _unitOfWork.MemberToProjectsRepository.Get(filter: x => x.ProjectId == projectId && x.IsAssigned)) { var member = _unitOfWork.MemberRepository.GetById(item.MemberId); //check if member start date is before the attendance week date //IF yes, member is included for the attendance ELSE not if (!member.StartDate.HasValue) { Utility.LogWarn( string.Format("CreateAttendanceDataForWeekAndProject: Start date not set for member {0} {1}", member.MemberId, member.MemberName)); } else { if (DateTime.Compare(member.StartDate.Value, weekRequested) > 0 && DateTime.Compare(member.StartDate.Value, weekRequested.AddDays(7)) > 0) //if(member.StartDate.Value <= weekRequested && member.StartDate.Value >= weekRequested.AddDays(7)) { //skip this member as member has not started for attendance in question continue; } } if (member.ExitDate.HasValue) { if (DateTime.Compare(member.ExitDate.Value, weekRequested) < 0 && DateTime.Compare(member.ExitDate.Value, weekRequested.AddDays(7)) < 0) { //skip this member as member exit date is before th attendance week in question continue; } } var memberAttendances = _unitOfWork.MemberAttendanceRepository.GetEmptyObjectCollection(); var memberAttendance = memberAttendances.FirstOrDefault(); memberAttendance.MemberId = member.MemberId; memberAttendance.Member = member; memberAttendance.Monday = false; memberAttendance.Tuesday = false; memberAttendance.Wednesday = false; memberAttendance.Thursday = false; memberAttendance.Friday = false; memberAttendance.Saturday = false; memberAttendance.Sunday = false; memberAttendance.MondayRate = memberAttendance.TuesdayRate = memberAttendance.WednesdayRate = memberAttendance.ThursdayRate = memberAttendance.FridayRate = memberAttendance.SaturdayRate = memberAttendance.SundayRate = item.MemberRate; listOfMemberAttendance.Add(memberAttendance); } var attendance = new Attendance {AttendanceId = -1}; return GetMemberAttendanceObject(attendance, listOfMemberAttendance, weekRequested); }
private MemberAttendanceForView GetMemberAttendanceObject(Attendance attendance, List<MemberAttendance> memberAttendances, DateTime weekRequested) { var mondayDate = weekRequested.StartOfWeek(DayOfWeek.Monday); // DateTime.Now.StartOfWeek(DayOfWeek.Monday); return new MemberAttendanceForView { Attendance = attendance, AttendanceId = attendance.AttendanceId, MemberAttendances = memberAttendances, Projects = _unitOfWork.ProjectRepository.Get().ToList(), MondayDate = mondayDate.ToShortDateString(), TuesdayDate = mondayDate.AddDays(1).ToShortDateString(), WednesdayDate = mondayDate.AddDays(2).ToShortDateString(), ThursdayDate = mondayDate.AddDays(3).ToShortDateString(), FridayDate = mondayDate.AddDays(4).ToShortDateString(), SaturdayDate = mondayDate.AddDays(5).ToShortDateString(), SundayDate = mondayDate.AddDays(6).ToShortDateString() }; }
private MemberAttendanceForView GetMemberAttendanceForViewById(Attendance attendance, DateTime weekRequested) { var memberAttendances = _unitOfWork.MemberAttendanceRepository.Get(filter: x => x.AttendanceId == attendance.AttendanceId).ToList(); var dataToView = GetMemberAttendanceObject(attendance, memberAttendances, weekRequested); return dataToView; }