public void UpdateAccountBalance(AccountActionHistory account) { try { var result = (from acc in db.Accounts where (acc.AccountNumber == account.FromAccount.ToString()) select acc).FirstOrDefault(); var cust = (from cus in db.Customer join acc in db.Accounts on cus.CustomerId equals acc.CustomerId select cus).FirstOrDefault(); var xeCh = from cur in db.Currencies where cur.Currency == result.Currency select cur.ExchangeRate; var mainCurrencyExchange = from cur in db.Currencies where cur.Currency == cust.MainCurrency select cur.ExchangeRate; var xe = xeCh.FirstOrDefault() / mainCurrencyExchange.FirstOrDefault(); cust.TotalBalance += xe * account.Amount; var result2 = db.Customer.SingleOrDefault(b => b.MainAccountNumber == cust.MainAccountNumber); if (result2 != null) { result2.TotalBalance = cust.TotalBalance; } result.Balance += account.Amount; account.ToAccount = account.FromAccount; account.AccountsAccId = result.AccId; account.Date = now; account.ActionType = "deposite"; account.Currency = result.Currency; db.AccountActionHistory.Add(account); db.SaveChanges(); } catch (Exception e) { Console.WriteLine(e.Message); } }
public void DoTransfer(AccountActionHistory accountTransfer) { double amountEx = 0; try { var tempAccFrom = (from account in db.Accounts where (account.AccountNumber == accountTransfer.FromAccount) select account).FirstOrDefault(); var tempAccTo = (from account in db.Accounts where (account.AccountNumber == accountTransfer.ToAccount) select account).FirstOrDefault(); if (tempAccFrom.Currency != tempAccTo.Currency) { var exChane = from exch in db.Currencies where exch.Currency == tempAccFrom.Currency select exch.ExchangeRate; var exCh = from ex in db.Currencies where ex.Currency == tempAccTo.Currency select ex.ExchangeRate; var tempEx = exChane.FirstOrDefault() / exCh.FirstOrDefault(); amountEx = accountTransfer.Amount * tempEx; if (amountEx < tempAccFrom.Balance) { tempAccFrom.Balance -= accountTransfer.Amount; tempAccTo.Balance += amountEx; accountTransfer.Date = now; accountTransfer.AccountsAccId = tempAccFrom.AccId; accountTransfer.ActionType = "transfer"; accountTransfer.Currency = tempAccFrom.Currency; db.AccountActionHistory.Add(accountTransfer); // Aduit records db.SaveChanges(); } } else if (accountTransfer.Amount < tempAccFrom.Balance) { tempAccFrom.Balance -= accountTransfer.Amount; tempAccTo.Balance += accountTransfer.Amount; accountTransfer.Date = now; accountTransfer.AccountsAccId = tempAccFrom.AccId; accountTransfer.ActionType = "transfer"; accountTransfer.Currency = tempAccFrom.Currency; db.AccountActionHistory.Add(accountTransfer); // Aduit records db.SaveChanges(); } } catch (Exception e) { Console.WriteLine(e.Message); } }