public bool ExternalTransfer(int dest, decimal amount, string notes) { var ret = false; Raise.ArgumentException.IfNot(AccountExists(dest), "", "Invalid account mentioned"); Raise.ArgumentException.If(amount == 0.0M, "", "Amount cannot be empty"); var account = _context.Accounts.FirstOrDefault(x => x.AccountNo == dest); if (amount < 0.0M && Math.Abs(amount) <= account.Balance) { //Withdrawn var trx = new Domain.Transaction { TransactionId = _context.TransactionId(), SourceNo = dest, AccountSource = account, TargetNo = null, AccountTarget = null, Amount = amount, PostedOn = DateTime.Now, Notes = notes }; account.Balance -= amount; _context.Transactions.Add(trx); } else { //Deposit var trx = new Domain.Transaction { TransactionId = _context.TransactionId(), TargetNo = dest, AccountTarget = account, SourceNo = null, AccountSource = null, Amount = amount, PostedOn = DateTime.Now, Notes = notes }; account.Balance += amount; _context.Transactions.Add(trx); } _context.SaveChanges(); ret = true; _log.Info($"{(amount > 0.0M ? "debit" : "credit")} has been made into {account.AccountName}: {account.AccountNo}"); return(ret); }
public bool InternalTransfer(int sourceAcc, int destAcc, decimal amount, string notes) { var ret = false; Raise.ArgumentException.If(amount <= 0, "", $"Empty transaction cannot be performed, amount is {amount}"); Raise.ArgumentException.IfNot(AccountExists(sourceAcc), "", $"Source account {sourceAcc} doe snot exist"); Raise.ArgumentException.IfNot(AccountExists(destAcc), $"Destination account { destAcc} does not exist"); var sourceAccount = _context.Accounts.FirstOrDefault(x => x.AccountNo == sourceAcc); var targetAccount = _context.Accounts.FirstOrDefault(x => x.AccountNo == destAcc); Raise.ArgumentException.If(amount > sourceAccount.Balance, "", $"Insufficient funds in the account {sourceAcc}"); var trx = new Domain.Transaction { TransactionId = _context.TransactionId(), SourceNo = sourceAcc, TargetNo = destAcc, AccountSource = sourceAccount, AccountTarget = targetAccount, Amount = amount, PostedOn = DateTime.Now, Notes = "" }; sourceAccount.Balance -= amount; targetAccount.Balance += amount; _context.Transactions.Add(trx); _context.SaveChanges(); ret = true; _log.Info($"${amount} has been moved from {sourceAcc} to {destAcc} successfully"); return(ret); }