Esempio n. 1
0
        public async void AddExpense_DecreasedFlowBalanceIncreasedVersion()
        {
            using (var session = await CreateDefaultSession())
            {
                session.CreateDefaultEntities();
                var expense = new ExpenseFlowExpense
                {
                    Correcting    = false,
                    Account       = session.DebitCardAccount.Name,
                    ExpenseFlowId = session.FoodExpenseFlow.Id,
                    Category      = session.FoodCategory.Name,
                    Cost          = 25.60m,
                    DateCreated   = DateTime.Today,
                    Product       = session.Meat.Name,
                };

                var now = DateTime.Now;
                var efc = session.CreateExpenseFlowCommands();
                await efc.AddExpense(expense);

                var flow = await session.UnitOfWork.GetQueryRepository <ExpenseFlow>().GetById(session.FoodExpenseFlow.Id);

                flow.Balance.ShouldBeEquivalentTo(session.FoodExpenseFlow.Balance - expense.Cost);
                flow.Version.ShouldBeEquivalentTo(session.FoodExpenseFlow.Version + 1);
                var account = await session.UnitOfWork.GetQueryRepository <Account>().GetById(session.DebitCardAccount.Id);

                account.Balance.ShouldBeEquivalentTo(session.DebitCardAccount.Balance - expense.Cost);
                account.AvailBalance.ShouldBeEquivalentTo(session.DebitCardAccount.AvailBalance);
                account.LastWithdraw.Should().NotBeNull();
                account.LastWithdraw?.Should().BeOnOrAfter(now);
            }
        }
Esempio n. 2
0
        public async Task AddExpense(ExpenseFlowExpense expense)
        {
            if (expense.Account.IsNullOrEmpty())
            {
                throw new ArgumentException("Необходимо указать счет");
            }

            var account = await _repository.FindByNameAsync <Account>(_currentSession.UserId, expense.Account);

            if (account == null)
            {
                throw new ArgumentException($"Нет счета с именем \"{expense.Account}\"");
            }

            var flow = await _repository.LoadAsync <ExpenseFlow>(expense.ExpenseFlowId);

            if (flow == null)
            {
                throw new ArgumentException($"Нет статьи расходов с идентификатором Id = {expense.ExpenseFlowId}");
            }

            if (string.IsNullOrEmpty(expense.Category) && string.IsNullOrEmpty(expense.Product))
            {
                throw new ArgumentException("Необходимо ввести хотя бы категорию или продукт");
            }

            Category category = null;

            if (!string.IsNullOrEmpty(expense.Category))
            {
                category = await _repository.FindByNameAsync <Category>(_currentSession.UserId, expense.Category);

                if (category == null)
                {
                    throw new ArgumentException($"Нет категории продуктов с именем \"{expense.Category}\"");
                }
            }

            Product product = null;

            if (!string.IsNullOrEmpty(expense.Product))
            {
                product = await _repository.FindByNameAsync <Product>(_currentSession.UserId, expense.Product);

                if (product == null)
                {
                    throw new ArgumentException($"Нет товара с именем \"{expense.Product}\"");
                }
            }

            if (product == null && category == null)
            {
                throw new ArgumentException("Были введены или категория или продукт, но ни один из них не найден");
            }

            var billModel = new ExpenseBillModel
            {
                AccountId     = account.Id,
                ExpenseFlowId = expense.ExpenseFlowId,
                DateTime      = expense.DateCreated,
                Cost          = expense.Cost,
                OwnerId       = _currentSession.UserId,
                Items         = new List <ExpenseItemModel>
                {
                    new ExpenseItemModel
                    {
                        CategoryId = category?.Id,
                        ProductId  = product?.Id,
                        Comment    = null,
                        Quantity   = null,
                        Cost       = expense.Cost,
                    }
                }
            };

            await _expensesBillCommands.Create(billModel, expense.Correcting).ConfigureAwait(false);
        }