Skip to content

Commit

Permalink
fix: Loan Write-off for term loans
Browse files Browse the repository at this point in the history
  • Loading branch information
deepeshgarg007 committed Aug 29, 2022
1 parent 6025df9 commit e64e812
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,7 @@ def get_amounts(amounts, against_loan, posting_date):
)
amounts["pending_accrual_entries"] = pending_accrual_entries
amounts["unaccrued_interest"] = flt(unaccrued_interest, precision)
amounts["written_off_amount"] = flt(against_loan_doc.written_off_amount, precision)

if final_due_date:
amounts["due_date"] = final_due_date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def process_loan_interest_accrual_for_demand_loans(

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()):
if not term_loan_accrual_pending(posting_date or nowdate(), loan=loan):
return

loan_process = frappe.new_doc("Process Loan Interest Accrual")
Expand All @@ -71,9 +71,14 @@ def process_loan_interest_accrual_for_term_loans(posting_date=None, loan_type=No
return loan_process.name


def term_loan_accrual_pending(date):
def term_loan_accrual_pending(date, loan=None):
filters = {"payment_date": ("<=", date), "is_accrued": 0}

if loan:
filters.update({'parent': loan})

pending_accrual = frappe.db.get_value(
"Repayment Schedule", {"payment_date": ("<=", date), "is_accrued": 0}
"Repayment Schedule", filters
)

return pending_accrual
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"fieldname": "total_payment",
"fieldtype": "Currency",
"in_list_view": 1,
"label": "Total Payment",
"label": "Paid Amount",
"options": "Company:company:default_currency"
},
{
Expand All @@ -87,7 +87,7 @@
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
"modified": "2022-01-31 14:50:14.823213",
"modified": "2022-08-29 08:50:39.030296",
"modified_by": "Administrator",
"module": "Loan Management",
"name": "Salary Slip Loan",
Expand All @@ -96,6 +96,5 @@
"quick_entry": 1,
"sort_field": "modified",
"sort_order": "DESC",
"states": [],
"track_changes": 1
}
3 changes: 1 addition & 2 deletions erpnext/payroll/doctype/salary_slip/salary_slip.json
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,6 @@
"fieldtype": "Column Break"
},
{
"depends_on": "total_loan_repayment",
"fieldname": "loan_repayment",
"fieldtype": "Section Break",
"label": "Loan Repayment"
Expand Down Expand Up @@ -647,7 +646,7 @@
"idx": 9,
"is_submittable": 1,
"links": [],
"modified": "2021-10-08 11:48:47.098248",
"modified": "2022-08-29 08:59:50.819986",
"modified_by": "Administrator",
"module": "Payroll",
"name": "Salary Slip",
Expand Down
30 changes: 23 additions & 7 deletions erpnext/payroll/doctype/salary_slip/salary_slip.py
Original file line number Diff line number Diff line change
Expand Up @@ -1379,12 +1379,28 @@ def set_loan_repayment(self):
for loan in self.get_loan_details():
amounts = calculate_amounts(loan.name, self.posting_date, "Regular Payment")

if amounts["interest_amount"] or amounts["payable_principal_amount"]:
if (amounts["interest_amount"] or amounts["payable_principal_amount"]) \
and (amounts["payable_principal_amount"] + amounts["interest_amount"] > amounts["written_off_amount"]):

print("Ininininin")
if amounts["interest_amount"] > amounts["written_off_amount"]:
amounts["interest_amount"] -= amounts["written_off_amount"]
amounts["written_off_amount"] = 0
else:
amounts["written_off_amount"] -= amounts["interest_amount"]
amounts["interest_amount"] = 0

if amounts["payable_principal_amount"] > amounts["written_off_amount"]:
amounts["payable_principal_amount"] -= amounts["written_off_amount"]
amounts["written_off_amount"] = 0
else:
amounts["written_off_amount"] -= amounts["payable_principal_amount"]
amounts["payable_principal_amount"] = 0

self.append(
"loans",
{
"loan": loan.name,
"total_payment": amounts["interest_amount"] + amounts["payable_principal_amount"],
"interest_amount": amounts["interest_amount"],
"principal_amount": amounts["payable_principal_amount"],
"loan_account": loan.loan_account,
Expand All @@ -1395,7 +1411,7 @@ def set_loan_repayment(self):
for payment in self.get("loans"):
amounts = calculate_amounts(payment.loan, self.posting_date, "Regular Payment")
total_amount = amounts["interest_amount"] + amounts["payable_principal_amount"]
if payment.total_payment > total_amount:
if flt(payment.total_payment) > total_amount:
frappe.throw(
_(
"""Row {0}: Paid amount {1} is greater than pending accrued amount {2} against loan {3}"""
Expand All @@ -1407,10 +1423,10 @@ def set_loan_repayment(self):
)
)

self.total_interest_amount += payment.interest_amount
self.total_principal_amount += payment.principal_amount
self.total_interest_amount += flt(payment.interest_amount)
self.total_principal_amount += flt(payment.principal_amount)

self.total_loan_repayment += payment.total_payment
self.total_loan_repayment += flt(payment.total_payment)

def get_loan_details(self):
loan_details = frappe.get_all(
Expand All @@ -1436,7 +1452,7 @@ def get_loan_details(self):
def make_loan_repayment_entry(self):
payroll_payable_account = get_payroll_payable_account(self.company, self.payroll_entry)
for loan in self.loans:
if loan.total_payment:
if flt(loan.total_payment) > 0:
repayment_entry = create_repayment_entry(
loan.loan,
self.employee,
Expand Down

0 comments on commit e64e812

Please sign in to comment.