public ActionResult TimeByWeek(string dummy) { string project = Request.Form["Projects"]; string fromDate = Request.Form["fromDate"]; WeekTasks taskData = new WeekTasks(); if (string.IsNullOrEmpty(fromDate)) { fromDate = DateTime.Now.ToString("MM/dd/yyyy"); } this.taskService.GetWeekTasks(fromDate, taskData, project); ViewBag.Message = "Result from " + fromDate + " to " + Convert.ToDateTime(taskData.EndDate).ToString("MM/dd/yyyy "); ViewBag.Projects = this.GetProjects(); return(View(taskData)); }
/// <summary> /// /// </summary> /// <param name="fromDate"></param> /// <param name="wtData"></param> /// <returns></returns> public void GetWeekTasks(string fromDate, WeekTasks wtData, string project) { List <Task> tasks = null; if (!string.IsNullOrEmpty(fromDate)) { DateTime fromdt = Convert.ToDateTime(fromDate); DateTime todt = Convert.ToDateTime(fromDate).AddDays(6); tasks = this.db.Tasks.Include("Project").Include("Employee").Include("WorkCodesActivity").Where(x => x.ExecutionDate >= fromdt && x.ExecutionDate <= todt).OrderBy(x => x.ExecutionDate).ToList(); if (tasks != null) { if (!string.IsNullOrEmpty(project)) { tasks = tasks.Where(x => x.Project.Name == project).ToList(); } } var wt = from f in tasks group f by new { f.EmployeeId } into myGroup select new { myGroup.Key.EmployeeId, EmployeeFirstName = myGroup.Select(h => h.Employee.FirstName).FirstOrDefault(), EmployeeLastName = myGroup.Select(h => h.Employee.LastName).FirstOrDefault(), Date1 = myGroup.Where(f => f.ExecutionDate == fromdt).Sum(c => c.Hours), Date2 = myGroup.Where(f => f.ExecutionDate == fromdt.AddDays(1)).Sum(c => c.Hours), Date3 = myGroup.Where(f => f.ExecutionDate == fromdt.AddDays(2)).Sum(c => c.Hours), Date4 = myGroup.Where(f => f.ExecutionDate == fromdt.AddDays(3)).Sum(c => c.Hours), Date5 = myGroup.Where(f => f.ExecutionDate == fromdt.AddDays(4)).Sum(c => c.Hours), Date6 = myGroup.Where(f => f.ExecutionDate == fromdt.AddDays(5)).Sum(c => c.Hours), Date7 = myGroup.Where(f => f.ExecutionDate == fromdt.AddDays(6)).Sum(c => c.Hours), TotalHours = myGroup.Sum(c => c.Hours) }; wtData.StartDate = fromdt; wtData.EndDate = todt; if (wt != null) { DataTable dt = new DataTable(); dt.Columns.Add("EmployeeId"); dt.Columns.Add("FirstName"); dt.Columns.Add("LastName"); dt.Columns.Add(fromdt.ToString("MM/dd/yyyy"), typeof(decimal)); dt.Columns.Add(fromdt.AddDays(1).ToString("MM/dd/yyyy"), typeof(decimal)); dt.Columns.Add(fromdt.AddDays(2).ToString("MM/dd/yyyy"), typeof(decimal)); dt.Columns.Add(fromdt.AddDays(3).ToString("MM/dd/yyyy"), typeof(decimal)); dt.Columns.Add(fromdt.AddDays(4).ToString("MM/dd/yyyy"), typeof(decimal)); dt.Columns.Add(fromdt.AddDays(5).ToString("MM/dd/yyyy"), typeof(decimal)); dt.Columns.Add(fromdt.AddDays(6).ToString("MM/dd/yyyy"), typeof(decimal)); dt.Columns.Add("Total Hours", typeof(decimal)); DataRow row = null; foreach (var rowObj in wt) { row = dt.NewRow(); dt.Rows.Add(rowObj.EmployeeId , rowObj.EmployeeFirstName , rowObj.EmployeeLastName , rowObj.Date1 , rowObj.Date2 , rowObj.Date3 , rowObj.Date4 , rowObj.Date5 , rowObj.Date6 , rowObj.Date7 , rowObj.TotalHours); } wtData.ReportData = dt; } } }