public ServiceResult<Transaction> AddTransaction(int accountId, DateTime timestamp, decimal amount, string memo = null, string notes = null, bool isReconciled = false, bool isExcludedFromBudget = false, bool isTransfer = false, int? categoryId = null, int? vendorId = null, int? billTransactionId = null) { var result = new ServiceResult<Transaction>(); // does account exist? var accountResult = _accountService.GetAccount(accountId); if (accountResult.HasErrors) { result.AddErrors(accountResult); return result; } // add transaction var transaction = new Transaction() { AccountId = accountId, Timestamp = timestamp, OriginalTimestamp = timestamp, Amount = amount, IsReconciled = isReconciled, VendorId = vendorId, BillTransactionId = billTransactionId }; _db.Insert<Transaction>(transaction); // add subtransaction var subTransaction = new Subtransaction() { TransactionId = transaction.Id, Amount = amount, CategoryId = categoryId, Memo = memo, Notes = notes, IsTransfer = isTransfer, IsExcludedFromBudget = isExcludedFromBudget, }; _db.Insert<Subtransaction>(subTransaction); transaction.Subtransactions = new List<Subtransaction>() { subTransaction }; _accountService.UpdateAccountBalances(); result.Result = transaction; return result; }
private void UpdateTransactions() { // add transactions var now = DateTime.Now; var accounts = _accountRepository.GetAll().ToList(); var vendors = _vendorRepository.GetAll().ToList(); var mappedVendors = _importDescriptionVendorMapRepository.GetAll().ToList(); var transactions = _transactionRepository.GetAll().ToList(); var importedTransactions = new List<Transaction>(); var maxTransactionDate = transactions.Any() ? transactions.Max(x => x.OriginalTimestamp) : DateTime.MinValue; foreach (ImportRecord row in _transactions.Where(x => x.Date >= maxTransactionDate).OrderBy(x => x.Date)) { // is amount credit or debit? var amount = row.Amount * (row.TransactionType.Equals("debit") ? -1.0M : 1.0M); // has this transaction already been imported? var existingTransaction = transactions.FirstOrDefault(x => x.Amount == amount && x.OriginalTimestamp == row.Date && x.Subtransactions.Any(y => y.Notes.Equals(row.OriginalDescription, StringComparison.CurrentCultureIgnoreCase))); if (existingTransaction != null) { Console.WriteLine("FOUND {0} {1} : {2}", row.Date.ToShortDateString(), amount, row.OriginalDescription); importedTransactions.Add(existingTransaction); continue; } Console.WriteLine("create {0} {1} : {2}", row.Date.ToShortDateString(), amount, row.OriginalDescription); // find the account in the local cache var account = accounts.SingleOrDefault(x => x.AccountNameMaps.Any(y => y.Name.Equals(row.AccountName, StringComparison.CurrentCultureIgnoreCase))); // if we didn't find one, make one if (account == null) { account = new Account() { Name = row.AccountName, AccountGroup = accounts.Any() ? accounts.FirstOrDefault().AccountGroup : new AccountGroup() { Name = row.AccountName }, BalanceTimestamp = now }; account.AccountNameMaps = new List<AccountNameMap>() { new AccountNameMap() { Account = account, Name = row.AccountName } }; _accountRepository.Add(account); _unitOfWork.Commit(); accounts.Add(account); } account.BalanceTimestamp = now; // find the vendor in the local cache var vendor = vendors.SingleOrDefault(x => x.Name.Equals(row.Description, StringComparison.CurrentCultureIgnoreCase)); // find the vendor in the description map var mappedVendor = mappedVendors.Where(x => x.Description.Equals(row.Description, StringComparison.CurrentCultureIgnoreCase)); if (mappedVendor.Any()) { vendor = mappedVendor.FirstOrDefault().Vendor; } // if we didn't find one, make one if (vendor == null) { vendor = new Vendor() { Name = row.Description }; _vendorRepository.Add(vendor); _unitOfWork.Commit(); vendors.Add(vendor); } Transaction transaction = new Transaction() { Timestamp = row.Date, OriginalTimestamp = row.Date, Amount = amount, Vendor = vendor, }; Subtransaction subtransaction = new Subtransaction() { Amount = amount, Memo = row.Description, Notes = row.OriginalDescription, CategoryId = account.DefaultCategoryId, IsTransfer = row.Description.StartsWith("Transfer from") || row.Description.StartsWith("Transfer to") }; transaction.Subtransactions.Add(subtransaction); account.Transactions.Add(transaction); _newTransactions.Add(transaction); _unitOfWork.Commit(); } }