Merge pull request #23279 from deepeshgarg007/loan_closure_interest

fix: Pending loan interest accrual on loan closure
This commit is contained in:
Deepesh Garg 2020-09-07 15:01:33 +05:30 committed by GitHub
commit af1c4ba443
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 6 deletions

View File

@ -199,10 +199,9 @@ class TestLoan(unittest.TestCase):
"Loan Closure", flt(loan.loan_amount + accrued_interest_amount))
repayment_entry.submit()
amounts = frappe.db.get_value('Loan Interest Accrual', {'loan': loan.name}, ['paid_interest_amount',
'paid_principal_amount'])
amount = frappe.db.get_value('Loan Interest Accrual', {'loan': loan.name}, ['sum(paid_interest_amount)'])
self.assertEquals(flt(amounts[0], 2),flt(accrued_interest_amount, 2))
self.assertEquals(flt(amount, 2),flt(accrued_interest_amount, 2))
self.assertEquals(flt(repayment_entry.penalty_amount, 5), 0)
loan.load_from_db()

View File

@ -213,7 +213,8 @@ def get_last_accural_date_in_current_month(loan):
WHERE loan = %s""", (loan.name))
if last_posting_date[0][0]:
return last_posting_date[0][0]
# interest for last interest accrual date is already booked, so add 1 day
return add_days(last_posting_date[0][0], 1)
else:
return loan.disbursement_date

View File

@ -13,6 +13,7 @@ from frappe.utils import date_diff, add_days, getdate, add_months, get_first_day
from erpnext.controllers.accounts_controller import AccountsController
from erpnext.accounts.general_ledger import make_gl_entries
from erpnext.loan_management.doctype.loan_security_shortfall.loan_security_shortfall import update_shortfall_status
from erpnext.loan_management.doctype.process_loan_interest_accrual.process_loan_interest_accrual import process_loan_interest_accrual_for_demand_loans
class LoanRepayment(AccountsController):
@ -22,6 +23,9 @@ class LoanRepayment(AccountsController):
self.validate_amount()
self.allocate_amounts(amounts['pending_accrual_entries'])
def before_submit(self):
self.book_unaccrued_interest()
def on_submit(self):
self.update_paid_amount()
self.make_gl_entries()
@ -72,6 +76,26 @@ class LoanRepayment(AccountsController):
msg = _("Amount of {0} is required for Loan closure").format(self.payable_amount)
frappe.throw(msg)
def book_unaccrued_interest(self):
if self.payment_type == 'Loan Closure':
total_interest_paid = 0
for payment in self.repayment_details:
total_interest_paid += payment.paid_interest_amount
if total_interest_paid < self.interest_payable:
if not self.is_term_loan:
process = process_loan_interest_accrual_for_demand_loans(posting_date=self.posting_date,
loan=self.against_loan)
lia = frappe.db.get_value('Loan Interest Accrual', {'process_loan_interest_accrual':
process}, ['name', 'interest_amount', 'payable_principal_amount'], as_dict=1)
self.append('repayment_details', {
'loan_interest_accrual': lia.name,
'paid_interest_amount': lia.interest_amount,
'paid_principal_amount': lia.payable_principal_amount
})
def update_paid_amount(self):
precision = cint(frappe.db.get_default("currency_precision")) or 2
@ -148,8 +172,6 @@ class LoanRepayment(AccountsController):
if self.payment_type == 'Loan Closure' and total_interest_paid < self.interest_payable:
unaccrued_interest = self.interest_payable - total_interest_paid
interest_paid -= unaccrued_interest
if self.repayment_details:
self.repayment_details[-1].paid_interest_amount += unaccrued_interest
if interest_paid:
self.principal_amount_paid += interest_paid

View File

@ -36,6 +36,8 @@ def process_loan_interest_accrual_for_demand_loans(posting_date=None, loan_type=
loan_process.submit()
return loan_process.name
def process_loan_interest_accrual_for_term_loans(posting_date=None, loan_type=None, loan=None):
if not term_loan_accrual_pending(posting_date or nowdate()):
@ -49,6 +51,8 @@ def process_loan_interest_accrual_for_term_loans(posting_date=None, loan_type=No
loan_process.submit()
return loan_process.name
def term_loan_accrual_pending(date):
pending_accrual = frappe.db.get_value('Repayment Schedule', {
'payment_date': ('<=', date),