public void ShowBudgetReport() { BudgetReport window = new BudgetReport(); BudgetReportViewModel vm = new BudgetReportViewModel(); window.DataContext = vm; window.Owner = mainWindow; window.ShowDialog(); }
public async Task <IActionResult> SpendingReport(int ListTypeId, int Year) { var user = await GetCurrentUserAsync(); var giftLists = await _context.GiftLists .Include(gl => gl.GiftListItems) .ThenInclude(gli => gli.Item) .Where(gl => gl.CreatorId == user.Id && gl.ListTypeId == ListTypeId && gl.DateNeeded.Year == Year) .ToListAsync(); foreach (var gl in giftLists) { gl.AmountSpent = gl.GiftListItems.Select(gli => gli.Item.PurchasedAmount).Sum(); } var viewModel = new BudgetReportViewModel { ListTypeId = ListTypeId, ListType = await _context.ListTypes.FindAsync(ListTypeId), Year = Year, GiftLists = giftLists, TotalAmountSpent = giftLists.Select(gl => gl.AmountSpent).Sum(), TotalBudget = giftLists.Select(gl => gl.Budget).Sum() }; if (viewModel.ListTypeId == 0 || viewModel.Year == 0) { TempData["SpendingErrorMessage"] = "Please select a category and year."; return(RedirectToAction(nameof(Index))); } if (viewModel.GiftLists.Count() == 0) { TempData["SpendingErrorMessage"] = "No report for the selected category and year. Please try again."; return(RedirectToAction(nameof(Index))); } return(View(viewModel)); }