コード例 #1
0
ファイル: AccountService.cs プロジェクト: kenwarner/scrilla
        public ServiceResult<BudgetAmountInfo> SetBudget(DateTime month, int categoryId, decimal amount)
        {
            var result = new ServiceResult<BudgetAmountInfo>();

            var budget = _budgetCategoryRepository.Get(x => x.Month == month && x.CategoryId == categoryId);
            if (budget == null)
            {
                budget = new BudgetCategory() { Month = month, CategoryId = categoryId };
                _budgetCategoryRepository.Add(budget);
            }

            var nextMonth = month.AddMonths(1);
            var bills = _billTransactionRepository.GetMany(x => x.CategoryId == categoryId && x.Timestamp >= month && x.Timestamp < nextMonth); // TODO paid vs !paid
            var sumBills = bills.Any() ? bills.Sum(x => x.Amount) : 0M;

            budget.Amount = amount;
            _unitOfWork.Commit();

            result.Result = new BudgetAmountInfo() { Month = month, CategoryId = categoryId, ExtraAmount = amount, BillsAmount = sumBills };
            return result;
        }
コード例 #2
0
ファイル: BudgetService.cs プロジェクト: kenwarner/scrilla.js
		public ServiceResult<BudgetAmountInfo> UpdateBudget(int categoryId, DateTime month, decimal amount)
		{
			var result = new ServiceResult<BudgetAmountInfo>();
			BudgetAmountInfo budgetAmountInfo = new BudgetAmountInfo() { CategoryId = categoryId, Month = month, ExtraAmount = amount };

			// does category exist?
			var categoryResult = _categoryService.GetCategory(categoryId);
			if (categoryResult.HasErrors)
			{
				result.AddErrors(categoryResult);
				return result;
			}

			// does budget category exist?
			var predicates = new List<IPredicate>();
			predicates.Add(Predicates.Field<BudgetCategory>(x => x.Month, Operator.Eq, month));
			predicates.Add(Predicates.Field<BudgetCategory>(x => x.CategoryId, Operator.Eq, categoryId));
			var predicate = new PredicateGroup { Operator = GroupOperator.And, Predicates = predicates };
			var budgetCategory = _db.GetList<BudgetCategory>(predicate);

			// are there multiple budget categories with the same month?
			if (budgetCategory.Count() > 1)
			{
				result.AddError(ErrorType.Generic, "Multiple Budget Categories for month {0} exist", month.ToShortDateString());
				return result;
			}

			// is this an existing budget category?
			else if (budgetCategory.Count() == 1)
			{
				var existingBudgetCategory = budgetCategory.First();
				existingBudgetCategory.Amount = amount;
				_db.Update<BudgetCategory>(existingBudgetCategory);
			}

			// is this a new budget category?
			else
			{
				var newBudgetCategory = new BudgetCategory()
				{
					CategoryId = categoryId,
					Month = month,
					Amount = amount
				};

				_db.Insert<BudgetCategory>(newBudgetCategory);
			}

			// what is the bills amount for this month?
			var nextMonth = month.AddMonths(1);
			predicates = new List<IPredicate>();
			predicates.Add(Predicates.Field<BillTransaction>(x => x.CategoryId, Operator.Eq, categoryId));
			predicates.Add(Predicates.Field<BillTransaction>(x => x.Timestamp, Operator.Ge, month));
			predicates.Add(Predicates.Field<BillTransaction>(x => x.Timestamp, Operator.Le, nextMonth));
			predicate = new PredicateGroup { Operator = GroupOperator.And, Predicates = predicates };
			var billTransactions = _db.GetList<BillTransaction>(predicate);
			var billsAmount = billTransactions.Any() ? billTransactions.Sum(x => x.Amount) : 0M;
			budgetAmountInfo.BillsAmount = billsAmount;

			result.Result = budgetAmountInfo;
			return result;
		}