From 6c8ffc7b7b0e152ff420cc6784a8090efc625e9e Mon Sep 17 00:00:00 2001 From: tabrez-fiter Date: Wed, 20 Nov 2024 13:18:57 +0800 Subject: [PATCH] SU-450 : Novelty management - update Charge-Off --- .../LoanEventApiJsonValidator.java | 5 ++- ...WritePlatformServiceJpaRepositoryImpl.java | 42 +++++++++++++++---- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/serialization/LoanEventApiJsonValidator.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/serialization/LoanEventApiJsonValidator.java index 7583ba916ae..dece67ee257 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/serialization/LoanEventApiJsonValidator.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/serialization/LoanEventApiJsonValidator.java @@ -435,7 +435,7 @@ public void validateChargeOffTransaction(final String json) { } final Set chargeOffParameters = new HashSet<>( - Arrays.asList("transactionDate", "note", "locale", "dateFormat", "chargeOffReasonId", "externalId")); + Arrays.asList("transactionDate", "note", "locale", "dateFormat", "chargeOffReasonId", "externalId", "incidentTypeId")); final Type typeOfMap = new TypeToken>() {}.getType(); fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, chargeOffParameters); @@ -456,6 +456,9 @@ public void validateChargeOffTransaction(final String json) { final Long chargeOffReasonId = fromApiJsonHelper.extractLongNamed("chargeOffReasonId", element); baseDataValidator.reset().parameter("chargeOffReasonId").value(chargeOffReasonId).ignoreIfNull().integerGreaterThanZero(); + final Long incidentTypeId = fromApiJsonHelper.extractLongNamed("incidentTypeId", element); + baseDataValidator.reset().parameter("incidentTypeId").value(incidentTypeId).ignoreIfNull().integerGreaterThanZero(); + throwExceptionIfValidationWarningsExist(dataValidationErrors); } diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanWritePlatformServiceJpaRepositoryImpl.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanWritePlatformServiceJpaRepositoryImpl.java index 4dc4a1b3ac7..7b69ee4d12c 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanWritePlatformServiceJpaRepositoryImpl.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanWritePlatformServiceJpaRepositoryImpl.java @@ -3612,18 +3612,37 @@ public CommandProcessingResult chargeOff(JsonCommand command) { checkIfProductAllowsCancelationOrReversal(loan); - businessEventNotifierService.notifyPreBusinessEvent(new LoanChargeOffPreBusinessEvent(loan)); + loan.markAsChargedOff(transactionDate, currentUser, null); - if (command.hasParameter(LoanApiConstants.chargeOffReasonIdParamName)) { - Long chargeOffReasonId = command.longValueOfParameterNamed(LoanApiConstants.chargeOffReasonIdParamName); - CodeValue chargeOffReason = this.codeValueRepository - .findOneByCodeNameAndIdWithNotFoundDetection(LoanApiConstants.CHARGE_OFF_REASONS, chargeOffReasonId); - changes.put(LoanApiConstants.chargeOffReasonIdParamName, chargeOffReasonId); - loan.markAsChargedOff(transactionDate, currentUser, chargeOffReason); - } else { - loan.markAsChargedOff(transactionDate, currentUser, null); + InsuranceIncidentType incidentType = InsuranceIncidentType.DEATH_CANCELLATION; + if (command.hasParameter("incidentTypeId")) { + Integer incidentTypeId = command.integerValueOfParameterNamed("incidentTypeId"); + incidentType = InsuranceIncidentType.fromInt(incidentTypeId); } + this.loanScheduleHistoryWritePlatformService.createAndSaveLoanScheduleArchive(loan.getRepaymentScheduleInstallments(), loan, null); + List cancelInsuranceInstallmentIds = this.loanReadPlatformService + .getLoanDataWithDefaultOrCancelInsurance(loanId, null, transactionDate); + InsuranceIncident incident = this.insuranceIncidentRepository.findByIncidentType(incidentType); + if (incident == null) { + throw new InsuranceIncidentNotFoundException(InsuranceIncidentType.DEATH_CANCELLATION.name()); + } + for (final DefaultOrCancelInsuranceInstallmentData data : cancelInsuranceInstallmentIds) { + LoanCharge loanCharge = null; + Optional loanChargeOptional = loan.getLoanCharges().stream() + .filter(lc -> Objects.equals(lc.getId(), data.loanChargeId())).findFirst(); + if (loanChargeOptional.isPresent()) { + loanCharge = loanChargeOptional.get(); + } + BigDecimal cumulative = BigDecimal.ZERO; + cumulative = processInsuranceChargeCancellation(cumulative, loan, loanCharge, data, true); + InsuranceIncidentNoveltyNews insuranceIncidentNoveltyNews = InsuranceIncidentNoveltyNews.instance(loan, loanCharge, + data.installment(), incident, transactionDate, cumulative); + this.insuranceIncidentNoveltyNewsRepository.saveAndFlush(insuranceIncidentNoveltyNews); + } + + businessEventNotifierService.notifyPreBusinessEvent(new LoanChargeOffPreBusinessEvent(loan)); + final List existingTransactionIds = loan.findExistingTransactionIds(); final List existingReversedTransactionIds = loan.findExistingReversedTransactionIds(); loan.getLoanCustomizationDetail().recordActivity(); @@ -3640,6 +3659,11 @@ public CommandProcessingResult chargeOff(JsonCommand command) { this.noteRepository.save(note); } + this.loanAccountDomainService.foreCloseLoan(loan, transactionDate, noteText, txnExternalId, changes); + final BlockingReasonSetting blockingReasonSetting = loanBlockingReasonRepository.getSingleBlockingReasonSettingByReason( + BlockingReasonSettingEnum.CREDIT_CANCELADO.getDatabaseString(), BlockLevel.CREDIT.toString()); + loanBlockWritePlatformService.blockLoan(loan.getId(), blockingReasonSetting, "CANCELADO", DateUtils.getLocalDateOfTenant()); + postJournalEntries(loan, existingTransactionIds, existingReversedTransactionIds); businessEventNotifierService.notifyPostBusinessEvent(new LoanChargeOffPostBusinessEvent(chargeOffTransaction)); return new CommandProcessingResultBuilder() //