public ActionResult Add(Bill collection)
        {
            using (LinkToDBDataContext context = new LinkToDBDataContext())
            {
                try
                {
                    Bill bill = new Bill();
                    bill.CreationDate = DateTime.Now;
                    bill.ModifyDate = DateTime.Now;
                    bill.Version = 1;
                    bill.UserId = 1;
                    bill.PaymentTypeId = 4;
                    bill.IsActive = true;

                    bill.Amount = collection.Amount;
                    bill.DueDate = collection.DueDate;
                    bill.IssueDate = collection.IssueDate;
                    bill.Name = collection.Name;
                    bill.Payee = collection.Payee;
                    bill.Shared = collection.Shared;
                    bill.StaysSame = collection.StaysSame;

                    context.Bills.InsertOnSubmit(bill);
                    context.SubmitChanges();

                    return RedirectToAction("Index");
                }
                catch
                {
                    return View(collection);
                }
            }
        }
        public ActionResult Add(Loan collection)
        {
            using (LinkToDBDataContext context = new LinkToDBDataContext())
            {
                try
                {
                    Loan loan = new Loan();
                    loan.CreationDate = DateTime.Now;
                    loan.ModifyDate = DateTime.Now;
                    loan.Version = 1;
                    loan.UserId = 1;
                    loan.PaymentTypeId = 4;
                    loan.IsActive = true;

                    loan.Name = collection.Name;
                    loan.FirstPaymentDate = collection.FirstPaymentDate;
                    loan.LoanAmount = collection.LoanAmount;
                    loan.InterestRate = collection.InterestRate;
                    loan.Term = collection.Term;
                    loan.AddPayment = collection.AddPayment;
                    loan.Escrow = collection.Escrow;
                    loan.InterestCompDaily = false;
                    loan.InterestCompMonthly = true;

                    context.Loans.InsertOnSubmit(loan);
                    context.SubmitChanges();

                    return RedirectToAction("Index");
                }
                catch
                {
                    return View(collection);
                }
            }
        }
        public ActionResult AddPayment(LoanHistory collection)
        {
            using (LinkToDBDataContext context = new LinkToDBDataContext())
            {
                try
                {
                    LoanHistory history = new LoanHistory();
                    history.CreationDate = DateTime.Now;
                    history.ModifyDate = DateTime.Now;
                    history.Version = 1;
                    history.LoanId = collection.Id;
                    history.DatePaid = collection.DatePaid;
                    history.BasicPayment = collection.BasicPayment;
                    history.AddPayment = collection.AddPayment;
                    history.Interest = collection.Interest;
                    history.Escrow = collection.Escrow;
                    history.PaymentTypeId = collection.PaymentTypeId;

                    Loan loan = context.GetLoan(history.LoanId);
                    loan.LoanHistories.Add(history);

                    context.SubmitChanges();

                    return RedirectToAction("Edit", new { id = history.LoanId });
                }
                catch
                {
                    return View(collection);
                }
            }
        }
        public ActionResult EditPayment(BillHistory collection)
        {
            using (LinkToDBDataContext context = new LinkToDBDataContext())
            {
                BillHistory history = context.GetBillHistoryItem(collection.Id);

                try
                {
                    history.ModifyDate = DateTime.Now;
                    history.Version += 1;
                    history.DatePaid = collection.DatePaid;
                    history.Amount = collection.Amount;
                    history.Payee = collection.Payee;
                    history.IssueDate = collection.IssueDate;

                    context.SubmitChanges();

                    return RedirectToAction("Edit", new { id = history.BillId });
                }
                catch
                {
                    return View(history);
                }
            }
        }
        public ActionResult Edit(int id, Loan collection, string button)
        {
            ViewBag.Calculate = false;
            using (LinkToDBDataContext context = new LinkToDBDataContext())
            {
                Loan loan = context.GetLoan(id);

                try
                {
                    loan.ModifyDate = DateTime.Now;
                    loan.Version += 1;
                    loan.Name = collection.Name;
                    loan.FirstPaymentDate = collection.FirstPaymentDate;
                    loan.LoanAmount = collection.LoanAmount;
                    loan.InterestRate = collection.InterestRate;
                    loan.Term = collection.Term;
                    loan.AddPayment = collection.AddPayment;
                    loan.Escrow = collection.Escrow;

                    switch (button)
                    {
                        case "Calculate":
                            loan = loan.LoadLoan();
                            ViewBag.Calculate = true;
                            return View(loan);
                        case "Save":
                            context.SubmitChanges();
                            return RedirectToAction("Index");
                        default:
                            return View(loan);
                    }
                }
                catch
                {
                    return View(loan);
                }
            }
        }
        public ActionResult AddPayment(BillHistory collection)
        {
            using (LinkToDBDataContext context = new LinkToDBDataContext())
            {
                try
                {
                    BillHistory history = new BillHistory();
                    history.CreationDate = DateTime.Now;
                    history.ModifyDate = DateTime.Now;
                    history.Version = 1;
                    history.BillId = collection.Id;

                    Bill bill = context.GetBill(history.BillId);
                    history.Bill = bill;

                    history.Amount = collection.Amount;
                    history.DatePaid = collection.DatePaid;
                    history.Payee = collection.Payee;
                    history.PaymentTypeId = collection.PaymentTypeId;
                    history.IssueDate = collection.IssueDate;

                    bill.BillHistories.Add(history);

                    if (bill.StaysSame || bill.BillHistoryAverage == null)
                    {
                        bill.DueDate = bill.DueDate.AddMonths(1);
                    }
                    else
                    {
                        IEnumerable<BillHistoryAverage> bha = bill.BillHistoryAverage.Where(x => x.Month.Month == bill.DueDate.AddMonths(1).Month);
                        if (bha.Any())
                        {
                            bill.DueDate = bha.FirstOrDefault().Month;
                            bill.Amount = bha.FirstOrDefault().Average;
                        }
                        else
                        {
                            bill.DueDate = bill.DueDate.AddMonths(1);
                        }
                    }

                    context.SubmitChanges();

                    return RedirectToAction("Edit", new { id = history.BillId });
                }
                catch
                {
                    return View(collection);
                }
            }
        }
        public ActionResult Paid(int id)
        {
            using (LinkToDBDataContext context = new LinkToDBDataContext())
            {
                BillHistory history = new BillHistory();
                history.CreationDate = DateTime.Now;
                history.ModifyDate = DateTime.Now;
                history.Version = 1;
                history.BillId = id;

                Bill bill = context.GetBill(id);

                history.Amount = bill.Amount;
                history.DatePaid = bill.DueDate;
                history.Payee = bill.Payee;
                history.PaymentTypeId = bill.PaymentTypeId;
                history.IssueDate = bill.IssueDate;
                history.Bill = bill;

                if (bill.StaysSame || bill.BillHistoryAverage == null)
                {
                    bill.DueDate = bill.DueDate.AddMonths(1);
                }
                else
                {
                    IEnumerable<BillHistoryAverage> bha = bill.BillHistoryAverage.Where(x => x.Month.Month == bill.DueDate.AddMonths(1).Month);
                    if (bha.Any())
                    {
                        bill.DueDate = bha.FirstOrDefault().Month;
                        bill.Amount = bha.FirstOrDefault().Average;
                    }
                    else
                    {
                        bill.DueDate = bill.DueDate.AddMonths(1);
                    }
                }

                context.SubmitChanges();
                return RedirectToAction("Index", "Home", null);
            }
        }
        public ActionResult Edit(int id, Bill collection)
        {
            using (LinkToDBDataContext context = new LinkToDBDataContext())
            {
                Bill bill = context.GetBill(id);

                try
                {
                    bill.ModifyDate = DateTime.Now;
                    bill.Version += 1;
                    bill.Name = collection.Name;
                    bill.Payee = collection.Payee;
                    bill.DueDate = collection.DueDate;
                    bill.Amount = collection.Amount;
                    bill.IssueDate = collection.IssueDate;
                    bill.StaysSame = collection.StaysSame;
                    bill.Shared = collection.Shared;

                    context.SubmitChanges();

                    return RedirectToAction("Index");
                }
                catch
                {
                    return View(bill);
                }
            }
        }