public TransactionViewModel ConvertToTransactionViewModel(int transactionID) { string retrieveHeader = $"SELECT * FROM [Recording].JournalTransactions WHERE TransactionID = {transactionID}"; JournalTransaction header = dbConnection.QueryFirstOrDefault <JournalTransaction>(retrieveHeader); if (header == null) { return(null); } string retrieveLineItems = $"SELECT * FROM [Recording].JournalEntries WHERE ParentTransactionID = {header.TransactionId}"; var lineItems = dbConnection.Query <JournalEntry>(retrieveLineItems).ToList(); if (lineItems == null) { return(null); } TransactionViewModel viewModel = new TransactionViewModel(); viewModel.JournalHeader.Memo = header.Memo; viewModel.JournalHeader.TransactionID = header.TransactionId; viewModel.DefaultEntryDate = DateTime.Parse(header.RecordedDateTime); viewModel.UserID = header.UserID; viewModel.EditMode = true; foreach (JournalEntry item in lineItems) { JournalLineItemViewModel newItem = new JournalLineItemViewModel(); newItem.AccountName = item.AccountName; if (item.DebitAmount != null) { newItem.DebitAmount = (decimal)item.DebitAmount; } else { newItem.CreditAmount = (decimal)item.CreditAmount; } viewModel.PreviousEntries.Add(newItem); } return(viewModel); }
public int CommitTransaction(TransactionViewModel transaction) { if (transaction.EditMode) { // Delete transaction in db because deemed easier to delete and recreate instead of update DeleteTransaction(transaction.JournalHeader.TransactionID); } // Check that user is not at transaction cap if (userInfoUtils.UserAtMaxTransactions(transaction.UserID)) { return(-1); } int tranID = -1; decimal?total = 0; foreach (JournalLineItemViewModel lineItem in transaction.PreviousEntries) { total += (lineItem.DebitAmount != null) ? lineItem.DebitAmount : 0; } JournalTransaction transactionEntry = new JournalTransaction() { Memo = transaction.JournalHeader.Memo, RecordedDateTime = DateTime.Now.ToString(), TotalAmount = (decimal)total, UserID = transaction.UserID }; try { dbContext.JournalTransactions.Add(transactionEntry); dbContext.SaveChanges(); } catch { return(-1); } tranID = transactionEntry.TransactionId; foreach (JournalLineItemViewModel lineItem in transaction.PreviousEntries) { JournalEntry entry = new JournalEntry() { AccountName = lineItem.AccountName, DebitBalance = AccountsInfo.DebitAccounts.Contains(lineItem.AccountName), DebitAmount = lineItem.DebitAmount, CreditAmount = lineItem.CreditAmount, ParentTransactionId = transactionEntry.TransactionId }; try { dbContext.JournalEntries.Add(entry); } catch { return(-1); } } try { dbContext.SaveChanges(); } catch { return(-1); } return(tranID); }