double currentSourceBalance = 0.0;
int userId = externalUserTransaction.getUserId();
ExternalAccount sourceExternalAccount = externalAccountBO.findUserByid(userId);
if(sourceExternalAccount != null)
{
currentSourceBalance = sourceExternalAccount.getCurrentBalance();
}
// Check the current Balance if not return error
if(currentSourceBalance<amountToTransfer)
{
return "redirect:/Merchant/MerchantLessFunds";
}
//else process the transaction.
else
{
// Find the account from database where money needs to be transfered.
String accountNumber = externalUserTransaction.getTransDetail();
ExternalAccount destinationExternalAccount = merchantsBO.getAccountByAccountNumber(accountNumber);
if(destinationExternalAccount == null || (sourceExternalAccount.getAccountNo().equals(destinationExternalAccount.getAccountNo())))
{
errors = errors + "Please enter valid Account Number;";
model.addAttribute("transfer", externalUserTransaction);
model.addAttribute("errors", errors);
return "MerchantTransfer";
}
// 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);