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


##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/portfolio/workingcapitalloan/data/RepaymentAmortizationData.java:
##########
@@ -1,24 +1 @@
-/**

Review Comment:
   Yes, done



##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/portfolio/workingcapitalloan/service/WorkingCapitalLoanWritePlatformServiceImpl.java:
##########
@@ -499,6 +503,84 @@ public CommandProcessingResult makeDiscountFee(Long 
loanId, JsonCommand command)
                 .withLoanId(loanId).with(changes).build();
     }
 
+    @Override
+    public CommandProcessingResult makeDiscountFeeAdjustment(final Long 
loanId, final JsonCommand command) {
+        final WorkingCapitalLoan loan = 
loanRepository.findById(loanId).orElseThrow(() -> new 
WorkingCapitalLoanNotFoundException(loanId));
+        final Long relatedDiscountTransactionId = 
fromApiJsonHelper.extractLongNamed(WorkingCapitalLoanConstants.relatedResourceIdParamName,
+                command.parsedJson());
+        if (relatedDiscountTransactionId == null) {
+            throw new 
PlatformApiDataValidationException("validation.msg.wc.loan.related.resource.id.required",
+                    "Related discount transaction ID is required for discount 
fee adjustment",
+                    WorkingCapitalLoanConstants.relatedResourceIdParamName);
+        }
+        final WorkingCapitalLoanTransaction relatedDiscountTransaction = 
transactionRepository.findById(relatedDiscountTransactionId)
+                .orElseThrow(() -> new 
PlatformApiDataValidationException("validation.msg.wc.loan.discount.transaction.not.found",
+                        "Discount transaction not found", 
WorkingCapitalLoanConstants.relatedResourceIdParamName));
+        if (!relatedDiscountTransaction.getTypeOf().isDiscountFee() || 
relatedDiscountTransaction.isReversed()) {
+            throw new 
PlatformApiDataValidationException("validation.msg.wc.loan.discount.transaction.invalid",
+                    "Related transaction must be an active discount fee 
transaction",
+                    WorkingCapitalLoanConstants.relatedResourceIdParamName);
+        }
+        final BigDecimal amount = 
fromApiJsonHelper.extractBigDecimalNamed(WorkingCapitalLoanConstants.transactionAmountParamName,
+                command.parsedJson(), new HashSet<>());
+        final BigDecimal totalAdjusted = relationRepository
+                
.findAllByToTransactionAndFromTransactionReversedAndFromTransactionTransactionType(relatedDiscountTransaction,
 false,
+                        LoanTransactionType.DISCOUNT_FEE_ADJUSTMENT)
+                .stream().map(relation -> 
relation.getFromTransaction().getTransactionAmount()).reduce(BigDecimal.ZERO, 
BigDecimal::add);
+        final BigDecimal remainingDiscountAmount = 
relatedDiscountTransaction.getTransactionAmount().subtract(totalAdjusted);
+
+        final LocalDate requestedTransactionDate = command
+                
.localDateValueOfParameterNamed(WorkingCapitalLoanConstants.transactionDateParamName);
+        final LocalDate transactionDate = requestedTransactionDate != null ? 
requestedTransactionDate
+                : relatedDiscountTransaction.getTransactionDate();
+        validator.validateDiscountAdjustmentTransaction(loan, command.json(), 
amount, relatedDiscountTransaction, remainingDiscountAmount,
+                transactionDate);
+        final Long classificationId = 
command.longValueOfParameterNamed(WorkingCapitalLoanConstants.classificationIdParamName);
+        final CodeValue classification = classificationId != null ? Optional
+                
.ofNullable(codeValueRepository.findByCodeNameAndId(WorkingCapitalLoanConstants.DISCOUNT_FEE_CLASSIFICATION_CODE_NAME,
+                        classificationId))
+                .orElseThrow(() -> new 
PlatformApiDataValidationException("validation.msg.wc.loan.classification.not.found",
+                        "Classification with ID " + classificationId + " not 
found", "classificationId"))
+                : null;
+        final ExternalId txnExternalId = 
externalIdFactory.createFromCommand(command, 
WorkingCapitalLoanConstants.externalIdParameterName);
+        final Map<String, Object> changes = new LinkedHashMap<>();
+        final PaymentDetail paymentDetail = 
createAndPersistPaymentDetailFromCommand(command, changes);
+        final WorkingCapitalLoanTransaction adjustmentTransaction = 
WorkingCapitalLoanTransaction.discountFeeAdjustment(loan, txnExternalId,
+                amount, transactionDate, classification, paymentDetail);
+        transactionRepository.saveAndFlush(adjustmentTransaction);
+        saveNewTransactionRelation(adjustmentTransaction, 
relatedDiscountTransaction, LoanTransactionRelationTypeEnum.RELATED);
+        
allocationRepository.saveAndFlush(WorkingCapitalLoanTransactionAllocation.forDiscountFeeAdjustment(adjustmentTransaction,
 amount));
+
+        if (loan.getLoanProductRelatedDetails() == null) {
+            throw new 
PlatformApiDataValidationException("validation.msg.wc.loan.discount.not.available",
+                    "Discount adjustment is not available when loan product 
details are missing", "loanProductRelatedDetails");
+        }
+        final BigDecimal currentDiscount = 
loan.getLoanProductRelatedDetails().getDiscount();
+        loan.getLoanProductRelatedDetails()
+                .setDiscount((currentDiscount != null ? currentDiscount : 
BigDecimal.ZERO).subtract(amount).max(BigDecimal.ZERO));
+
+        amortizationScheduleWriteService.applyDiscountFeeAdjustment(loan, 
transactionDate);
+        updateBalanceForDiscountChange(loan);

Review Comment:
   Done



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