Exemple #1
0
        public static List <DashboardItem> GetDashboardItems(this IEnumerable <Bill> bills, DashboardDateRange range, ApplicationUser user)
        {
            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));
                }

                DateTime date = bill.DueDate;
                while (date <= range.EndDate)
                {
                    Tuple <DateTime, decimal> newDateAmount = bill.GetDateAmount(bill.GetNextDate(date, user), bill.Amount);
                    date = newDateAmount.Item1;
                    if (newDateAmount.Item1 >= range.StartDate && date <= range.EndDate)
                    {
                        DashboardItem item = new DashboardItem(bill)
                        {
                            Date   = date,
                            Amount = newDateAmount.Item2
                        };
                        if (bill.NotOwner)
                        {
                            item.YouPay = bill.SharedWith.Where(x => x.SharedWithUser.Id == user.Id).FirstOrDefault().Amount;
                        }
                        else
                        {
                            item.YouPay = item.Amount - bill.SharedWith.Sum(x => x.Amount);
                        }
                        items.Add(item);
                    }
                }

                if (bill.BillPayments.Any())
                {
                    items.AddRange(bill.BillPayments.Where(x => x.DatePaid >= range.StartDate && x.DatePaid <= range.EndDate)
                                   .Where(x => x.User.Id == user.Id || (x.Bill.IsShared && x.SharedWith.Any() && x.SharedWith.Where(y => y.SharedWithUser.Id == user.Id).Any()))
                                   .Select(x => new DashboardItem(x, user)));
                }
            }

            return(items);
        }
        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);
            }
        }