예제 #1
0
        public async Task <int> ProcessReducingBalanceAsCreditFees(Account account, decimal reduceAmount)
        {
            if (account == null)
            {
                throw new ArgumentNullException(nameof(account));
            }
            if (account.AccountType != AccountType.CreditCard)
            {
                throw new InvalidOperationException("Счет должен быть кредитной картой");
            }

            var category = await _repository.FindByNameAsync <Category>(
                _currentSession.UserId, "Кредиты").ConfigureAwait(false);

            if (category == null)
            {
                category = new Category
                {
                    OwnerId = _currentSession.UserId,
                    Name    = "Кредиты",
                };
                _repository.Create(category);
                await _repository.SaveChangesAsync().ConfigureAwait(false);
            }

            var mustSave = false;
            var product  = await _repository.FindByNameAsync <Product>(_currentSession.UserId, account.Name).ConfigureAwait(false);

            if (product == null)
            {
                product = new Product
                {
                    OwnerId    = _currentSession.UserId,
                    Name       = account.Name,
                    CategoryId = category.Id
                };
                _repository.Create(product);
                mustSave = true;
            }

            var flow = await _repository.FindByNameAsync <ExpenseFlow>(_currentSession.UserId, "Кредиты").ConfigureAwait(false);

            if (flow == null)
            {
                flow = new ExpenseFlow
                {
                    OwnerId     = _currentSession.UserId,
                    Balance     = 0,
                    DateCreated = _timeService.ClientLocalNow,
                    Name        = "Кредиты",
                    Number      = 1,
                    Version     = 1
                };
                _repository.Create(flow);
                mustSave = true;
            }

            if (mustSave)
            {
                await _repository.SaveChangesAsync().ConfigureAwait(false);
            }

            var bill = new ExpenseBillModel
            {
                DateTime      = _timeService.ClientLocalNow,
                OwnerId       = _currentSession.UserId,
                AccountId     = account.Id,
                ExpenseFlowId = flow.Id,
                IsCorection   = true,
            };
            var item = new ExpenseItemModel
            {
                CategoryId = category.Id,
                ProductId  = product.Id,
                Cost       = reduceAmount
            };

            bill.AddItem(item);

            return(await _expensesBillCommands.Create(bill, true).ConfigureAwait(false));
        }
예제 #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);
        }