oleksii-novikov-onix commented on code in PR #5760:
URL: https://github.com/apache/fineract/pull/5760#discussion_r3073974119


##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/portfolio/workingcapitalloan/service/WorkingCapitalLoanAmortizationScheduleWriteServiceImpl.java:
##########
@@ -143,6 +146,37 @@ private void generateAndSaveForApprovedLoanState(final 
WorkingCapitalLoan loan)
         scheduleRepositoryWrapper.writeModel(loan, model);
     }
 
+    @Override
+    public RepaymentAmortizationData applyRepayment(final WorkingCapitalLoan 
loan, final LocalDate transactionDate,
+            final BigDecimal repaymentAmount) {
+        Validate.notNull(loan, "loan must not be null");
+        Validate.notNull(transactionDate, "transactionDate must not be null");
+        Validate.notNull(repaymentAmount, "repaymentAmount must not be null");
+
+        final MathContext mc = MoneyHelper.getMathContext();
+        final ProjectedAmortizationScheduleModel model = 
scheduleRepositoryWrapper.readModel(loan.getId(), mc, resolveCurrency(loan))
+                .orElseThrow(() -> new IllegalStateException("Projected 
amortization schedule is not found for loan " + loan.getId()));
+
+        final BigDecimal previousTotalAmortized = sumRunningNpv(model);
+        model.applyPayment(transactionDate, repaymentAmount);
+        
model.recalculateNetAmortizationAndDeferredBalanceFrom(transactionDate);
+        final BigDecimal totalAmortized = sumRunningNpv(model);
+
+        scheduleRepositoryWrapper.writeModel(loan, model);
+        return new 
RepaymentAmortizationData(totalAmortized.subtract(previousTotalAmortized, mc), 
totalAmortized);
+    }
+
+    private BigDecimal sumRunningNpv(final ProjectedAmortizationScheduleModel 
model) {
+        final MathContext mc = MoneyHelper.getMathContext();
+        BigDecimal result = BigDecimal.ZERO;
+        for (ProjectedPayment payment : model.payments()) {
+            if (payment.paymentNo() > 0 && payment.npvValue() != null && 
payment.npvValue().getAmount() != null) {
+                result = 
result.add(Objects.requireNonNullElse(payment.npvValue().getAmount(), 
BigDecimal.ZERO), mc);

Review Comment:
   `Objects.requireNonNullElse(payment.npvValue().getAmount(), 
BigDecimal.ZERO)` is redundant, the if guard above already ensures `getAmount() 
!= null`



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