Esempio n. 1
0
        public ActionResult AddCategoryToBudgetPlan(BudgetPlanCategoryDto bpc)
        //public ActionResult AddCategoryToBudgetPlan(int BudgetPlanId, int CategoryId, bool UsePercent, decimal? AllocatedAmount, decimal? AllocatedPercentage)
        {
            string errMessage = null;

            if (bpc.AllocatedAmount == null && bpc.AllocatedPercentage == null)
            {
                ModelState.AddModelError("Amount unset", "Either a dollar amount or a percentage must set for each budget category.");
            }
            if (ModelState.IsValid)
            {
                BudgetPlanningService s = new BudgetPlanningService();
                s.AddCategoryToBudgetPlan(bpc);
            }
            else
            {
                errMessage = "";
                foreach (var val in ModelState.Values)
                {
                    foreach (var err in val.Errors)
                    {
                        errMessage += "\n" + err.ErrorMessage;
                    }
                }
            }
            return(BudgetPlanCategoryListing(bpc.BudgetPlanId, errMessage));
        }
Esempio n. 2
0
        public ActionResult AddCategoryToBudgetPlan_Blank(int budgetPlanId)
        {
            BudgetPlanCategoryDto b = new BudgetPlanCategoryDto();
            LookupService         s = new LookupService();

            b.CategoryOptions = s.GetCategoryList(budgetPlanId);
            return(PartialView("AddCategoryToBudgetPlan", b));
        }
Esempio n. 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="dateOfInterest"></param>
        /// <returns></returns>
        public List <BudgetPlanCategoryDto> GetMonthActivitiesByCategory(DateTime dateOfInterest)
        {
            DateTime firstDayOfMonth          = new DateTime(dateOfInterest.Year, dateOfInterest.Month, 1);
            BudgetPlanningService        s    = new BudgetPlanningService();
            List <BudgetPlanCategoryDto> list = new List <BudgetPlanCategoryDto>();

            using (BudgetingEntities db = new BudgetingEntities())
            {
                int budgetPlanId = s.GetSelectedBudgetPlan();

                // initialize list
                list = s.GetBudgetCategoryList(budgetPlanId);
                foreach (BudgetPlanCategoryDto temp in list)
                {
                    temp.Activities = new List <ActivityDto>();
                }
                list.Add(new BudgetPlanCategoryDto {
                    CategoryName = "Spending outside of budget",
                    Activities   = new List <ActivityDto>()
                });

                // get all activities from this month
                List <ActivityDto> thisMonthsActivities = GetThisMonthsActivities(dateOfInterest);

                // sort this month's activities into the correct categories
                foreach (ActivityDto dto in thisMonthsActivities)
                {
                    if (dto.Pretax)
                    {
                        dto.Description = dto.Description + " (Pre-tax)";
                    }
                    BudgetPlanCategoryDto b = list.Find(x => x.CategoryId == dto.CategoryId);
                    if (b == null)
                    {
                        b = list.Find(x => x.CategoryId == 0);
                        if (b == null)
                        {
                            throw new Exception("Unable to add spending activity outside of budget plan.");
                        }
                    }
                    b.Activities.Add(dto);
                }

                // calculate spend information for each category
                foreach (BudgetPlanCategoryDto c in list)
                {
                    CalculateSpentAndRemaining(c);

                    // sort activities
                    c.Activities = c.Activities.OrderByDescending(x => x.DateOfActivity.Day).ToList();
                }

                // list sort
                list = list.OrderByDescending(x => x.SpendingStatus).ToList();
            }
            return(list);
        }
        public int AddCategoryToBudgetPlan(BudgetPlanCategoryDto dto)
        {
            BudgetPlanCategory bc = new BudgetPlanCategory();

            using (BudgetingEntities db = new BudgetingEntities())
            {
                //if (dto.CategoryId == 0)
                //{
                //    // add to category table
                //    Category c = new Category();
                //    c.UserId = UserId;
                //    c.Name = dto.Name;
                //    db.Categories.Add(c);
                //    db.Entry(c).State = System.Data.Entity.EntityState.Added;
                //    db.SaveChanges();
                //    dto.CategoryId = c.CategoryId;
                //}

                // add to budget table
                //decimal baseSalary = (from u in db.Users
                //                      where u.UserId == UserId
                //                      select u.BaseSalary)
                //                      .Single() ?? 0;
                //baseSalary = baseSalary / 12; // get month salary
                bc.CategoryId   = dto.CategoryId;
                bc.BudgetPlanId = dto.BudgetPlanId;
                bc.UsePercent   = dto.UsePercent;
                decimal tempVal;
                if (dto.UsePercent)
                {
                    if (Decimal.TryParse(dto.AllocatedPercentage, out tempVal))
                    {
                        bc.AllocatedPercentage = tempVal;
                        //bc.AllocatedAmount = (baseSalary == 0 ? null : (bc.AllocatedPercentage * baseSalary) / 100);
                    }
                }
                else
                {
                    if (Decimal.TryParse(dto.AllocatedAmount, out tempVal))
                    {
                        bc.AllocatedAmount = tempVal;
                        //bc.AllocatedPercentage = (baseSalary == 0 ? null : (bc.AllocatedAmount / baseSalary) * 100);
                    }
                }
                db.BudgetPlanCategories.Add(bc);
                db.Entry(bc).State = System.Data.Entity.EntityState.Added;
                db.SaveChanges();
            }
            return(bc.BudgetPlanCategoryId);
        }
 public void SaveBudgetPlanCategory(BudgetPlanCategoryDto dto)
 {
     using (BudgetingEntities db = new BudgetingEntities())
     {
         BudgetPlanCategory bpc = GetBudgetPlanCategory(dto.BudgetPlanCategoryId);
         if (!dto.UsePercent)
         {
             bpc.AllocatedAmount = Decimal.Parse(dto.AllocatedAmount);
         }
         else
         {
             bpc.AllocatedPercentage = Decimal.Parse(dto.AllocatedPercentage);
         }
         db.Entry(bpc).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
     }
 }
Esempio n. 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="c"></param>
        private void CalculateSpentAndRemaining(BudgetPlanCategoryDto c)
        {
            decimal runningSpentSum = 0;

            foreach (ActivityDto act in c.Activities)
            {
                //act.DateOfActivity = GetDateOfActivity(act, dateOfInterest);
                runningSpentSum += (act.Amount ?? 0);
            }
            c.SpentAmount    = runningSpentSum.ToString();
            c.SpendingStatus = CategorySpendingStatus.Neutral;
            decimal allocatedAmount;

            if (Decimal.TryParse(c.AllocatedAmount, out allocatedAmount))
            {
                if (allocatedAmount > 0)
                {
                    decimal percentage = (runningSpentSum / allocatedAmount) * 100;
                    percentage        = Decimal.Round(percentage, 2);
                    c.SpentPercentage = percentage.ToString();
                    if (percentage < 95)
                    {
                        c.SpendingStatus = CategorySpendingStatus.Green;
                    }
                    else if (percentage > 100)
                    {
                        c.SpendingStatus = CategorySpendingStatus.Red;
                    }
                    else
                    {
                        c.SpendingStatus = CategorySpendingStatus.Yellow;
                    }

                    decimal remainingAmount = allocatedAmount - runningSpentSum;
                    c.RemainingAmount     = Decimal.Round(remainingAmount, 2).ToString();
                    c.RemainingPercentage = Decimal.Round(((remainingAmount / allocatedAmount) * 100), 2).ToString();
                }
            }
            //return c;
        }
Esempio n. 7
0
        public ActionResult SavePercent(int budgetPlanCategoryId, string allocatedPercent)
        {
            BudgetPlanningService s     = new BudgetPlanningService();
            int            budgetPlanId = s.GetSelectedBudgetPlan();
            SummaryService ss           = new SummaryService();
            decimal        temp         = 0;

            allocatedPercent = allocatedPercent.Replace('%', (char)0);
            if (Decimal.TryParse(allocatedPercent, out temp))
            {
                BudgetPlanCategoryDto b = new BudgetPlanCategoryDto();
                b.BudgetPlanCategoryId = budgetPlanCategoryId;
                b.AllocatedPercentage  = allocatedPercent;
                b.UsePercent           = true;
                s.SaveBudgetPlanCategory(b);
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Allocated percentage cannot be parsed as a decimal number. Please make sure Percent is a number.");
            }
            return(BudgetPlanCategoryListing(budgetPlanId));
        }
Esempio n. 8
0
        public ActionResult SaveAmount(int budgetPlanCategoryId, string allocatedAmount)
        {
            BudgetPlanningService s = new BudgetPlanningService();
            int budgetPlanId        = s.GetSelectedBudgetPlan();

            allocatedAmount = allocatedAmount.TrimStart('$'); // get rid of the dollar signs
            decimal temp = 0;

            if (Decimal.TryParse(allocatedAmount, out temp))
            {
                BudgetPlanCategoryDto b = new BudgetPlanCategoryDto();
                b.BudgetPlanCategoryId = budgetPlanCategoryId;
                b.AllocatedAmount      = allocatedAmount;
                b.UsePercent           = false;
                s.SaveBudgetPlanCategory(b);
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Allocated amount cannot be parsed as a decimal number. Please make sure Amount is a number.");
            }
            return(BudgetPlanCategoryListing(budgetPlanId));
        }
Esempio n. 9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="budgetPlanCategoryId"></param>
        /// <param name="dateOfInterest"></param>
        /// <returns></returns>
        public BudgetPlanCategoryDto GetMonthCategoryActivities(int budgetPlanCategoryId, DateTime dateOfInterest)
        {
            BudgetPlanCategoryDto dto = new BudgetPlanCategoryDto();
            DateTime firstDayOfMonth  = new DateTime(dateOfInterest.Year, dateOfInterest.Month, 1);

            var summary = GetMonthActivitiesByCategory(DateTime.Now);

            dto = summary.Where(x => x.BudgetPlanCategoryId == budgetPlanCategoryId).Single();
            return(dto);
            //if (budgetPlanCategoryId == 0)
            //{
            //}
            //else
            //{
            //    dto.BudgetPlanCategoryId = budgetPlanCategoryId;
            //    using (BudgetingEntities db = new BudgetingEntities())
            //    {
            //        var searchingForBPC = (from bpc in db.BudgetPlanCategories
            //                               join cat in db.Categories on bpc.CategoryId equals cat.CategoryId
            //                               where bpc.BudgetPlanCategoryId == budgetPlanCategoryId
            //                               select new BudgetPlanCategoryDto
            //                               {
            //                                   BudgetPlanId = bpc.BudgetPlanId,
            //                                   BudgetPlanCategoryId = bpc.BudgetPlanCategoryId,
            //                                   CategoryId = cat.CategoryId,
            //                                   CategoryName = cat.Name,
            //                                   AllocatedAmount = (bpc.AllocatedAmount ?? 0).ToString(),
            //                                   AllocatedPercentage = (bpc.AllocatedPercentage ?? 0).ToString(),
            //                                   UsePercent = bpc.UsePercent
            //                               })
            //                               .ToList();
            //        if (!searchingForBPC.Any())
            //        {
            //            throw new Exception("Unable to find the budget plan category to delete from.");
            //        }
            //        if (searchingForBPC.Count > 1)
            //        {
            //            // TODO database shenanigans
            //        }
            //        dto = searchingForBPC.First();
            //        dto.Activities = (from a in db.Activities
            //                          where a.CategoryId == dto.CategoryId
            //                                && (a.DateOfActivity >= firstDayOfMonth
            //                                    || (a.Recurring == true
            //                                        && a.RecurringDay <= dateOfInterest.Day
            //                                        && a.DateOfActivity <= dateOfInterest))
            //                                && a.Active == true
            //                          select new ActivityDto
            //                          {
            //                              ActivityId = a.ActivityId,
            //                              CategoryId = a.CategoryId,
            //                              Amount = a.Amount,
            //                              Description = a.Description,
            //                              Recurring = a.Recurring ?? false,
            //                              RecurringDay = a.RecurringDay ?? 1,
            //                              DateOfActivity = a.DateOfActivity
            //                          })
            //                          .ToList();
            //    }
            //    dto.Activities = dto.Activities.OrderBy(x => x.DateOfActivity.Day).ToList();
            //}
            //CalculateSpentAndRemaining(dto);
            //return dto;
        }