public ActionResult Create(Transaction tr) { var accId = (int)TempData["AccId"]; if(ModelState.IsValid) { tr.AccountId = accId; var user = db.Users.Find(User.Identity.GetUserId()); tr.UpdatedBy = user.Name; db.Transactions.Add(tr); var acc = db.Accounts.Find(accId); var cat = db.Categories.Find(tr.CategoryId); if(cat.CategoryTypeId == 2) // Expense { acc.Balance -= tr.Amount; } else { acc.Balance += tr.Amount; } db.Entry(acc).Property("Balance").IsModified = true; db.SaveChanges(); if(acc.Balance <100) { ViewBag.Note = "Your account balance is less than $100."; } return RedirectToAction("Details", "Accounts", new { id = accId}); } var hh = db.Households.Find(Convert.ToInt32(User.Identity.GetHouseholdId())); ViewBag.CategoryId = new SelectList(hh.Categories, "Id", "Name",tr.CategoryId); return View(tr); //return RedirectToAction("Details", "Accounts", new { Id = accId, tr }); }
// GET: Transactions/_Transactions public PartialViewResult _Transactions(int? bankAccountId) { var user = db.Users.Find(User.Identity.GetUserId()); BankAccount bankAccounts = db.BankAccountData.Find(bankAccountId); Transaction transactions = new Transaction(); var isEdited = bankAccounts.Transactions.Where(e => e.DateEdited != null); var transaction = bankAccounts.Transactions.Where(t => t.IsDeleted == false).ToList().OrderByDescending(o => o.DateCreated); var transactionEdited = bankAccounts.Transactions.Where(t => t.IsDeleted == false).ToList().OrderByDescending(o => o.DateEdited); if(transactions.DateEdited != null) { return PartialView(transactionEdited); } else { return PartialView(transaction); } //return PartialView(transaction); }
public ActionResult Edit(Transaction transaction) { var user = db.Users.Find(User.Identity.GetUserId()); var household = db.HouseHoldData.Where(u => u.Id == user.HouseHoldId); var bankAccount = db.BankAccountData.Single(b => b.Id == transaction.BankAccountId); if(ModelState.IsValid) { transaction.DateEdited = System.DateTimeOffset.Now; transaction.UserId = user.Id; db.TransactionData.Attach(transaction); db.Entry(transaction).Property("CategoryId").IsModified = true; db.Entry(transaction).Property("BankAccountId").IsModified = true; db.Entry(transaction).Property("TransactionAmount").IsModified = true; db.Entry(transaction).Property("ReconsiliationAmount").IsModified = true; db.Entry(transaction).Property("TransactionDescription").IsModified = true; if(db.Entry(transaction).Property(t => t.TransactionAmount).IsModified == true) { if(transaction.TransactionAmount < 0 && transaction.IsWithdrawl == false) { transaction.IsWithdrawl = true; bankAccount.Balance = bankAccount.Balance + transaction.TransactionAmount; transaction.DateEdited = System.DateTimeOffset.Now; } else { transaction.IsWithdrawl = false; bankAccount.Balance = bankAccount.Balance + transaction.TransactionAmount; transaction.DateEdited = System.DateTimeOffset.Now; } } db.SaveChanges(); return RedirectToAction("Index", "Households"); } ViewBag.BankAccountId = new SelectList(db.BankAccountData, "Id", "Name", transaction.BankAccountId); ViewBag.CategoryId = new SelectList(db.CategoryData, "Id", "Name", transaction.CategoryId); ViewBag.UserId = new SelectList(db.Users, "Id", "FirstName", transaction.UserId); return View(transaction); }
public ActionResult Upload(int? id, HttpPostedFileBase fileCSV) { var hh = db.Households.Find(User.Identity.GetHouseholdId<int>()); if (fileCSV != null && fileCSV.ContentLength > 0) { //check the file name to make sure its an image var ext = Path.GetExtension(fileCSV.FileName).ToLower(); if (ext != ".csv") ModelState.AddModelError("fileCSV", "Invalid Format."); // throw an error } if (ModelState.IsValid) { if (fileCSV != null) { var filePath = "/Uploads/"; var absPath = Server.MapPath("~" + filePath); var FileUrl = filePath + fileCSV.FileName; ////save image filePath = Path.Combine(absPath, fileCSV.FileName); fileCSV.SaveAs(filePath); var fileContent = System.IO.File.ReadAllText(filePath); //char[] delim = {'\r', '\n'}; string [] records = fileContent.Split('\r','\n'); string[] fields={" "," "}; var acc = db.Accounts.Find(id); var balance = acc.Balance; foreach (var rec in records) { if(rec!="") { Transaction tr = new Transaction(); tr.TransDate = System.DateTimeOffset.Now; tr.AccountId = (int)id; var user = db.Users.Find(User.Identity.GetUserId()); tr.UpdatedBy = user.Name; fields = rec.Split(','); tr.Description = fields[0]; tr.Amount = Convert.ToDecimal(fields[1]); tr.RecAmount = Convert.ToDecimal(fields[1]); var type = fields[3]; var Category = hh.Categories.FirstOrDefault(c=>c.Name == fields[2]); if (Category != null) tr.CategoryId = Category.Id; else { if (type == "Expense") tr.CategoryId = hh.Categories.FirstOrDefault(z => z.Name == "MiscExpense").Id; else tr.CategoryId = hh.Categories.FirstOrDefault(z => z.Name == "MiscIncome").Id; } db.Transactions.Add(tr); var cat = db.Categories.Find(tr.CategoryId); if (cat.CategoryType.Name == "Expense") // Expense { balance -= tr.Amount; } else { balance += tr.Amount; } //db.SaveChanges(); } } acc.Balance = balance; db.Entry(acc).Property("Balance").IsModified = true; db.SaveChanges(); return RedirectToAction("Details", "Accounts", new { id = id }); } } return Json(" ", JsonRequestBehavior.AllowGet); }
public ActionResult GetChart() { ApplicationDbContext db = new ApplicationDbContext(); Transaction transaction = new Transaction(); HouseHold household = db.HouseHoldData.Find(Convert.ToInt32(User.Identity.GetHouseholdid())); var withdrawlChartArray = (from c in household.Categories where c.IsDeposit == false let sum = (from b in household.BudgetItems where b.CategoryId == c.Id select b.Amount).DefaultIfEmpty().Sum() select new { label = c.Name, data = sum }).ToArray(); var depositChartArray = (from c in household.Categories where c.IsDeposit == true let sum = (from b in household.BudgetItems where b.CategoryId == c.Id select b.Amount).DefaultIfEmpty().Sum() select new { label = c.Name, data = sum }).ToArray(); var jsonData = new { Expense = withdrawlChartArray, Income = depositChartArray }; return Content(JsonConvert.SerializeObject(jsonData), "application/json"); }