public ActionResult InsertSavingsGoal(SavingsGoalsViewModel model) { SavingsGoal newSavingsGoal = new SavingsGoal(); newSavingsGoal = model.savingsGoal; newSavingsGoal.ClientID = CLIENT_ID; newSavingsGoal.SavingsPointValue = 0; newSavingsGoal.Status = "Active"; if (newSavingsGoal.SavingGoalID == 0) { newSavingsGoal.StartDate = model.savingsGoal.StartDate; newSavingsGoal.CurrentGoalAmount = 0; newSavingsGoal.SavingsGoalAmount = model.savingsGoal.SavingsGoalAmount; newSavingsGoal.EndDate = model.savingsGoal.EndDate; newSavingsGoal.GoalDescription = model.savingsGoal.GoalDescription; newSavingsGoal.Recurring = model.savingsGoal.Recurring; dbContext.SavingsGoals.Add(newSavingsGoal); ModelState.Remove("savingsGoal.SavingGoalId"); //remove the error saying that this id is needed, when it will be automatically generated on insert } else { dbContext.Entry(newSavingsGoal).State = EntityState.Modified; } if (ModelState.IsValid) { dbContext.SaveChanges(); return(RedirectToAction("index")); } return(View(model)); }
// GET: SavingsGoal public ActionResult Index() { if (Session["UserID"] == null) { return(RedirectToAction("Login", "Home")); } else { CLIENT_ID = int.Parse(Session["UserID"].ToString()); } List <SavingsGoal> SavingsGoalList = new List <SavingsGoal>(); checkGoalStatus(); //trigger for all savings goals passed their end date //any messages to move on to the page? if (TempData["SuccessTitle"] != null) { ViewBag.SuccessTitle = TempData["SuccessTitle"]; ViewBag.SuccessBody = TempData["SuccessBody"]; } SavingsGoalList = dbContext.SavingsGoals.Where(x => x.ClientID == CLIENT_ID && x.Status == "Active").OrderByDescending(x => x.CurrentGoalAmount / x.SavingsGoalAmount).ToList(); SavingsGoalsViewModel savingsGoal = new SavingsGoalsViewModel(); savingsGoal.savingsView = SavingsGoalList; savingsGoal.savingsViewFails = dbContext.SavingsGoals.Where(x => x.ClientID == CLIENT_ID && x.Status == "Fail").ToList(); savingsGoal.savingsViewSuccesses = dbContext.SavingsGoals.Where(x => x.ClientID == CLIENT_ID && x.Status == "Success").ToList(); savingsGoal.totalBudgeted = savingsGoal.savingsView.Select(x => x).Where(x => x.SavingGoalID != 0).Sum(x => Convert.ToDouble(x.SavingsGoalAmount)); savingsGoal.totalSaved = savingsGoal.savingsView.Select(x => x).Sum(x => Convert.ToDouble(x.CurrentGoalAmount)); addBadge(); return(View(savingsGoal)); }
public ActionResult AddFunds(int?id) { if (Session["UserID"] == null) { return(RedirectToAction("Login", "Home")); } else { CLIENT_ID = int.Parse(Session["UserID"].ToString()); } if (id == null) //Only allow updates to a valid goal { return(RedirectToAction("index")); } SavingsGoal goal = dbContext.SavingsGoals.Where(x => x.ClientID == CLIENT_ID && x.SavingGoalID == id).FirstOrDefault(); if (goal == null) //allow only valid updates { return(RedirectToAction("index")); } SavingsGoalsViewModel model = new SavingsGoalsViewModel(); model.clientTransaction = new HomeController().getTransactionInfo(); //get total amount available on each client's accounts model.savingsGoal = goal; return(View(model)); }
private SavingsGoalsViewModel getSavingGoals() { List <SavingsGoal> SavingsGoalList = new List <SavingsGoal>(); SavingsGoalList = dbContext.SavingsGoals.Where(x => x.ClientID == CLIENT_ID && x.Status == "Active").OrderByDescending(x => x.CurrentGoalAmount / x.SavingsGoalAmount).ToList(); SavingsGoalsViewModel savingsGoal = new SavingsGoalsViewModel(); savingsGoal.savingsView = SavingsGoalList; savingsGoal.totalBudgeted = savingsGoal.savingsView.Select(x => x).Where(x => x.SavingGoalID != 0).Sum(x => Convert.ToDouble(x.SavingsGoalAmount)); savingsGoal.totalSaved = savingsGoal.savingsView.Select(x => x).Sum(x => Convert.ToDouble(x.CurrentGoalAmount)); return(savingsGoal); }
public ActionResult InsertSavingsGoal(int?id) { if (Session["UserID"] == null) { return(RedirectToAction("Login", "Home")); } else { CLIENT_ID = int.Parse(Session["UserID"].ToString()); } SavingsGoalsViewModel goal = new SavingsGoalsViewModel(); if (id == null) { return(View(goal)); } goal.savingsGoal = dbContext.SavingsGoals.Find(id); return(View(goal)); }
public ActionResult AddFunds(SavingsGoalsViewModel model) { if (model.addToGoal + model.savingsGoal.CurrentGoalAmount > model.savingsGoal.SavingsGoalAmount) { //Trying to add more than specified goal amount error ModelState.AddModelError("addToGoal", "The amount you want to add cannot exceed the savings goal limit."); } if (model.addToGoal <= 0) { //less than 0 transaction error ModelState.AddModelError("addToGoal", "The amount you want to enter cannot be less than 0."); } if (model.addToGoal > dbContext.Transactions.Where(x => x.TransactionAccountNo == model.transaction.TransactionAccountNo).Sum(x => x.TransactionAmount)) { //prevents user from overdrafting ModelState.AddModelError("addToGoal", "The amount you want to enter cannot exceed account balance."); } SavingsGoal goal = dbContext.SavingsGoals.Where(x => x.SavingGoalID == model.savingsGoal.SavingGoalID).FirstOrDefault(); if (goal == null) { //error getting goal to update ModelState.AddModelError("addToGoal", "Error trying to add to this goal."); } if (ModelState.IsValid) { Transaction trans = new Transaction(); trans.TransactionAccountNo = model.transaction.TransactionAccountNo; trans.TransactionAmount = model.addToGoal * -1; trans.TransactionDate = DateTime.Now; trans.Description = "Added to goal: " + model.savingsGoal.GoalDescription; trans.CategoryID = 17; goal.CurrentGoalAmount = goal.CurrentGoalAmount + model.addToGoal; if (goal.CurrentGoalAmount >= goal.SavingsGoalAmount) //User just achieved goal { goal.Status = "Success"; TimeSpan difference = goal.EndDate.Subtract(DateTime.Now); TempData["SuccessTitle"] = "Congratulations! \"" + goal.GoalDescription + "\" Completed!"; TempData["SuccessBody"] = "You just achieved your goal of saving " + goal.CurrentGoalAmount + " by " + goal.EndDate.ToLongDateString() + ". You finished this " + difference.Days + " Days before your target date!"; if (goal.Recurring.Trim().ToUpper() == "YES") { SavingsGoal newSavingGoal = new SavingsGoal(); newSavingGoal.Status = "Active"; newSavingGoal.StartDate = goal.StartDate.AddMonths(1); newSavingGoal.EndDate = goal.EndDate.AddMonths(1); newSavingGoal.GoalDescription = goal.GoalDescription; newSavingGoal.Recurring = goal.Recurring; newSavingGoal.SavingsGoalAmount = goal.SavingsGoalAmount; newSavingGoal.CurrentGoalAmount = 0; newSavingGoal.ClientID = goal.ClientID; newSavingGoal.SavingsPointValue = 0; dbContext.SavingsGoals.Add(newSavingGoal); } } dbContext.Transactions.Add(trans); dbContext.SaveChanges(); return(RedirectToAction("index")); } //repopluate the transactions for the dropdown list model.clientTransaction = new HomeController().getTransactionInfo(); return(View(model)); }