/// <summary> /// Create a budget plan. /// </summary> /// <param name="budgetPlanDetails"></param> /// <param name="accountId"></param> public void CreateBudgetPlan(BudgetPlanDetails budgetPlanDetails, int accountId) { var budgetPlan = Mapper.Map <BudgetPlan>(budgetPlanDetails); _budgetPlanRepository.Create(budgetPlan); foreach (var ExpenseType in budgetPlanDetails.ExpenseTypes) { var plannedExpense = new BudgetByExpenseType { Budget = ExpenseType.ExpectedValue, ExpenseTypeId = ExpenseType.ExpenseType.Id, BudgetPlanId = budgetPlan.Id, AccountId = accountId }; // Account ID is defined at BudgetByExpenseType time because at first, I wanted to plan budget through several accounts. // It might evolve later so for now, I keep it here, independently from BudgetByExpenseType. _budgetByExpenseTypeRepository.Create(plannedExpense); } }
/// <summary> /// Edit a budget plan. /// </summary> /// <param name="budgetPlanDetails"></param> /// <param name="accountId"></param> public void EditBudgetPlan(BudgetPlanDetails budgetPlanDetails, int accountId) { var budgetPlan = _budgetPlanRepository.GetById(budgetPlanDetails.Id); budgetPlan.Name = budgetPlanDetails.Name; budgetPlan.ExpectedIncomes = budgetPlanDetails.ExpectedIncomes; budgetPlan.ExpectedSavings = budgetPlanDetails.ExpectedSavings; _budgetPlanRepository.Update(budgetPlan); var existingBudgetPlanExpenses = _budgetByExpenseTypeRepository.GetList2(x => x.ExpenseType) .Where(x => x.BudgetPlanId == budgetPlanDetails.Id); foreach (var budgetExpenseType in budgetPlanDetails.ExpenseTypes) { var existingBudgetExpenseType = existingBudgetPlanExpenses.SingleOrDefault(x => x.ExpenseType.Id == budgetExpenseType.ExpenseType.Id); if (existingBudgetExpenseType == null) { // Add var plannedExpense = new BudgetByExpenseType() { Budget = budgetExpenseType.ExpectedValue, ExpenseTypeId = budgetExpenseType.ExpenseType.Id, BudgetPlanId = budgetPlanDetails.Id, AccountId = accountId }; _budgetByExpenseTypeRepository.Create(plannedExpense); } else { // Update existingBudgetExpenseType.Budget = budgetExpenseType.ExpectedValue; _budgetByExpenseTypeRepository.Update(existingBudgetExpenseType); } } }