public DashboardItem(BillHistory history)
 {
     Id = history.BillId;
     Name = history.Bill.Name;
     Date = history.DatePaid;
     Amount = history.Amount;
     IsPastDue = false;
     IsShared = history.Bill.Shared;
     Type = "Bill";
     IsPaid = true;
 }
        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);
                }
            }
        }
        /* =========================
         * Bill History Functions
         * ========================= */
        public ActionResult AddPayment(int id)
        {
            using (LinkToDBDataContext context = new LinkToDBDataContext())
            {
                Bill bill = context.GetBill(id);

                BillHistory history = new BillHistory();
                history.Amount = bill.Amount;
                history.DatePaid = bill.DueDate;
                history.Payee = bill.Payee;
                history.PaymentTypeId = bill.PaymentTypeId;
                history.IssueDate = bill.IssueDate;
                history.Bill = bill;

                return View(history);
            }
        }
        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);
                }
            }
        }
		private void detach_BillHistories(BillHistory entity)
		{
			this.SendPropertyChanging();
			entity.Bill = null;
		}
		private void attach_BillHistories(BillHistory entity)
		{
			this.SendPropertyChanging();
			entity.Bill = this;
		}
 partial void DeleteBillHistory(BillHistory instance);
 partial void UpdateBillHistory(BillHistory instance);
 partial void InsertBillHistory(BillHistory instance);
        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);
            }
        }