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


##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/portfolio/workingcapitalloan/service/WorkingCapitalLoanWritePlatformServiceImpl.java:
##########
@@ -606,12 +606,60 @@ public CommandProcessingResult undoTransaction(final Long 
loanId, final Long tra
                         "Working capital loan transaction not found", 
WorkingCapitalLoanConstants.transactionIdParamName));
         return switch (transaction.getTypeOf()) {
             case DISCOUNT_FEE_ADJUSTMENT -> undoDiscountFeeAdjustment(loan, 
transaction, command);
+            case CHARGE_ADJUSTMENT -> undoChargeAdjustment(loan, transaction, 
command);
             default -> throw new 
PlatformApiDataValidationException("validation.msg.wc.loan.transaction.undo.not.supported",
                     "Undo is not supported for transaction type " + 
transaction.getTypeOf(),
                     WorkingCapitalLoanConstants.transactionTypeParamName);
         };
     }
 
+    private CommandProcessingResult undoChargeAdjustment(final 
WorkingCapitalLoan loan,
+            final WorkingCapitalLoanTransaction adjustmentTransaction, final 
JsonCommand command) {
+        if (adjustmentTransaction.isReversed()) {
+            throw new 
PlatformApiDataValidationException("validation.msg.wc.loan.charge.adjustment.already.reversed",
+                    "Charge adjustment transaction is already reversed", 
WorkingCapitalLoanConstants.transactionIdParamName);
+        }
+
+        final WorkingCapitalLoanTransactionRelation chargeRelation = 
adjustmentTransaction.getLoanTransactionRelations().stream()
+                .filter(r -> r.getToCharge() != null && r.getRelationType() == 
LoanTransactionRelationTypeEnum.CHARGE_ADJUSTMENT)
+                .findFirst()
+                .orElseThrow(() -> new 
PlatformApiDataValidationException("validation.msg.wc.loan.charge.adjustment.relation.missing",
+                        "Charge adjustment transaction is missing the link to 
the charge",
+                        WorkingCapitalLoanConstants.transactionIdParamName));
+
+        final 
org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanCharge
 wcCharge = chargeRelation.getToCharge();
+        final BigDecimal amount = adjustmentTransaction.getTransactionAmount();
+
+        reverseTransaction(adjustmentTransaction);
+        accountingProcessor.postReversalJournalEntries(loan, 
adjustmentTransaction);
+
+        final BigDecimal newPaid = 
MathUtil.subtract(MathUtil.nullToZero(wcCharge.getAmountPaid()), 
amount).max(BigDecimal.ZERO);
+        wcCharge.setAmountPaid(newPaid);
+        if (newPaid.compareTo(MathUtil.nullToZero(wcCharge.getAmount())) < 0) {
+            wcCharge.setPaid(false);
+        }
+
+        final WorkingCapitalLoanBalance balance = 
balanceRepository.findByWcLoan_Id(loan.getId())
+                .orElseGet(() -> WorkingCapitalLoanBalance.createFor(loan));
+        if (wcCharge.isPenaltyCharge()) {
+            
balance.setPenaltyPaid(MathUtil.subtract(MathUtil.nullToZero(balance.getPenaltyPaid()),
 amount).max(BigDecimal.ZERO));
+        } else {
+            
balance.setFeePaid(MathUtil.subtract(MathUtil.nullToZero(balance.getFeePaid()), 
amount).max(BigDecimal.ZERO));
+        }
+        balanceRepository.saveAndFlush(balance);

Review Comment:
   Not needed



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