double currentBalance = 0.0;
int userId = externalUserTransaction.getUserId();
ExternalAccount externalAccount = externalAccountBO.findUserByid(userId);
if(externalAccount != null)
{
currentBalance = externalAccount.getCurrentBalance();
} else{
errors = errors + "User ID not found;";
ModelAndView modelAndView = new ModelAndView("ExternalUserTransactionCreation");
modelAndView.addObject("exttrans", externalUserTransaction);
modelAndView.addObject("currentuser", currentuser);
modelAndView.addObject("errors", errors);
return modelAndView;
}
// Check the current Balance if not return error
if(currentBalance<amount)
{
errors = errors + "No sufficient balance;";
ModelAndView modelAndView = new ModelAndView("ExternalUserTransactionCreation");
modelAndView.addObject("exttrans", externalUserTransaction);
modelAndView.addObject("currentuser", currentuser);
modelAndView.addObject("errors", errors);
return modelAndView;
}
//else process the transaction.
else
{
//Update the current Balance
amount=amount*-1;
currentBalance = currentBalance + amount;
externalAccount.setCurrentBalance(currentBalance);
externalAccountBO.update(externalAccount);
// Create New Transaction
externalUserTransaction.setAmountInvolved(amount);
externalTransactionBO.save(externalUserTransaction);
ModelAndView modelAndView = new ModelAndView("genericsuccess");
String message = "Transaction created successfully!";
modelAndView.addObject("message", message);
return modelAndView;
}
}
else if(externalUserTransaction.getTransType().matches("Credit")){
double amount = externalUserTransaction.getAmountInvolved();
double currentBalance = 0.0;
int userId = externalUserTransaction.getUserId();
ExternalAccount externalAccount = externalAccountBO.findUserByid(userId);
if(externalAccount != null)
{
currentBalance = externalAccount.getCurrentBalance();
currentBalance += amount;
}else{
errors = errors + "User ID not found;";
ModelAndView modelAndView = new ModelAndView("ExternalUserTransactionCreation");
modelAndView.addObject("exttrans", externalUserTransaction);
modelAndView.addObject("currentuser", currentuser);
modelAndView.addObject("errors", errors);
return modelAndView;
}
externalAccount.setCurrentBalance(currentBalance);
externalAccountBO.update(externalAccount);
// Create New Transaction
externalUserTransaction.setAmountInvolved(amount);
externalTransactionBO.save(externalUserTransaction);
ModelAndView modelAndView = new ModelAndView("genericsuccess");
String message = "Transaction created successfully!";
modelAndView.addObject("message", message);
return modelAndView;
}
else {
double amountToTransfer = externalUserTransaction.getAmountInvolved();
double currentSourceBalance = 0.0;
int userId = externalUserTransaction.getUserId();
ExternalAccount sourceExternalAccount = externalAccountBO.findUserByid(userId);
if(sourceExternalAccount != null)
{
currentSourceBalance = sourceExternalAccount.getCurrentBalance();
}else{
errors = errors + "User ID not found;";
ModelAndView modelAndView = new ModelAndView("ExternalUserTransactionCreation");
modelAndView.addObject("exttrans", externalUserTransaction);
modelAndView.addObject("currentuser", currentuser);
modelAndView.addObject("errors", errors);
return modelAndView;
}
// Check the current Balance if not return error
if(currentSourceBalance<amountToTransfer)
{
errors = errors + "No sufficient balance;";
ModelAndView modelAndView = new ModelAndView("ExternalUserTransactionCreation");
modelAndView.addObject("exttrans", externalUserTransaction);
modelAndView.addObject("currentuser", currentuser);
modelAndView.addObject("errors", errors);
return modelAndView;
}
//else process the transaction.
else
{
// Find the account from database where money needs to be transfered.
String accountNumber = externalUserTransaction.getTransDetail();
ExternalAccount destinationExternalAccount = individualUserBO.getAccountByAccountNumber(accountNumber);
if(destinationExternalAccount == null ||(sourceExternalAccount.getAccountNo().equals(destinationExternalAccount.getAccountNo())))
{
errors = errors + "Please enter valid Account Number;";
ModelAndView modelAndView = new ModelAndView("ExternalUserTransactionCreation");
modelAndView.addObject("exttrans", externalUserTransaction);
modelAndView.addObject("currentuser", currentuser);
modelAndView.addObject("errors", errors);
return modelAndView;
}
// Update Source Account Balance
sourceExternalAccount.setCurrentBalance(currentSourceBalance-amountToTransfer);
externalAccountBO.update(sourceExternalAccount);
// Create Transaction For Source Account
externalUserTransaction.setTransDetail("Transfer To A/C" + accountNumber);
externalUserTransaction.setAmountInvolved(amountToTransfer*-1);
externalTransactionBO.save(externalUserTransaction);
// Update Destination Account Balance
double destinationBalance = destinationExternalAccount.getCurrentBalance();
destinationBalance+=amountToTransfer;
destinationExternalAccount.setCurrentBalance(destinationBalance);
externalAccountBO.update(destinationExternalAccount);
//Create Transaction for Destination Balance
externalUserTransaction.setUserId(destinationExternalAccount.getUserid());
externalUserTransaction.setTransDetail("Transfer From A/C" + sourceExternalAccount.getAccountNo());
externalUserTransaction.setAmountInvolved(amountToTransfer);