public ActionResult GetReport(ReportViewModel report) { if (ModelState.IsValid) return View(repo.GetSales().AsReport(report.MinDate, report.MaxDate)); else return View(report); }
/// <summary> /// Make Reports A and B from Sales /// </summary> /// <param name="Sales">List of Sales</param> /// <param name="MinDate">Begin Date</param> /// <param name="MaxDate">End Date</param> /// <returns></returns> public static ReportViewModel AsReport(this IQueryable<Sale> Sales, DateTime MinDate, DateTime MaxDate) { List<Sale> filteredSales = Sales.Where(s => s.Date >= MinDate && s.Date <= MaxDate).ToList(); ReportViewModel reportViewModel = new ReportViewModel(); reportViewModel.TotalIncome = filteredSales.Sum(s => s.Quantity * s.Book.Price); reportViewModel.ReportA = (from author in filteredSales.GroupBy(e => new { e.Book.AuthorId, Author = e.Book.Author.FirstName + " " + e.Book.Author.LastName }) select new ReportARecord { Author = author.Key.Author, Quantity = filteredSales.Where(s => s.Book.AuthorId == author.Key.AuthorId).Sum(e => e.Quantity) }).ToList(); reportViewModel.ReportB = (from theme in filteredSales.GroupBy(e => new { e.Book.ThemeId, Theme = e.Book.Theme.ThemeName }) select new ReportBRecord { Theme = theme.Key.Theme, Quantity = filteredSales.Where(s => s.Book.ThemeId == theme.Key.ThemeId).Sum(e => e.Quantity) }).ToList(); return reportViewModel; }