private bool UpdateDbAccountBalances(Transaction transaction, EventArgumentEnum eventArgument) { try { switch (eventArgument) { case EventArgumentEnum.Create: { if (transaction.DebitAccount != null && transaction.DebitAccount.Id != 0) { var originalBalance = transaction.DebitAccount.Balance; // logs beginning balance transaction.DebitAccount.Balance += transaction.Amount; Logger.Instance.Calculation($"{transaction.DebitAccount.Name}.balance ({originalBalance}) + {transaction.Amount} = {transaction.DebitAccount.Balance}"); // Update Account's required savings transaction.CreditAccount.BalanceSurplus = _calc.UpdateBalanceSurplus(transaction.CreditAccount); _db.Entry(transaction.DebitAccount).State = EntityState.Modified; } if (transaction.CreditAccount != null && transaction.CreditAccount.Id != 0) { var originalBalance = transaction.CreditAccount.Balance; // logs beginning balance transaction.CreditAccount.Balance -= transaction.Amount; Logger.Instance.Calculation($"{transaction.CreditAccount.Name}.balance ({originalBalance}) + {transaction.Amount} = {transaction.CreditAccount.Balance}"); // Update Account's required savings transaction.CreditAccount.BalanceSurplus = _calc.UpdateBalanceSurplus(transaction.CreditAccount); _db.Entry(transaction.CreditAccount).State = EntityState.Modified; } _db.SaveChanges(); return(true); } case EventArgumentEnum.Delete: case EventArgumentEnum.Update: { var accountManager = new AccountManager(); var calculations = new Calculations(); var originalTransaction = _db.Transactions .AsNoTracking() .Where(t => t.Id == transaction.Id) .Cast <Transaction>() .FirstOrDefault(); if (originalTransaction == null) { return(false); } var originalCreditAccount = _db.Accounts.FirstOrDefault(a => a.Id == (originalTransaction.CreditAccountId ?? 0)); var originalDebitAccount = transaction.Type == TransactionTypesEnum.Income ? accountManager.GetPoolAccount() : _db.Accounts.FirstOrDefault(a => a.Id == (originalTransaction.DebitAccountId ?? 0)); var originalAmount = originalTransaction.Amount; // Reassign the Debit/Credit Account Id's to Transaction Model transaction.CreditAccountId = originalTransaction.CreditAccountId; transaction.DebitAccountId = originalTransaction.DebitAccountId; if (transaction.Type == TransactionTypesEnum.Income) { var accounts = accountManager.GetAllAccounts(); switch (eventArgument) { case EventArgumentEnum.Delete: { if (originalDebitAccount != null) { // Pool all Account balances (including deficits) foreach (Account account in accounts.Where(a => a.Balance != 0.0m)) { try { if (account.Balance > 0.0m) { var balance = account.Balance; account.Balance -= balance; originalDebitAccount.Balance += balance; } else if (account.Balance < 0.0m) { var deficit = account.Balance * -1; account.Balance -= deficit; originalDebitAccount.Balance -= deficit; } account.BalanceSurplus = calculations.UpdateBalanceSurplus(account); } catch (Exception e) { Logger.Instance.Error(e); } } // Subtract the amount of the income transaction from the Pool originalDebitAccount.Balance -= transaction.Amount; // If there's still a balance left, rebalance Accounts with a negative balance surplus if (originalDebitAccount.Balance > 0.0m) { foreach (Account account in accounts.Where(a => !a.ExcludeFromSurplus)) { try { if (account.BalanceSurplus > 0.0m) { var balance = account.Balance; account.Balance -= balance; originalDebitAccount.Balance += balance; } else { var deficit = account.BalanceSurplus * -1; if (originalDebitAccount.Balance < deficit) { account.Balance += originalDebitAccount.Balance; originalDebitAccount.Balance -= originalDebitAccount.Balance; } else // Make account whole { account.Balance += deficit; originalDebitAccount.Balance -= deficit; } } account.BalanceSurplus = calculations.UpdateBalanceSurplus(account); _db.Entry(account).State = EntityState.Modified; } catch (Exception e) { Logger.Instance.Error(e); } } } } else { Logger.Instance.Debug("Debit account for income transaction is null"); } if (_db.ChangeTracker.HasChanges()) { _db.SaveChanges(); } return(true); } case EventArgumentEnum.Update: { if (originalDebitAccount != null) { // Pool Account balances (including deficits) foreach (Account account in accounts.Where(a => a.Balance != 0.0m)) { try { if (account.Balance > 0.0m) { var balance = account.Balance; account.Balance -= balance; originalDebitAccount.Balance += balance; } else if (account.Balance < 0.0m) { var deficit = account.Balance * -1; account.Balance -= deficit; originalDebitAccount.Balance -= deficit; } account.BalanceSurplus = calculations.UpdateBalanceSurplus(account); } catch (Exception e) { Logger.Instance.Error(e); } } // Subtract difference of the updated transaction and the original transaction amount originalDebitAccount.Balance += transaction.Amount - originalAmount; // if there's a balance left, rebalance Accounts with a negative balance surplus if (originalDebitAccount.Balance > 0.0m) { foreach (Account account in accounts.Where(a => !a.ExcludeFromSurplus)) { try { if (account.BalanceSurplus > 0.0m) { var balance = account.Balance; account.Balance -= balance; originalDebitAccount.Balance += balance; } else { var deficit = account.BalanceSurplus * -1; if (originalDebitAccount.Balance < deficit) { account.Balance += originalDebitAccount.Balance; originalDebitAccount.Balance -= originalDebitAccount.Balance; } else // Make account whole { account.Balance += deficit; originalDebitAccount.Balance -= deficit; } } account.BalanceSurplus = calculations.UpdateBalanceSurplus(account); _db.Entry(account).State = EntityState.Modified; } catch (Exception e) { Logger.Instance.Error(e); } } } } else { Logger.Instance.Debug("Debit account for income transaction is null"); } if (_db.ChangeTracker.HasChanges()) { _db.SaveChanges(); } return(true); } default: throw new NotImplementedException(); } } switch (eventArgument) { case EventArgumentEnum.Delete: { if (originalDebitAccount != null) { originalDebitAccount.Balance -= transaction.Amount; // Update Account's required savings originalDebitAccount.BalanceSurplus = _calc.UpdateBalanceSurplus(originalDebitAccount); _db.Entry(originalDebitAccount).State = EntityState.Modified; } if (originalCreditAccount != null) { originalCreditAccount.Balance += transaction.Amount; // Update Account's required savings originalCreditAccount.BalanceSurplus = _calc.UpdateBalanceSurplus(originalCreditAccount); _db.Entry(originalCreditAccount).State = EntityState.Modified; } _db.SaveChanges(); return(true); } case EventArgumentEnum.Update: { var amountDifference = transaction.Amount - originalAmount; if (originalDebitAccount != null) { originalDebitAccount.Balance += amountDifference; originalDebitAccount.BalanceSurplus = _calc.UpdateBalanceSurplus(originalDebitAccount); _db.Entry(originalDebitAccount).State = EntityState.Modified; } if (originalCreditAccount != null) { originalCreditAccount.Balance -= amountDifference; originalCreditAccount.BalanceSurplus = _calc.UpdateBalanceSurplus(originalCreditAccount); _db.Entry(originalCreditAccount).State = EntityState.Modified; } _db.SaveChanges(); return(true); } default: throw new NotImplementedException(); } } default: throw new NotImplementedException($"{eventArgument} is not an accepted type for TransactionController.UpdateDbAccountBalances method"); } } catch (Exception e) { Logger.Instance.Error(e); return(false); } }
private async Task <bool> UpdateDbAccountBalances(Transaction transaction, EventArgumentEnum eventArgument, bool save = true) { try { switch (eventArgument) { case EventArgumentEnum.Create: { if (transaction.DebitAccount != null && transaction.DebitAccount.Id != 0) { var originalBalance = transaction.DebitAccount.Balance; // logs beginning balance transaction.DebitAccount.Balance += transaction.Amount; // Update Account's required savings var balanceSurplus = AccountService.UpdateBalanceSurplus(transaction.DebitAccount); transaction.DebitAccount.BalanceSurplus = balanceSurplus; _accountService.UpdateAccount(transaction.DebitAccount, false); } if (transaction.CreditAccount != null && transaction.CreditAccount.Id != 0) { var originalBalance = transaction.CreditAccount.Balance; // logs beginning balance transaction.CreditAccount.Balance -= transaction.Amount; // Update Account's required savings transaction.CreditAccount.BalanceSurplus = AccountService.UpdateBalanceSurplus(transaction.CreditAccount); _accountService.UpdateAccount(transaction.CreditAccount, false); } if (save) { _accountService.SaveAccount(); } return(true); } case EventArgumentEnum.Delete: case EventArgumentEnum.Update: { //var originalTransaction = _db.Transactions // .AsNoTracking() // .Where(t => t.Id == transaction.Id) // .Cast<Transaction>() // .FirstOrDefault(); var originalTransaction = await _transactionRepository.GetTransactionByIdAsync(transaction.Id); if (originalTransaction == null) { return(false); } var originalCreditAccount = await _accountService.GetAccountByIdAsync(originalTransaction.CreditAccountId ?? 0); var originalDebitAccount = transaction.Type == TransactionTypesEnum.Income ? await _accountService.GetDisposableIncomeAccountAsync() : await _accountService.GetAccountByIdAsync(originalTransaction.DebitAccountId ?? 0); var originalAmount = originalTransaction.Amount; // Reassign the Debit/Credit Account Id's to Transaction Model transaction.CreditAccountId = originalTransaction.CreditAccountId; transaction.DebitAccountId = originalTransaction.DebitAccountId; if (transaction.Type == TransactionTypesEnum.Income) { var accounts = await _accountService.GetAllAccountsAsync(); var enumerable1 = accounts.ToList(); var enumerable = enumerable1.ToList(); switch (eventArgument) { case EventArgumentEnum.Delete: { if (originalDebitAccount != null) { // Pool all Account balances (including deficits) foreach (Account account in enumerable.Where(a => a.Balance != 0.0m)) { try { if (account.Balance > 0.0m) { var balance = account.Balance; account.Balance -= balance; originalDebitAccount.Balance += balance; } else if (account.Balance < 0.0m) { var deficit = account.Balance * -1; account.Balance -= deficit; originalDebitAccount.Balance -= deficit; } account.BalanceSurplus = AccountService.UpdateBalanceSurplus(account); } catch (SqlException e) { Logger.Instance.Error(e); return(false); } catch (Exception ex) { Logger.Instance.Error(ex); return(false); } } // Subtract the amount of the income transaction from the Pool originalDebitAccount.Balance -= transaction.Amount; // If there's still a balance left, rebalance Accounts with a negative balance surplus if (originalDebitAccount.Balance > 0.0m) { foreach (Account account in enumerable.Where(a => !a.ExcludeFromSurplus)) { try { if (account.BalanceSurplus > 0.0m) { var balance = account.Balance; account.Balance -= balance; originalDebitAccount.Balance += balance; } else { var deficit = account.BalanceSurplus * -1; if (originalDebitAccount.Balance < deficit) { account.Balance += originalDebitAccount.Balance; originalDebitAccount.Balance -= originalDebitAccount.Balance; } else // Make account whole { account.Balance += deficit; originalDebitAccount.Balance -= deficit; } } account.BalanceSurplus = AccountService.UpdateBalanceSurplus(account); _accountService.UpdateAccount(account, false); } catch (SqlException e) { Logger.Instance.Error(e); return(false); } catch (Exception ex) { Logger.Instance.Error(ex); return(false); } } } } else { Logger.Instance.Debug("Debit account for income transaction is null"); } //TODO: expose context through services to update db or use repository pattern? if (_accountService.Context.ChangeTracker.HasChanges() && save) { _accountService.SaveAccount(); } return(true); } case EventArgumentEnum.Update: { if (originalDebitAccount != null) { // Pool Account balances (including deficits) foreach (Account account in enumerable1.Where(a => a.Balance != 0.0m)) { try { if (account.Balance > 0.0m) { var balance = account.Balance; account.Balance -= balance; originalDebitAccount.Balance += balance; } else if (account.Balance < 0.0m) { var deficit = account.Balance * -1; account.Balance -= deficit; originalDebitAccount.Balance -= deficit; } account.BalanceSurplus = AccountService.UpdateBalanceSurplus(account); } catch (SqlException e) { Logger.Instance.Error(e); return(false); } catch (Exception ex) { Logger.Instance.Error(ex); return(false); } } // Subtract difference of the updated transaction and the original transaction amount originalDebitAccount.Balance += transaction.Amount - originalAmount; // if there's a balance left, rebalance Accounts with a negative balance surplus if (originalDebitAccount.Balance > 0.0m) { foreach (Account account in enumerable.Where(a => !a.ExcludeFromSurplus)) { try { if (account.BalanceSurplus > 0.0m) { var balance = account.Balance; account.Balance -= balance; originalDebitAccount.Balance += balance; } else { var deficit = account.BalanceSurplus * -1; if (originalDebitAccount.Balance < deficit) { account.Balance += originalDebitAccount.Balance; originalDebitAccount.Balance -= originalDebitAccount.Balance; } else // Make account whole { account.Balance += deficit; originalDebitAccount.Balance -= deficit; } } account.BalanceSurplus = AccountService.UpdateBalanceSurplus(account); _accountService.UpdateAccount(account, false); } catch (SqlException e) { Logger.Instance.Error(e); return(false); } catch (Exception ex) { Logger.Instance.Error(ex); return(false); } } } } else { Logger.Instance.Debug("Debit account for income transaction is null"); } if (_accountService.Context.ChangeTracker.HasChanges() && save) { _accountService.SaveAccount(); } return(true); } default: throw new NotImplementedException(); } } switch (eventArgument) { case EventArgumentEnum.Delete: { if (originalDebitAccount != null) { originalDebitAccount.Balance -= transaction.Amount; // Update Account's required savings originalDebitAccount.BalanceSurplus = AccountService.UpdateBalanceSurplus(originalDebitAccount); _accountService.UpdateAccount(originalDebitAccount, false); } if (originalCreditAccount != null) { originalCreditAccount.Balance += transaction.Amount; // Update Account's required savings originalCreditAccount.BalanceSurplus = AccountService.UpdateBalanceSurplus(originalCreditAccount); _accountService.UpdateAccount(originalCreditAccount, false); } if (save) { _accountService.SaveAccount(); } return(true); } case EventArgumentEnum.Update: { var amountDifference = transaction.Amount - originalAmount; if (originalDebitAccount != null) { originalDebitAccount.Balance += amountDifference; originalDebitAccount.BalanceSurplus = AccountService.UpdateBalanceSurplus(originalDebitAccount); _accountService.UpdateAccount(originalDebitAccount, false); } if (originalCreditAccount != null) { originalCreditAccount.Balance -= amountDifference; originalCreditAccount.BalanceSurplus = AccountService.UpdateBalanceSurplus(originalCreditAccount); _accountService.UpdateAccount(originalCreditAccount, false); } if (save) { _accountService.SaveAccount(); } return(true); } default: throw new NotImplementedException(); } } default: throw new NotImplementedException($"{eventArgument} is not an accepted type for TransactionController.UpdateDbAccountBalances method"); } } catch (SqlException e) { Logger.Instance.Error(e); return(false); } catch (Exception ex) { Logger.Instance.Error(ex); return(false); } }