@Override
 @Transactional
 public void reverseAllTransactions(
     final Long accountId, final PortfolioAccountType accountTypeId) {
   List<AccountTransfer> acccountTransfers = null;
   if (accountTypeId.isLoanAccount()) {
     acccountTransfers = this.accountTransferRepository.findAllByLoanId(accountId);
   }
   if (acccountTransfers != null && acccountTransfers.size() > 0) {
     undoTransactions(acccountTransfers);
   }
 }
 @Override
 @Transactional
 public void reverseTransfersWithFromAccountType(
     final Long accountNumber, final PortfolioAccountType accountTypeId) {
   List<AccountTransfer> acccountTransfers = null;
   if (accountTypeId.isLoanAccount()) {
     acccountTransfers = this.accountTransferRepository.findByFromLoanId(accountNumber);
   }
   if (acccountTransfers != null && acccountTransfers.size() > 0) {
     undoTransactions(acccountTransfers);
   }
 }
  @Override
  public boolean isAccountTransfer(Long transactionId, PortfolioAccountType accountType) {
    StringBuilder sql =
        new StringBuilder("select count(*) from m_savings_account_transfer at where ");
    if (accountType.isLoanAccount()) {
      sql.append("at.from_loan_transaction_id=")
          .append(transactionId)
          .append(" or at.to_loan_transaction_id=")
          .append(transactionId);
    } else {
      sql.append("at.from_savings_transaction_id=")
          .append(transactionId)
          .append(" or at.to_savings_transaction_id=")
          .append(transactionId);
    }

    int count = this.jdbcTemplate.queryForInt(sql.toString());
    return count > 0;
  }
  @Transactional
  @Override
  public CommandProcessingResult create(final JsonCommand command) {

    this.accountTransfersDataValidator.validate(command);

    final LocalDate transactionDate = command.localDateValueOfParameterNamed(transferDateParamName);
    final BigDecimal transactionAmount =
        command.bigDecimalValueOfParameterNamed(transferAmountParamName);

    final Locale locale = command.extractLocale();
    final DateTimeFormatter fmt =
        DateTimeFormat.forPattern(command.dateFormat()).withLocale(locale);

    final Integer fromAccountTypeId =
        command.integerValueSansLocaleOfParameterNamed(fromAccountTypeParamName);
    final PortfolioAccountType fromAccountType = PortfolioAccountType.fromInt(fromAccountTypeId);

    final Integer toAccountTypeId =
        command.integerValueSansLocaleOfParameterNamed(toAccountTypeParamName);
    final PortfolioAccountType toAccountType = PortfolioAccountType.fromInt(toAccountTypeId);

    final PaymentDetail paymentDetail = null;
    Long fromSavingsAccountId = null;
    Long transferTransactionId = null;
    if (isSavingsToSavingsAccountTransfer(fromAccountType, toAccountType)) {

      fromSavingsAccountId = command.longValueOfParameterNamed(fromAccountIdParamName);
      final SavingsAccount fromSavingsAccount =
          this.savingsAccountAssembler.assembleFrom(fromSavingsAccountId);

      final SavingsAccountTransaction withdrawal =
          this.savingsAccountDomainService.handleWithdrawal(
              fromSavingsAccount,
              fmt,
              transactionDate,
              transactionAmount,
              paymentDetail,
              fromSavingsAccount.isWithdrawalFeeApplicableForTransfer());

      final Long toSavingsId = command.longValueOfParameterNamed(toAccountIdParamName);
      final SavingsAccount toSavingsAccount =
          this.savingsAccountAssembler.assembleFrom(toSavingsId);

      final SavingsAccountTransaction deposit =
          this.savingsAccountDomainService.handleDeposit(
              toSavingsAccount, fmt, transactionDate, transactionAmount, paymentDetail);

      final AccountTransfer transferTransaction =
          this.accountTransferAssembler.assembleSavingsToSavingsTransfer(
              command, withdrawal, deposit);
      this.accountTransferRepository.saveAndFlush(transferTransaction);
      transferTransactionId = transferTransaction.getId();

    } else if (isSavingsToLoanAccountTransfer(fromAccountType, toAccountType)) {
      //
      fromSavingsAccountId = command.longValueOfParameterNamed(fromAccountIdParamName);
      final SavingsAccount fromSavingsAccount =
          this.savingsAccountAssembler.assembleFrom(fromSavingsAccountId);

      final SavingsAccountTransaction withdrawal =
          this.savingsAccountDomainService.handleWithdrawal(
              fromSavingsAccount,
              fmt,
              transactionDate,
              transactionAmount,
              paymentDetail,
              fromSavingsAccount.isWithdrawalFeeApplicableForTransfer());

      final Long toLoanAccountId = command.longValueOfParameterNamed(toAccountIdParamName);
      final Loan toLoanAccount = this.loanAccountAssembler.assembleFrom(toLoanAccountId);

      final LoanTransaction loanRepaymentTransaction =
          this.loanAccountDomainService.makeRepayment(
              toLoanAccount,
              new CommandProcessingResultBuilder(),
              transactionDate,
              transactionAmount,
              paymentDetail,
              null,
              null);

      final AccountTransfer transferTransaction =
          this.accountTransferAssembler.assembleSavingsToLoanTransfer(
              command, fromSavingsAccount, toLoanAccount, withdrawal, loanRepaymentTransaction);
      this.accountTransferRepository.saveAndFlush(transferTransaction);
      transferTransactionId = transferTransaction.getId();

    } else if (isLoanToSavingsAccountTransfer(fromAccountType, toAccountType)) {
      // FIXME - kw - ADD overpaid loan to savings account transfer
      // support.

      //
      final Long fromLoanAccountId = command.longValueOfParameterNamed(fromAccountIdParamName);
      final Loan fromLoanAccount = this.loanAccountAssembler.assembleFrom(fromLoanAccountId);

      final LoanTransaction loanRefundTransaction =
          this.loanAccountDomainService.makeRefund(
              fromLoanAccountId,
              new CommandProcessingResultBuilder(),
              transactionDate,
              transactionAmount,
              paymentDetail,
              null,
              null);

      final Long toSavingsAccountId = command.longValueOfParameterNamed(toAccountIdParamName);
      final SavingsAccount toSavingsAccount =
          this.savingsAccountAssembler.assembleFrom(toSavingsAccountId);

      final SavingsAccountTransaction deposit =
          this.savingsAccountDomainService.handleDeposit(
              toSavingsAccount, fmt, transactionDate, transactionAmount, paymentDetail);

      final AccountTransfer transferTransaction =
          this.accountTransferAssembler.assembleLoanToSavingsTransfer(
              command, fromLoanAccount, toSavingsAccount, deposit, loanRefundTransaction);
      this.accountTransferRepository.saveAndFlush(transferTransaction);
      transferTransactionId = transferTransaction.getId();

    } else {

    }

    final CommandProcessingResultBuilder builder =
        new CommandProcessingResultBuilder().withEntityId(transferTransactionId);

    if (fromAccountType.isSavingsAccount()) {

      builder.withSavingsId(fromSavingsAccountId);
    }

    return builder.build();
  }
 private boolean isSavingsToSavingsAccountTransfer(
     final PortfolioAccountType fromAccountType, final PortfolioAccountType toAccountType) {
   return fromAccountType.isSavingsAccount() && toAccountType.isSavingsAccount();
 }