// GET: Budget public ActionResult Budget() { if (!User.Identity.IsAuthenticated) { return(RedirectToAction("login", "Home")); } UserRepository UserRepo = new UserRepository(Properties.Settings.Default._connectionString); User CurrentUser = UserRepo.GetUserByEmail(User.Identity.Name); BudgetRepository BudgetRepo = new BudgetRepository(Properties.Settings.Default._connectionString); var BudgetModel = new BudgetViewModel(); BudgetModel.CurrentUser = BudgetRepo.UserWithHistory(CurrentUser.Id); BudgetModel.MonthsBudgeted = BudgetRepo.MonthYearForUser(CurrentUser.Id); BudgetModel.AllCategories = BudgetRepo.GetAllCategories(); if (BudgetModel.MonthsBudgeted.Count != 0) { //gets debits for last month budgeted BudgetModel.DebitsForLatestMonth = BudgetRepo.GetDebitsByMonthId(CurrentUser.Id, BudgetModel.MonthsBudgeted[BudgetModel.MonthsBudgeted.Count - 1].Id); BudgetModel.CreditsForLastMonth = BudgetRepo.GetCreditsByMonthId(CurrentUser.Id, BudgetModel.MonthsBudgeted[BudgetModel.MonthsBudgeted.Count - 1].Id); BudgetModel.LatestMonth = BudgetRepo.GetMonthInfo(BudgetModel.MonthsBudgeted[BudgetModel.MonthsBudgeted.Count - 1].Id); } return(View(BudgetModel)); }
public ActionResult SwitchMonth(int NewMonthId) { if (!User.Identity.IsAuthenticated) { return(RedirectToAction("login", "Home")); } var UserRepo = new UserRepository(Properties.Settings.Default._connectionString); var budgetRepo = new BudgetRepository(Properties.Settings.Default._connectionString); var userId = UserRepo.GetUserByEmail(User.Identity.Name).Id; if (!budgetRepo.DoesMonthIdExistForUser(userId, NewMonthId)) { return(Json(0)); } List <Credit> Credits = budgetRepo.GetCreditsByMonthId(userId, NewMonthId); decimal TotalCredit = Credits.Sum(C => C.Amount); List <Debit> Debits = budgetRepo.GetDebitsByMonthId(userId, NewMonthId); return(Json(new { credits = Credits.Select(C => new { Id = C.Id, Amount = C.Amount.ToString("C"), Date = C.Date, Source = C.Source }), creditTotal = Credits.Sum(C => C.Amount).ToString("C"), debits = Debits.Select(D => new { id = D.Id, amount = D.Amount.ToString("C"), date = D.Date, category = D.Category.CategoryName, detail = D.Details }), debitTotal = Debits.Sum(D => D.Amount).ToString("C") }, JsonRequestBehavior.AllowGet)); }
// returns list of credits and sum of credits public ActionResult AddCredit(Credit credit) { if (!User.Identity.IsAuthenticated) { return(RedirectToAction("login", "Home")); } var UserRepo = new UserRepository(Properties.Settings.Default._connectionString); var budgetRepo = new BudgetRepository(Properties.Settings.Default._connectionString); credit.UserId = UserRepo.GetUserByEmail(User.Identity.Name).Id; budgetRepo.AddCredit(credit); List <Credit> Credits = budgetRepo.GetCreditsByMonthId(credit.UserId, credit.MonthId); return(Json(new { credits = Credits.Select(C => new { Id = C.Id, Amount = C.Amount.ToString("C"), Date = C.Date, Source = C.Source }), creditTotal = Credits.Sum(C => C.Amount).ToString("C") })); }