public IActionResult PostAccount([FromBody] AccountForPosting account)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var newAccount = new Account
            {
                AccountId       = Guid.NewGuid(),
                Name            = account.Name,
                Currency        = account.Currency,
                Vendor          = account.Vendor,
                AccountType     = account.AccountType,
                MortgagePayment = account.MortgagePayment,
                MortgageType    = account.MortgageType
            };

            _accountRepo.Insert(newAccount);

            var category    = TransactionHelpers.GetOrCreateCategory("Starting Balance", _categoryRepo);
            var transaction = new TransactionForDisplay
            {
                TransactionId   = Guid.NewGuid(),
                AccountId       = newAccount.AccountId,
                Amount          = account.StartingBalance,
                TransactionDate = account.StartDate,
                Vendor          = "",
                Description     = "",
                CategoryId      = category.CategoryId,
                CategoryName    = category.Name,
                EnteredDate     = account.StartDate,
                IsReconciled    = true
            };

            _transactionRepo.Insert(transaction);

            var model = new AccountWithTransactions
            {
                AccountId      = newAccount.AccountId,
                Name           = newAccount.Name,
                Transactions   = new List <TransactionForDisplay>(new[] { transaction }),
                CurrentBalance = account.StartingBalance
            };

            return(CreatedAtAction("PostAccount", new { id = newAccount.AccountId }, model));
        }
        public IActionResult MakeCorrectingEntries()
        {
            var investments        = GetInvestments();
            var currencyController = new CurrenciesController(_currencyRepo);
            var currency           = currencyController.GetExchangeRateFor("CAD").GetAwaiter().GetResult();
            var investmentsTotal   = investments.Sum(i => i.Price * i.Shares);
            var totalInUsd         = investmentsTotal / currency;
            var investmentAccount  = _accountRepo.GetAll().FirstOrDefault(a => a.AccountType == "Investment");

            if (investmentAccount == null)
            {
                return(Ok());
            }

            var bookBalance = _transactionRepo.GetByAccount(investmentAccount.AccountId).Sum(i => i.Amount);

            var difference = Math.Round(totalInUsd - bookBalance, 2);

            if (Math.Abs(difference) >= 1)
            {
                var category    = TransactionHelpers.GetOrCreateCategory("Gain/loss on investments", _categoryRepo);
                var transaction = new TransactionForDisplay {
                    TransactionId   = Guid.NewGuid(),
                    AccountId       = investmentAccount.AccountId,
                    Amount          = difference,
                    CategoryId      = category.CategoryId,
                    CategoryName    = category.Name,
                    CategoryDisplay = category.Name,
                    TransactionDate = DateTime.Now,
                    EnteredDate     = DateTime.Now,
                    Description     = "Gain/loss"
                };
                transaction.SetDebitAndCredit();
                _transactionRepo.Insert(transaction);
                var accountBalances = _accountRepo.GetAccountBalances().Select(a => new { a.AccountId, a.CurrentBalance });
                var transactions    = new[] { transaction };

                return(CreatedAtAction("PostTransaction", new { id = transaction.TransactionId }, new { transactions, accountBalances }));
            }
            else
            {
                return(Ok());
            }
        }
Exemple #3
0
        private List <TransactionForDisplay> ParseQif(StreamReader reader, Guid accountId, DateTime?fromDate)
        {
            var transactions = new List <TransactionForDisplay>();
            var trx          = new TransactionForDisplay
            {
                TransactionId = Guid.NewGuid(),
                AccountId     = accountId,
                EnteredDate   = DateTime.Now,
                Vendor        = ""
            };

            while (reader.Peek() >= 0)
            {
                var line = reader.ReadLine();
                if (line.Length > 1 || line == "^")
                {
                    switch (line[0])
                    {
                    case '^':
                        if (!fromDate.HasValue || trx.TransactionDate >= fromDate.Value)
                        {
                            if (trx.Vendor.StartsWith("Transfer :", StringComparison.CurrentCultureIgnoreCase))
                            {
                                // For positive transfers, assume they've been handled elsewhere
                                if (trx.Amount > 0)
                                {
                                    break;
                                }
                                var accountName        = trx.Vendor.Replace("Transfer : ", "");
                                var account            = _accountRepo.GetAll().Single(a => a.Name == accountName);
                                var relatedTransaction = new TransactionForDisplay
                                {
                                    TransactionId        = Guid.NewGuid(),
                                    AccountId            = account.AccountId,
                                    EnteredDate          = trx.EnteredDate,
                                    Amount               = 0 - trx.Amount,
                                    Description          = trx.Description,
                                    IsReconciled         = trx.IsReconciled,
                                    TransactionDate      = trx.TransactionDate,
                                    RelatedTransactionId = trx.TransactionId
                                };
                                trx.Vendor = "";
                                trx.RelatedTransactionId = relatedTransaction.TransactionId;
                                transactions.Add(relatedTransaction);
                            }
                            trx.SetDebitAndCredit();
                            transactions.Add(trx);
                        }
                        trx = new TransactionForDisplay
                        {
                            TransactionId = Guid.NewGuid(),
                            AccountId     = accountId,
                            EnteredDate   = DateTime.Now,
                            Vendor        = ""
                        };
                        break;

                    case 'D':
                        trx.TransactionDate = DateTime.Parse(line.Substring(1));
                        break;

                    case 'T':
                        trx.Amount = decimal.Parse(line.Substring(1));
                        break;

                    case 'P':
                        trx.Vendor = line.Substring(1);
                        break;

                    case 'L':
                        var category = line.Substring(1)
                                       .Replace("Everyday Expenses:", "")
                                       .Replace("Rainy Day Funds:", "")
                                       .Replace("Monthly Bills:", "");
                        trx.CategoryId = TransactionHelpers.GetOrCreateCategory(category, _categoryRepo).CategoryId;
                        break;

                    case 'M':
                        trx.Description = line.Substring(1);
                        break;

                    case 'C':
                        trx.IsReconciled = line.Substring(1) == "c";
                        break;
                    }
                }
            }
            return(transactions);
        }
Exemple #4
0
        public IActionResult PostMortgagePayment([FromBody] TransactionForDisplay transaction)
        {
            var principal = transaction.Debit.Value;
            var interest  = transaction.Credit.Value;

            transaction.Credit = null;
            var transactions = new List <TransactionForDisplay>();

            if (transaction.TransactionId == null || transaction.TransactionId == Guid.Empty)
            {
                transaction.TransactionId = Guid.NewGuid();
            }
            if (!transaction.AccountId.HasValue)
            {
                transaction.AccountId = _accountRepo.GetAll().Single(
                    a => a.Name.Equals(transaction.AccountName, StringComparison.CurrentCultureIgnoreCase)).AccountId;
            }
            transaction.SetAmount();
            var relatedTransactionId = Guid.NewGuid();
            var relatedTransaction   = new TransactionForDisplay {
                TransactionDate      = transaction.TransactionDate,
                TransactionId        = relatedTransactionId,
                Vendor               = transaction.Vendor,
                Description          = string.IsNullOrWhiteSpace(transaction.Description) ? "Mortgage Payment" : transaction.Description,
                AccountId            = transaction.RelatedAccountId.Value,
                Amount               = 0 - transaction.Amount,
                EnteredDate          = DateTime.Now,
                RelatedTransactionId = transaction.TransactionId
            };

            relatedTransaction.SetDebitAndCredit();

            transaction.RelatedTransactionId = relatedTransactionId;

            var mortgageInterestCategoryId = TransactionHelpers.GetOrCreateCategory("Mortgage Interest", _categoryRepo).CategoryId;
            var interestTransaction        = new TransactionForDisplay {
                TransactionDate = transaction.TransactionDate,
                TransactionId   = Guid.NewGuid(),
                Vendor          = transaction.Vendor,
                AccountId       = transaction.AccountId,
                Amount          = 0 - interest,
                Debit           = interest,
                EnteredDate     = DateTime.Now,
                CategoryId      = mortgageInterestCategoryId,
                CategoryDisplay = "Mortgage Interest"
            };

            var bankFeeTransactions = TransactionHelpers.GetBankFeeTransactions(transaction, _categoryRepo, _accountRepo);

            transactions.Add(transaction);
            transactions.Add(relatedTransaction);
            transactions.Add(interestTransaction);
            transactions.AddRange(bankFeeTransactions);
            _transactionRepo.InsertRelatedTransaction(transaction, relatedTransaction);
            _transactionRepo.Insert(interestTransaction);
            foreach (var trx in bankFeeTransactions)
            {
                _transactionRepo.Insert(trx);
            }

            var accountBalances = _accountRepo.GetAccountBalances();

            return(CreatedAtAction("PostTransfer",
                                   new { id = transaction.TransactionId }, new { transactions, accountBalances }));
        }