public EmailContext ComposeEmailContextForTimesheetReminder(int employeeId, DateTime date) { var emailContext = new EmailContext(); var selectedEmployee = _userRepository.Get(employeeId, "Person,ReportingPerson.Person,Manager.Person"); // Kind of Bad logic, But will work for now. So let's keep it. var settings = _settingsService.GetSiteSettings(); if (settings.TimeSheetSettings?.MaxTimeSheetMisses > 0) { var currentCount = _missedTimeSheetRepository.Count(m => m.UserId == employeeId && m.FilledOn == null); if (currentCount < settings.TimeSheetSettings.MaxTimeSheetMisses) { emailContext.DropEmail = true; } } var timeSheetMisses = _missedTimeSheetRepository.GetAllBy(u => u.UserId == employeeId && u.FilledOn == null).ToList(); var pendingCorrectionTimeSheets = _timeSheetRepository.GetAllBy(u => u.CreatedByUserId == employeeId && u.State == TimeSheetState.NeedsCorrection).ToList(); if (selectedEmployee != null) { var reportCard = new StringBuilder(); reportCard.AppendLine("<table width='900px' bgcolor='#999999'>"); var yesterday = DateTime.Today.AddDays(-1); reportCard.AppendLine("<tr bgcolor='#ffffff'>"); reportCard.AppendLine($"<td>{yesterday.Date.DayOfWeek}- {yesterday.Date.ToShortDateString()} - No TimeSheet</td>"); reportCard.AppendLine("</tr>"); foreach (var timesheetMiss in timeSheetMisses) { reportCard.AppendLine("<tr bgcolor='#ffffff'>"); reportCard.AppendLine($"<td>{timesheetMiss.Date.DayOfWeek}- {timesheetMiss.Date.ToShortDateString()} - No TimeSheet</td>"); reportCard.AppendLine("</tr>"); } foreach (var pendingTimesheet in pendingCorrectionTimeSheets) { reportCard.AppendLine("<tr bgcolor='#ffffff'>"); reportCard.AppendLine($"<td>{pendingTimesheet.Date.DayOfWeek}- {pendingTimesheet.Date.ToShortDateString()} - Needs Correction</td>"); reportCard.AppendLine("</tr>"); } reportCard.AppendLine("</table>"); emailContext.PlaceHolders = new List <PlaceHolder> { new PlaceHolder("[Name]", selectedEmployee.Person.Name), new PlaceHolder("[Report]", reportCard.ToString()) }; emailContext.Subject = "Timesheet Reminder"; emailContext.ToAddress.Add(new MailAddress(selectedEmployee.OfficialEmail, selectedEmployee.Person.Name)); if (selectedEmployee.ReportingPerson != null) { if (selectedEmployee.ReportingPerson.OfficialEmail != null) { emailContext.CcAddress.Add(new MailAddress(selectedEmployee.ReportingPerson.OfficialEmail, selectedEmployee.ReportingPerson.Person.Name)); } } if (selectedEmployee.Manager != null) { if (selectedEmployee.Manager.OfficialEmail != null) { emailContext.CcAddress.Add(new MailAddress(selectedEmployee.Manager.OfficialEmail, selectedEmployee.Manager.Person.Name)); } } } return(emailContext); }
public ActionResult CreateSheet(TimeSheetModel timeSheet) { var date = timeSheet.Date.DateFromUsFormat(); var exists = _timeSheetRepository.Any(t => t.CreatedByUserId == WebUser.Id && t.Date == date.Date); if (!exists) { var newTimeSheet = new TimeSheet { State = TimeSheetState.PendingApproval, Title = $"{WebUser.Name}'s {"TimeSheet for "} {timeSheet.Date}", Date = date.Date, TotalHours = timeSheet.Rows.Sum(r => r.Effort), Comments = timeSheet.Comments, CreatedByUserId = WebUser.Id }; _timeSheetRepository.Create(newTimeSheet); foreach (var lineItem in timeSheet.Rows) { var newTimeSheetLineItem = new TimeSheetLineItem { TimeSheetId = newTimeSheet.Id, ProjectId = lineItem.ProjectId, TaskId = lineItem.TaskId, TaskSummary = lineItem.TaskSummary, Effort = lineItem.Effort, Comments = lineItem.Comments, WorkType = lineItem.WorkType }; _timeSheetLineItemRepository.Create(newTimeSheetLineItem); } _unitOfWork.Commit(); // Log Activity var activity = new TimeSheetActivity { TimeSheetId = newTimeSheet.Id, Title = "Created", Comment = $"{WebUser.Name} created the TimeSheet at {DateTime.UtcNow.ToString("G")} with state {newTimeSheet.State} & hours {newTimeSheet.TotalHours}", CreatedByUserId = WebUser.Id }; _timeSheetActivityRepository.Create(activity); _unitOfWork.Commit(); //Update if there is a missing timesheet entry var missedList = _missedTimeSheetRepository.GetAllBy(m => m.UserId == newTimeSheet.CreatedByUserId && m.Date == newTimeSheet.Date && m.FilledOn == null); foreach (var missedTimeSheet in missedList) { missedTimeSheet.FilledOn = DateTime.UtcNow; _missedTimeSheetRepository.Update(missedTimeSheet); } _unitOfWork.Commit(); // Notify the reporting manager online var loginEmployee = _employeeRepository.GetBy(r => r.UserId == WebUser.Id, "ReportingPerson"); if (loginEmployee?.ReportingPerson != null) { var message = $"{WebUser.Name} has submitted the timesheet with {newTimeSheet.TotalHours} hours"; _notificationService.NotifyUser("Timesheet Submitted", message, loginEmployee.ReportingPerson.Code); } // Send Email, Email Template name is hard corded - Need to change later // Replace the hard coded emails with settings or a team. #if !DEBUG _emailComposerService.TimeSheetSubmitted(newTimeSheet.Id); #endif return(Json(true)); } return(Json(false)); }