Exemplo n.º 1
0
        private static ExpenseBillModel CreateBill(UserQuerySession session, int?accountId = null)
        {
            var bill = new ExpenseBillModel
            {
                OwnerId       = session.UserSession.UserId,
                AccountId     = accountId ?? session.DebitCardAccount.Id,
                ExpenseFlowId = session.FoodExpenseFlow.Id,
                DateTime      = DateTime.Now,
            };

            bill.AddItem(new ExpenseItemModel
            {
                Category = session.FoodCategory.Name,
                Cost     = 120.45m,
                Product  = session.Meat.Name
            });
            return(bill);
        }
Exemplo n.º 2
0
        public async Task <ExpenseBillModel> CreateExpenseBill(int accountId, int flowId, DateTime dateTime,
                                                               Product product, decimal cost)
        {
            var bill = new ExpenseBillModel
            {
                AccountId     = accountId,
                OwnerId       = UserSession.UserId,
                ExpenseFlowId = flowId,
                DateTime      = dateTime
            };

            bill.AddItem(new ExpenseItemModel
            {
                CategoryId = product.CategoryId,
                ProductId  = product.Id,
                Cost       = cost
            });
            var commands = CreateExpensesBillCommands();
            await commands.Save(bill);

            return(bill);
        }
Exemplo n.º 3
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));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> OnPostAddAsync()
        {
            return(await Good.ProcessAsync(ModelState, nameof(Good),
                                           async() =>
            {
                await PrepareModelsAsync(Good.FlowId);
                Bill = JsonConvert.DeserializeObject <ExpenseBillModel>(Good.Bill);
                Bill.AddItem(await GetExpenseItem());
                Good.ClearInput();
                Good.Bill = JsonConvert.SerializeObject(Bill);
                return Page();
            },
                                           async() =>
            {
                await PrepareModelsAsync(Good.FlowId);
                Bill = JsonConvert.DeserializeObject <ExpenseBillModel>(Good.Bill);
                return Page();
            },
                                           async vrList =>
            {
                if (Good.Account.IsNullOrEmpty())
                {
                    vrList.Add(new ModelValidationResult(nameof(Good.Account), "Укажите счет"));
                }
                else
                {
                    var account = await _accountQueries.GetByName(Good.Account);
                    if (account == null)
                    {
                        vrList.Add(new ModelValidationResult(nameof(Good.Account), "Нет такого счета"));
                    }
                }

                if (Good.FlowName.IsNullOrEmpty())
                {
                    vrList.Add(new ModelValidationResult(nameof(Good.FlowName), "Укажите статью расходов"));
                    return;
                }

                var flowId = await _expenseFlowQueries.GetIdByName(Good.FlowName);
                if (flowId == null)
                {
                    vrList.Add(new ModelValidationResult(nameof(Good.FlowName), "Нет такой статьи расходов"));
                    return;
                }

                Good.FlowId = flowId.Value;

                CategoryModel category = null;
                if (!Good.Category.IsNullOrEmpty())
                {
                    category = await _categoriesQueries.GetFlowCategoryByName(Good.FlowId, Good.Category);
                    if (category == null)
                    {
                        if (Good.Category == Good.CategoryToAdd)
                        {
                            category = await _categoriesCommands.CreateNewOrBind(Good.FlowId, Good.Category);
                            Good.CategoryToAdd = null;
                        }
                        else
                        {
                            vrList.Add(new ModelValidationResult(nameof(Good.Category),
                                                                 "Нет такой категории, добавить ее?"));
                            Good.CategoryToAdd = Good.Category;
                        }
                    }
                }
                if (!Good.Product.IsNullOrEmpty())
                {
                    var product = await _productQueries.GetFlowProductByName(Good.FlowId, Good.Product);
                    if (product == null)
                    {
                        if (Good.Product == Good.ProductToAdd && category != null)
                        {
                            await _productCommands.AddProductToCategory(category.Id, Good.Product);
                            Good.ProductToAdd = null;
                        }
                        else
                        {
                            vrList.Add(new ModelValidationResult(nameof(Good.Product),
                                                                 "Нет такого товара, добавить его?"));
                            Good.ProductToAdd = Good.Product;
                        }
                    }
                }
            }));
        }