Skip to content

Commit

Permalink
fix: close employee loan on write off
Browse files Browse the repository at this point in the history
  • Loading branch information
anandbaburajan committed Oct 23, 2023
1 parent 77b1526 commit 108b55b
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions erpnext/loan_management/doctype/loan_write_off/loan_write_off.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import erpnext
from erpnext.accounts.general_ledger import make_gl_entries
from erpnext.controllers.accounts_controller import AccountsController
from erpnext.loan_management.doctype.loan_repayment.loan_repayment import (
get_pending_principal_amount,
)


class LoanWriteOff(AccountsController):
Expand Down Expand Up @@ -39,11 +42,13 @@ def validate_write_off_amount(self):
def on_submit(self):
self.update_outstanding_amount()
self.make_gl_entries()
self.close_employee_loan()

def on_cancel(self):
self.update_outstanding_amount(cancel=1)
self.ignore_linked_doctypes = ["GL Entry", "Payment Ledger Entry"]
self.make_gl_entries(cancel=1)
self.close_employee_loan(cancel=1)

def update_outstanding_amount(self, cancel=0):
written_off_amount = frappe.db.get_value("Loan", self.loan, "written_off_amount")
Expand Down Expand Up @@ -94,3 +99,39 @@ def make_gl_entries(self, cancel=0):
)

make_gl_entries(gl_entries, cancel=cancel, merge_entries=False)

def close_employee_loan(self, cancel=0):
if not frappe.db.has_column("Loan", "repay_from_salary"):
return

loan = frappe.get_value(
"Loan",
self.loan,
[
"total_payment",
"total_principal_paid",
"loan_amount",
"total_interest_payable",
"written_off_amount",
"disbursed_amount",
"status",
"is_secured_loan",
"repay_from_salary",
"name",
],
as_dict=1,
)

if loan.is_secured_loan or not loan.repay_from_salary:
return

if not cancel:
pending_principal_amount = get_pending_principal_amount(loan)

precision = cint(frappe.db.get_default("currency_precision")) or 2

if flt(pending_principal_amount, precision) <= 0:
frappe.db.set_value("Loan", loan.name, "status", "Closed")
frappe.msgprint(_("Loan {0} closed").format(loan.name))
else:
frappe.db.set_value("Loan", loan.loan, "status", "Disbursed")

0 comments on commit 108b55b

Please sign in to comment.