From 75e6a98cfcca8a7939acf72a0f4bbf6600e5da2e Mon Sep 17 00:00:00 2001 From: Faheem Ahmad Date: Thu, 14 Nov 2024 14:03:05 +0500 Subject: [PATCH] SU-442 (#1289) * SU-442 --- .../portfolio/loanaccount/domain/Loan.java | 42 +- .../loanaccount/domain/LoanRepository.java | 12 +- .../domain/LoanRepositoryWrapper.java | 8 +- .../service/LoanReadPlatformServiceImpl.java | 604 +++++++++++------- ...WritePlatformServiceJpaRepositoryImpl.java | 9 +- .../db/changelog/tenant/changelog-tenant.xml | 1 + ..._442_add_columns_for_reclaim_exclusion.xml | 589 +++++++++++++++++ 7 files changed, 1002 insertions(+), 263 deletions(-) create mode 100644 fineract-provider/src/main/resources/db/changelog/tenant/parts/20241112011009115_SU_442_add_columns_for_reclaim_exclusion.xml diff --git a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/Loan.java b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/Loan.java index a241dba0d45..5877d88a3ca 100644 --- a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/Loan.java +++ b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/Loan.java @@ -474,12 +474,14 @@ public class Loan extends AbstractAuditableWithUTCDateTimeCustom { @Embedded private LoanCustomizationDetail loanCustomizationDetail; - @Column(name = "excluded_from_reclaim", nullable = false) - private boolean excludedFromReclaim = false; + @Column(name = "excluded_for_insurance_claim") + private String excludedForInsuranceClaim; - @Column(name = "excluded_for_claim_type") - private String excludedForClaimType; + @Column(name = "excluded_for_aval_claim") + private String excludedForAvalClaim; + @Column(name = "excluded_for_castigado_claim") + private String excludedForCastigadoClaim; // This attribute is used only to capture the repayment strategy (VERTICAL/HORIZONTAL). Updating all the related // methods to add this attribute as a parameter was creating a mess @Transient @@ -8030,12 +8032,28 @@ public void setRecalculateEMI(boolean recalculateEMI) { this.recalculateEMI = recalculateEMI; } - public boolean excludedFromReclaim() { - return excludedFromReclaim; + public String excludedForInsuranceClaim() { + return excludedForInsuranceClaim; } - public void setExcludedFromReclaim(boolean excludedFromReclaim) { - this.excludedFromReclaim = excludedFromReclaim; + public void setExcludedForInsuranceClaim(String excludedForInsuranceClaim) { + this.excludedForInsuranceClaim = excludedForInsuranceClaim; + } + + public String excludedForAvalClaim() { + return excludedForAvalClaim; + } + + public void setExcludedForAvalClaim(String excludedForAvalClaim) { + this.excludedForAvalClaim = excludedForAvalClaim; + } + + public String excludedForCastigadoClaim() { + return excludedForCastigadoClaim; + } + + public void setExcludedForCastigadoClaim(String excludedForCastigadoClaim) { + this.excludedForCastigadoClaim = excludedForCastigadoClaim; } public String claimType() { @@ -8054,14 +8072,6 @@ public void setClaimDate(LocalDate claimDate) { this.claimDate = claimDate; } - public String excludedForClaimType() { - return excludedForClaimType; - } - - public void setExcludedForClaimType(String excludedForClaimType) { - this.excludedForClaimType = excludedForClaimType; - } - public Integer getLastInstallmentChargeCalculatedOn() { return lastInstallmentChargeCalculatedOn; } diff --git a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/LoanRepository.java b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/LoanRepository.java index 559a23b0577..4357b03b0cb 100644 --- a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/LoanRepository.java +++ b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/LoanRepository.java @@ -243,6 +243,14 @@ List findAllNonClosedLoansByLastClosedBusinessDateNotNullAndMinAndMaxLoanI List findLoanIdByStatusId(@Param("statusId") Integer statusId); @Modifying - @Query("update Loan l set l.excludedFromReclaim = false, l.excludedForClaimType = null WHERE l.excludedForClaimType = :claimType") - void removeLoanExclusion(@Param("claimType") String claimType); + @Query("update Loan l set l.excludedForInsuranceClaim = null") + void removeLoanInsuranceExclusion(); + + @Modifying + @Query("update Loan l set l.excludedForAvalClaim = null") + void removeLoanAvalExclusion(); + + @Modifying + @Query("update Loan l set l.excludedForCastigadoClaim = null") + void removeLoanCastigadoExclusion(); } diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/LoanRepositoryWrapper.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/LoanRepositoryWrapper.java index 86e776574ee..78e5cb3bbea 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/LoanRepositoryWrapper.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/LoanRepositoryWrapper.java @@ -264,7 +264,13 @@ public List findLoanIdsByStatusId(Integer statusId) { } public void removeLoanExclusion(String claimType) { - repository.removeLoanExclusion(claimType); + if (claimType.equals("insurance")) { + repository.removeLoanInsuranceExclusion(); + } else if (claimType.equals("guarantor")) { + repository.removeLoanAvalExclusion(); + } else { + repository.removeLoanCastigadoExclusion(); + } } } diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanReadPlatformServiceImpl.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanReadPlatformServiceImpl.java index 518f58eb9af..ece1e27004d 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanReadPlatformServiceImpl.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanReadPlatformServiceImpl.java @@ -3418,270 +3418,385 @@ private static final class LoanReclaimMapper implements RowMapper= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) and mlrs.duedate <= current_date - where mlc.charge_calculation_enum IN (468, 575, 231) - group by mlc.loan_id) insurance_chg ON insurance_chg.loan_id = ml.id - LEFT JOIN (SELECT sum(mlic.amount) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 - inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id\s - inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id and mlrs.completed_derived != true and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) and mlrs.duedate <= current_date - JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 - JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id - WHERE parent_charge.charge_calculation_enum IN (468, 575, 231) - group by mlc2.loan_id) vat_chg ON vat_chg.loan_id = ml.id - left join( select mlc.loan_id, sum(mlic.amount) outstanding_amount from m_loan_charge mlc - inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id\s - inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id and mlrs.completed_derived != true and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) and mlrs.duedate <= current_date - where mlc.charge_calculation_enum = 41 group by mlc.loan_id) aval_chg ON aval_chg.loan_id = ml.id - LEFT JOIN (SELECT sum(mlc2.amount_outstanding_derived) outstanding_amount, mlc2.loan_id - FROM m_loan_charge mlc2 - JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 - JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id - WHERE parent_charge.charge_calculation_enum = 41 - group by mlc2.loan_id) aval_vat_chg ON aval_vat_chg.loan_id = ml.id - left join( select mlc.loan_id, sum(mlic.amount) outstanding_amount from m_loan_charge mlc - inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id\s - inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id and mlrs.completed_derived != true and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) and mlrs.duedate <= current_date - where mlc.charge_calculation_enum NOT IN (468, 575, 231, 342, 41) - and mlc.is_penalty = false - group by mlc.loan_id) other_chg ON other_chg.loan_id = ml.id - LEFT JOIN (SELECT sum(mlic.amount) outstanding_amount, mlc2.loan_id - FROM m_loan_charge mlc2 - inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id\s - inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id and mlrs.completed_derived != true and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) and mlrs.duedate <= current_date - JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 - JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id - WHERE parent_charge.charge_calculation_enum NOT IN (468, 575, 231)group by mlc2.loan_id) other_vat_chg ON other_vat_chg.loan_id = ml.id - left join(select mlc.loan_id, sum(mlic.amount) outstanding_amount from m_loan_charge mlc - inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id\s - inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id and mlrs.completed_derived != true and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) and mlrs.duedate <= current_date - where mlc.is_penalty = true group by mlc.loan_id) penalty_chg ON penalty_chg.loan_id = ml.id - LEFT JOIN (SELECT sum(mlic.amount) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 - inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id\s - inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id and mlrs.completed_derived != true and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) and mlrs.duedate <= current_date - JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 - JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id - WHERE parent_charge.is_penalty = true group by mlc2.loan_id) penalty_vat_chg ON penalty_vat_chg.loan_id = ml.id - left join (SELECT mlrs.loan_id, - Round(mlrs.interest_amount / (duedate-fromdate) * (current_date - fromdate)) as interstTillDay\s - FROM m_loan_repayment_schedule mlrs - WHERE duedate >= current_date and mlrs.completed_derived != true\s - AND fromdate <= current_date) tillday on tillday.loan_id = ml.id - join (select mlrs.loan_id,sum(mlrs.interest_amount) as interstAmount from m_loan_repayment_schedule mlrs where mlrs.completed_derived != true and duedate <= current_date group by loan_id) ovrd on ovrd.loan_id = ml.id - join (select mlc.loan_id, sum(mlic.amount) outstanding_amount from m_loan_charge mlc - inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id\s - inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id and mlrs.completed_derived != true\s - and mlrs.fromdate <= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) and mlrs.duedate >= current_date\s - and fromdate <= current_date where mlc.charge_calculation_enum IN (468, 575, 231) - group by mlc.loan_id) as tiilcharge on tiilcharge.loan_id = ml.id - join (select mlc.loan_id, sum(mlic.amount) outstanding_amount from m_loan_charge mlc - inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id\s - inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id and mlrs.completed_derived != true and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) and mlrs.duedate >= current_date and fromdate <= current_date - where mlc.charge_calculation_enum NOT IN (468, 575, 231, 342, 41) and mlc.is_penalty = false group by mlc.loan_id)\s - tilldayOthrCharge on tilldayOthrCharge.loan_id = ml.id - join (select mlc.loan_id, sum(mlic.amount) outstanding_amount - from m_loan_charge mlc - inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id\s - inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id and mlrs.completed_derived != true and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) and mlrs.duedate >= current_date and fromdate <= current_date - where mlc.charge_calculation_enum = 41 - group by mlc.loan_id) tillDayaAvalCharge on tillDayaAvalCharge.loan_id = ml.id - WHERE - ml.loan_status_id = 300 - and (CURRENT_DATE - mlaa.overdue_since_date_derived) > ? + ml.id loanId, + ml.account_no loanAccoutNumber, + mcl.display_name clientName, + mpl.name productName, + (CURRENT_DATE - mlaa.overdue_since_date_derived) as daysInArrears, + ml.principal_outstanding_derived outstandingPrincipal, + COALESCE(due_interest.intersetAmount,0) + COALESCE(overdue_interest.intersetAmount,0) outstandingInterest, + (COALESCE(insurance_chg.outstanding_amount,0) + COALESCE(vat_chg.outstanding_amount,0)) outstandingMandatoryInsuranceAmount, + (COALESCE(aval_chg.outstanding_amount,0) + COALESCE(aval_vat_chg.outstanding_amount,0)) AS outstandingAvalAmount, + (COALESCE(other_chg.outstanding_amount,0) + COALESCE(other_vat_chg.outstanding_amount,0)) AS outstandingOtherChargesAmount, + (COALESCE(penalty_chg.outstanding_amount,0) + COALESCE(penalty_vat_chg.outstanding_amount,0)) AS outstandingPenaltyAmount, + ml.principal_outstanding_derived + + COALESCE(due_interest.intersetAmount,0) + COALESCE(overdue_interest.intersetAmount,0) + + COALESCE(insurance_chg.outstanding_amount,0) + COALESCE(vat_chg.outstanding_amount,0) + + COALESCE(aval_chg.outstanding_amount,0) + COALESCE(aval_vat_chg.outstanding_amount,0) + + COALESCE(other_chg.outstanding_amount,0) + COALESCE(other_vat_chg.outstanding_amount,0) + + COALESCE(penalty_chg.outstanding_amount,0) + COALESCE(penalty_vat_chg.outstanding_amount,0) totalOutstandingAmount + from + m_loan ml + join m_product_loan mpl on mpl.id = ml.product_id + join m_client mcl on mcl.id = ml.client_id + join m_loan_arrears_aging mlaa on mlaa.loan_id = ml.id + left join ( + SELECT mlrs.loan_id, + Round(mlrs.interest_amount / (duedate-fromdate) * (current_date - fromdate)) as intersetAmount + FROM m_loan_repayment_schedule mlrs + WHERE duedate >= current_date and mlrs.completed_derived != true + AND fromdate <= current_date + ) due_interest on due_interest.loan_id = ml.id + left join ( + select mlrs.loan_id,sum(mlrs.interest_amount) as intersetAmount + from m_loan_repayment_schedule mlrs + where mlrs.completed_derived != true and duedate <= current_date group by loan_id + ) overdue_interest on overdue_interest.loan_id = ml.id + LEFT join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + where mlc.charge_calculation_enum IN (468, 575, 231) + group by mlc.loan_id + ) insurance_chg ON insurance_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.charge_calculation_enum IN (468, 575, 231) + group by mlc2.loan_id + ) vat_chg ON vat_chg.loan_id = ml.id + join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + where mlc.charge_calculation_enum = 41 + group by mlc.loan_id + ) aval_chg ON aval_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.charge_calculation_enum = 41 + group by mlc2.loan_id + ) aval_vat_chg ON aval_vat_chg.loan_id = ml.id + LEFT join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + where mlc.charge_calculation_enum NOT IN (468, 575, 231, 342, 41) + group by mlc.loan_id + ) other_chg ON other_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.charge_calculation_enum NOT IN (468, 575, 231, 41) + group by mlc2.loan_id + ) other_vat_chg ON other_vat_chg.loan_id = ml.id + LEFT join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and mlrs.duedate <= current_date + where mlc.is_penalty = true group by mlc.loan_id + ) penalty_chg ON penalty_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and mlrs.duedate <= current_date + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.is_penalty = true group by mlc2.loan_id + ) penalty_vat_chg ON penalty_vat_chg.loan_id = ml.id + WHERE + ml.loan_status_id = 300 + and (CURRENT_DATE - mlaa.overdue_since_date_derived) > ? """; } public String loanReclaimSchemaForInsurance() { return """ - ml.id loanId, - ml.account_no loanAccoutNumber, - mcl.display_name clientName, - mpl.name productName, - (CURRENT_DATE - mlaa.overdue_since_date_derived) as daysInArrears, - ml.principal_outstanding_derived outstandingPrincipal, - (coalesce(interstTillDay+interstAmount,0)) as outstandingInterest, - (COALESCE(insurance_chg.outstanding_amount,0) + COALESCE(vat_chg.outstanding_amount,0) + COALESCE(tiilcharge.outstanding_amount,0)) outstandingMandatoryInsuranceAmount, - (COALESCE(aval_chg.outstanding_amount,0) + COALESCE(aval_vat_chg.outstanding_amount,0)+ COALESCE(tillDayaAvalCharge.outstanding_amount,0)) AS outstandingAvalAmount, - (COALESCE(other_chg.outstanding_amount,0) + COALESCE(other_vat_chg.outstanding_amount,0)+COALESCE(tilldayOthrCharge.outstanding_amount,0)) AS outstandingOtherChargesAmount, - (COALESCE(penalty_chg.outstanding_amount,0) + COALESCE(penalty_vat_chg.outstanding_amount,0)) AS outstandingPenaltyAmount, - (ml.principal_outstanding_derived+coalesce(interstTillDay+interstAmount,0)+(COALESCE(insurance_chg.outstanding_amount,0) + COALESCE(vat_chg.outstanding_amount,0) + COALESCE(tiilcharge.outstanding_amount,0)) +(COALESCE(aval_chg.outstanding_amount,0) + COALESCE(aval_vat_chg.outstanding_amount,0)+ COALESCE(tillDayaAvalCharge.outstanding_amount,0))+(COALESCE(other_chg.outstanding_amount,0) + COALESCE(other_vat_chg.outstanding_amount,0)+COALESCE(tilldayOthrCharge.outstanding_amount,0))+(COALESCE(penalty_chg.outstanding_amount,0) + COALESCE(penalty_vat_chg.outstanding_amount,0))) as totalOutstandingAmount - from - m_loan ml - join m_product_loan mpl on mpl.id = ml.product_id - join m_client mcl on mcl.id = ml.client_id - join m_loan_arrears_aging mlaa on mlaa.loan_id = ml.id - join(select mlc.loan_id, sum(mlic.amount) outstanding_amount from m_loan_charge mlc - inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id\s - inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id and mlrs.completed_derived != true and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) and mlrs.duedate <= current_date - where mlc.charge_calculation_enum IN (468, 575, 231) - group by mlc.loan_id) insurance_chg ON insurance_chg.loan_id = ml.id - LEFT JOIN (SELECT sum(mlic.amount) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 - inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id\s - inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id and mlrs.completed_derived != true and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) and mlrs.duedate <= current_date - JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 - JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id - WHERE parent_charge.charge_calculation_enum IN (468, 575, 231) - group by mlc2.loan_id) vat_chg ON vat_chg.loan_id = ml.id - left join( select mlc.loan_id, sum(mlic.amount) outstanding_amount from m_loan_charge mlc - inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id\s - inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id and mlrs.completed_derived != true and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) and mlrs.duedate <= current_date - where mlc.charge_calculation_enum = 41 group by mlc.loan_id) aval_chg ON aval_chg.loan_id = ml.id - LEFT JOIN (SELECT sum(mlc2.amount_outstanding_derived) outstanding_amount, mlc2.loan_id - FROM m_loan_charge mlc2 - JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 - JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id - WHERE parent_charge.charge_calculation_enum = 41 - group by mlc2.loan_id) aval_vat_chg ON aval_vat_chg.loan_id = ml.id - left join( select mlc.loan_id, sum(mlic.amount) outstanding_amount from m_loan_charge mlc - inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id\s - inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id and mlrs.completed_derived != true and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) and mlrs.duedate <= current_date - where mlc.charge_calculation_enum NOT IN (468, 575, 231, 342, 41) - and mlc.is_penalty = false - group by mlc.loan_id) other_chg ON other_chg.loan_id = ml.id - LEFT JOIN (SELECT sum(mlic.amount) outstanding_amount, mlc2.loan_id - FROM m_loan_charge mlc2 - inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id\s - inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id and mlrs.completed_derived != true and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) and mlrs.duedate <= current_date - JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 - JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id - WHERE parent_charge.charge_calculation_enum NOT IN (468, 575, 231)group by mlc2.loan_id) other_vat_chg ON other_vat_chg.loan_id = ml.id - left join(select mlc.loan_id, sum(mlic.amount) outstanding_amount from m_loan_charge mlc - inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id\s - inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id and mlrs.completed_derived != true and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) and mlrs.duedate <= current_date - where mlc.is_penalty = true group by mlc.loan_id) penalty_chg ON penalty_chg.loan_id = ml.id - LEFT JOIN (SELECT sum(mlic.amount) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 - inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id\s - inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id and mlrs.completed_derived != true and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) and mlrs.duedate <= current_date - JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 - JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id - WHERE parent_charge.is_penalty = true group by mlc2.loan_id) penalty_vat_chg ON penalty_vat_chg.loan_id = ml.id - left join (SELECT mlrs.loan_id, - Round(mlrs.interest_amount / (duedate-fromdate) * (current_date - fromdate)) as interstTillDay\s - FROM m_loan_repayment_schedule mlrs - WHERE duedate >= current_date and mlrs.completed_derived != true\s - AND fromdate <= current_date) tillday on tillday.loan_id = ml.id - join (select mlrs.loan_id,sum(mlrs.interest_amount) as interstAmount from m_loan_repayment_schedule mlrs where mlrs.completed_derived != true and duedate <= current_date group by loan_id) ovrd on ovrd.loan_id = ml.id - join (select mlc.loan_id, sum(mlic.amount) outstanding_amount from m_loan_charge mlc - inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id\s - inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id and mlrs.completed_derived != true\s - and mlrs.fromdate <= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) and mlrs.duedate >= current_date\s - and fromdate <= current_date where mlc.charge_calculation_enum IN (468, 575, 231) - group by mlc.loan_id) as tiilcharge on tiilcharge.loan_id = ml.id - join (select mlc.loan_id, sum(mlic.amount) outstanding_amount from m_loan_charge mlc - inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id\s - inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id and mlrs.completed_derived != true and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) and mlrs.duedate >= current_date and fromdate <= current_date - where mlc.charge_calculation_enum NOT IN (468, 575, 231, 342, 41) and mlc.is_penalty = false group by mlc.loan_id)\s - tilldayOthrCharge on tilldayOthrCharge.loan_id = ml.id - join (select mlc.loan_id, sum(mlic.amount) outstanding_amount - from m_loan_charge mlc - inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id\s - inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id and mlrs.completed_derived != true and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) and mlrs.duedate >= current_date and fromdate <= current_date - where mlc.charge_calculation_enum = 41 - group by mlc.loan_id) tillDayaAvalCharge on tillDayaAvalCharge.loan_id = ml.id - WHERE - ml.loan_status_id = 300 - and (CURRENT_DATE - mlaa.overdue_since_date_derived) > ? - and ml.excluded_from_reclaim = false and ml.claim_type is null and ml.loan_schedule_type = 'PROGRESSIVE'\s + ml.id loanId, + ml.account_no loanAccoutNumber, + mcl.display_name clientName, + mpl.name productName, + (CURRENT_DATE - mlaa.overdue_since_date_derived) as daysInArrears, + ml.principal_outstanding_derived outstandingPrincipal, + COALESCE(due_interest.intersetAmount,0) + COALESCE(overdue_interest.intersetAmount,0) outstandingInterest, + (COALESCE(insurance_chg.outstanding_amount,0) + COALESCE(vat_chg.outstanding_amount,0)) outstandingMandatoryInsuranceAmount, + (COALESCE(aval_chg.outstanding_amount,0) + COALESCE(aval_vat_chg.outstanding_amount,0)) AS outstandingAvalAmount, + (COALESCE(other_chg.outstanding_amount,0) + COALESCE(other_vat_chg.outstanding_amount,0)) AS outstandingOtherChargesAmount, + (COALESCE(penalty_chg.outstanding_amount,0) + COALESCE(penalty_vat_chg.outstanding_amount,0)) AS outstandingPenaltyAmount, + ml.principal_outstanding_derived + + COALESCE(due_interest.intersetAmount,0) + COALESCE(overdue_interest.intersetAmount,0) + + COALESCE(insurance_chg.outstanding_amount,0) + COALESCE(vat_chg.outstanding_amount,0) + + COALESCE(aval_chg.outstanding_amount,0) + COALESCE(aval_vat_chg.outstanding_amount,0) + + COALESCE(other_chg.outstanding_amount,0) + COALESCE(other_vat_chg.outstanding_amount,0) + + COALESCE(penalty_chg.outstanding_amount,0) + COALESCE(penalty_vat_chg.outstanding_amount,0) totalOutstandingAmount + from + m_loan ml + join m_product_loan mpl on mpl.id = ml.product_id + join m_client mcl on mcl.id = ml.client_id + join m_loan_arrears_aging mlaa on mlaa.loan_id = ml.id + left join ( + SELECT mlrs.loan_id, + Round(mlrs.interest_amount / (duedate-fromdate) * (current_date - fromdate)) as intersetAmount + FROM m_loan_repayment_schedule mlrs + WHERE duedate >= current_date and mlrs.completed_derived != true + AND fromdate <= current_date + ) due_interest on due_interest.loan_id = ml.id + left join ( + select mlrs.loan_id,sum(mlrs.interest_amount) as intersetAmount + from m_loan_repayment_schedule mlrs + where mlrs.completed_derived != true and duedate <= current_date group by loan_id + ) overdue_interest on overdue_interest.loan_id = ml.id + join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + where mlc.charge_calculation_enum IN (468, 575, 231) + group by mlc.loan_id + ) insurance_chg ON insurance_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.charge_calculation_enum IN (468, 575, 231) + group by mlc2.loan_id + ) vat_chg ON vat_chg.loan_id = ml.id + LEFT join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + where mlc.charge_calculation_enum = 41 + group by mlc.loan_id + ) aval_chg ON aval_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.charge_calculation_enum = 41 + group by mlc2.loan_id + ) aval_vat_chg ON aval_vat_chg.loan_id = ml.id + LEFT join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + where mlc.charge_calculation_enum NOT IN (468, 575, 231, 342, 41) + group by mlc.loan_id + ) other_chg ON other_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.charge_calculation_enum NOT IN (468, 575, 231, 41) + group by mlc2.loan_id + ) other_vat_chg ON other_vat_chg.loan_id = ml.id + LEFT join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and mlrs.duedate <= current_date + where mlc.is_penalty = true group by mlc.loan_id + ) penalty_chg ON penalty_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and mlrs.duedate <= current_date + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.is_penalty = true group by mlc2.loan_id + ) penalty_vat_chg ON penalty_vat_chg.loan_id = ml.id + WHERE + ml.loan_status_id = 300 + and (CURRENT_DATE - mlaa.overdue_since_date_derived) > ? """; } public String loanReclaimSchemaForWriteoff() { return """ - ml.id loanId, + ml.id loanId, ml.account_no loanAccoutNumber, mcl.display_name clientName, mpl.name productName, (CURRENT_DATE - mlaa.overdue_since_date_derived) as daysInArrears, ml.principal_outstanding_derived outstandingPrincipal, - ml.interest_outstanding_derived outstandingInterest, + COALESCE(due_interest.intersetAmount,0) + COALESCE(overdue_interest.intersetAmount,0) outstandingInterest, (COALESCE(insurance_chg.outstanding_amount,0) + COALESCE(vat_chg.outstanding_amount,0)) outstandingMandatoryInsuranceAmount, (COALESCE(aval_chg.outstanding_amount,0) + COALESCE(aval_vat_chg.outstanding_amount,0)) AS outstandingAvalAmount, (COALESCE(other_chg.outstanding_amount,0) + COALESCE(other_vat_chg.outstanding_amount,0)) AS outstandingOtherChargesAmount, (COALESCE(penalty_chg.outstanding_amount,0) + COALESCE(penalty_vat_chg.outstanding_amount,0)) AS outstandingPenaltyAmount, - ml.total_outstanding_derived totalOutstandingAmount + ml.principal_outstanding_derived + + COALESCE(due_interest.intersetAmount,0) + COALESCE(overdue_interest.intersetAmount,0) + + COALESCE(insurance_chg.outstanding_amount,0) + COALESCE(vat_chg.outstanding_amount,0) + + COALESCE(aval_chg.outstanding_amount,0) + COALESCE(aval_vat_chg.outstanding_amount,0) + + COALESCE(other_chg.outstanding_amount,0) + COALESCE(other_vat_chg.outstanding_amount,0) + + COALESCE(penalty_chg.outstanding_amount,0) + COALESCE(penalty_vat_chg.outstanding_amount,0) totalOutstandingAmount from m_loan ml join m_product_loan mpl on mpl.id = ml.product_id join m_client mcl on mcl.id = ml.client_id join m_loan_arrears_aging mlaa on mlaa.loan_id = ml.id - LEFT JOIN - ( - select mlc.loan_id, sum(mlc.amount_outstanding_derived) outstanding_amount - from m_loan_charge mlc - where mlc.charge_calculation_enum IN (468, 575, 231) - group by mlc.loan_id - ) insurance_chg ON insurance_chg.loan_id = ml.id - LEFT JOIN (SELECT sum(mlc2.amount_outstanding_derived) outstanding_amount, mlc2.loan_id - FROM m_loan_charge mlc2 - JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 - JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id - WHERE parent_charge.charge_calculation_enum IN (468, 575, 231) - group by mlc2.loan_id - ) vat_chg ON vat_chg.loan_id = ml.id - LEFT JOIN - ( - select mlc.loan_id, sum(mlc.amount_outstanding_derived) outstanding_amount - from m_loan_charge mlc - where mlc.charge_calculation_enum = 41 - group by mlc.loan_id - ) aval_chg ON aval_chg.loan_id = ml.id - LEFT JOIN (SELECT sum(mlc2.amount_outstanding_derived) outstanding_amount, mlc2.loan_id - FROM m_loan_charge mlc2 - JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 - JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id - WHERE parent_charge.charge_calculation_enum = 41 - group by mlc2.loan_id - ) aval_vat_chg ON aval_vat_chg.loan_id = ml.id - LEFT JOIN - ( - select mlc.loan_id, sum(mlc.amount_outstanding_derived) outstanding_amount - from m_loan_charge mlc - where mlc.charge_calculation_enum NOT IN (468, 575, 231, 342, 41) - and mlc.is_penalty = false - group by mlc.loan_id - ) other_chg ON other_chg.loan_id = ml.id - LEFT JOIN (SELECT sum(mlc2.amount_outstanding_derived) outstanding_amount, mlc2.loan_id - FROM m_loan_charge mlc2 - JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 - JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id - WHERE parent_charge.charge_calculation_enum NOT IN (468, 575, 231) - group by mlc2.loan_id - ) other_vat_chg ON other_vat_chg.loan_id = ml.id - LEFT JOIN - ( - select mlc.loan_id, sum(mlc.amount_outstanding_derived) outstanding_amount - from m_loan_charge mlc - where mlc.is_penalty = true - group by mlc.loan_id - ) penalty_chg ON penalty_chg.loan_id = ml.id - LEFT JOIN (SELECT sum(mlc2.amount_outstanding_derived) outstanding_amount, mlc2.loan_id - FROM m_loan_charge mlc2 - JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 - JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id - WHERE parent_charge.is_penalty = true - group by mlc2.loan_id - ) penalty_vat_chg ON penalty_vat_chg.loan_id = ml.id + left join ( + SELECT mlrs.loan_id, + Round(mlrs.interest_amount / (duedate-fromdate) * (current_date - fromdate)) as intersetAmount + FROM m_loan_repayment_schedule mlrs + WHERE duedate >= current_date and mlrs.completed_derived != true + AND fromdate <= current_date + ) due_interest on due_interest.loan_id = ml.id + left join ( + select mlrs.loan_id,sum(mlrs.interest_amount) as intersetAmount + from m_loan_repayment_schedule mlrs + where mlrs.completed_derived != true and duedate <= current_date group by loan_id + ) overdue_interest on overdue_interest.loan_id = ml.id + LEFT join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + where mlc.charge_calculation_enum IN (468, 575, 231) + group by mlc.loan_id + ) insurance_chg ON insurance_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.charge_calculation_enum IN (468, 575, 231) + group by mlc2.loan_id + ) vat_chg ON vat_chg.loan_id = ml.id + LEFT join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + where mlc.charge_calculation_enum = 41 + group by mlc.loan_id + ) aval_chg ON aval_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.charge_calculation_enum = 41 + group by mlc2.loan_id + ) aval_vat_chg ON aval_vat_chg.loan_id = ml.id + LEFT join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + where mlc.charge_calculation_enum NOT IN (468, 575, 231, 342, 41) + group by mlc.loan_id + ) other_chg ON other_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.charge_calculation_enum NOT IN (468, 575, 231, 41) + group by mlc2.loan_id + ) other_vat_chg ON other_vat_chg.loan_id = ml.id + LEFT join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and mlrs.duedate <= current_date + where mlc.is_penalty = true group by mlc.loan_id + ) penalty_chg ON penalty_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and mlrs.duedate <= current_date + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.is_penalty = true group by mlc2.loan_id + ) penalty_vat_chg ON penalty_vat_chg.loan_id = ml.id WHERE - ml.loan_status_id = 300 - and (CURRENT_DATE - mlaa.overdue_since_date_derived) > ? + ml.loan_status_id = 300 + and (CURRENT_DATE - mlaa.overdue_since_date_derived) > ? """; } @@ -3718,12 +3833,15 @@ public List retrieveClaimTemplate(String claimType) { final StringBuilder sqlBuilder = new StringBuilder(200); if (claimType.equals("guarantor")) { sqlBuilder.append("select ").append(loanReclaimMapper.loanReclaimSchemaForAval()); + sqlBuilder.append(" and ml.excluded_for_aval_claim is null "); } else if (claimType.equals("insurance")) { sqlBuilder.append("select ").append(loanReclaimMapper.loanReclaimSchemaForInsurance()); + sqlBuilder.append(" and ml.excluded_for_insurance_claim is null "); } else { sqlBuilder.append("select ").append(loanReclaimMapper.loanReclaimSchemaForWriteoff()); + sqlBuilder.append(" and ml.excluded_for_castigado_claim is null "); } - sqlBuilder.append(" and ml.excluded_from_reclaim = false and ml.claim_type is null and ml.loan_schedule_type = 'PROGRESSIVE' "); + sqlBuilder.append(" and ml.claim_type is null and ml.loan_schedule_type = 'PROGRESSIVE' "); Long minimDaysToReclaim = 0L; if (claimType.equals("guarantor") || claimType.equals("insurance")) { @@ -3748,13 +3866,15 @@ public List retrieveExcludedTemplate(String claimType) { final StringBuilder sqlBuilder = new StringBuilder(200); if (claimType.equals("guarantor")) { sqlBuilder.append("select ").append(loanReclaimMapper.loanReclaimSchemaForAval()); + sqlBuilder.append(" and ml.excluded_for_aval_claim = ? "); } else if (claimType.equals("insurance")) { sqlBuilder.append("select ").append(loanReclaimMapper.loanReclaimSchemaForInsurance()); + sqlBuilder.append(" and ml.excluded_for_insurance_claim = ? "); } else { sqlBuilder.append("select ").append(loanReclaimMapper.loanReclaimSchemaForWriteoff()); + sqlBuilder.append(" and ml.excluded_for_castigado_claim = ? "); } - sqlBuilder.append( - " and ml.excluded_from_reclaim = true and ml.excluded_for_claim_type = ? and ml.claim_type is null and ml.loan_schedule_type = 'PROGRESSIVE' "); + sqlBuilder.append(" and ml.claim_type is null and ml.loan_schedule_type = 'PROGRESSIVE' "); Long minimDaysToReclaim = 0L; if (claimType.equals("guarantor") || claimType.equals("insurance")) { 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 3aeefa2a962..4dc4a1b3ac7 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 @@ -4220,8 +4220,13 @@ private BigDecimal processInsuranceChargeCancellation(BigDecimal cumulative, Loa public CommandProcessingResult excludeLoanFromReclaim(final Long loanId, final JsonCommand command) { Loan loan = this.loanAssembler.assembleFrom(loanId); String claimType = command.stringValueOfParameterNamed("claimType"); - loan.setExcludedFromReclaim(true); - loan.setExcludedForClaimType(claimType); + if (claimType.equals("guarantor")) { + loan.setExcludedForAvalClaim(claimType); + } else if (claimType.equals("insurance")) { + loan.setExcludedForInsuranceClaim(claimType); + } else { + loan.setExcludedForCastigadoClaim(claimType); + } this.loanRepositoryWrapper.saveAndFlush(loan); return new CommandProcessingResultBuilder() // .withCommandId(command.commandId()) // diff --git a/fineract-provider/src/main/resources/db/changelog/tenant/changelog-tenant.xml b/fineract-provider/src/main/resources/db/changelog/tenant/changelog-tenant.xml index 88fe3669a19..f1533c0a56a 100644 --- a/fineract-provider/src/main/resources/db/changelog/tenant/changelog-tenant.xml +++ b/fineract-provider/src/main/resources/db/changelog/tenant/changelog-tenant.xml @@ -314,4 +314,5 @@ + diff --git a/fineract-provider/src/main/resources/db/changelog/tenant/parts/20241112011009115_SU_442_add_columns_for_reclaim_exclusion.xml b/fineract-provider/src/main/resources/db/changelog/tenant/parts/20241112011009115_SU_442_add_columns_for_reclaim_exclusion.xml new file mode 100644 index 00000000000..676b8dc5939 --- /dev/null +++ b/fineract-provider/src/main/resources/db/changelog/tenant/parts/20241112011009115_SU_442_add_columns_for_reclaim_exclusion.xml @@ -0,0 +1,589 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + = current_date and mlrs.completed_derived != true + AND fromdate <= current_date + ) due_interest on due_interest.loan_id = ml.id + left join ( + select mlrs.loan_id,sum(mlrs.interest_amount) as intersetAmount + from m_loan_repayment_schedule mlrs + where mlrs.completed_derived != true and duedate <= current_date group by loan_id + ) overdue_interest on overdue_interest.loan_id = ml.id + join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + where mlc.charge_calculation_enum IN (468, 575, 231) + group by mlc.loan_id + ) insurance_chg ON insurance_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.charge_calculation_enum IN (468, 575, 231) + group by mlc2.loan_id + ) vat_chg ON vat_chg.loan_id = ml.id + LEFT join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + where mlc.charge_calculation_enum = 41 + group by mlc.loan_id + ) aval_chg ON aval_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.charge_calculation_enum = 41 + group by mlc2.loan_id + ) aval_vat_chg ON aval_vat_chg.loan_id = ml.id + LEFT join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + where mlc.charge_calculation_enum NOT IN (468, 575, 231, 342, 41) + group by mlc.loan_id + ) other_chg ON other_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.charge_calculation_enum NOT IN (468, 575, 231, 41) + group by mlc2.loan_id + ) other_vat_chg ON other_vat_chg.loan_id = ml.id + LEFT join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and mlrs.duedate <= current_date + where mlc.is_penalty = true group by mlc.loan_id + ) penalty_chg ON penalty_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and mlrs.duedate <= current_date + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.is_penalty = true group by mlc2.loan_id + ) penalty_vat_chg ON penalty_vat_chg.loan_id = ml.id + WHERE + ml.loan_status_id = 300 + and (CURRENT_DATE - mlaa.overdue_since_date_derived) > (select value from c_configuration where name = 'Dias de mora minimos para reclamar') + and ml.excluded_for_insurance_claim is null and ml.claim_type is null and ml.loan_schedule_type = 'PROGRESSIVE' + ]]> + + report_name='Reporte de reclamación de seguro' + + + + + + = current_date and mlrs.completed_derived != true + AND fromdate <= current_date + ) due_interest on due_interest.loan_id = ml.id + left join ( + select mlrs.loan_id,sum(mlrs.interest_amount) as intersetAmount + from m_loan_repayment_schedule mlrs + where mlrs.completed_derived != true and duedate <= current_date group by loan_id + ) overdue_interest on overdue_interest.loan_id = ml.id + left join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + where mlc.charge_calculation_enum IN (468, 575, 231) + group by mlc.loan_id + ) insurance_chg ON insurance_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.charge_calculation_enum IN (468, 575, 231) + group by mlc2.loan_id + ) vat_chg ON vat_chg.loan_id = ml.id + join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + where mlc.charge_calculation_enum = 41 + group by mlc.loan_id + ) aval_chg ON aval_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.charge_calculation_enum = 41 + group by mlc2.loan_id + ) aval_vat_chg ON aval_vat_chg.loan_id = ml.id + LEFT join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + where mlc.charge_calculation_enum NOT IN (468, 575, 231, 342, 41) + group by mlc.loan_id + ) other_chg ON other_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.charge_calculation_enum NOT IN (468, 575, 231, 41) + group by mlc2.loan_id + ) other_vat_chg ON other_vat_chg.loan_id = ml.id + LEFT join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and mlrs.duedate <= current_date + where mlc.is_penalty = true group by mlc.loan_id + ) penalty_chg ON penalty_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and mlrs.duedate <= current_date + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.is_penalty = true group by mlc2.loan_id + ) penalty_vat_chg ON penalty_vat_chg.loan_id = ml.id + WHERE + ml.loan_status_id = 300 + and (CURRENT_DATE - mlaa.overdue_since_date_derived) > (select value from c_configuration where name = 'Dias de mora minimos para reclamar') + and ml.excluded_for_insurance_claim is null and ml.claim_type is null and ml.loan_schedule_type = 'PROGRESSIVE' + ]]> + + report_name='Reporte de reclamación de Aval' + + + + + + = current_date and mlrs.completed_derived != true + AND fromdate <= current_date + ) due_interest on due_interest.loan_id = ml.id + left join ( + select mlrs.loan_id,sum(mlrs.interest_amount) as intersetAmount + from m_loan_repayment_schedule mlrs + where mlrs.completed_derived != true and duedate <= current_date group by loan_id + ) overdue_interest on overdue_interest.loan_id = ml.id + left join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + where mlc.charge_calculation_enum IN (468, 575, 231) + group by mlc.loan_id + ) insurance_chg ON insurance_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.charge_calculation_enum IN (468, 575, 231) + group by mlc2.loan_id + ) vat_chg ON vat_chg.loan_id = ml.id + left join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + where mlc.charge_calculation_enum = 41 + group by mlc.loan_id + ) aval_chg ON aval_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.charge_calculation_enum = 41 + group by mlc2.loan_id + ) aval_vat_chg ON aval_vat_chg.loan_id = ml.id + LEFT join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + where mlc.charge_calculation_enum NOT IN (468, 575, 231, 342, 41) + group by mlc.loan_id + ) other_chg ON other_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.charge_calculation_enum NOT IN (468, 575, 231, 41) + group by mlc2.loan_id + ) other_vat_chg ON other_vat_chg.loan_id = ml.id + LEFT join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and mlrs.duedate <= current_date + where mlc.is_penalty = true group by mlc.loan_id + ) penalty_chg ON penalty_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and mlrs.duedate <= current_date + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.is_penalty = true group by mlc2.loan_id + ) penalty_vat_chg ON penalty_vat_chg.loan_id = ml.id + WHERE + ml.loan_status_id = 300 + and (CURRENT_DATE - mlaa.overdue_since_date_derived) > (select value from c_configuration where name = 'Dias de mora minimos para reclamar') + and ml.excluded_for_insurance_claim is null and ml.claim_type is null and ml.loan_schedule_type = 'PROGRESSIVE' + ]]> + + report_name='Reporte de Castigo de cartera' + + + + + + = current_date and mlrs.completed_derived != true + AND fromdate <= current_date + ) due_interest on due_interest.loan_id = ml.id + left join ( + select mlrs.loan_id,sum(mlrs.interest_amount) as intersetAmount + from m_loan_repayment_schedule mlrs + where mlrs.completed_derived != true and duedate <= current_date group by loan_id + ) overdue_interest on overdue_interest.loan_id = ml.id + left join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + where mlc.charge_calculation_enum IN (468, 575, 231) + group by mlc.loan_id + ) insurance_chg ON insurance_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.charge_calculation_enum IN (468, 575, 231) + group by mlc2.loan_id + ) vat_chg ON vat_chg.loan_id = ml.id + left join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + where mlc.charge_calculation_enum = 41 + group by mlc.loan_id + ) aval_chg ON aval_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.charge_calculation_enum = 41 + group by mlc2.loan_id + ) aval_vat_chg ON aval_vat_chg.loan_id = ml.id + LEFT join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + where mlc.charge_calculation_enum NOT IN (468, 575, 231, 342, 41) + group by mlc.loan_id + ) other_chg ON other_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date)) + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.charge_calculation_enum NOT IN (468, 575, 231, 41) + group by mlc2.loan_id + ) other_vat_chg ON other_vat_chg.loan_id = ml.id + LEFT join ( + select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from m_loan_charge mlc + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id) + and mlrs.duedate <= current_date + where mlc.is_penalty = true group by mlc.loan_id + ) penalty_chg ON penalty_chg.loan_id = ml.id + LEFT JOIN ( + SELECT sum(mlic.amount) outstanding_amount, mlc2.loan_id FROM m_loan_charge mlc2 + inner join m_loan_installment_charge mlic ON mlic.loan_charge_id = mlc2.id + inner join m_loan_repayment_schedule mlrs on mlic.loan_schedule_id = mlrs.id + and mlrs.completed_derived != true + and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc2.loan_id) + and mlrs.duedate <= current_date + JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342 + JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id + WHERE parent_charge.is_penalty = true group by mlc2.loan_id + ) penalty_vat_chg ON penalty_vat_chg.loan_id = ml.id + WHERE + ml.loan_status_id = 300 + and (CURRENT_DATE - mlaa.overdue_since_date_derived) > (select value from c_configuration where name = 'Dias de mora minimos para reclamar') + and ml.id in ('${loanIds}') + ]]> + + report_name='Reporte de Castigo de cartera' + + +