mariiaKraievska commented on code in PR #4867:
URL: https://github.com/apache/fineract/pull/4867#discussion_r2219088544


##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanReadPlatformServiceImpl.java:
##########
@@ -2448,6 +2451,44 @@ public boolean existsByLoanId(Long loanId) {
         return loanRepositoryWrapper.existsByLoanId(loanId);
     }
 
+    @Override
+    public LoanTransactionData retrieveManualInterestRefundTemplate(final Long 
loanId, final Long targetTransactionId) {
+        final Loan loan = 
loanRepositoryWrapper.findOneWithNotFoundDetection(loanId);
+        final LoanTransaction targetTxn = loan.getLoanTransaction(txn -> 
txn.getId() != null && txn.getId().equals(targetTransactionId));
+        if (targetTxn == null) {
+            throw new LoanTransactionNotFoundException(targetTransactionId);
+        }
+        if (!(targetTxn.isMerchantIssuedRefund() || 
targetTxn.isPayoutRefund())) {
+            throw new 
GeneralPlatformDomainRuleException("error.msg.loan.transaction.not.refund.type",
 "Only for refund transactions");
+        }
+        if (targetTxn.isReversed()) {
+            throw new 
GeneralPlatformDomainRuleException("error.msg.loan.transaction.reversed", 
"Refund transaction is reversed");
+        }
+        final boolean alreadyExists = 
loan.getLoanTransactions().stream().anyMatch(txn -> txn.isInterestRefund() && 
!txn
+                .getLoanTransactionRelations(rel -> rel.getToTransaction() != 
null && rel.getToTransaction().equals(targetTxn)).isEmpty());
+        if (alreadyExists) {
+            throw new 
GeneralPlatformDomainRuleException("error.msg.loan.transaction.interest.refund.already.exists",
+                    "Interest Refund already exists for this refund");
+        }
+
+        final InterestRefundService interestRefundService = 
interestRefundServiceDelegate.lookupInterestRefundService(loan);
+        final Money totalInterest = 
interestRefundService.totalInterestByTransactions(null, loan.getId(), 
targetTxn.getTransactionDate(),
+                List.of(), 
loan.getLoanTransactions().stream().map(AbstractPersistableCustom::getId).toList());
+        final Money newTotalInterest = 
interestRefundService.totalInterestByTransactions(null, loan.getId(), 
targetTxn.getTransactionDate(),
+                List.of(targetTxn), 
loan.getLoanTransactions().stream().map(AbstractPersistableCustom::getId).toList());
+        final BigDecimal interestRefundAmount = 
totalInterest.minus(newTotalInterest).getAmount();
+
+        if (MathUtil.isZero(interestRefundAmount)) {
+            throw new 
GeneralPlatformDomainRuleException("error.msg.loan.transaction.interest.refund.zero",

Review Comment:
   That's a good point. I initially added this check assuming the amount from 
the template would be final and non-zero, but I forgot to remove it ...



##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/LoanAccountDomainServiceJpa.java:
##########
@@ -206,7 +206,16 @@ private LoanTransaction 
createInterestRefundLoanTransaction(Loan loan, LoanTrans
         final ExternalId txnExternalId = externalIdFactory.create();
         businessEventNotifierService.notifyPreBusinessEvent(new 
LoanTransactionInterestRefundPreBusinessEvent(loan));
         return LoanTransaction.interestRefund(loan, interestRefundAmount, 
refundTransaction.getDateOf(), txnExternalId);
+    }
 
+    @Override
+    public LoanTransaction createManualInterestRefundWithAmount(final Loan 
loan, final LoanTransaction targetTransaction,
+            final BigDecimal interestRefundAmount, final PaymentDetail 
paymentDetail, final ExternalId txnExternalId) {
+        if (MathUtil.isZero(interestRefundAmount)) {

Review Comment:
   You are correct, this check is redundant here. I removed it



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to