From 8116b9b62f16461e615a08d0da98458a403b85e8 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Wed, 20 Oct 2021 19:55:00 +0530 Subject: [PATCH 1/7] fix: Updates in term loan processing --- .../loan_management/doctype/loan/loan.json | 7 +- erpnext/loan_management/doctype/loan/loan.py | 4 +- .../loan_management/doctype/loan/test_loan.py | 27 +++- .../loan_application/loan_application.py | 2 +- .../loan_interest_accrual.py | 33 +++++ .../doctype/loan_repayment/loan_repayment.py | 135 +++++++++++++++--- 6 files changed, 181 insertions(+), 27 deletions(-) diff --git a/erpnext/loan_management/doctype/loan/loan.json b/erpnext/loan_management/doctype/loan/loan.json index c9f23ca4df..10676809de 100644 --- a/erpnext/loan_management/doctype/loan/loan.json +++ b/erpnext/loan_management/doctype/loan/loan.json @@ -240,12 +240,14 @@ "label": "Repayment Schedule" }, { + "allow_on_submit": 1, "depends_on": "eval:doc.is_term_loan == 1", "fieldname": "repayment_schedule", "fieldtype": "Table", "label": "Repayment Schedule", "no_copy": 1, - "options": "Repayment Schedule" + "options": "Repayment Schedule", + "read_only": 1 }, { "fieldname": "section_break_17", @@ -360,10 +362,11 @@ "index_web_pages_for_search": 1, "is_submittable": 1, "links": [], - "modified": "2021-04-19 18:10:32.360818", + "modified": "2021-10-20 08:28:16.796105", "modified_by": "Administrator", "module": "Loan Management", "name": "Loan", + "naming_rule": "Expression (old style)", "owner": "Administrator", "permissions": [ { diff --git a/erpnext/loan_management/doctype/loan/loan.py b/erpnext/loan_management/doctype/loan/loan.py index 7dbd42297e..73134eedd2 100644 --- a/erpnext/loan_management/doctype/loan/loan.py +++ b/erpnext/loan_management/doctype/loan/loan.py @@ -65,7 +65,7 @@ class Loan(AccountsController): self.rate_of_interest = frappe.db.get_value("Loan Type", self.loan_type, "rate_of_interest") if self.repayment_method == "Repay Over Number of Periods": - self.monthly_repayment_amount = get_monthly_repayment_amount(self.repayment_method, self.loan_amount, self.rate_of_interest, self.repayment_periods) + self.monthly_repayment_amount = get_monthly_repayment_amount(self.loan_amount, self.rate_of_interest, self.repayment_periods) def check_sanctioned_amount_limit(self): sanctioned_amount_limit = get_sanctioned_amount_limit(self.applicant_type, self.applicant, self.company) @@ -207,7 +207,7 @@ def validate_repayment_method(repayment_method, loan_amount, monthly_repayment_a if monthly_repayment_amount > loan_amount: frappe.throw(_("Monthly Repayment Amount cannot be greater than Loan Amount")) -def get_monthly_repayment_amount(repayment_method, loan_amount, rate_of_interest, repayment_periods): +def get_monthly_repayment_amount(loan_amount, rate_of_interest, repayment_periods): if rate_of_interest: monthly_interest_rate = flt(rate_of_interest) / (12 *100) monthly_repayment_amount = math.ceil((loan_amount * monthly_interest_rate * diff --git a/erpnext/loan_management/doctype/loan/test_loan.py b/erpnext/loan_management/doctype/loan/test_loan.py index ec0aebbb8a..c10cf36d9d 100644 --- a/erpnext/loan_management/doctype/loan/test_loan.py +++ b/erpnext/loan_management/doctype/loan/test_loan.py @@ -297,6 +297,27 @@ class TestLoan(unittest.TestCase): self.assertEqual(amounts[0], 11250.00) self.assertEqual(amounts[1], 78303.00) + def test_repayment_schedule_update(self): + loan = create_loan(self.applicant2, "Personal Loan", 200000, "Repay Over Number of Periods", 4, + applicant_type='Customer', repayment_start_date='2021-04-30', posting_date='2021-04-01') + + loan.submit() + + make_loan_disbursement_entry(loan.name, loan.loan_amount, disbursement_date='2021-04-01') + + process_loan_interest_accrual_for_term_loans(posting_date='2021-05-01') + process_loan_interest_accrual_for_term_loans(posting_date='2021-06-01') + + repayment_entry = create_repayment_entry(loan.name, self.applicant2, '2021-06-05', 120000) + repayment_entry.submit() + + loan.load_from_db() + + self.assertEqual(flt(loan.get('repayment_schedule')[3].principal_amount, 2), 32151.83) + self.assertEqual(flt(loan.get('repayment_schedule')[3].interest_amount, 2), 225.06) + self.assertEqual(flt(loan.get('repayment_schedule')[3].total_payment, 2), 32376.89) + self.assertEqual(flt(loan.get('repayment_schedule')[3].balance_loan_amount, 2), 0) + def test_security_shortfall(self): pledges = [{ "loan_security": "Test Security 2", @@ -940,18 +961,18 @@ def create_loan_application(company, applicant, loan_type, proposed_pledges, rep def create_loan(applicant, loan_type, loan_amount, repayment_method, repayment_periods, - repayment_start_date=None, posting_date=None): + applicant_type=None, repayment_start_date=None, posting_date=None): loan = frappe.get_doc({ "doctype": "Loan", - "applicant_type": "Employee", + "applicant_type": applicant_type or "Employee", "company": "_Test Company", "applicant": applicant, "loan_type": loan_type, "loan_amount": loan_amount, "repayment_method": repayment_method, "repayment_periods": repayment_periods, - "repayment_start_date": nowdate(), + "repayment_start_date": repayment_start_date or nowdate(), "is_term_loan": 1, "posting_date": posting_date or nowdate() }) diff --git a/erpnext/loan_management/doctype/loan_application/loan_application.py b/erpnext/loan_management/doctype/loan_application/loan_application.py index e492920abb..e24692e34a 100644 --- a/erpnext/loan_management/doctype/loan_application/loan_application.py +++ b/erpnext/loan_management/doctype/loan_application/loan_application.py @@ -83,7 +83,7 @@ class LoanApplication(Document): if self.is_term_loan: if self.repayment_method == "Repay Over Number of Periods": - self.repayment_amount = get_monthly_repayment_amount(self.repayment_method, self.loan_amount, self.rate_of_interest, self.repayment_periods) + self.repayment_amount = get_monthly_repayment_amount(self.loan_amount, self.rate_of_interest, self.repayment_periods) if self.repayment_method == "Repay Fixed Amount per Period": monthly_interest_rate = flt(self.rate_of_interest) / (12 *100) diff --git a/erpnext/loan_management/doctype/loan_interest_accrual/loan_interest_accrual.py b/erpnext/loan_management/doctype/loan_interest_accrual/loan_interest_accrual.py index 93513a83e7..4cd4c75a4b 100644 --- a/erpnext/loan_management/doctype/loan_interest_accrual/loan_interest_accrual.py +++ b/erpnext/loan_management/doctype/loan_interest_accrual/loan_interest_accrual.py @@ -76,6 +76,39 @@ class LoanInterestAccrual(AccountsController): }) ) + if self.payable_principal_amount: + gle_map.append( + self.get_gl_dict({ + "account": self.loan_account, + "party_type": self.applicant_type, + "party": self.applicant, + "against": self.interest_income_account, + "debit": self.payable_principal_amount, + "debit_in_account_currency": self.interest_amount, + "against_voucher_type": "Loan", + "against_voucher": self.loan, + "remarks": _("Interest accrued from {0} to {1} against loan: {2}").format( + self.last_accrual_date, self.posting_date, self.loan), + "cost_center": erpnext.get_default_cost_center(self.company), + "posting_date": self.posting_date + }) + ) + + gle_map.append( + self.get_gl_dict({ + "account": self.interest_income_account, + "against": self.loan_account, + "credit": self.payable_principal_amount, + "credit_in_account_currency": self.interest_amount, + "against_voucher_type": "Loan", + "against_voucher": self.loan, + "remarks": ("Interest accrued from {0} to {1} against loan: {2}").format( + self.last_accrual_date, self.posting_date, self.loan), + "cost_center": erpnext.get_default_cost_center(self.company), + "posting_date": self.posting_date + }) + ) + if gle_map: make_gl_entries(gle_map, cancel=cancel, adv_adj=adv_adj) diff --git a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py index 13b7357327..9f3fe76198 100644 --- a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py +++ b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py @@ -6,7 +6,7 @@ from __future__ import unicode_literals import frappe from frappe import _ -from frappe.utils import add_days, cint, date_diff, flt, get_datetime, getdate +from frappe.utils import add_days, add_months, cint, date_diff, flt, get_datetime, getdate from six import iteritems import erpnext @@ -38,10 +38,12 @@ class LoanRepayment(AccountsController): def on_submit(self): self.update_paid_amount() + self.update_repayment_schedule() self.make_gl_entries() def on_cancel(self): self.mark_as_unpaid() + self.update_repayment_schedule() self.ignore_linked_doctypes = ['GL Entry'] self.make_gl_entries(cancel=1) @@ -164,6 +166,10 @@ class LoanRepayment(AccountsController): if loan.status == "Loan Closure Requested": frappe.db.set_value("Loan", self.against_loan, "status", "Disbursed") + def update_repayment_schedule(self): + if self.is_term_loan and self.principal_amount_paid > self.payable_principal_amount: + regenerate_repayment_schedule(self.against_loan) + def allocate_amounts(self, repayment_details): self.set('repayment_details', []) self.principal_amount_paid = 0 @@ -185,50 +191,93 @@ class LoanRepayment(AccountsController): interest_paid -= self.total_penalty_paid - total_interest_paid = 0 - # interest_paid = self.amount_paid - self.principal_amount_paid - self.penalty_amount + if self.is_term_loan: + interest_paid, updated_entries = self.allocate_interest_amount(interest_paid, repayment_details) + self.allocate_principal_amount_for_term_loans(interest_paid, repayment_details, updated_entries) + else: + interest_paid, updated_entries = self.allocate_interest_amount(interest_paid, repayment_details) + self.allocate_excess_payment_for_demand_loans(interest_paid, repayment_details) + + def allocate_interest_amount(self, interest_paid, repayment_details): + updated_entries = {} + self.total_interest_paid = 0 + idx = 1 if interest_paid > 0: for lia, amounts in iteritems(repayment_details.get('pending_accrual_entries', [])): - if amounts['interest_amount'] + amounts['payable_principal_amount'] <= interest_paid: + interest_amount = 0 + if amounts['interest_amount'] <= interest_paid: interest_amount = amounts['interest_amount'] - paid_principal = amounts['payable_principal_amount'] - self.principal_amount_paid += paid_principal - interest_paid -= (interest_amount + paid_principal) + self.total_interest_paid += interest_amount + interest_paid -= interest_amount elif interest_paid: if interest_paid >= amounts['interest_amount']: interest_amount = amounts['interest_amount'] - paid_principal = interest_paid - interest_amount - self.principal_amount_paid += paid_principal + self.total_interest_paid += interest_amount interest_paid = 0 else: interest_amount = interest_paid + self.total_interest_paid += interest_amount interest_paid = 0 - paid_principal=0 - total_interest_paid += interest_amount - self.append('repayment_details', { - 'loan_interest_accrual': lia, - 'paid_interest_amount': interest_amount, - 'paid_principal_amount': paid_principal - }) + if interest_amount: + self.append('repayment_details', { + 'loan_interest_accrual': lia, + 'paid_interest_amount': interest_amount, + 'paid_principal_amount': 0 + }) + updated_entries[lia] = idx + idx += 1 + + return interest_paid, updated_entries + + def allocate_principal_amount_for_term_loans(self, interest_paid, repayment_details, updated_entries): + if interest_paid > 0: + for lia, amounts in iteritems(repayment_details.get('pending_accrual_entries', [])): + paid_principal = 0 + if amounts['payable_principal_amount'] <= interest_paid: + paid_principal = amounts['payable_principal_amount'] + self.principal_amount_paid += paid_principal + interest_paid -= paid_principal + elif interest_paid: + if interest_paid >= amounts['payable_principal_amount']: + paid_principal = amounts['payable_principal_amount'] + self.principal_amount_paid += paid_principal + interest_paid = 0 + else: + paid_principal = interest_paid + self.principal_amount_paid += paid_principal + interest_paid = 0 + + if updated_entries.get(lia): + idx = updated_entries.get(lia) + self.get('repayment_details')[idx-1].paid_principal_amount += paid_principal + else: + self.append('repayment_details', { + 'loan_interest_accrual': lia, + 'paid_interest_amount': 0, + 'paid_principal_amount': paid_principal + }) + + if interest_paid > 0: + self.principal_amount_paid += interest_paid + def allocate_excess_payment_for_demand_loans(self, interest_paid, repayment_details): if repayment_details['unaccrued_interest'] and interest_paid > 0: # no of days for which to accrue interest # Interest can only be accrued for an entire day and not partial if interest_paid > repayment_details['unaccrued_interest']: interest_paid -= repayment_details['unaccrued_interest'] - total_interest_paid += repayment_details['unaccrued_interest'] + self.total_interest_paid += repayment_details['unaccrued_interest'] else: # get no of days for which interest can be paid per_day_interest = get_per_day_interest(self.pending_principal_amount, self.rate_of_interest, self.posting_date) no_of_days = cint(interest_paid/per_day_interest) - total_interest_paid += no_of_days * per_day_interest + self.total_interest_paid += no_of_days * per_day_interest interest_paid -= no_of_days * per_day_interest - self.total_interest_paid = total_interest_paid if interest_paid > 0: self.principal_amount_paid += interest_paid @@ -364,6 +413,54 @@ def get_penalty_details(against_loan): else: return None, 0 +def regenerate_repayment_schedule(loan): + from erpnext.loan_management.doctype.loan.loan import get_monthly_repayment_amount + + loan_doc = frappe.get_doc('Loan', loan) + next_accrual_date = None + + for term in reversed(loan_doc.get('repayment_schedule')): + if not term.is_accrued: + next_accrual_date = term.payment_date + + if not term.is_accrued: + loan_doc.remove(term) + + loan_doc.save() + + if loan_doc.status in ('Disbursed', 'Loan Closure Requested', 'Closed'): + balance_amount = loan_doc.total_payment - loan_doc.total_principal_paid \ + - loan_doc.total_interest_payable - loan_doc.written_off_amount + else: + balance_amount = loan_doc.disbursed_amount - loan_doc.total_principal_paid \ + - loan_doc.total_interest_payable - loan_doc.written_off_amount + + monthly_repayment_amount = get_monthly_repayment_amount(loan_doc.loan_amount, + loan_doc.rate_of_interest, loan_doc.repayment_periods) + + payment_date = next_accrual_date + + while(balance_amount > 0): + interest_amount = flt(balance_amount * flt(loan_doc.rate_of_interest) / (12*100)) + principal_amount = monthly_repayment_amount - interest_amount + balance_amount = flt(balance_amount + interest_amount - monthly_repayment_amount) + if balance_amount < 0: + principal_amount += balance_amount + balance_amount = 0.0 + + total_payment = principal_amount + interest_amount + loan_doc.append("repayment_schedule", { + "payment_date": payment_date, + "principal_amount": principal_amount, + "interest_amount": interest_amount, + "total_payment": total_payment, + "balance_loan_amount": balance_amount + }) + next_payment_date = add_months(payment_date, 1) + payment_date = next_payment_date + + loan_doc.save() + # This function returns the amounts that are payable at the time of loan repayment based on posting date # So it pulls all the unpaid Loan Interest Accrual Entries and calculates the penalty if applicable From 1a5f0da6cafa7001373a5463f04c09fcbd0dd29e Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Fri, 22 Oct 2021 10:46:56 +0530 Subject: [PATCH 2/7] fix: Loan repayment schedule date --- erpnext/loan_management/doctype/loan/loan.py | 10 ++++++++-- .../doctype/loan_repayment/loan_repayment.py | 7 +++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/erpnext/loan_management/doctype/loan/loan.py b/erpnext/loan_management/doctype/loan/loan.py index 73134eedd2..878fd183d1 100644 --- a/erpnext/loan_management/doctype/loan/loan.py +++ b/erpnext/loan_management/doctype/loan/loan.py @@ -9,7 +9,7 @@ import math import frappe from frappe import _ -from frappe.utils import add_months, flt, getdate, now_datetime, nowdate +from frappe.utils import add_months, flt, get_last_day, getdate, now_datetime, nowdate from six import string_types import erpnext @@ -102,7 +102,7 @@ class Loan(AccountsController): "total_payment": total_payment, "balance_loan_amount": balance_amount }) - next_payment_date = add_months(payment_date, 1) + next_payment_date = add_single_month(payment_date) payment_date = next_payment_date def set_repayment_period(self): @@ -391,3 +391,9 @@ def get_shortfall_applicants(): "value": len(applicants), "fieldtype": "Int" } + +def add_single_month(date): + if getdate(date) == get_last_day(date): + return get_last_day(add_months(date, 1)) + else: + return add_months(date, 1) \ No newline at end of file diff --git a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py index 9f3fe76198..53ff43a507 100644 --- a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py +++ b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py @@ -414,7 +414,10 @@ def get_penalty_details(against_loan): return None, 0 def regenerate_repayment_schedule(loan): - from erpnext.loan_management.doctype.loan.loan import get_monthly_repayment_amount + from erpnext.loan_management.doctype.loan.loan import ( + add_single_month, + get_monthly_repayment_amount, + ) loan_doc = frappe.get_doc('Loan', loan) next_accrual_date = None @@ -456,7 +459,7 @@ def regenerate_repayment_schedule(loan): "total_payment": total_payment, "balance_loan_amount": balance_amount }) - next_payment_date = add_months(payment_date, 1) + next_payment_date = add_single_month(payment_date) payment_date = next_payment_date loan_doc.save() From dcae9ba86e20daa8a5eac2990607c434b812af79 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Sat, 23 Oct 2021 18:06:34 +0530 Subject: [PATCH 3/7] fix: Unsecured loan status update --- .../loan_disbursement/loan_disbursement.py | 13 ++-- .../loan_interest_accrual.py | 17 +++-- .../doctype/loan_repayment/loan_repayment.py | 68 ++++++++++++------- .../loan_security_unpledge.py | 12 ++-- 4 files changed, 64 insertions(+), 46 deletions(-) diff --git a/erpnext/loan_management/doctype/loan_disbursement/loan_disbursement.py b/erpnext/loan_management/doctype/loan_disbursement/loan_disbursement.py index 6d9d4f490d..d072422010 100644 --- a/erpnext/loan_management/doctype/loan_disbursement/loan_disbursement.py +++ b/erpnext/loan_management/doctype/loan_disbursement/loan_disbursement.py @@ -178,20 +178,19 @@ def get_total_pledged_security_value(loan): @frappe.whitelist() def get_disbursal_amount(loan, on_current_security_price=0): + from erpnext.loan_management.doctype.loan_repayment.loan_repayment import ( + get_pending_principal_amount, + ) + loan_details = frappe.get_value("Loan", loan, ["loan_amount", "disbursed_amount", "total_payment", "total_principal_paid", "total_interest_payable", "status", "is_term_loan", "is_secured_loan", - "maximum_loan_amount"], as_dict=1) + "maximum_loan_amount", "written_off_amount"], as_dict=1) if loan_details.is_secured_loan and frappe.get_all('Loan Security Shortfall', filters={'loan': loan, 'status': 'Pending'}): return 0 - if loan_details.status == 'Disbursed': - pending_principal_amount = flt(loan_details.total_payment) - flt(loan_details.total_interest_payable) \ - - flt(loan_details.total_principal_paid) - else: - pending_principal_amount = flt(loan_details.disbursed_amount) - flt(loan_details.total_interest_payable) \ - - flt(loan_details.total_principal_paid) + pending_principal_amount = get_pending_principal_amount(loan_details) security_value = 0.0 if loan_details.is_secured_loan and on_current_security_price: diff --git a/erpnext/loan_management/doctype/loan_interest_accrual/loan_interest_accrual.py b/erpnext/loan_management/doctype/loan_interest_accrual/loan_interest_accrual.py index 4cd4c75a4b..3dd1328124 100644 --- a/erpnext/loan_management/doctype/loan_interest_accrual/loan_interest_accrual.py +++ b/erpnext/loan_management/doctype/loan_interest_accrual/loan_interest_accrual.py @@ -117,7 +117,10 @@ class LoanInterestAccrual(AccountsController): # rate of interest is 13.5 then first loan interest accural will be on '01-10-2019' # which means interest will be accrued for 30 days which should be equal to 11095.89 def calculate_accrual_amount_for_demand_loans(loan, posting_date, process_loan_interest, accrual_type): - from erpnext.loan_management.doctype.loan_repayment.loan_repayment import calculate_amounts + from erpnext.loan_management.doctype.loan_repayment.loan_repayment import ( + calculate_amounts, + get_pending_principal_amount, + ) no_of_days = get_no_of_days_for_interest_accural(loan, posting_date) precision = cint(frappe.db.get_default("currency_precision")) or 2 @@ -125,12 +128,7 @@ def calculate_accrual_amount_for_demand_loans(loan, posting_date, process_loan_i if no_of_days <= 0: return - if loan.status == 'Disbursed': - pending_principal_amount = flt(loan.total_payment) - flt(loan.total_interest_payable) \ - - flt(loan.total_principal_paid) - flt(loan.written_off_amount) - else: - pending_principal_amount = flt(loan.disbursed_amount) - flt(loan.total_interest_payable) \ - - flt(loan.total_principal_paid) - flt(loan.written_off_amount) + pending_principal_amount = get_pending_principal_amount(loan) interest_per_day = get_per_day_interest(pending_principal_amount, loan.rate_of_interest, posting_date) payable_interest = interest_per_day * no_of_days @@ -168,7 +166,7 @@ def make_accrual_interest_entry_for_demand_loans(posting_date, process_loan_inte if not open_loans: open_loans = frappe.get_all("Loan", - fields=["name", "total_payment", "total_amount_paid", "loan_account", "interest_income_account", + fields=["name", "total_payment", "total_amount_paid", "loan_account", "interest_income_account", "loan_amount", "is_term_loan", "status", "disbursement_date", "disbursed_amount", "applicant_type", "applicant", "rate_of_interest", "total_interest_payable", "written_off_amount", "total_principal_paid", "repayment_start_date"], filters=query_filters) @@ -225,7 +223,8 @@ def get_term_loans(date, term_loan=None, loan_type=None): AND l.is_term_loan =1 AND rs.payment_date <= %s AND rs.is_accrued=0 {0} - AND l.status = 'Disbursed'""".format(condition), (getdate(date)), as_dict=1) + AND l.status = 'Disbursed' + ORDER BY rs.payment_date""".format(condition), (getdate(date)), as_dict=1) return term_loans diff --git a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py index 53ff43a507..9f13ee68ba 100644 --- a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py +++ b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py @@ -126,7 +126,18 @@ class LoanRepayment(AccountsController): }) def update_paid_amount(self): - loan = frappe.get_doc("Loan", self.against_loan) + loan = frappe.get_value("Loan", self.against_loan, ['total_amount_paid', 'total_principal_paid', + 'status', 'is_secured_loan', 'total_payment', 'loan_amount', 'total_interest_payable', + 'written_off_amount'], as_dict=1) + + loan.update({ + 'total_amount_paid': loan.total_amount_paid + self.amount_paid, + 'total_principal_paid': loan.total_principal_paid + self.principal_amount_paid + }) + + pending_principal_amount = get_pending_principal_amount(loan) + if not loan.is_secured_loan and pending_principal_amount < 0: + loan.update({'status': 'Loan Closure Requested'}) for payment in self.repayment_details: frappe.db.sql(""" UPDATE `tabLoan Interest Accrual` @@ -135,17 +146,31 @@ class LoanRepayment(AccountsController): WHERE name = %s""", (flt(payment.paid_principal_amount), flt(payment.paid_interest_amount), payment.loan_interest_accrual)) - frappe.db.sql(""" UPDATE `tabLoan` SET total_amount_paid = %s, total_principal_paid = %s - WHERE name = %s """, (loan.total_amount_paid + self.amount_paid, - loan.total_principal_paid + self.principal_amount_paid, self.against_loan)) + frappe.db.sql(""" UPDATE `tabLoan` + SET total_amount_paid = %s, total_principal_paid = %s, status = %s + WHERE name = %s """, (loan.total_amount_paid, loan.total_principal_paid, loan.status, + self.against_loan)) update_shortfall_status(self.against_loan, self.principal_amount_paid) def mark_as_unpaid(self): - loan = frappe.get_doc("Loan", self.against_loan) + loan = frappe.get_value("Loan", self.against_loan, ['total_amount_paid', 'total_principal_paid', + 'status', 'is_secured_loan', 'total_payment', 'loan_amount', 'total_interest_payable', + 'written_off_amount'], as_dict=1) no_of_repayments = len(self.repayment_details) + loan.update({ + 'total_amount_paid': loan.total_amount_paid - self.amount_paid, + 'total_principal_paid': loan.total_principal_paid - self.principal_amount_paid + }) + + if loan.status == 'Loan Closure Requested': + if loan.disbursed_amount >= loan.loan_amount: + loan['status'] = 'Disbursed' + else: + loan['status'] = 'Partially Disbursed' + for payment in self.repayment_details: frappe.db.sql(""" UPDATE `tabLoan Interest Accrual` SET paid_principal_amount = `paid_principal_amount` - %s, @@ -159,12 +184,9 @@ class LoanRepayment(AccountsController): lia_doc = frappe.get_doc('Loan Interest Accrual', payment.loan_interest_accrual) lia_doc.cancel() - frappe.db.sql(""" UPDATE `tabLoan` SET total_amount_paid = %s, total_principal_paid = %s - WHERE name = %s """, (loan.total_amount_paid - self.amount_paid, - loan.total_principal_paid - self.principal_amount_paid, self.against_loan)) - - if loan.status == "Loan Closure Requested": - frappe.db.set_value("Loan", self.against_loan, "status", "Disbursed") + frappe.db.sql(""" UPDATE `tabLoan` + SET total_amount_paid = %s, total_principal_paid = %s, status = %s + WHERE name = %s """, (loan.total_amount_paid, loan.total_principal_paid, loan.status, self.against_loan)) def update_repayment_schedule(self): if self.is_term_loan and self.principal_amount_paid > self.payable_principal_amount: @@ -431,12 +453,7 @@ def regenerate_repayment_schedule(loan): loan_doc.save() - if loan_doc.status in ('Disbursed', 'Loan Closure Requested', 'Closed'): - balance_amount = loan_doc.total_payment - loan_doc.total_principal_paid \ - - loan_doc.total_interest_payable - loan_doc.written_off_amount - else: - balance_amount = loan_doc.disbursed_amount - loan_doc.total_principal_paid \ - - loan_doc.total_interest_payable - loan_doc.written_off_amount + balance_amount = get_pending_principal_amount(loan_doc) monthly_repayment_amount = get_monthly_repayment_amount(loan_doc.loan_amount, loan_doc.rate_of_interest, loan_doc.repayment_periods) @@ -464,6 +481,16 @@ def regenerate_repayment_schedule(loan): loan_doc.save() +def get_pending_principal_amount(loan): + if loan.status in ('Disbursed', 'Closed') or loan.disbursed_amount >= loan.loan_amount: + pending_principal_amount = flt(loan.total_payment) - flt(loan.total_principal_paid) \ + - flt(loan.total_interest_payable) - flt(loan.written_off_amount) + else: + pending_principal_amount = flt(loan.disbursed_amount) - flt(loan.total_principal_paid) \ + - flt(loan.total_interest_payable) - flt(loan.written_off_amount) + + return pending_principal_amount + # This function returns the amounts that are payable at the time of loan repayment based on posting date # So it pulls all the unpaid Loan Interest Accrual Entries and calculates the penalty if applicable @@ -511,12 +538,7 @@ def get_amounts(amounts, against_loan, posting_date): if due_date and not final_due_date: final_due_date = add_days(due_date, loan_type_details.grace_period_in_days) - if against_loan_doc.status in ('Disbursed', 'Loan Closure Requested', 'Closed'): - pending_principal_amount = against_loan_doc.total_payment - against_loan_doc.total_principal_paid \ - - against_loan_doc.total_interest_payable - against_loan_doc.written_off_amount - else: - pending_principal_amount = against_loan_doc.disbursed_amount - against_loan_doc.total_principal_paid \ - - against_loan_doc.total_interest_payable - against_loan_doc.written_off_amount + pending_principal_amount = get_pending_principal_amount(against_loan_doc) unaccrued_interest = 0 if due_date: diff --git a/erpnext/loan_management/doctype/loan_security_unpledge/loan_security_unpledge.py b/erpnext/loan_management/doctype/loan_security_unpledge/loan_security_unpledge.py index 0af0de1a53..cb3ded7488 100644 --- a/erpnext/loan_management/doctype/loan_security_unpledge/loan_security_unpledge.py +++ b/erpnext/loan_management/doctype/loan_security_unpledge/loan_security_unpledge.py @@ -30,6 +30,9 @@ class LoanSecurityUnpledge(Document): d.idx, frappe.bold(d.loan_security))) def validate_unpledge_qty(self): + from erpnext.loan_management.doctype.loan_repayment.loan_repayment import ( + get_pending_principal_amount, + ) from erpnext.loan_management.doctype.loan_security_shortfall.loan_security_shortfall import ( get_ltv_ratio, ) @@ -46,15 +49,10 @@ class LoanSecurityUnpledge(Document): "valid_upto": (">=", get_datetime()) }, as_list=1)) - loan_details = frappe.get_value("Loan", self.loan, ['total_payment', 'total_principal_paid', + loan_details = frappe.get_value("Loan", self.loan, ['total_payment', 'total_principal_paid', 'loan_amount', 'total_interest_payable', 'written_off_amount', 'disbursed_amount', 'status'], as_dict=1) - if loan_details.status == 'Disbursed': - pending_principal_amount = flt(loan_details.total_payment) - flt(loan_details.total_interest_payable) \ - - flt(loan_details.total_principal_paid) - flt(loan_details.written_off_amount) - else: - pending_principal_amount = flt(loan_details.disbursed_amount) - flt(loan_details.total_interest_payable) \ - - flt(loan_details.total_principal_paid) - flt(loan_details.written_off_amount) + pending_principal_amount = get_pending_principal_amount(loan_details) security_value = 0 unpledge_qty_map = {} From 8f6600b27a697d36300d5bb4b3e3303d511839bb Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Mon, 25 Oct 2021 16:21:26 +0530 Subject: [PATCH 4/7] fix: Repayment schedule revert on cancel --- .../doctype/loan_repayment/loan_repayment.py | 38 +++++++++++++++---- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py index 962ae5ea09..8cc53ca854 100644 --- a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py +++ b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py @@ -42,8 +42,9 @@ class LoanRepayment(AccountsController): self.make_gl_entries() def on_cancel(self): + self.check_future_accruals() + self.update_repayment_schedule(cancel=1) self.mark_as_unpaid() - self.update_repayment_schedule() self.ignore_linked_doctypes = ['GL Entry'] self.make_gl_entries(cancel=1) @@ -188,9 +189,16 @@ class LoanRepayment(AccountsController): SET total_amount_paid = %s, total_principal_paid = %s, status = %s WHERE name = %s """, (loan.total_amount_paid, loan.total_principal_paid, loan.status, self.against_loan)) - def update_repayment_schedule(self): + def check_future_accruals(self): + future_accrual_date = frappe.db.get_value("Loan Interest Accrual", {"posting_date": (">", self.posting_date), + "docstatus": 1, "loan": self.against_loan}, 'posting_date') + + if future_accrual_date: + frappe.throw("Cannot cancel. Interest accruals already processed till {0}".format(get_datetime(future_accrual_date))) + + def update_repayment_schedule(self, cancel=0): if self.is_term_loan and self.principal_amount_paid > self.payable_principal_amount: - regenerate_repayment_schedule(self.against_loan) + regenerate_repayment_schedule(self.against_loan, cancel) def allocate_amounts(self, repayment_details): self.set('repayment_details', []) @@ -435,7 +443,7 @@ def get_penalty_details(against_loan): else: return None, 0 -def regenerate_repayment_schedule(loan): +def regenerate_repayment_schedule(loan, cancel=0): from erpnext.loan_management.doctype.loan.loan import ( add_single_month, get_monthly_repayment_amount, @@ -443,20 +451,34 @@ def regenerate_repayment_schedule(loan): loan_doc = frappe.get_doc('Loan', loan) next_accrual_date = None + accrued_entries = 0 + last_repayment_amount = 0 + last_balance_amount = 0 for term in reversed(loan_doc.get('repayment_schedule')): if not term.is_accrued: next_accrual_date = term.payment_date - - if not term.is_accrued: loan_doc.remove(term) + else: + accrued_entries += 1 + if not last_repayment_amount: + last_repayment_amount = term.total_payment + if not last_balance_amount: + last_balance_amount = term.balance_loan_amount loan_doc.save() balance_amount = get_pending_principal_amount(loan_doc) - monthly_repayment_amount = get_monthly_repayment_amount(loan_doc.loan_amount, - loan_doc.rate_of_interest, loan_doc.repayment_periods) + if loan_doc.repayment_method == 'Repay Fixed Amount per Period': + monthly_repayment_amount = flt(balance_amount/len(loan_doc.get('repayment_schedule')) - accrued_entries) + else: + if not cancel: + monthly_repayment_amount = get_monthly_repayment_amount(balance_amount, + loan_doc.rate_of_interest, loan_doc.repayment_periods - accrued_entries) + else: + monthly_repayment_amount = last_repayment_amount + balance_amount = last_balance_amount payment_date = next_accrual_date From c572a4cb8828a4d045b3dbdb05db6b8f093489d7 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Mon, 15 Nov 2021 09:28:56 +0530 Subject: [PATCH 5/7] fix: Book unaccrued interest check --- .../loan_management/doctype/loan_repayment/loan_repayment.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py index a6fa639870..f7d9e6602e 100644 --- a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py +++ b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py @@ -93,7 +93,7 @@ class LoanRepayment(AccountsController): def book_unaccrued_interest(self): precision = cint(frappe.db.get_default("currency_precision")) or 2 - if self.total_interest_paid > self.interest_payable: + if flt(self.total_interest_paid, precision) > flt(self.interest_payable, precision): if not self.is_term_loan: # get last loan interest accrual date last_accrual_date = get_last_accrual_date(self.against_loan) From f78bf4c6ef513fb4e0dde26b4eccecd1075c337d Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Tue, 28 Dec 2021 17:40:05 +0530 Subject: [PATCH 6/7] fix: Test cases --- erpnext/loan_management/doctype/loan/test_loan.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/erpnext/loan_management/doctype/loan/test_loan.py b/erpnext/loan_management/doctype/loan/test_loan.py index f66cc54a13..b232b8761c 100644 --- a/erpnext/loan_management/doctype/loan/test_loan.py +++ b/erpnext/loan_management/doctype/loan/test_loan.py @@ -311,9 +311,9 @@ class TestLoan(unittest.TestCase): loan.load_from_db() - self.assertEqual(flt(loan.get('repayment_schedule')[3].principal_amount, 2), 32151.83) - self.assertEqual(flt(loan.get('repayment_schedule')[3].interest_amount, 2), 225.06) - self.assertEqual(flt(loan.get('repayment_schedule')[3].total_payment, 2), 32376.89) + self.assertEqual(flt(loan.get('repayment_schedule')[3].principal_amount, 2), 41369.83) + self.assertEqual(flt(loan.get('repayment_schedule')[3].interest_amount, 2), 289.59) + self.assertEqual(flt(loan.get('repayment_schedule')[3].total_payment, 2), 41659.41) self.assertEqual(flt(loan.get('repayment_schedule')[3].balance_loan_amount, 2), 0) def test_security_shortfall(self): From 68d49817a1da8bdd42502d86fb51c7d8df3289eb Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Tue, 28 Dec 2021 18:10:52 +0530 Subject: [PATCH 7/7] fix: Add test for loan repayment cancellation --- erpnext/loan_management/doctype/loan/test_loan.py | 8 ++++++++ .../doctype/loan_repayment/loan_repayment.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/erpnext/loan_management/doctype/loan/test_loan.py b/erpnext/loan_management/doctype/loan/test_loan.py index b232b8761c..1676c218c8 100644 --- a/erpnext/loan_management/doctype/loan/test_loan.py +++ b/erpnext/loan_management/doctype/loan/test_loan.py @@ -218,6 +218,14 @@ class TestLoan(unittest.TestCase): self.assertEqual(flt(loan.total_principal_paid, 0), flt(repayment_entry.amount_paid - penalty_amount - total_interest_paid, 0)) + # Check Repayment Entry cancel + repayment_entry.load_from_db() + repayment_entry.cancel() + + loan.load_from_db() + self.assertEqual(loan.total_principal_paid, 0) + self.assertEqual(loan.total_principal_paid, 0) + def test_loan_closure(self): pledge = [{ "loan_security": "Test Security 1", diff --git a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py index f7d9e6602e..2abb3957b2 100644 --- a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py +++ b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py @@ -134,7 +134,7 @@ class LoanRepayment(AccountsController): }) pending_principal_amount = get_pending_principal_amount(loan) - if not loan.is_secured_loan and pending_principal_amount < 0: + if not loan.is_secured_loan and pending_principal_amount <= 0: loan.update({'status': 'Loan Closure Requested'}) for payment in self.repayment_details: