|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.fineract.portfolio.loanaccount.service; |
| 20 | + |
| 21 | +import jakarta.annotation.PostConstruct; |
| 22 | +import lombok.RequiredArgsConstructor; |
| 23 | +import lombok.extern.slf4j.Slf4j; |
| 24 | +import org.apache.fineract.infrastructure.core.service.DateUtils; |
| 25 | +import org.apache.fineract.infrastructure.event.business.BusinessEventListener; |
| 26 | +import org.apache.fineract.infrastructure.event.business.domain.loan.LoanBalanceChangedBusinessEvent; |
| 27 | +import org.apache.fineract.infrastructure.event.business.domain.loan.LoanCloseBusinessEvent; |
| 28 | +import org.apache.fineract.infrastructure.event.business.domain.loan.transaction.LoanChargeOffPostBusinessEvent; |
| 29 | +import org.apache.fineract.infrastructure.event.business.domain.loan.transaction.LoanChargeOffPreBusinessEvent; |
| 30 | +import org.apache.fineract.infrastructure.event.business.domain.loan.transaction.LoanUndoChargeOffBusinessEvent; |
| 31 | +import org.apache.fineract.infrastructure.event.business.service.BusinessEventNotifierService; |
| 32 | +import org.apache.fineract.portfolio.loanaccount.domain.Loan; |
| 33 | +import org.apache.fineract.portfolio.loanaccount.domain.LoanStatus; |
| 34 | +import org.apache.fineract.portfolio.loanaccount.domain.LoanTransaction; |
| 35 | + |
| 36 | +@Slf4j |
| 37 | +@RequiredArgsConstructor |
| 38 | +public class LoanBuyDownFeeAmortizationEventService { |
| 39 | + |
| 40 | + private final BusinessEventNotifierService businessEventNotifierService; |
| 41 | + private final LoanBuyDownFeeAmortizationProcessingService loanBuyDownFeeAmortizationProcessingService; |
| 42 | + |
| 43 | + @PostConstruct |
| 44 | + public void addListeners() { |
| 45 | + businessEventNotifierService.addPreBusinessEventListener(LoanCloseBusinessEvent.class, new LoanCloseListener()); |
| 46 | + businessEventNotifierService.addPostBusinessEventListener(LoanBalanceChangedBusinessEvent.class, new LoanBalanceChangedListener()); |
| 47 | + businessEventNotifierService.addPostBusinessEventListener(LoanChargeOffPostBusinessEvent.class, new LoanChargeOffEventListener()); |
| 48 | + businessEventNotifierService.addPostBusinessEventListener(LoanUndoChargeOffBusinessEvent.class, |
| 49 | + new LoanUndoChargeOffEventListener()); |
| 50 | + businessEventNotifierService.addPreBusinessEventListener(LoanChargeOffPreBusinessEvent.class, new LoanChargeOffPreEventListener()); |
| 51 | + } |
| 52 | + |
| 53 | + private final class LoanCloseListener implements BusinessEventListener<LoanCloseBusinessEvent> { |
| 54 | + |
| 55 | + @Override |
| 56 | + public void onBusinessEvent(final LoanCloseBusinessEvent event) { |
| 57 | + final Loan loan = event.get(); |
| 58 | + final LoanStatus status = loan.getStatus(); |
| 59 | + if (loan.getLoanProductRelatedDetail().isEnableBuyDownFee() |
| 60 | + && (status.isClosedObligationsMet() || status.isClosedWrittenOff() || status.isOverpaid())) { |
| 61 | + log.debug("Loan closure on buy down fee amortization for loan {}", loan.getId()); |
| 62 | + loanBuyDownFeeAmortizationProcessingService.processBuyDownFeeAmortizationOnLoanClosure(loan, false); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private final class LoanBalanceChangedListener implements BusinessEventListener<LoanBalanceChangedBusinessEvent> { |
| 68 | + |
| 69 | + @Override |
| 70 | + public void onBusinessEvent(final LoanBalanceChangedBusinessEvent event) { |
| 71 | + final Loan loan = event.get(); |
| 72 | + final LoanStatus status = loan.getStatus(); |
| 73 | + if (loan.getLoanProductRelatedDetail().isEnableBuyDownFee() |
| 74 | + && (status.isClosedObligationsMet() || status.isClosedWrittenOff() || status.isOverpaid())) { |
| 75 | + log.debug("Loan balance change on buy down fee amortization for loan {}", loan.getId()); |
| 76 | + loanBuyDownFeeAmortizationProcessingService.processBuyDownFeeAmortizationOnLoanClosure(loan, true); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private final class LoanChargeOffEventListener implements BusinessEventListener<LoanChargeOffPostBusinessEvent> { |
| 82 | + |
| 83 | + @Override |
| 84 | + public void onBusinessEvent(final LoanChargeOffPostBusinessEvent event) { |
| 85 | + final LoanTransaction loanTransaction = event.get(); |
| 86 | + final Loan loan = loanTransaction.getLoan(); |
| 87 | + if (loan.getLoanProductRelatedDetail().isEnableBuyDownFee() && loan.isChargedOff() && loanTransaction.isChargeOff()) { |
| 88 | + log.debug("Loan charge-off on buy down fee amortization for loan {}", loan.getId()); |
| 89 | + loanBuyDownFeeAmortizationProcessingService.processBuyDownFeeAmortizationOnLoanChargeOff(loan, loanTransaction); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private final class LoanChargeOffPreEventListener implements BusinessEventListener<LoanChargeOffPreBusinessEvent> { |
| 95 | + |
| 96 | + @Override |
| 97 | + public void onBusinessEvent(final LoanChargeOffPreBusinessEvent event) { |
| 98 | + final Loan loan = event.get(); |
| 99 | + if (loan.getLoanProductRelatedDetail().isEnableBuyDownFee()) { |
| 100 | + log.debug("Loan pre charge-off buy down fee amortization for loan {}", loan.getId()); |
| 101 | + loanBuyDownFeeAmortizationProcessingService.processBuyDownFeeAmortizationTillDate(loan, DateUtils.getBusinessLocalDate(), |
| 102 | + true); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private final class LoanUndoChargeOffEventListener implements BusinessEventListener<LoanUndoChargeOffBusinessEvent> { |
| 108 | + |
| 109 | + @Override |
| 110 | + public void onBusinessEvent(final LoanUndoChargeOffBusinessEvent event) { |
| 111 | + final LoanTransaction loanTransaction = event.get(); |
| 112 | + final Loan loan = loanTransaction.getLoan(); |
| 113 | + final LoanStatus status = loan.getStatus(); |
| 114 | + if (loan.getLoanProductRelatedDetail().isEnableBuyDownFee() && loanTransaction.getTypeOf().isChargeOff() |
| 115 | + && !(loan.isChargedOff() || status.isClosedObligationsMet() || status.isClosedWrittenOff() || status.isOverpaid())) { |
| 116 | + log.debug("Loan undo charge-off on buy down fee amortization for loan {}", loan.getId()); |
| 117 | + loanBuyDownFeeAmortizationProcessingService.processBuyDownFeeAmortizationOnLoanUndoChargeOff(loanTransaction); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments