fix: Interest calculations in Loan Interest Report
diff --git a/erpnext/loan_management/report/applicant_wise_loan_security_exposure/applicant_wise_loan_security_exposure.py b/erpnext/loan_management/report/applicant_wise_loan_security_exposure/applicant_wise_loan_security_exposure.py
index 0aff2e3..6d7c3b7 100644
--- a/erpnext/loan_management/report/applicant_wise_loan_security_exposure/applicant_wise_loan_security_exposure.py
+++ b/erpnext/loan_management/report/applicant_wise_loan_security_exposure/applicant_wise_loan_security_exposure.py
@@ -59,7 +59,6 @@
return data
def get_loan_security_details(filters):
- update_time = get_datetime()
security_detail_map = {}
loan_security_price_map = frappe._dict(frappe.db.sql("""
diff --git a/erpnext/loan_management/report/loan_interest_report/loan_interest_report.py b/erpnext/loan_management/report/loan_interest_report/loan_interest_report.py
index 730176f..aa0325e 100644
--- a/erpnext/loan_management/report/loan_interest_report/loan_interest_report.py
+++ b/erpnext/loan_management/report/loan_interest_report/loan_interest_report.py
@@ -5,7 +5,7 @@
import frappe
import erpnext
from frappe import _
-from frappe.utils import flt, get_first_day, getdate
+from frappe.utils import flt, getdate, add_days
def execute(filters=None):
@@ -22,13 +22,13 @@
{"label": _("Loan Type"), "fieldname": "loan_type", "fieldtype": "Link", "options": "Loan Type", "width": 100},
{"label": _("Sanctioned Amount"), "fieldname": "sanctioned_amount", "fieldtype": "Currency", "options": "currency", "width": 120},
{"label": _("Disbursed Amount"), "fieldname": "disbursed_amount", "fieldtype": "Currency", "options": "currency", "width": 120},
- {"label": _("Interest For The Month"), "fieldname": "month_interest", "fieldtype": "Currency", "options": "currency", "width": 120},
- {"label": _("Penalty For The Month"), "fieldname": "penalty", "fieldtype": "Currency", "options": "currency", "width": 120},
+ {"label": _("Penalty Amount"), "fieldname": "penalty", "fieldtype": "Currency", "options": "currency", "width": 120},
{"label": _("Accrued Interest"), "fieldname": "accrued_interest", "fieldtype": "Currency", "options": "currency", "width": 120},
{"label": _("Total Repayment"), "fieldname": "total_repayment", "fieldtype": "Currency", "options": "currency", "width": 120},
{"label": _("Principal Outstanding"), "fieldname": "principal_outstanding", "fieldtype": "Currency", "options": "currency", "width": 120},
{"label": _("Interest Outstanding"), "fieldname": "interest_outstanding", "fieldtype": "Currency", "options": "currency", "width": 120},
{"label": _("Total Outstanding"), "fieldname": "total_outstanding", "fieldtype": "Currency", "options": "currency", "width": 120},
+ {"label": _("Undue Booked Interest"), "fieldname": "undue_interest", "fieldtype": "Currency", "options": "currency", "width": 120},
{"label": _("Interest %"), "fieldname": "rate_of_interest", "fieldtype": "Percent", "width": 100},
{"label": _("Penalty Interest %"), "fieldname": "penalty_interest", "fieldtype": "Percent", "width": 100},
{"label": _("Currency"), "fieldname": "currency", "fieldtype": "Currency", "options": "Currency", "hidden": 1, "width": 100},
@@ -62,11 +62,11 @@
"principal_outstanding": flt(loan.total_payment) - flt(loan.total_principal_paid) \
- flt(loan.total_interest_payable) - flt(loan.written_off_amount),
"total_repayment": flt(payments.get(loan.loan)),
- "month_interest": flt(accrual_map.get(loan.loan, {}).get("month_interest")),
"accrued_interest": flt(accrual_map.get(loan.loan, {}).get("accrued_interest")),
"interest_outstanding": flt(accrual_map.get(loan.loan, {}).get("interest_outstanding")),
"penalty": flt(accrual_map.get(loan.loan, {}).get("penalty")),
"penalty_interest": penal_interest_rate_map.get(loan.loan_type),
+ "undue_interest": flt(accrual_map.get(loan.loan, {}).get("undue_interest")),
"currency": currency
})
@@ -85,26 +85,38 @@
def get_interest_accruals(loans):
accrual_map = {}
- current_month_start = get_first_day(getdate())
interest_accruals = frappe.get_all("Loan Interest Accrual",
fields=["loan", "interest_amount", "posting_date", "penalty_amount",
- "paid_interest_amount"], filters={"loan": ("in", loans)}, order_by="posting_date")
+ "paid_interest_amount", "accrual_type"], filters={"loan": ("in", loans)}, order_by="posting_date desc")
for entry in interest_accruals:
accrual_map.setdefault(entry.loan, {
- "month_interest": 0.0,
"accrued_interest": 0.0,
- "interest_outstanding": 0.0
+ "undue_interest": 0.0,
+ "interest_outstanding": 0.0,
+ "last_accrual_date": '',
+ "due_date": ''
})
- if getdate(entry.posting_date) < getdate(current_month_start):
- accrual_map[entry.loan]["accrued_interest"] += entry.interest_amount
- else:
- accrual_map[entry.loan]["month_interest"] += entry.interest_amount
+ if entry.accrual_type == 'Regular':
+ if not accrual_map[entry.loan]['due_date']:
+ accrual_map[entry.loan]['due_date'] = add_days(entry.posting_date, 1)
+ if not accrual_map[entry.loan]['last_accrual_date']:
+ accrual_map[entry.loan]['last_accrual_date'] = entry.posting_date
- accrual_map[entry.loan]["interest_outstanding"] += entry.interest_amount - entry.paid_interest_amount
- accrual_map[entry.loan]["penalty"] = entry.penalty_amount
+ due_date = accrual_map[entry.loan]['due_date']
+ last_accrual_date = accrual_map[entry.loan]['last_accrual_date']
+
+ if due_date and getdate(entry.posting_date) < getdate(due_date):
+ accrual_map[entry.loan]["interest_outstanding"] += entry.interest_amount - entry.paid_interest_amount
+ else:
+ accrual_map[entry.loan]['undue_interest'] += entry.interest_amount - entry.paid_interest_amount
+
+ accrual_map[entry.loan]["accrued_interest"] += entry.interest_amount
+
+ if last_accrual_date and getdate(entry.posting_date) == last_accrual_date:
+ accrual_map[entry.loan]["penalty"] = entry.penalty_amount
return accrual_map
diff --git a/erpnext/loan_management/report/loan_security_exposure/loan_security_exposure.py b/erpnext/loan_management/report/loan_security_exposure/loan_security_exposure.py
index 2f4d980..3ef10c0 100644
--- a/erpnext/loan_management/report/loan_security_exposure/loan_security_exposure.py
+++ b/erpnext/loan_management/report/loan_security_exposure/loan_security_exposure.py
@@ -2,10 +2,9 @@
# For license information, please see license.txt
from __future__ import unicode_literals
-import frappe
import erpnext
from frappe import _
-from frappe.utils import get_datetime, flt
+from frappe.utils import flt
from six import iteritems
from erpnext.loan_management.report.applicant_wise_loan_security_exposure.applicant_wise_loan_security_exposure \
import get_loan_security_details, get_applicant_wise_total_loan_security_qty
@@ -56,7 +55,6 @@
return data
-
def get_company_wise_loan_security_details(filters, loan_security_details):
pledge_values, total_value_map, applicant_type_map = get_applicant_wise_total_loan_security_qty(filters,
loan_security_details)