Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -500,15 +500,21 @@ public AccountTransferDetails repayLoanWithTopup(AccountTransferDTO accountTrans

@Override
public CommandProcessingResult undo(JsonCommand command) {
AccountTransferDetails accountTransferDetails = accountTransferDetailRepository.findById(command.entityId())
.orElseThrow(() -> new AccountTransferNotFoundException(command.entityId()));

if (accountTransferDetails.getAccountTransferTransactions().stream().anyMatch(AccountTransferTransaction::isReversed)) {
final Long accountTransferId = command.entityId();
// accountTransferId is the id exposed by the transfer read/list APIs, i.e. m_account_transfer_transaction.id.
// Resolving against that table (rather than m_account_transfer_details) means the lookup always matches
// what a caller can actually see, and reversal is scoped to this single transaction only - not every
// transaction sharing the same details record (e.g. a recurring standing instruction).
final AccountTransferTransaction transaction = accountTransferRepository.findById(accountTransferId)
.orElseThrow(() -> new AccountTransferNotFoundException(accountTransferId));

if (transaction.isReversed()) {
throw new GeneralPlatformDomainRuleException("error.msg.account.transfer.already.reversed",
"Account transfer is already reverted", command.entityId());
"Account transfer is already reverted", accountTransferId);
}

final PaymentDetail paymentDetail = null;
final AccountTransferDetails accountTransferDetails = transaction.getAccountTransferDetails();

PortfolioAccountType fromAccountType = accountTransferDetails.fromLoanAccount() != null ? PortfolioAccountType.LOAN
: accountTransferDetails.fromSavingsAccount() != null ? PortfolioAccountType.SAVINGS : throwUnsupported();
Expand All @@ -517,32 +523,28 @@ public CommandProcessingResult undo(JsonCommand command) {
: accountTransferDetails.toSavingsAccount() != null ? PortfolioAccountType.SAVINGS : throwUnsupported();

if (isSavingsToSavingsAccountTransfer(fromAccountType, toAccountType)) {
accountTransferDetails.getAccountTransferTransactions().forEach(transaction -> {
this.savingsAccountWritePlatformService.undoTransaction(transaction.getFromSavingsTransaction().getSavingsAccount().getId(),
transaction.getFromSavingsTransaction().getId(), true);
this.savingsAccountWritePlatformService.undoTransaction(transaction.getToSavingsTransaction().getSavingsAccount().getId(),
transaction.getToSavingsTransaction().getId(), true);
transaction.reverse();
});
this.savingsAccountWritePlatformService.undoTransaction(transaction.getFromSavingsTransaction().getSavingsAccount().getId(),
transaction.getFromSavingsTransaction().getId(), true);
this.savingsAccountWritePlatformService.undoTransaction(transaction.getToSavingsTransaction().getSavingsAccount().getId(),
transaction.getToSavingsTransaction().getId(), true);
transaction.reverse();
} else if (isSavingsToLoanAccountTransfer(fromAccountType, toAccountType)) {
accountTransferDetails.getAccountTransferTransactions().forEach(transaction -> {
this.savingsAccountWritePlatformService.undoTransaction(transaction.getFromSavingsTransaction().getSavingsAccount().getId(),
transaction.getFromSavingsTransaction().getId(), true);
final ExternalId reversalTxnExternalId = externalIdFactory.create();
LoanAdjustmentParameter parameter = LoanAdjustmentParameter.builder().transactionAmount(BigDecimal.ZERO)
.paymentDetail(paymentDetail).transactionDate(transaction.getToLoanTransaction().getTransactionDate())
.txnExternalId(transaction.getToLoanTransaction().getExternalId()).reversalTxnExternalId(reversalTxnExternalId)
.noteText(null).build();
this.loanAdjustmentService.adjustLoanTransaction(transaction.getToLoanTransaction().getLoan(),
transaction.getToLoanTransaction(), parameter, null, new HashMap<>());
transaction.reverse();
});
this.savingsAccountWritePlatformService.undoTransaction(transaction.getFromSavingsTransaction().getSavingsAccount().getId(),
transaction.getFromSavingsTransaction().getId(), true);
final ExternalId reversalTxnExternalId = externalIdFactory.create();
LoanAdjustmentParameter parameter = LoanAdjustmentParameter.builder().transactionAmount(BigDecimal.ZERO)
.paymentDetail(paymentDetail).transactionDate(transaction.getToLoanTransaction().getTransactionDate())
.txnExternalId(transaction.getToLoanTransaction().getExternalId()).reversalTxnExternalId(reversalTxnExternalId)
.noteText(null).build();
this.loanAdjustmentService.adjustLoanTransaction(transaction.getToLoanTransaction().getLoan(),
transaction.getToLoanTransaction(), parameter, null, new HashMap<>());
transaction.reverse();
} else if (isLoanToSavingsAccountTransfer(fromAccountType, toAccountType)) {
throw new UnsupportedOperationException("Undo Loan to Savings Account Transfer is not implemented");
}

final CommandProcessingResultBuilder builder = new CommandProcessingResultBuilder() //
.withEntityId(accountTransferDetails.getId());
.withEntityId(transaction.getId());

return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,5 @@
<include file="parts/0238_transaction_summary_report_update_originator_inline_join.xml" relativeToChangelogFile="true" />
<include file="parts/0239_add_retained_earning_job.xml" relativeToChangelogFile="true"/>
<include file="parts/0240_add_retained_earning_global_configuration.xml" relativeToChangelogFile="true"/>
<include file="parts/0242_add_undo_accounttransfer_permission.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.

-->
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.3.xsd">
<changeSet id="1" author="fineract">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">
SELECT COUNT(1) FROM m_permission WHERE code = 'UNDO_ACCOUNTTRANSFER'
</sqlCheck>
</preConditions>
<insert tableName="m_permission">
<column name="grouping" value="transaction_savings"/>
<column name="code" value="UNDO_ACCOUNTTRANSFER"/>
<column name="entity_name" value="ACCOUNTTRANSFER"/>
<column name="action_name" value="UNDO"/>
<column name="can_maker_checker" valueBoolean="false"/>
</insert>
</changeSet>

</databaseChangeLog>
Loading