Пример #1
0
        public static List<DailySummary> GetDailySummary(int daysInFuture)
        {
            MoneyTrakrEntities db = new MoneyTrakrEntities();

            DateTime startDate = db.Transactions.Where(t => t.StartTransaction).FirstOrDefault().CreatedDate.Date;
            var recurrenceDates = new Dictionary<DateTime,List<TransactionItem>>();
            DateTime today = DateTime.Now.Date;

            DateTime maxDate = DateTime.Now.AddDays(daysInFuture);
            foreach (var recur in db.Recurrings)
            {
                DateTime now = today;
                int daysBetweenPayment = 0;

                if (recur.RecurBiWeekly)
                {
                    daysBetweenPayment = 14;
                }
                else if (recur.RecurWeekly)
                {
                    daysBetweenPayment = 7;
                }

                if (recur.RecurMontly)
                {

                    while (now <= maxDate)
                    {
                        DateTime recurringDate = DateTime.Parse(String.Format("{0}/{1}/{2}", now.Month, recur.StartDate.Day, now.Year));
                        if (recurringDate.Date > DateTime.Now.Date){
                            if (!recurrenceDates.ContainsKey(recurringDate))
                            {
                                recurrenceDates.Add(recurringDate, new List<TransactionItem>());
                            }

                        recurrenceDates[recurringDate].Add(new TransactionItem(recur) { CreatedDate = recurringDate });
                        }
                       // recurringDate = recurringDate.AddMonths(1);
                        now = now.AddMonths(1);
                    }
                }
                else
                {
                    while (now <= maxDate)
                    {

                        if (now.Date > DateTime.Now.Date)
                        {
                            if (!recurrenceDates.ContainsKey(now))
                            {
                                recurrenceDates.Add(now, new List<TransactionItem>());
                            }

                            recurrenceDates[now].Add(new TransactionItem(recur) { CreatedDate = now });
                        }

                        now = now.AddDays(daysBetweenPayment);
                    }
                }
            }

            foreach(var t in db.Transactions){

                if(!recurrenceDates.ContainsKey(t.CreatedDate.Date)){
                    recurrenceDates.Add(t.CreatedDate.Date,new List<TransactionItem>());
                }

                recurrenceDates[t.CreatedDate.Date].Add(new TransactionItem(t));
            }

            List<DailySummary> dailySummaries = new List<DailySummary>();
            for (int i = 0; i < daysInFuture; i++)
            {
                DailySummary ds = new DailySummary();
                var currentDay = startDate.AddDays(i);
                ds.Date = currentDay;
                if (recurrenceDates.ContainsKey(currentDay))
                {
                    ds.TransactionItems.AddRange(recurrenceDates[currentDay].OrderByDescending(x => x.IsStartAmount));
                }
                decimal todaysMoney = ds.TransactionItems.Sum(t=>t.Amount);
                if(i>0){
                    ds.EndingDayBalance = todaysMoney + dailySummaries[i-1].EndingDayBalance;
                }else{
                    ds.EndingDayBalance = todaysMoney;
                }
                dailySummaries.Add(ds);
            }

            return dailySummaries.OrderBy(x => x.Date).ToList();
        }
Пример #2
0
        public static List<ProjectionData> GetGroupedProjectionData(int futureMonths)
        {
            MoneyTrakrEntities db = new MoneyTrakrEntities();
            List<TransactionItem> AllLineItems = new List<TransactionItem>();

            DateTime startDate = db.Transactions.OrderBy(x => x.CreatedDate).FirstOrDefault().CreatedDate;

            foreach (Transaction trans in db.Transactions.ToList())
            {
                AllLineItems.Add(new TransactionItem(trans));
            }
            foreach (RecurringItem recurs in RecurringItem.GetAllRecurringItems(DateTime.Now.AddDays(1).Date, futureMonths))
            {
               AllLineItems.Add(new TransactionItem(recurs));
            }
            List<TransactionItem> sortedItems = TransactionItem.CalculateTotals(AllLineItems.Where(y => y.CreatedDate >= startDate).OrderBy(x => x.CreatedDate).ToList());
            decimal cumulativeBalance = 0;
            List<DailySummary> summaries = new List<DailySummary>();
            for (int i = 0; i < sortedItems.Count; i++)
            {
                DailySummary ds = new DailySummary();
                ds.Date = sortedItems[i].CreatedDate;
                cumulativeBalance = cumulativeBalance + sortedItems[i].Amount;
                if (i >0)
                    ds.EndingDayBalance = sortedItems[i].Amount;
                else
                    ds.EndingDayBalance = sortedItems[i].Amount + cumulativeBalance;

            }

            List<ProjectionData> grouped = (from p in sortedItems
                          group p by new { month = p.CreatedDate.Month, year = p.CreatedDate.Year } into d
                          select new ProjectionData { Month = string.Format("{0}/{1}", d.Key.month, d.Key.year), Amount = d.Sum(s => s.RunningTotal) }).ToList();

            return grouped;
        }