コード例 #1
0
ファイル: Category.cs プロジェクト: Sefirosu/accounting
        public void AddTransaction(Transaction transaction, bool from)
        {
            if (transaction == null)
            {
                throw new InvalidOperationException("Transaction cannot be null");
            }

            if (this.CategoryType == null)
            {
                throw new InvalidOperationException("This category doesn't have a category type");
            }

            if (from)
            {
                if (this.CategoryType.AddFrom)
                {
                    this.Total = this.Total + transaction.Total;
                }
                else
                {
                    this.Total = this.Total - transaction.Total;
                }
            }
            else if (!from)
            {
                if (this.CategoryType.AddTo)
                {
                    this.Total = this.Total + transaction.Total;
                }
                else
                {
                    this.Total = this.Total - transaction.Total;
                }
            }
        }
コード例 #2
0
        public static void CreateTransaction(HttpContext context, Transaction transaction)
        {
            var dbContext = context.RequestServices.GetRequiredService<AccountingContext>();
            if (!dbContext.TransactionTypes.Any(tt => tt == transaction.TransactionType))
            {
                dbContext.TransactionTypes.Add(transaction.TransactionType);
            }

            if (!dbContext.Categories.Any(ct => ct == transaction.From))
            {
                dbContext.Categories.Add(transaction.From);
            }

            dbContext.Transactions.Add(transaction);
            dbContext.SaveChanges();
        }
コード例 #3
0
ファイル: SeedData.cs プロジェクト: Sefirosu/accounting
        private async Task SeedDevelopmentData()
        {
            if (await this.userManager.FindByEmailAsync("*****@*****.**") == null)
            {
                var newUser = new AccountingUser
                {
                    UserName = "******",
                    Email = "*****@*****.**"
                };

                var result = await this.userManager.CreateAsync(newUser, "password");
            }
            
            var base64Token = "OMuo2TLAB/iOX4IQs98+ag=="; 
            SignupToken token = null;
            if (!this.context.SignupTokens.Any(t => t.Token == base64Token))
            {
                this.context.SignupTokens.Add(new SignupToken { Token = base64Token });
            }
            else
            {
                token = this.context.SignupTokens.First(t => t.Token == base64Token);
                token.Used = false;
                token.AccountingUser = null;
                token.AccountingUserId = null;
                this.context.SignupTokens.Update(token);
            }
            

            var user = await this.userManager.FindByNameAsync("testuser");

            var expenseType = new CategoryType { Name = "Expenses", DisplayOrder = 2, AddTo = true };
            var assetType = new CategoryType { Name = "Assets", DisplayOrder = 1, AddTo = true, AddFrom = false };
            var incomeType = new CategoryType { Name = "Income", DisplayOrder = 3, AddFrom = true };

            if (!this.context.CategoryTypes.Any(c => c.Name == expenseType.Name))
            {
                this.context.CategoryTypes.Add(expenseType);
            }
            else
            {
                expenseType = this.context.CategoryTypes.First(c => c.Name == expenseType.Name);
            }

            if (!this.context.CategoryTypes.Any(c => c.Name == assetType.Name))
            {
                this.context.CategoryTypes.Add(assetType);
            }
            else
            {
                assetType = this.context.CategoryTypes.First(c => c.Name == assetType.Name);
            }

            if (!this.context.CategoryTypes.Any(c => c.Name == incomeType.Name))
            {
                this.context.CategoryTypes.Add(incomeType);
            }
            else
            {
                incomeType = this.context.CategoryTypes.First(c => c.Name == incomeType.Name);
            }

            var savings = new Category { Name = "Savings", NormalizedName = Category.NormalizeName("Savings"), CategoryType = assetType, AccountingUser = user };
            var grocceries = new Category { Name = "Grocceries", NormalizedName = Category.NormalizeName("Grocceries"), CategoryType = expenseType, AccountingUser = user };
            var salary = new Category { Name = "Salary", NormalizedName = Category.NormalizeName("Salary"), CategoryType = incomeType, AccountingUser = user };
            var work = new Category { Name = "Work", NormalizedName = Category.NormalizeName("Work"), CategoryType = incomeType, AccountingUser = user, ParentCategory = salary };
            var subwork = new Category { Name = "Subwork", NormalizedName = Category.NormalizeName("Subwork"), CategoryType = incomeType, AccountingUser = user, ParentCategory = work };
            var incomeTransType = new TransactionType { Name = "Income", From = incomeType, To = assetType };
            var expenseTransType = new TransactionType { Name = "Expense", From = assetType, To = expenseType };
            var transferTransType = new TransactionType { Name = "Transfer", From = assetType, To = assetType };

            if (!this.context.Categories.Any(c => c.Name == savings.Name))
            {
                this.context.Categories.Add(savings);
                this.context.Categories.Add(grocceries);
                this.context.Categories.Add(salary);
                this.context.Categories.Add(work);
                this.context.Categories.Add(subwork);
            }
            else
            {
                savings = this.context.Categories.SingleOrDefault(c => c.Name == savings.Name);
                grocceries = this.context.Categories.SingleOrDefault(c => c.Name == grocceries.Name);
                salary = this.context.Categories.SingleOrDefault(c => c.Name == salary.Name);
                work = this.context.Categories.SingleOrDefault(c => c.Name == work.Name);
                subwork = this.context.Categories.SingleOrDefault(c => c.Name == subwork.Name);
            }

            if (!this.context.TransactionTypes.Any(t => t.Name == incomeTransType.Name))
            {
                this.context.TransactionTypes.Add(incomeTransType);
            }
            else
            {
                incomeTransType = this.context.TransactionTypes.SingleOrDefault(t => t.Name == incomeTransType.Name);
            }

            if (!this.context.TransactionTypes.Any(t => t.Name == expenseTransType.Name))
            {
                this.context.TransactionTypes.Add(expenseTransType);
            }
            else
            {
                expenseTransType = this.context.TransactionTypes.SingleOrDefault(t => t.Name == expenseTransType.Name);
            }
            
            if (!this.context.TransactionTypes.Any(t => t.Name == transferTransType.Name))
            {
                this.context.TransactionTypes.Add(transferTransType);
            }
            else
            {
                transferTransType = this.context.TransactionTypes.SingleOrDefault(t => t.Name == transferTransType.Name);
            }

            this.context.SaveChanges();

            var trans = new Transaction { From = salary, To = savings, TransactionType = incomeTransType, Total = 100, DateTime = DateTime.UtcNow, AccountingUser = user };

            if (!this.context.Transactions.Any())
            {
                this.context.Transactions.Add(trans);
                trans.To.AddTransaction(trans, from: false);
                trans.From.AddTransaction(trans, from: true);
                this.context.SaveChanges();
            }

            
        }
コード例 #4
0
        public bool ValidateAndMapCreateTransaction(TransactionViewModel vm, Controller controller, string userId, out Transaction transaction, out ErrorViewModel errors)
        {
            var modelState = controller.ModelState;
            transaction = null;
            errors = null;

            if (vm.Total == null || vm.Total <= 0)
            {
                errors = new ErrorViewModel { Error = TransactionErrors.ValidationErrors.TransactionInvalidTotalError };
                return false;
            }

            var transactionType = this.transTypeRepo.FindById(vm.TransactionTypeId, tt => tt.From, tt => tt.To);
            if (transactionType == null)
            {
                errors = new ErrorViewModel { Error = string.Format(TransactionErrors.ValidationErrors.TransactionTypeNotFoundError, vm.TransactionTypeId) };
                return false;
            }

            if (vm.FromId == vm.ToId)
            {
                errors = new ErrorViewModel { Error = TransactionErrors.ValidationErrors.TransactionToSameCategoryError };
                return false;
            }

            var fromCategory = this.categoryRepo.FindById(vm.FromId, c => c.CategoryType);
            if (fromCategory == null || !string.Equals(fromCategory.AccountingUserId, userId))
            {
                errors = new ErrorViewModel { Error = string.Format(TransactionErrors.ValidationErrors.TransactionFromCategoryNotFoundError, vm.FromId) };
                return false;
            }

            var toCategory = this.categoryRepo.FindById(vm.ToId, c => c.CategoryType);
            if (toCategory == null || !string.Equals(toCategory.AccountingUserId, userId))
            {
                errors = new ErrorViewModel { Error = string.Format(TransactionErrors.ValidationErrors.TransactionToCategoryNotFoundError, vm.ToId) };
                return false;
            }

            if (toCategory.CategoryType.Id != transactionType.To.Id || fromCategory.CategoryType.Id != transactionType.From.Id)
            {
                errors = new ErrorViewModel { Error = TransactionErrors.ValidationErrors.TransactionTypeCategoryTypeMismatchError };
                return false;
            }

            if (!modelState.IsValid)
            {
                modelState.Clear();
                controller.TryValidateModel(vm);
            }

            if (!modelState.IsValid)
            {
                errors = new ErrorViewModel
                {
                    Error = TransactionErrors.ValidationErrors.TransactionInvalidError,
                    Errors = modelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList()
                };

                this.logger.LogWarning("Invalid transaction model:" + string.Join(";",
                    modelState.Values.SelectMany(v => v.Errors)
                    .Select(e => e.ErrorMessage)));
                return false;
            }

            transaction = Mapper.Map<Transaction>(vm);
            transaction.Id = 0;
            transaction.Total = Math.Round(transaction.Total, 2);
            transaction.From = fromCategory;
            transaction.To = toCategory;
            transaction.TransactionType = transactionType;
            transaction.AccountingUserId = userId;

            return true;
        }