|
| 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.accounting.journalentry; |
| 20 | + |
| 21 | +import static org.mockito.Mockito.mock; |
| 22 | +import static org.mockito.Mockito.times; |
| 23 | +import static org.mockito.Mockito.verify; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | + |
| 26 | +import java.math.BigDecimal; |
| 27 | +import java.time.LocalDate; |
| 28 | +import java.time.ZoneId; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.List; |
| 31 | +import org.apache.fineract.accounting.closure.domain.GLClosure; |
| 32 | +import org.apache.fineract.accounting.common.AccountingConstants.AccrualAccountsForLoan; |
| 33 | +import org.apache.fineract.accounting.glaccount.domain.GLAccount; |
| 34 | +import org.apache.fineract.accounting.journalentry.data.LoanDTO; |
| 35 | +import org.apache.fineract.accounting.journalentry.data.LoanTransactionDTO; |
| 36 | +import org.apache.fineract.accounting.journalentry.service.AccountingProcessorHelper; |
| 37 | +import org.apache.fineract.accounting.journalentry.service.AccrualBasedAccountingProcessorForLoan; |
| 38 | +import org.apache.fineract.accounting.producttoaccountmapping.domain.ProductToGLAccountMapping; |
| 39 | +import org.apache.fineract.organisation.office.domain.Office; |
| 40 | +import org.apache.fineract.portfolio.loanaccount.data.LoanTransactionEnumData; |
| 41 | +import org.junit.jupiter.api.BeforeEach; |
| 42 | +import org.junit.jupiter.api.Test; |
| 43 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 44 | +import org.mockito.InjectMocks; |
| 45 | +import org.mockito.Mock; |
| 46 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 47 | + |
| 48 | +@ExtendWith(MockitoExtension.class) |
| 49 | +class CreateJournalEntriesForChargeOffLoanTest { |
| 50 | + |
| 51 | + private static final Integer chargeOffReasons = 15; |
| 52 | + |
| 53 | + @Mock |
| 54 | + private AccountingProcessorHelper helper; |
| 55 | + @InjectMocks |
| 56 | + private AccrualBasedAccountingProcessorForLoan processor; |
| 57 | + private LoanDTO loanDTO; |
| 58 | + |
| 59 | + @BeforeEach |
| 60 | + void setUp() { |
| 61 | + Office office = Office.headOffice("Main Office", LocalDate.now(ZoneId.systemDefault()), null); |
| 62 | + when(helper.getOfficeById(1L)).thenReturn(office); |
| 63 | + |
| 64 | + GLClosure mockClosure = mock(GLClosure.class); |
| 65 | + when(helper.getLatestClosureByBranch(1L)).thenReturn(mockClosure); |
| 66 | + |
| 67 | + LoanTransactionEnumData transactionType = mock(LoanTransactionEnumData.class); |
| 68 | + when(transactionType.isChargeoff()).thenReturn(true); |
| 69 | + |
| 70 | + LoanTransactionDTO loanTransactionDTO = new LoanTransactionDTO(1L, 1L, "txn-123", LocalDate.now(ZoneId.systemDefault()), |
| 71 | + transactionType, new BigDecimal("500.00"), new BigDecimal("500.00"), null, null, null, null, false, Collections.emptyList(), |
| 72 | + Collections.emptyList(), false, "", null, null, null, null); |
| 73 | + |
| 74 | + loanDTO = new LoanDTO(1L, 1L, 1L, "USD", false, true, true, List.of(loanTransactionDTO), false, false, chargeOffReasons); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void shouldCreateJournalEntriesForChargeOff() { |
| 79 | + GLAccount chargeOffGLAccount = new GLAccount(); |
| 80 | + chargeOffGLAccount.setId(15L); |
| 81 | + chargeOffGLAccount.setName("Charge-Off Account"); |
| 82 | + chargeOffGLAccount.setGlCode("12345"); |
| 83 | + |
| 84 | + ProductToGLAccountMapping chargeToGLAccountMapper = new ProductToGLAccountMapping(); |
| 85 | + chargeToGLAccountMapper.setGlAccount(chargeOffGLAccount); |
| 86 | + |
| 87 | + when(helper.getChargeOffMappingByCodeValue(chargeOffReasons)).thenReturn(chargeToGLAccountMapper); |
| 88 | + |
| 89 | + GLAccount loanPortfolioGLAccount = new GLAccount(); |
| 90 | + loanPortfolioGLAccount.setId(20L); |
| 91 | + loanPortfolioGLAccount.setName("Loan Portfolio Account"); |
| 92 | + loanPortfolioGLAccount.setGlCode("54321"); |
| 93 | + |
| 94 | + when(helper.getLinkedGLAccountForLoanProduct(1L, AccrualAccountsForLoan.LOAN_PORTFOLIO.getValue(), 1L)) |
| 95 | + .thenReturn(loanPortfolioGLAccount); |
| 96 | + |
| 97 | + processor.createJournalEntriesForLoan(loanDTO); |
| 98 | + |
| 99 | + verify(helper, times(1)).getChargeOffMappingByCodeValue(chargeOffReasons); |
| 100 | + verify(helper, times(1)).getLinkedGLAccountForLoanProduct(1L, AccrualAccountsForLoan.LOAN_PORTFOLIO.getValue(), 1L); |
| 101 | + verify(helper, times(1)).createCreditJournalEntryOrReversalForLoan(helper.getOfficeById(1L), "USD", |
| 102 | + AccrualAccountsForLoan.LOAN_PORTFOLIO, 1L, null, 1L, "txn-123", LocalDate.now(ZoneId.systemDefault()), |
| 103 | + new BigDecimal("500.00"), false); |
| 104 | + verify(helper, times(1)).createDebitJournalEntryOrReversalForLoan(helper.getOfficeById(1L), "USD", |
| 105 | + AccrualAccountsForLoan.CHARGE_OFF_EXPENSE.getValue(), 1L, null, 1L, "txn-123", LocalDate.now(ZoneId.systemDefault()), |
| 106 | + new BigDecimal("500.00"), false); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + void shouldCreateJournalEntriesForChargeOffWithFraud() { |
| 111 | + loanDTO.setMarkedAsFraud(true); |
| 112 | + |
| 113 | + when(helper.getChargeOffMappingByCodeValue(chargeOffReasons)).thenReturn(null); |
| 114 | + |
| 115 | + GLAccount loanPortfolioGLAccount = new GLAccount(); |
| 116 | + loanPortfolioGLAccount.setId(20L); |
| 117 | + loanPortfolioGLAccount.setName("Loan Portfolio Account"); |
| 118 | + loanPortfolioGLAccount.setGlCode("54321"); |
| 119 | + |
| 120 | + GLAccount fraudExpenseGLAccount = new GLAccount(); |
| 121 | + fraudExpenseGLAccount.setId(30L); |
| 122 | + fraudExpenseGLAccount.setName("Fraud Expense Account"); |
| 123 | + fraudExpenseGLAccount.setGlCode("98765"); |
| 124 | + |
| 125 | + when(helper.getLinkedGLAccountForLoanProduct(1L, AccrualAccountsForLoan.LOAN_PORTFOLIO.getValue(), 1L)) |
| 126 | + .thenReturn(loanPortfolioGLAccount); |
| 127 | + |
| 128 | + when(helper.getLinkedGLAccountForLoanProduct(1L, AccrualAccountsForLoan.CHARGE_OFF_FRAUD_EXPENSE.getValue(), 1L)) |
| 129 | + .thenReturn(fraudExpenseGLAccount); |
| 130 | + |
| 131 | + processor.createJournalEntriesForLoan(loanDTO); |
| 132 | + |
| 133 | + verify(helper, times(1)).getLinkedGLAccountForLoanProduct(1L, AccrualAccountsForLoan.LOAN_PORTFOLIO.getValue(), 1L); |
| 134 | + verify(helper, times(1)).getLinkedGLAccountForLoanProduct(1L, AccrualAccountsForLoan.CHARGE_OFF_FRAUD_EXPENSE.getValue(), 1L); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + void shouldCreateJournalEntriesForChargeOffWithoutFraud() { |
| 139 | + loanDTO.setMarkedAsFraud(false); |
| 140 | + |
| 141 | + when(helper.getChargeOffMappingByCodeValue(chargeOffReasons)).thenReturn(null); |
| 142 | + |
| 143 | + GLAccount loanPortfolioGLAccount = new GLAccount(); |
| 144 | + loanPortfolioGLAccount.setId(20L); |
| 145 | + loanPortfolioGLAccount.setName("Loan Portfolio Account"); |
| 146 | + loanPortfolioGLAccount.setGlCode("54321"); |
| 147 | + |
| 148 | + GLAccount expenseGLAccount = new GLAccount(); |
| 149 | + expenseGLAccount.setId(40L); |
| 150 | + expenseGLAccount.setName("Expense Account"); |
| 151 | + expenseGLAccount.setGlCode("67890"); |
| 152 | + |
| 153 | + when(helper.getLinkedGLAccountForLoanProduct(1L, AccrualAccountsForLoan.LOAN_PORTFOLIO.getValue(), 1L)) |
| 154 | + .thenReturn(loanPortfolioGLAccount); |
| 155 | + |
| 156 | + when(helper.getLinkedGLAccountForLoanProduct(1L, AccrualAccountsForLoan.CHARGE_OFF_EXPENSE.getValue(), 1L)) |
| 157 | + .thenReturn(expenseGLAccount); |
| 158 | + |
| 159 | + processor.createJournalEntriesForLoan(loanDTO); |
| 160 | + |
| 161 | + verify(helper, times(1)).getLinkedGLAccountForLoanProduct(1L, AccrualAccountsForLoan.LOAN_PORTFOLIO.getValue(), 1L); |
| 162 | + verify(helper, times(1)).getLinkedGLAccountForLoanProduct(1L, AccrualAccountsForLoan.CHARGE_OFF_EXPENSE.getValue(), 1L); |
| 163 | + } |
| 164 | +} |
0 commit comments