public ActionResult Index() { using (LinkToDBDataContext context = new LinkToDBDataContext()) { DashboardViewModel viewModel = new DashboardViewModel(); IEnumerable<Bill> bills = context.GetBills(); IEnumerable<Loan> loans = context.GetLoans(); DateTime startDate = DateTime.Now; foreach (Bill bill in bills) { startDate = (bill.DueDate <= startDate) ? bill.DueDate : startDate; } foreach (Loan loan in loans) { startDate = (loan.DueDate <= startDate) ? loan.DueDate : startDate; } if (startDate.Day > 1) { //startDate = startDate.AddMonths(-1); startDate = new DateTime(startDate.Year, startDate.Month, 1); } viewModel.DateRanges = InitiateDateRanges(startDate); foreach (DashboardDateRange range in viewModel.DateRanges) { List<DashboardItem> items = new List<DashboardItem>(); foreach (Bill bill in bills) { if (bill.DueDate >= range.StartDate && bill.DueDate <= range.EndDate) { items.Add(new DashboardItem(bill)); } if (bill.StaysSame || !bill.BillHistories.Any()) { for (int i = 1; i <= 3; i++) { DateTime date = bill.DueDate.AddMonths(i); if (date >= range.StartDate && date <= range.EndDate) { DashboardItem item = new DashboardItem(bill); item.Date = date; item.IsPastDue = item.DueInDays < 0; items.Add(item); } } } else { Bill difBill = context.GetBill(bill.Id); foreach (BillHistoryAverage bha in difBill.BillHistoryAverage) { if (bha.Month >= range.StartDate && bha.Month <= range.EndDate && bha.Month.Month != bill.DueDate.Month) { DashboardItem item = new DashboardItem(bha); item.Id = bill.Id; item.Name = bill.Name; items.Add(item); } } } if (bill.BillHistories.Any()) { foreach (BillHistory history in bill.BillHistory) { if (history.DatePaid >= range.StartDate && history.DatePaid <= range.EndDate) { items.Add(new DashboardItem(history)); } } } } foreach (Loan loan in loans) { foreach (LoanOutlook outlook in loan.LoanOutlook) { if (outlook.Date >= range.StartDate && outlook.Date <= range.EndDate) { DashboardItem dbItem = new DashboardItem(outlook); dbItem.Name = loan.Name; dbItem.Id = loan.Id; items.Add(dbItem); } } foreach (LoanHistory history in loan.LoanHistory) { if (history.DatePaid >= range.StartDate && history.DatePaid <= range.EndDate) { items.Add(new DashboardItem(history)); } } } range.Items = items.OrderBy(x => x.Date); } return View(viewModel); } }
/* ========================= * Bill Functions * ========================= */ public ActionResult Index() { using (LinkToDBDataContext context = new LinkToDBDataContext()) { return View(context.GetBills().Where(x => x.IsActive == true && x.DueInDays <= 45).OrderBy(x => x.DueDate)); } }