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


##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanWritePlatformServiceJpaRepositoryImpl.java:
##########
@@ -1184,6 +1189,117 @@ public CommandProcessingResult 
adjustLoanTransaction(final Long loanId, final Lo
                 .with(changes).build();
     }
 
+    @Transactional
+    @Override
+    public CommandProcessingResult chargebackLoanTransaction(final Long 
loanId, final Long transactionId, final JsonCommand command) {
+        final boolean paymentDetailRequired = true;
+        this.loanEventApiJsonValidator.validateTransaction(command.json(), 
paymentDetailRequired);
+
+        Loan loan = this.loanAssembler.assembleFrom(loanId);
+        if 
(this.accountTransfersReadPlatformService.isAccountTransfer(transactionId, 
PortfolioAccountType.LOAN)) {
+            throw new 
PlatformServiceUnavailableException("error.msg.loan.transfer.transaction.update.not.allowed",
+                    "Loan transaction:" + transactionId + " chargeback not 
allowed as it involves in account transfer", transactionId);
+        }
+        if (loan.isClosedWrittenOff()) {
+            throw new 
PlatformServiceUnavailableException("error.msg.loan.chargeback.operation.not.allowed",
+                    "Loan transaction:" + transactionId + " chargeback not 
allowed as loan status is written off", transactionId);
+        }
+        if (loan.repaymentScheduleDetail().isInterestRecalculationEnabled()) {
+            throw new 
PlatformServiceUnavailableException("error.msg.loan.chargeback.operation.not.allowed",
+                    "Loan transaction:" + transactionId + " chargeback not 
allowed as loan product is interest recalculation enabled",
+                    transactionId);
+        }
+
+        final List<Long> existingTransactionIds = new ArrayList<>();
+        final List<Long> existingReversedTransactionIds = new ArrayList<>();
+
+        checkClientOrGroupActive(loan);
+
+        LoanTransaction loanTransaction = 
this.loanTransactionRepository.findById(transactionId)
+                .orElseThrow(() -> new 
LoanTransactionNotFoundException(transactionId));
+
+        if (!loanTransaction.isRepayment()) {
+            throw new 
PlatformServiceUnavailableException("error.msg.loan.chargeback.operation.not.allowed",
+                    "Loan transaction:" + transactionId + " chargeback not 
allowed as loan transaction is not repayment", transactionId);
+        }
+
+        if (loanTransaction.isFullChargeback()) {
+            throw new 
PlatformServiceUnavailableException("error.msg.loan.chargeback.operation.not.allowed",
+                    "Loan transaction:" + transactionId + " chargeback not 
allowed as loan transaction is full chargeback", transactionId);
+        }
+
+        businessEventNotifierService.notifyPreBusinessEvent(new 
LoanChargebackTransactionBusinessEvent(loanTransaction));
+
+        final LocalDate transactionDate = 
command.localDateValueOfParameterNamed(LoanApiConstants.transactionDateParamName);
+        final BigDecimal transactionAmount = 
command.bigDecimalValueOfParameterNamed(LoanApiConstants.TRANSACTION_AMOUNT_PARAMNAME);
+        final String txnExternalId = 
command.stringValueOfParameterNamedAllowingNull(LoanApiConstants.externalIdParameterName);
+
+        final Map<String, Object> changes = new LinkedHashMap<>();
+        changes.put("transactionDate", 
command.stringValueOfParameterNamed(LoanApiConstants.transactionDateParamName));
+        changes.put("transactionAmount", 
command.stringValueOfParameterNamed(LoanApiConstants.TRANSACTION_AMOUNT_PARAMNAME));
+        changes.put("locale", command.locale());
+        changes.put("dateFormat", command.dateFormat());
+        changes.put("paymentTypeId", 
command.stringValueOfParameterNamed(LoanApiConstants.PAYMENT_TYPE_PARAMNAME));
+
+        final Money transactionAmountAsMoney = Money.of(loan.getCurrency(), 
transactionAmount);
+        final PaymentDetail paymentDetail = 
this.paymentDetailWritePlatformService.createPaymentDetail(command, changes);
+        LoanTransaction newTransaction = LoanTransaction.chargeback(loan, 
transactionAmountAsMoney, paymentDetail, transactionDate,
+                txnExternalId);
+
+        final Integer validateLoanTransactionAmount = 
validateLoanTransactionAmountChargeBack(loanTransaction, newTransaction);
+        if (validateLoanTransactionAmount > 0) {
+            throw new 
PlatformServiceUnavailableException("error.msg.loan.chargeback.operation.not.allowed",
+                    "Loan transaction:" + transactionId + " chargeback not 
allowed as loan transaction amount is not enough",
+                    transactionId);
+        }
+
+        if (validateLoanTransactionAmount == 0) {
+            loanTransaction.markAsFullChargeback();

Review Comment:
   I dont think we need this. In the validateLoanTransactionAmountChargeBack we 
are iterating through the repayment transaction and all the existing chargeback 
transaction anyway, we can easily decide there whether the total chargeback 
amount is equals with the repayment amount (cannot accept any new chargeback) 
or is it lower and with the new transaction amount it remains smaller or got 
equals (if it would go higher, it must fail). We dont need to store 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