You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fineract.apache.org by "ruchiD (via GitHub)" <gi...@apache.org> on 2023/02/03 09:12:25 UTC

[GitHub] [fineract] ruchiD commented on a diff in pull request #2928: FINERACT-1855-Arrears-aging-job-as-business-step

ruchiD commented on code in PR #2928:
URL: https://github.com/apache/fineract/pull/2928#discussion_r1095527638


##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/jobs/updateloanarrearsageing/LoanArrearsAgeingUpdateHandler.java:
##########
@@ -0,0 +1,202 @@
+/**
+ * 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.jobs.updateloanarrearsageing;
+
+import java.sql.Types;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
+import org.apache.fineract.infrastructure.core.service.database.DatabaseSpecificSQLGenerator;
+import org.apache.fineract.portfolio.loanaccount.loanschedule.data.LoanSchedulePeriodData;
+import org.apache.fineract.portfolio.loanaccount.service.LoanArrearsAgingService;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.stereotype.Component;
+
+@Slf4j
+@Component
+@RequiredArgsConstructor
+public class LoanArrearsAgeingUpdateHandler {
+
+    private final JdbcTemplate jdbcTemplate;
+    private final DatabaseSpecificSQLGenerator sqlGenerator;
+    private final LoanArrearsAgingService loanArrearsAgingService;
+
+    private void truncateLoanArrearsAgingDetails() {
+        jdbcTemplate.execute("truncate table m_loan_arrears_aging");
+    }
+
+    private void deleteLoanArrearsAgingDetails(List<Long> loanIds) {
+        // delete existing record for loan from m_loan_arrears_aging table
+        for (Long loanId : loanIds) {
+            jdbcTemplate.update("delete from m_loan_arrears_aging where loan_id=?", loanId);
+        }
+    }
+
+    public void updateLoanArrearsAgeingDetailsForAllLoans() {
+        truncateLoanArrearsAgingDetails();
+        String updateSQL = buildQueryForUpdateAgeingDetails(Boolean.TRUE);
+        List<String> insertStatements = updateLoanArrearsAgeingDetailsWithOriginalScheduleForAllLoans();
+        insertStatements.add(0, updateSQL);
+        final int[] records = this.jdbcTemplate.batchUpdate(insertStatements.toArray(new String[0]));
+        int result = 0;
+        for (int record : records) {
+            result += record;
+        }
+
+        log.debug("{}: Records affected by updateLoanArrearsAgeingDetails: {}", ThreadLocalContextUtil.getTenant().getName(), result);
+    }
+
+    public void updateLoanArrearsAgeingDetails(List<Long> loanIdsForUpdate) {
+
+        deleteLoanArrearsAgingDetails(loanIdsForUpdate);
+        String updateSQL = buildQueryForUpdateAgeingDetails(Boolean.FALSE);
+        List<Object[]> batch = new ArrayList<Object[]>();
+        if (!loanIdsForUpdate.isEmpty()) {
+            for (Long loanId : loanIdsForUpdate) {
+                Object[] values = new Object[] { loanId };
+                batch.add(values);
+            }
+        }
+        final int[] recordsUpdatedWithoutOriginalSchedule = this.jdbcTemplate.batchUpdate(updateSQL, batch);
+        int result = 0;
+        for (int recordWithoutOriginalSchedule : recordsUpdatedWithoutOriginalSchedule) {
+            result += recordWithoutOriginalSchedule;
+        }
+        List<String> insertStatements = updateLoanArrearsAgeingDetailsWithOriginalSchedule(loanIdsForUpdate);
+        if (!insertStatements.isEmpty()) {
+            final int[] recordsUpdatedWithOriginalSchedule = this.jdbcTemplate.batchUpdate(insertStatements.toArray(new String[0]));
+            for (int recordWithOriginalSchedule : recordsUpdatedWithOriginalSchedule) {
+                result += recordWithOriginalSchedule;
+            }
+        }
+        log.debug("{}: Records affected by updateLoanArrearsAgeingDetails: {}", ThreadLocalContextUtil.getTenant().getName(), result);
+
+    }
+
+    private String buildQueryForUpdateAgeingDetails(Boolean isForAllLoans) {
+        final StringBuilder updateSqlBuilder = new StringBuilder(900);
+        final String principalOverdueCalculationSql = "SUM(COALESCE(mr.principal_amount, 0) - coalesce(mr.principal_completed_derived, 0) - coalesce(mr.principal_writtenoff_derived, 0))";
+        final String interestOverdueCalculationSql = "SUM(COALESCE(mr.interest_amount, 0) - coalesce(mr.interest_writtenoff_derived, 0) - coalesce(mr.interest_waived_derived, 0) - "
+                + "coalesce(mr.interest_completed_derived, 0))";
+        final String feeChargesOverdueCalculationSql = "SUM(COALESCE(mr.fee_charges_amount, 0) - coalesce(mr.fee_charges_writtenoff_derived, 0) - "
+                + "coalesce(mr.fee_charges_waived_derived, 0) - coalesce(mr.fee_charges_completed_derived, 0))";
+        final String penaltyChargesOverdueCalculationSql = "SUM(COALESCE(mr.penalty_charges_amount, 0) - coalesce(mr.penalty_charges_writtenoff_derived, 0) - "
+                + "coalesce(mr.penalty_charges_waived_derived, 0) - coalesce(mr.penalty_charges_completed_derived, 0))";
+
+        updateSqlBuilder.append(
+                "INSERT INTO m_loan_arrears_aging(loan_id,principal_overdue_derived,interest_overdue_derived,fee_charges_overdue_derived,penalty_charges_overdue_derived,total_overdue_derived,overdue_since_date_derived)");
+        updateSqlBuilder.append("select ml.id as loanId,");

Review Comment:
   done.



##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/jobs/updateloanarrearsageing/LoanArrearsAgeingUpdateHandler.java:
##########
@@ -0,0 +1,202 @@
+/**
+ * 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.jobs.updateloanarrearsageing;
+
+import java.sql.Types;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
+import org.apache.fineract.infrastructure.core.service.database.DatabaseSpecificSQLGenerator;
+import org.apache.fineract.portfolio.loanaccount.loanschedule.data.LoanSchedulePeriodData;
+import org.apache.fineract.portfolio.loanaccount.service.LoanArrearsAgingService;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.stereotype.Component;
+
+@Slf4j
+@Component
+@RequiredArgsConstructor
+public class LoanArrearsAgeingUpdateHandler {
+
+    private final JdbcTemplate jdbcTemplate;
+    private final DatabaseSpecificSQLGenerator sqlGenerator;
+    private final LoanArrearsAgingService loanArrearsAgingService;
+
+    private void truncateLoanArrearsAgingDetails() {
+        jdbcTemplate.execute("truncate table m_loan_arrears_aging");
+    }
+
+    private void deleteLoanArrearsAgingDetails(List<Long> loanIds) {
+        // delete existing record for loan from m_loan_arrears_aging table
+        for (Long loanId : loanIds) {
+            jdbcTemplate.update("delete from m_loan_arrears_aging where loan_id=?", loanId);
+        }
+    }
+
+    public void updateLoanArrearsAgeingDetailsForAllLoans() {
+        truncateLoanArrearsAgingDetails();
+        String updateSQL = buildQueryForUpdateAgeingDetails(Boolean.TRUE);
+        List<String> insertStatements = updateLoanArrearsAgeingDetailsWithOriginalScheduleForAllLoans();
+        insertStatements.add(0, updateSQL);
+        final int[] records = this.jdbcTemplate.batchUpdate(insertStatements.toArray(new String[0]));
+        int result = 0;
+        for (int record : records) {
+            result += record;
+        }
+
+        log.debug("{}: Records affected by updateLoanArrearsAgeingDetails: {}", ThreadLocalContextUtil.getTenant().getName(), result);
+    }
+
+    public void updateLoanArrearsAgeingDetails(List<Long> loanIdsForUpdate) {
+
+        deleteLoanArrearsAgingDetails(loanIdsForUpdate);
+        String updateSQL = buildQueryForUpdateAgeingDetails(Boolean.FALSE);
+        List<Object[]> batch = new ArrayList<Object[]>();
+        if (!loanIdsForUpdate.isEmpty()) {
+            for (Long loanId : loanIdsForUpdate) {
+                Object[] values = new Object[] { loanId };
+                batch.add(values);
+            }
+        }
+        final int[] recordsUpdatedWithoutOriginalSchedule = this.jdbcTemplate.batchUpdate(updateSQL, batch);
+        int result = 0;
+        for (int recordWithoutOriginalSchedule : recordsUpdatedWithoutOriginalSchedule) {
+            result += recordWithoutOriginalSchedule;
+        }
+        List<String> insertStatements = updateLoanArrearsAgeingDetailsWithOriginalSchedule(loanIdsForUpdate);
+        if (!insertStatements.isEmpty()) {
+            final int[] recordsUpdatedWithOriginalSchedule = this.jdbcTemplate.batchUpdate(insertStatements.toArray(new String[0]));
+            for (int recordWithOriginalSchedule : recordsUpdatedWithOriginalSchedule) {
+                result += recordWithOriginalSchedule;
+            }
+        }
+        log.debug("{}: Records affected by updateLoanArrearsAgeingDetails: {}", ThreadLocalContextUtil.getTenant().getName(), result);
+
+    }
+
+    private String buildQueryForUpdateAgeingDetails(Boolean isForAllLoans) {

Review Comment:
   done



-- 
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: commits-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org