public void Save(BudgetDTO dto)
        {
            var budget = _budgets.Get(dto.BudgetID);

            if (budget != null)
            {
                dto.MapTo(budget);
                _budgets.Update(budget);
            }
            else
            {
                budget = _budgets.Add(dto.MapTo<Budget>());
                dto.BudgetID = budget.BudgetID;
            }
        }
 public void Save(BudgetDTO dto)
 {
     _budgetServices.Save(dto);
 }
        private BudgetDTO MapBudget(BudgetDTO budget)
        {
            if (budget == null)
                return null;

            var balanceAtBudgetStart = _transactions.GetTotalForAccountUpTo(budget.Account_AccountID, budget.Start);

            var budgetTransactions = _transactions.GetForAccount(budget.Account_AccountID, budget.Start, budget.End);

            // Get any categories which aren't deleted, or which were deleted *after* this budget period ended
            var category_budgets = _categories_budgets.AllIncludingDeleted(budget.BudgetID).Select(x => new Category_BudgetDTO {
                                                          Budget_BudgetID = x.Budget_BudgetID,
                                                          Category_CategoryID = x.Category_CategoryID,
                                                          CategoryName = x.CategoryName,
                                                          CategoryType = (CategoryTypeDTO)x.CategoryType,
                                                          Amount = x.Amount
                                                      }).ToList();

            if (category_budgets.Count > 0)
            {
                // Assign the categories for this budget
                budget.Category_Budgets = category_budgets;
                // Work out the total spent in each category so far
                budget.Category_Budgets.ForEach(x => x.Total = budgetTransactions.Where(t => t.Category_CategoryID == x.Category_CategoryID).Sum(t => Math.Abs(t.Amount)));

                // Work out the total amount overspent across all categories
                var overspend = budget.Category_Budgets.Where(x => x.Total > x.Amount).Select(x => x.Total - x.Amount).Sum();

                // Work out how much money has been allocated to budget categories
                var allocated = budget.Category_Budgets.Sum(x => x.Amount);

                // Work out how much money has been spent in budget categories
                var allocatedSpent = budget.Category_Budgets.Sum(x => x.Total);

                var unallocatedAmount = ((balanceAtBudgetStart - allocated) - overspend) + budgetTransactions.Where(x => x.Amount > 0).Sum(x => x.Amount);

                // Work out how much money was spent in transactions not assigned to a category
                var unallocatedSpent = budgetTransactions.Where(x => x.Amount < 0 && x.Category_CategoryID == null).ToList().Sum(x => Math.Abs(x.Amount));

                // If there is money left over after all budget category amounts and any overspend have been subtracted
                if (unallocatedAmount > 0)
                {
                    // Show how much and how much we've spent so far
                    budget.Category_Budgets.Add(new Category_BudgetDTO {
                        Budget_BudgetID = budget.BudgetID,
                        Category_CategoryID = 0,
                        CategoryName = "Unallocated",
                        CategoryType = CategoryTypeDTO.Expense,
                        Amount = unallocatedAmount,
                        Total = unallocatedSpent
                    });
                }
            }
            else
            {
                budget.Category_Budgets = new List<Category_BudgetDTO>();
            }

            return budget;
        }