FINERACT-1968: Advanced repayment allocation strategy - data model

- [x] m_loan_product_payment_allocation_rule table created
- [x] m_loan_payment_allocation_rule table created
- [x] LoanProductPaymentAllocationRule entity
- [x] LoanPaymentAllocationRule entity
- [x] List of enum converter to String converter to use `AllocationType` list
This commit is contained in:
Janos Haber
2023-08-11 14:58:56 +02:00
committed by Adam Saghy
parent d6c10e8303
commit 07122fd10a
12 changed files with 542 additions and 0 deletions
@@ -0,0 +1,66 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.core.data;
import jakarta.persistence.AttributeConverter;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;
public abstract class GenericEnumListConverter<E extends Enum<E>> implements AttributeConverter<List<E>, String> {
private static final String SPLIT_CHAR = ",";
private final Class<E> clazz;
protected boolean isUnique() {
return false;
}
protected GenericEnumListConverter(Class<E> clazz) {
this.clazz = clazz;
}
@Override
public String convertToDatabaseColumn(List<E> values) {
if (values.isEmpty()) {
return null;
}
Stream<E> valueStream;
if (isUnique()) {
valueStream = values.stream().distinct();
} else {
valueStream = values.stream();
}
return valueStream.map(Enum::name).collect(Collectors.joining(SPLIT_CHAR));
}
@Override
public List<E> convertToEntityAttribute(String string) {
if (StringUtils.isBlank(string)) {
return List.of();
}
Stream<E> stream = Stream.of(string.split(SPLIT_CHAR)).map(e -> Enum.valueOf(clazz, e));
if (isUnique()) {
return stream.distinct().toList();
}
return stream.toList();
}
}
@@ -0,0 +1,66 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.portfolio.loanaccount.domain;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import java.util.List;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.fineract.infrastructure.core.domain.AbstractAuditableWithUTCDateTimeCustom;
import org.apache.fineract.portfolio.loanproduct.domain.FutureInstallmentAllocationRule;
import org.apache.fineract.portfolio.loanproduct.domain.PaymentAllocationTransactionType;
import org.apache.fineract.portfolio.loanproduct.domain.PaymentAllocationType;
@Getter
@Setter
@Entity
@Table(name = "m_loan_payment_allocation", uniqueConstraints = {
@UniqueConstraint(columnNames = { "loan_id", "transaction_type" }, name = "uq_m_loan_payment_allocation_rule") })
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class LoanPaymentAllocationRule extends AbstractAuditableWithUTCDateTimeCustom {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "loan_id", nullable = false)
private Loan loan;
@Column(name = "transaction_type", nullable = false)
@Enumerated(EnumType.STRING)
private PaymentAllocationTransactionType transactionType;
@ElementCollection(fetch = FetchType.EAGER)
@Column(name = "allocation_types", nullable = false)
private List<PaymentAllocationType> allocationTypes;
@Column(name = "future_installment_allocation_rule", nullable = false)
@Enumerated(EnumType.STRING)
private FutureInstallmentAllocationRule futureInstallmentAllocationRule;
}
@@ -0,0 +1,26 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.portfolio.loanproduct.domain;
public enum AllocationType {
PENALTY, //
FEE, //
PRINCIPAL, //
INTEREST //
}
@@ -0,0 +1,36 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.portfolio.loanproduct.domain;
import jakarta.persistence.Converter;
import org.apache.fineract.infrastructure.core.data.GenericEnumListConverter;
@Converter(autoApply = true)
public class AllocationTypeListConverter extends GenericEnumListConverter<PaymentAllocationType> {
@Override
protected boolean isUnique() {
return true;
}
protected AllocationTypeListConverter() {
super(PaymentAllocationType.class);
}
}
@@ -0,0 +1,25 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.portfolio.loanproduct.domain;
public enum DueType {
PAST_DUE, //
DUE, //
IN_ADVANCE //
}
@@ -0,0 +1,25 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.portfolio.loanproduct.domain;
public enum FutureInstallmentAllocationRule {
NEXT_INSTALLMENT, //
LAST_INSTALLMENT, //
REAMORTIZATION //
}
@@ -0,0 +1,63 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.portfolio.loanproduct.domain;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import java.util.List;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.fineract.infrastructure.core.domain.AbstractAuditableWithUTCDateTimeCustom;
@Getter
@Setter
@Entity
@Table(name = "m_loan_product_payment_allocation", uniqueConstraints = {
@UniqueConstraint(columnNames = { "loan_product_id", "transaction_type" }, name = "uq_m_loan_product_payment_allocation_rule") })
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class LoanProductPaymentAllocationRule extends AbstractAuditableWithUTCDateTimeCustom {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "loan_product_id", nullable = false)
private LoanProduct loanProduct;
@Column(name = "transaction_type", nullable = false)
@Enumerated(EnumType.STRING)
private PaymentAllocationTransactionType transactionType;
@ElementCollection(fetch = FetchType.EAGER)
@Column(name = "allocation_types", nullable = false)
private List<PaymentAllocationType> allocationTypes;
@Enumerated(EnumType.STRING)
@Column(name = "future_installment_allocation_rule", nullable = false)
private FutureInstallmentAllocationRule futureInstallmentAllocationRule;
}
@@ -0,0 +1,33 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.portfolio.loanproduct.domain;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionType;
@Getter
@RequiredArgsConstructor
public enum PaymentAllocationTransactionType {
DEFAULT(null), //
REPAYMENT(LoanTransactionType.REPAYMENT); //
private final LoanTransactionType loanTransactionType;
}
@@ -0,0 +1,52 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.portfolio.loanproduct.domain;
import static org.apache.fineract.portfolio.loanproduct.domain.AllocationType.FEE;
import static org.apache.fineract.portfolio.loanproduct.domain.AllocationType.INTEREST;
import static org.apache.fineract.portfolio.loanproduct.domain.AllocationType.PENALTY;
import static org.apache.fineract.portfolio.loanproduct.domain.AllocationType.PRINCIPAL;
import static org.apache.fineract.portfolio.loanproduct.domain.DueType.DUE;
import static org.apache.fineract.portfolio.loanproduct.domain.DueType.IN_ADVANCE;
import static org.apache.fineract.portfolio.loanproduct.domain.DueType.PAST_DUE;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@Getter
@RequiredArgsConstructor
public enum PaymentAllocationType {
PAST_DUE_PENALTY(PAST_DUE, PENALTY), //
PAST_DUE_FEE(PAST_DUE, FEE), //
PAST_DUE_PRINCIPAL(PAST_DUE, PRINCIPAL), //
PAST_DUE_INTEREST(PAST_DUE, INTEREST), //
DUE_PENALTY(DUE, PENALTY), //
DUE_FEE(DUE, FEE), //
DUE_PRINCIPAL(DUE, PRINCIPAL), //
DUE_INTEREST(DUE, INTEREST), //
IN_ADVANCE_PENALTY(IN_ADVANCE, PENALTY), //
IN_ADVANCE_FEE(IN_ADVANCE, FEE), //
IN_ADVANCE_PRINCIPAL(IN_ADVANCE, PRINCIPAL), //
IN_ADVANCE_INTEREST(IN_ADVANCE, INTEREST); //
private final DueType dueType;
private final AllocationType allocationType;
}
@@ -24,4 +24,5 @@
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.1.xsd">
<!-- Sequence is starting from 1000 to make it easier to move existing liquibase changesets here -->
<include relativeToChangelogFile="true" file="parts/1001_add_audit_fields_to_loan_charge.xml"/>
<include relativeToChangelogFile="true" file="parts/1002_add_payment_allocation_rule.xml"/>
</databaseChangeLog>
@@ -0,0 +1,141 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.3.xsd">
<changeSet author="fineract" id="1002-1">
<createTable tableName="m_loan_product_payment_allocation_rule">
<column autoIncrement="true" name="id" type="BIGINT">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="loan_product_id" type="BIGINT">
<constraints nullable="false" foreignKeyName="m_loan_product_payment_allocation_rule_fk" referencedTableName="m_product_loan" referencedColumnNames="id"/>
</column>
<column name="transaction_type" type="VARCHAR(255)">
<constraints nullable="false"/>
</column>
<column name="allocation_types" type="TEXT">
<constraints nullable="false"/>
</column>
<column name="future_installment_allocation_rule" type="VARCHAR(255)">
<constraints nullable="false"/>
</column>
<column name="created_by" type="BIGINT">
<constraints nullable="false"/>
</column>
<column name="last_modified_by" type="BIGINT">
<constraints nullable="false"/>
</column>
</createTable>
<createTable tableName="m_loan_payment_allocation_rule">
<column autoIncrement="true" name="id" type="BIGINT">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="loan_id" type="BIGINT">
<constraints nullable="false" foreignKeyName="m_loan_payment_allocation_rule_fk" referencedTableName="m_loan" referencedColumnNames="id"/>
</column>
<column name="transaction_type" type="VARCHAR(255)">
<constraints nullable="false"/>
</column>
<column name="allocation_types" type="TEXT">
<constraints nullable="false"/>
</column>
<column name="future_installment_allocation_rule" type="VARCHAR(255)">
<constraints nullable="false"/>
</column>
<column name="created_by" type="BIGINT">
<constraints nullable="false"/>
</column>
<column name="last_modified_by" type="BIGINT">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
<changeSet author="fineract" id="1002-2" context="mysql">
<addColumn tableName="m_loan_product_payment_allocation_rule">
<column name="created_on_utc" type="DATETIME(6)">
<constraints nullable="false"/>
</column>
<column name="last_modified_on_utc" type="DATETIME(6)">
<constraints nullable="false"/>
</column>
</addColumn>
<addColumn tableName="m_loan_payment_allocation_rule">
<column name="created_on_utc" type="DATETIME(6)">
<constraints nullable="false"/>
</column>
<column name="last_modified_on_utc" type="DATETIME(6)">
<constraints nullable="false"/>
</column>
</addColumn>
</changeSet>
<changeSet author="fineract" id="1002-2" context="postgresql">
<addColumn tableName="m_loan_product_payment_allocation_rule">
<column name="created_on_utc" type="TIMESTAMP WITH TIME ZONE">
<constraints nullable="false"/>
</column>
<column name="last_modified_on_utc" type="TIMESTAMP WITH TIME ZONE">
<constraints nullable="false"/>
</column>
</addColumn>
<addColumn tableName="m_loan_payment_allocation_rule">
<column name="created_on_utc" type="TIMESTAMP WITH TIME ZONE">
<constraints nullable="false"/>
</column>
<column name="last_modified_on_utc" type="TIMESTAMP WITH TIME ZONE">
<constraints nullable="false"/>
</column>
</addColumn>
</changeSet>
<changeSet author="fineract" id="1002-3">
<addUniqueConstraint tableName="m_loan_product_payment_allocation_rule" columnNames="loan_product_id,transaction_type" constraintName="uq_m_loan_product_payment_allocation_rule"/>
<addUniqueConstraint tableName="m_loan_payment_allocation_rule" columnNames="loan_id,transaction_type" constraintName="uq_m_loan_payment_allocation_rule"/>
</changeSet>
<changeSet author="fineract" id="1002-4">
<addForeignKeyConstraint baseColumnNames="created_by" baseTableName="m_loan_product_payment_allocation_rule"
constraintName="FK_loan_product_payment_allocation_rule_created_by" deferrable="false" initiallyDeferred="false"
onDelete="RESTRICT" onUpdate="RESTRICT" referencedColumnNames="id"
referencedTableName="m_appuser" validate="true"/>
<addForeignKeyConstraint baseColumnNames="last_modified_by" baseTableName="m_loan_product_payment_allocation_rule"
constraintName="FK_loan_product_payment_allocation_rule_last_modified_by" deferrable="false"
initiallyDeferred="false"
onDelete="RESTRICT" onUpdate="RESTRICT" referencedColumnNames="id"
referencedTableName="m_appuser" validate="true"/>
<addForeignKeyConstraint baseColumnNames="created_by" baseTableName="m_loan_payment_allocation_rule"
constraintName="FK_loan_payment_allocation_rule_created_by" deferrable="false" initiallyDeferred="false"
onDelete="RESTRICT" onUpdate="RESTRICT" referencedColumnNames="id"
referencedTableName="m_appuser" validate="true"/>
<addForeignKeyConstraint baseColumnNames="last_modified_by" baseTableName="m_loan_payment_allocation_rule"
constraintName="FK_loan_payment_allocation_rule_last_modified_by" deferrable="false"
initiallyDeferred="false"
onDelete="RESTRICT" onUpdate="RESTRICT" referencedColumnNames="id"
referencedTableName="m_appuser" validate="true"/>
</changeSet>
<changeSet id="fineract" author="1002-5">
<createIndex tableName="m_loan_product_payment_allocation_rule" indexName="IND_loan_product_payment_allocation_rule_loan_product_id">
<column name="loan_product_id"/>
</createIndex>
<createIndex tableName="m_loan_payment_allocation_rule" indexName="IND_loan_payment_allocation_rule_loan_id">
<column name="loan_id"/>
</createIndex>
</changeSet>
</databaseChangeLog>
@@ -106,6 +106,14 @@
<class>org.apache.fineract.portfolio.loanproduct.domain.LoanProductInterestRecalculationDetails</class>
<class>org.apache.fineract.portfolio.loanproduct.domain.LoanProductVariableInstallmentConfig</class>
<class>org.apache.fineract.portfolio.repaymentwithpostdatedchecks.domain.PostDatedChecks</class>
<class>org.apache.fineract.portfolio.loanproduct.domain.PaymentAllocationType</class>
<class>org.apache.fineract.portfolio.loanproduct.domain.DueType</class>
<class>org.apache.fineract.portfolio.loanproduct.domain.AllocationType</class>
<class>org.apache.fineract.portfolio.loanproduct.domain.FutureInstallmentAllocationRule</class>
<class>org.apache.fineract.portfolio.loanproduct.domain.PaymentAllocationTransactionType</class>
<class>org.apache.fineract.portfolio.loanproduct.domain.AllocationTypeListConverter</class>
<class>org.apache.fineract.portfolio.loanproduct.domain.LoanProductPaymentAllocationRule</class>
<class>org.apache.fineract.portfolio.loanaccount.domain.LoanPaymentAllocationRule</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.weaving" value="static" />