public IHttpActionResult Get(int?userId) { var suppliedUserId = userId == null?User.Identity.GetUserId <int>() : userId.GetValueOrDefault(); var isAdministrator = User.IsInRole(ConnectRoles.Admin); using (var context = new ConnectContext()) { if (suppliedUserId != User.Identity.GetUserId <int>() && !isAdministrator) { return(NotFound()); } var last12Months = StatusReportHelper.GetLast12Months(); var from = last12Months.First(); var to = last12Months.Last().AddMonths(1).AddDays(-1); var technicians = context.Technicians.Where(c => c.UserId == suppliedUserId).ToList(); var users = isAdministrator ? context.Users.Where(u => u.Deleted == null) .OrderBy(c => c.CompanyKey) .Select(c => new StatusReportUserViewModel { Name = c.CompanyKey, Id = c.Id }) .ToList() : null; if (technicians.Count > 0) { var statusReportData = new StatusReportData { Technicians = technicians, WorkshopSettings = context.WorkshopSettings.Where(c => c.UserId == suppliedUserId) .OrderByDescending(c => c.Created) .FirstOrDefault(), Last12Months = last12Months, Documents = context.TachographDocuments.Where(c => c.Created >= from && c.Created <= to) .Select(c => new ReportDocumentViewModel { Technician = c.Technician, Created = c.Created }).ToList() }; return(Ok(StatusReportHelper.GenerateStatusReport(statusReportData, users))); } return(Ok(new StatusReportViewModel(null, null, users) { Users = users })); } }
public static StatusReportViewModel GenerateStatusReport(StatusReportData data, List <StatusReportUserViewModel> users) { var result = new StatusReportViewModel(data.Technicians, data.WorkshopSettings, users) { Performance = new List <StatusReportTechnician>() }; GetTechniciansPerformanceData(data, result); result.LineChartLabels = data.Last12Months.Select(c => c.ToString("MMM yy")).ToList(); string tachoCentreQuarterlyStatusText; result.TachoCentreQuarterlyStatusTextColor = "#" + GetStatus(result.TachoCentreQuarterlyStatus, out tachoCentreQuarterlyStatusText); result.TachoCentreQuarterlyStatusText = tachoCentreQuarterlyStatusText; string gv212StatusText; result.GV212StatusTextColor = "#" + GetStatus(result.GV212Status, out gv212StatusText); result.GV212StatusText = gv212StatusText; return(result); }
private static void GetTechniciansPerformanceData(StatusReportData data, StatusReportViewModel result) { var technicianViewModels = data.Technicians.Select(c => new TechnicianViewModel(data.Documents, data.Last12Months, c)).ToList(); for (var index = 0; index < technicianViewModels.Count; index++) { var technician = technicianViewModels[index]; string halfYearStatusText; var halfYearColor = GetStatus(technician.HalfYearStatus(), out halfYearStatusText); string threeYearStatusText; var threeYearColor = GetStatus(technician.ThreeYearStatus(), out threeYearStatusText); string statusTextColor; var statusText = GetStatusText(halfYearStatusText, threeYearStatusText, out statusTextColor); result.Performance.Add(new StatusReportTechnician { Value = technician.JobsDoneInLast12Months, Color = GetColor(index), Label = technician.Technician.Name.ToTitleCase(), DateOfNextCheck = technician.DateOfLastCheck == null ? (DateTime?)null : technician.DateOfLastCheck.GetValueOrDefault().AddMonths(6), TrainingDateExpiry = technician.DateOfLast3YearCheck == null ? (DateTime?)null : technician.DateOfLast3YearCheck.GetValueOrDefault().AddYears(3), StatusColor = "#" + halfYearColor, ThreeYearColor = "#" + threeYearColor, Status = statusText }); } result.LineChartData = new List <int>(); foreach (var month in data.Last12Months) { var count = technicianViewModels.Sum(technicianViewModel => technicianViewModel.JobsMonthByMonth.First(d => d.Key == month).Value); result.LineChartData.Add(count); } }