// GET api/paycheckbudgetapi/5
        public PaycheckBudgetViewModel Get(int id)
        {
            var dbPaycheckBudget = _repo.FetchById(id);
            var items            = _repo.FetchAllItemsForPaycheckBudget(id);
            var itemTotal        = items.Sum(x => x.Amount);

            var vm = new PaycheckBudgetViewModel
            {
                PaycheckBudgetId = id,
                Date             = dbPaycheckBudget.PaycheckBudgetDate.ToShortDateString(),
                Amount           = dbPaycheckBudget.PaycheckBudgetAmount,
                Remaining        = dbPaycheckBudget.PaycheckBudgetAmount - itemTotal,
                Items            = items.Select(x => new PaycheckBudgetItemViewModel
                {
                    PaycheckBudgetId     = id,
                    PaycheckBudgetItemId = x.PaycheckBudgetItemId,
                    Description          = x.Description,
                    Amount     = x.Amount,
                    DueDate    = x.PaidDate.ToShortDateString(),
                    PaidStatus = x.IsPaid,
                    IsPaid     = (x.IsPaid) ? "Paid" : "Unpaid"
                })
            };

            return(vm);
        }
Ejemplo n.º 2
0
        public PaycheckBudgetResponse AddBudget(PaycheckBudgetViewModel budgetViewModel)
        {
            var budget = new Budget
            {
                BudgetAmount = budgetViewModel.Amount,
                BudgetDate   = budgetViewModel.Date
            };

            foreach (var budgetItem in budgetViewModel.BudgetItems)
            {
                budget.BudgetItems.Add(new BudgetItem
                {
                    Amount      = budgetItem.Amount,
                    Description = budgetItem.Description,
                    IsPaid      = budgetItem.IsPaid,
                    DueDate     = budgetItem.DueDate,
                    DatePaid    = budgetItem.DueDate                  // When adding a new budget, the date paid has to have a valid date, so give it the due date.
                });
            }

            _db.Budgets.AddObject(budget);
            _db.SaveChanges();

            return(GetCurrentBudgets());
        }
        // GET api/paycheckbudgetapi
        public IEnumerable <PaycheckBudgetViewModel> Get()
        {
            var paycheckBudgets          = _repo.FetchAll().ToList();
            var paycheckBudgetViewModels = new List <PaycheckBudgetViewModel>();

            foreach (var paycheckBudget in paycheckBudgets)
            {
                var items     = _repo.FetchAllItemsForPaycheckBudget(paycheckBudget.PaycheckBudgetId).ToList();
                var itemTotal = items.Sum(pbi => pbi.Amount);

                var vm = new PaycheckBudgetViewModel
                {
                    PaycheckBudgetId = paycheckBudget.PaycheckBudgetId,
                    Date             = paycheckBudget.PaycheckBudgetDate.ToShortDateString(),
                    Amount           = paycheckBudget.PaycheckBudgetAmount,
                    Remaining        = paycheckBudget.PaycheckBudgetAmount - itemTotal,
                    Items            = items.Select(pbi => new PaycheckBudgetItemViewModel
                    {
                        PaycheckBudgetItemId = pbi.PaycheckBudgetItemId,
                        PaycheckBudgetId     = pbi.PaycheckBudgetId,
                        Description          = pbi.Description,
                        Amount     = pbi.Amount,
                        DueDate    = pbi.PaidDate.ToShortDateString(),
                        PaidStatus = pbi.IsPaid,
                        IsPaid     = (pbi.IsPaid) ? "Paid" : "Unpaid"
                    })
                };

                paycheckBudgetViewModels.Add(vm);
            }

            return(paycheckBudgetViewModels);
        }
Ejemplo n.º 4
0
        private PaycheckBudgetResponse GetBudgets(DateTime offset)
        {
            var budgetResponse = new PaycheckBudgetResponse
            {
                PagingInfo = new PagingInfo(),
                Budgets    = new List <PaycheckBudgetViewModel>()
            };

            var budgets        = _db.Budgets.ToList();
            var currentBudgets = budgets.Where(b => b.BudgetDate >= offset.Date).OrderBy(b => b.BudgetDate).Take(2).ToList();

            budgetResponse.PagingInfo.HasPrevious = budgets.Any(b => b.BudgetDate.Date < offset.Date);
            if (currentBudgets.Count > 1)
            {
                budgetResponse.PagingInfo.HasFuture = budgets.Any(b => b.BudgetDate.Date > currentBudgets[1].BudgetDate.Date);
            }
            else
            {
                budgetResponse.PagingInfo.HasFuture = false;
            }

            foreach (var budget in currentBudgets)
            {
                var paycheckBudget = new PaycheckBudgetViewModel
                {
                    Id          = budget.BudgetId,
                    Amount      = budget.BudgetAmount,
                    Date        = budget.BudgetDate,
                    BudgetItems = budget.BudgetItems.Select(x => new PaycheckBudgetItemViewModel
                    {
                        Id          = x.BudgetItemId,
                        Amount      = x.Amount,
                        Description = x.Description,
                        IsPaid      = x.IsPaid,
                        DueDate     = x.DueDate,
                        DatePaid    = x.DatePaid
                    }).ToList()
                };
                budgetResponse.Budgets.Add(paycheckBudget);
            }

            return(budgetResponse);
        }
Ejemplo n.º 5
0
        public PaycheckBudgetViewModel GetBudget(int id)
        {
            var budget         = _db.Budgets.FirstOrDefault(b => b.BudgetId == id);
            var paycheckBudget = new PaycheckBudgetViewModel
            {
                Id          = budget.BudgetId,
                Amount      = budget.BudgetAmount,
                Date        = budget.BudgetDate,
                BudgetItems = budget.BudgetItems.Select(x => new PaycheckBudgetItemViewModel
                {
                    Id          = x.BudgetItemId,
                    Amount      = x.Amount,
                    Description = x.Description,
                    IsPaid      = x.IsPaid,
                    DueDate     = x.DueDate,
                    DatePaid    = x.DatePaid
                }).ToList()
            };

            return(paycheckBudget);
        }
        // POST api/paycheckbudgetapi
        public void Post(PaycheckBudgetViewModel budgetViewModel)
        {
            var paycheckBudget = new PaycheckBudget
            {
                PaycheckBudgetId     = budgetViewModel.PaycheckBudgetId,
                PaycheckBudgetAmount = budgetViewModel.Amount,
                PaycheckBudgetDate   = Convert.ToDateTime(budgetViewModel.Date)
            };

            var paycheckBudgetItems = budgetViewModel.Items.Select(x => new PaycheckBudgetItem
            {
                PaycheckBudgetId = x.PaycheckBudgetId,
                Amount           = x.Amount,
                Description      = x.Description,
                IsPaid           = x.PaidStatus,
                PaidDate         = Convert.ToDateTime(x.DueDate)
            });

            _repo.Update(paycheckBudget);
            _repo.UpdateAll(paycheckBudget.PaycheckBudgetId, paycheckBudgetItems);
        }
Ejemplo n.º 7
0
        public HttpResponseMessage UpdateBudget(int id, PaycheckBudgetViewModel budgetViewModel)
        {
            var dbBudget = _db.Budgets.FirstOrDefault(b => b.BudgetId == id);

            dbBudget.BudgetAmount = budgetViewModel.Amount;
            dbBudget.BudgetDate   = budgetViewModel.Date;

            foreach (var budgetItem in budgetViewModel.BudgetItems)
            {
                var dbBudgetItem = dbBudget.BudgetItems.FirstOrDefault(bi => bi.BudgetItemId == budgetItem.Id);
                dbBudgetItem.Amount      = budgetItem.Amount;
                dbBudgetItem.Description = budgetItem.Description;
                dbBudgetItem.IsPaid      = budgetItem.IsPaid;
                dbBudgetItem.DueDate     = budgetItem.DueDate;
                dbBudgetItem.DatePaid    = budgetItem.DatePaid;
            }

            _db.SaveChanges();

            return(new HttpResponseMessage {
                StatusCode = HttpStatusCode.OK
            });
        }
Ejemplo n.º 8
0
 public PaycheckBudgetResponse GetFutureBudgets(PaycheckBudgetViewModel budgetViewModel)
 {
     return(GetBudgets(budgetViewModel.Date));
 }
Ejemplo n.º 9
0
        public PaycheckBudgetResponse GetPreviousBudgets(PaycheckBudgetViewModel budgetViewModel)
        {
            var dateOffset = budgetViewModel.Date.AddDays(-14); // We want to go back 2 weeks from the given date

            return(GetBudgets(dateOffset));
        }