Skip to content

Commit

Permalink
fix: close employee loan on write off (frappe#37638)
Browse files Browse the repository at this point in the history
* fix: exclude written off amount while calculating loan repayment

* fix: revert exclude written off amount while calculating loan repayment

* fix: close employee loan on write off
  • Loading branch information
anandbaburajan authored Oct 24, 2023
1 parent 920de79 commit 922ace4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
5 changes: 0 additions & 5 deletions erpnext/loan_management/doctype/loan/loan.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,6 @@ def close_unsecured_term_loan(loan):
frappe.throw(_("Cannot close this loan until full repayment"))


def close_loan(loan, total_amount_paid):
frappe.db.set_value("Loan", loan, "total_amount_paid", total_amount_paid)
frappe.db.set_value("Loan", loan, "status", "Closed")


@frappe.whitelist()
def make_loan_disbursement(loan, company, applicant_type, applicant, pending_amount=0, as_dict=0):
disbursement_entry = frappe.new_doc("Loan Disbursement")
Expand Down
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 922ace4

Please sign in to comment.