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


##########
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:
   Why to throw error just because it calculated zero? This is a template 
call... zero amount sounds okay to me...



-- 
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