public ResultMessage CreatePayoffAccount(HttpRequestMessage request)
        {
            try
            {
                using (var db = new SES_ServicesEntities())
                {
                    // TODO: Perform entity permission check here

                    // TODO: Check to see if the entity passing the payoff account to this can pre-approve payoff accounts

                    var doc = new XmlDocument();
                    doc.Load(request.Content.ReadAsStreamAsync().Result);

                    var validationResult = ValidatePayoffAccountXml(doc);
                    if (validationResult.Result != ResultEnum.Success)
                    {
                        return(validationResult);
                    }

                    var payoffAccount = ConvertXmlToPayoffAccount(doc.DocumentElement);

                    var existingPayoffAccount = db.PayoffBankAccounts.FirstOrDefault(p => p.AccountNumber == payoffAccount.AccountNumber && p.RoutingNumber == payoffAccount.RoutingNumber);
                    if (existingPayoffAccount != null)
                    {
                        return(new ResultMessage(ResultEnum.FailureDuplicateData, "Unable to create payoff account.  An account with the same account and routing numbers already exists."));
                    }

                    payoffAccount.Status      = PayoffAccountStatusEnum.PendingApproval; // TODO: Determine beginning status based on entity
                    payoffAccount.CreatedDate = DateTime.Now;

                    payoffAccount.PayoffBankAccountsHistories.Add(new PayoffBankAccountsHistory
                    {
                        EntityId      = payoffAccount.CreatedByEntity_Id,
                        UserProfileId = payoffAccount.CreatedBy_Id,
                        TimeStamp     = DateTime.Now,
                        Username      = payoffAccount.CreatedByUsername,
                        Message       = "Payoff account created."
                    });

                    db.PayoffBankAccounts.Add(payoffAccount);
                    db.SaveChanges();

                    return(new ResultMessage(ResultEnum.Success, "Success."));
                }
            }
            catch (Exception ex)
            {
                // TODO: log the actual exception
                return(new ResultMessage(ResultEnum.Error, "There was a problem creating a new payoff account."));
            }
        }
        public ResultMessage UpdatePayoffAccount(HttpRequestMessage request)
        {
            try
            {
                using (var db = new SES_ServicesEntities())
                {
                    // TODO: Perform entity permission check here

                    var doc = new XmlDocument();
                    doc.Load(request.Content.ReadAsStreamAsync().Result);

                    var validationResult = ValidatePayoffAccountXml(doc);
                    if (validationResult.Result != ResultEnum.Success)
                    {
                        return(validationResult);
                    }

                    var updatedAccount = ConvertXmlToPayoffAccount(doc.DocumentElement);

                    var duplicateAccount = db.PayoffBankAccounts.FirstOrDefault(p => p.Id != updatedAccount.Id && p.AccountNumber == updatedAccount.AccountNumber && p.RoutingNumber == updatedAccount.RoutingNumber);
                    if (duplicateAccount != null)
                    {
                        return(new ResultMessage(ResultEnum.FailureDuplicateData, "Unable to save payoff account information.  The account and routing number pair is already in use by another payoff account."));
                    }

                    var existingAccount = db.PayoffBankAccounts.FirstOrDefault(p => p.Id == updatedAccount.Id);
                    if (existingAccount == null)
                    {
                        return(new ResultMessage(ResultEnum.FailureExistingDataNotFound, "Unable to locate the existing payoff account with the given Id."));
                    }

                    if (existingAccount.Status != PayoffAccountStatusEnum.PendingApproval)
                    {
                        return(new ResultMessage(ResultEnum.FailureStatusError, "You may only make changes to existing payoff accounts if the account is still pending approval."));
                    }

                    if (existingAccount.AccountNumber != updatedAccount.AccountNumber)
                    {
                        existingAccount.PayoffBankAccountsHistories.Add(new PayoffBankAccountsHistory
                        {
                            EntityId      = updatedAccount.CreatedByEntity_Id,
                            UserProfileId = updatedAccount.CreatedBy_Id,
                            TimeStamp     = DateTime.Now,
                            //Username = updatedAccount.CreatedByUsername,
                            Message = $"Account number changed from {existingAccount.AccountNumber} to {updatedAccount.AccountNumber}."
                        });
                        existingAccount.AccountNumber = updatedAccount.AccountNumber;
                    }

                    if (existingAccount.RoutingNumber != updatedAccount.RoutingNumber)
                    {
                        existingAccount.PayoffBankAccountsHistories.Add(new PayoffBankAccountsHistory
                        {
                            EntityId      = updatedAccount.CreatedByEntity_Id,
                            UserProfileId = updatedAccount.CreatedBy_Id,
                            TimeStamp     = DateTime.Now,
                            //Username = updatedAccount.CreatedByUsername,
                            Message = $"Routing number changed from {existingAccount.RoutingNumber} to {updatedAccount.RoutingNumber}."
                        });
                        existingAccount.RoutingNumber = updatedAccount.RoutingNumber;
                    }

                    if (existingAccount.Name != updatedAccount.Name)
                    {
                        existingAccount.PayoffBankAccountsHistories.Add(new PayoffBankAccountsHistory
                        {
                            EntityId      = updatedAccount.CreatedByEntity_Id,
                            UserProfileId = updatedAccount.CreatedBy_Id,
                            TimeStamp     = DateTime.Now,
                            //Username = updatedAccount.CreatedByUsername,
                            Message = $"Name changed from {existingAccount.Name} to {updatedAccount.Name}."
                        });
                        existingAccount.Name = updatedAccount.Name;
                    }

                    if (existingAccount.Description != updatedAccount.Description)
                    {
                        existingAccount.PayoffBankAccountsHistories.Add(new PayoffBankAccountsHistory
                        {
                            EntityId      = updatedAccount.CreatedByEntity_Id,
                            UserProfileId = updatedAccount.CreatedBy_Id,
                            TimeStamp     = DateTime.Now,
                            //Username = updatedAccount.CreatedByUsername,
                            Message = $"Description changed from {(string.IsNullOrEmpty(existingAccount.Description) ? "no description" : existingAccount.Description)} to {(string.IsNullOrEmpty(updatedAccount.Description) ? "no description" : updatedAccount.Description)}."
                        });
                        existingAccount.Description = updatedAccount.Description;
                    }

                    if (existingAccount.BankName != updatedAccount.BankName)
                    {
                        existingAccount.PayoffBankAccountsHistories.Add(new PayoffBankAccountsHistory
                        {
                            EntityId      = updatedAccount.CreatedByEntity_Id,
                            UserProfileId = updatedAccount.CreatedBy_Id,
                            TimeStamp     = DateTime.Now,
                            //Username = updatedAccount.CreatedByUsername,
                            Message = $"Bank Name changed from {existingAccount.BankName} to {updatedAccount.BankName}."
                        });
                        existingAccount.BankName = updatedAccount.BankName;
                    }

                    if (existingAccount.Address1 != updatedAccount.Address1)
                    {
                        existingAccount.PayoffBankAccountsHistories.Add(new PayoffBankAccountsHistory
                        {
                            EntityId      = updatedAccount.CreatedByEntity_Id,
                            UserProfileId = updatedAccount.CreatedBy_Id,
                            TimeStamp     = DateTime.Now,
                            //Username = updatedAccount.CreatedByUsername,
                            Message = $"Address 1 changed from {existingAccount.Address1} to {updatedAccount.Address1}."
                        });
                        existingAccount.Address1 = updatedAccount.Address1;
                    }

                    if (existingAccount.Address2 != updatedAccount.Address2)
                    {
                        existingAccount.PayoffBankAccountsHistories.Add(new PayoffBankAccountsHistory
                        {
                            EntityId      = updatedAccount.CreatedByEntity_Id,
                            UserProfileId = updatedAccount.CreatedBy_Id,
                            TimeStamp     = DateTime.Now,
                            //Username = updatedAccount.CreatedByUsername,
                            Message = $"Address 2 changed from {(string.IsNullOrEmpty(existingAccount.Address2) ? "no address 2" : existingAccount.Address2)} to {(string.IsNullOrEmpty(updatedAccount.Address2) ? "no address 2" : updatedAccount.Address2)}."
                        });
                        existingAccount.Address2 = updatedAccount.Address2;
                    }

                    if (existingAccount.City != updatedAccount.City)
                    {
                        existingAccount.PayoffBankAccountsHistories.Add(new PayoffBankAccountsHistory
                        {
                            EntityId      = updatedAccount.CreatedByEntity_Id,
                            UserProfileId = updatedAccount.CreatedBy_Id,
                            TimeStamp     = DateTime.Now,
                            //Username = updatedAccount.CreatedByUsername,
                            Message = $"City changed from {existingAccount.City} to {updatedAccount.City}."
                        });
                        existingAccount.City = updatedAccount.City;
                    }

                    if (existingAccount.State != updatedAccount.State)
                    {
                        existingAccount.PayoffBankAccountsHistories.Add(new PayoffBankAccountsHistory
                        {
                            EntityId      = updatedAccount.CreatedByEntity_Id,
                            UserProfileId = updatedAccount.CreatedBy_Id,
                            TimeStamp     = DateTime.Now,
                            //Username = updatedAccount.CreatedByUsername,
                            Message = $"State changed from {existingAccount.State} to {updatedAccount.State}."
                        });
                        existingAccount.State = updatedAccount.State;
                    }

                    if (existingAccount.Zip != updatedAccount.Zip)
                    {
                        existingAccount.PayoffBankAccountsHistories.Add(new PayoffBankAccountsHistory
                        {
                            EntityId      = updatedAccount.CreatedByEntity_Id,
                            UserProfileId = updatedAccount.CreatedBy_Id,
                            TimeStamp     = DateTime.Now,
                            //Username = updatedAccount.CreatedByUsername,
                            Message = $"Zip code changed from {existingAccount.Zip} to {updatedAccount.Zip}."
                        });
                        existingAccount.Zip = updatedAccount.Zip;
                    }

                    db.SaveChanges();

                    return(new ResultMessage(ResultEnum.Success, "Payoff account updated."));
                }
            }
            catch (Exception ex)
            {
                // TODO: Log the actual exception
                return(new ResultMessage(ResultEnum.Error, "There was a problem updating the payoff account."));
            }
        }