Skip to content

Commit

Permalink
fix(Payroll): calculate loan repayment amount based on payroll end da…
Browse files Browse the repository at this point in the history
…te instead of posting date (#2154)

* fix(Payroll): calculate loan repayment amount based on payroll end date instead of posting date

- for backdated salary slips fetching loan repayments based on posting date leads to more deductions than earnings sometimes. Process this relative to the payroll dates instead of posting date

* refactor: move out process accrual entries from `get_loan_details`

- doing extra actions in a function named get is misleading
  • Loading branch information
ruchamahabal authored Sep 4, 2024
1 parent eaa8d3d commit 5669fab
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions hrms/payroll/doctype/salary_slip/salary_slip_loan_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ def set_loan_repayment(doc: "SalarySlip"):
doc.total_principal_amount = 0

if not doc.get("loans", []):
for loan in _get_loan_details(doc):
amounts = calculate_amounts(loan.name, doc.posting_date, "Regular Payment")
loan_details = _get_loan_details(doc)
if loan_details:
process_loan_interest_accruals(loan_details, doc.end_date)

for loan in loan_details:
amounts = calculate_amounts(loan.name, doc.end_date, "Regular Payment")

if amounts["interest_amount"] or amounts["payable_principal_amount"]:
doc.append(
Expand All @@ -49,7 +53,7 @@ def set_loan_repayment(doc: "SalarySlip"):
doc.set("loans", [])

for payment in doc.get("loans", []):
amounts = calculate_amounts(payment.loan, doc.posting_date, "Regular Payment")
amounts = calculate_amounts(payment.loan, doc.end_date, "Regular Payment")
total_amount = amounts["interest_amount"] + amounts["payable_principal_amount"]
if payment.total_payment > total_amount:
frappe.throw(
Expand All @@ -68,11 +72,7 @@ def set_loan_repayment(doc: "SalarySlip"):
doc.total_loan_repayment += payment.total_payment


def _get_loan_details(doc: "SalarySlip"):
from lending.loan_management.doctype.process_loan_interest_accrual.process_loan_interest_accrual import (
process_loan_interest_accrual_for_term_loans,
)

def _get_loan_details(doc: "SalarySlip") -> dict[str, str | bool]:
loan_details = frappe.get_all(
"Loan",
fields=["name", "interest_income_account", "loan_account", "loan_product", "is_term_loan"],
Expand All @@ -84,15 +84,19 @@ def _get_loan_details(doc: "SalarySlip"):
"status": ("!=", "Closed"),
},
)
return loan_details

if loan_details:
for loan in loan_details:
if loan.is_term_loan:
process_loan_interest_accrual_for_term_loans(
posting_date=doc.posting_date, loan_product=loan.loan_product, loan=loan.name
)

return loan_details
def process_loan_interest_accruals(loan_details: dict[str, str | bool], posting_date: str):
from lending.loan_management.doctype.process_loan_interest_accrual.process_loan_interest_accrual import (
process_loan_interest_accrual_for_term_loans,
)

for loan in loan_details:
if loan.is_term_loan:
process_loan_interest_accrual_for_term_loans(
posting_date=posting_date, loan_product=loan.loan_product, loan=loan.name
)


@if_lending_app_installed
Expand Down

0 comments on commit 5669fab

Please sign in to comment.