Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add configurable breaks in accrual frequency #240

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lending/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@

scheduler_events = {
"daily_long": [
"lending.loan_management.doctype.process_loan_interest_accrual.process_loan_interest_accrual.process_loan_interest_accrual_for_loans",
"lending.loan_management.doctype.process_loan_interest_accrual.process_loan_interest_accrual.schedule_accrual",
"lending.loan_management.doctype.process_loan_demand.process_loan_demand.process_daily_loan_demands",
"lending.loan_management.doctype.process_loan_security_shortfall.process_loan_security_shortfall.create_process_loan_security_shortfall",
"lending.loan_management.doctype.process_loan_classification.process_loan_classification.create_process_loan_classification",
Expand Down
7 changes: 7 additions & 0 deletions lending/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@
"insert_after": "interest_day_count_convention",
"non_negative": 1,
},
{
"fieldname": "loan_accrual_frequency",
"label": "Loan Accrual Frequency",
"fieldtype": "Select",
"options": "Daily\nWeekly\nMonthly",
"insert_after": "min_days_bw_disbursement_first_repayment",
},
{
"fieldname": "loan_column_break",
"fieldtype": "Column Break",
Expand Down
11 changes: 9 additions & 2 deletions lending/loan_management/doctype/loan/loan.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"applicant",
"applicant_name",
"loan_application",
"repost_days_past_due_log",
"column_break_3",
"company",
"posting_date",
Expand Down Expand Up @@ -661,12 +662,18 @@
"fieldtype": "Date",
"label": "Settlement Date",
"read_only": 1
},
{
"default": "0",
"fieldname": "repost_days_past_due_log",
"fieldtype": "Int",
"label": "Repost"
}
],
"index_web_pages_for_search": 1,
"is_submittable": 1,
"links": [],
"modified": "2024-12-31 22:50:42.962589",
"modified": "2025-02-03 00:52:13.170705",
"modified_by": "Administrator",
"module": "Loan Management",
"name": "Loan",
Expand Down Expand Up @@ -698,4 +705,4 @@
"sort_order": "DESC",
"states": [],
"track_changes": 1
}
}
86 changes: 86 additions & 0 deletions lending/loan_management/doctype/loan/test_loan.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
)
from lending.loan_management.doctype.process_loan_interest_accrual.process_loan_interest_accrual import (
process_loan_interest_accrual_for_loans,
schedule_accrual,
)
from lending.loan_management.doctype.process_loan_security_shortfall.process_loan_security_shortfall import (
create_process_loan_security_shortfall,
Expand All @@ -56,6 +57,7 @@ def setUp(self):
create_loan_accounts()
setup_loan_demand_offset_order()

set_loan_accrual_frequency("Daily")
simple_terms_loans = [
["Personal Loan", 500000, 8.4, "Monthly as per repayment start date"],
["Term Loan Product 1", 12000, 7.5, "Monthly as per repayment start date"],
Expand Down Expand Up @@ -1709,6 +1711,71 @@ def test_charges_payment(self):
self.assertEqual(repayment.total_charges_paid, 500)
self.assertEqual(repayment.repayment_details[0].paid_amount, 500)

def test_accrual_background_job(self):
loan = create_loan(
"_Test Customer 1",
"Term Loan Product 4",
100000,
"Repay Over Number of Periods",
22,
repayment_start_date="2024-08-16",
posting_date="2024-08-16",
rate_of_interest=8.5,
applicant_type="Customer",
)
loan.submit()
# Daily accrual
make_loan_disbursement_entry(
loan.name, loan.loan_amount, disbursement_date="2024-08-16", repayment_start_date="2024-08-16"
)

set_loan_accrual_frequency("Daily")
process_loan_interest_accrual_for_loans(loan=loan.name, posting_date="2024-08-20")

loan_interest_accruals = get_loan_interest_accrual(
loan=loan, from_date="2024-08-16", to_date="2024-08-20"
)
expected_dates = [
"2024-08-16",
"2024-08-17",
"2024-08-18",
"2024-08-19",
"2024-08-20",
]
expected_dates = [getdate(i) for i in expected_dates]
self.assertEqual(loan_interest_accruals, expected_dates)

set_loan_accrual_frequency("Weekly")
process_loan_interest_accrual_for_loans(loan=loan.name, posting_date="2024-08-31")

loan_interest_accruals = get_loan_interest_accrual(
loan=loan, from_date="2024-08-21", to_date="2024-08-31"
)
expected_dates = [
"2024-08-25",
"2024-08-31",
]
expected_dates = [getdate(i) for i in expected_dates]

self.assertEqual(loan_interest_accruals, expected_dates)

set_loan_accrual_frequency("Monthly")
process_loan_interest_accrual_for_loans(loan=loan.name, posting_date="2024-11-01")

loan_interest_accruals = get_loan_interest_accrual(
loan=loan, from_date="2024-09-01", to_date="2024-11-05"
)
expected_dates = [
"2024-09-01",
"2024-09-15",
"2024-10-01",
"2024-10-15",
"2024-11-01",
]
expected_dates = [getdate(i) for i in expected_dates]

self.assertEqual(loan_interest_accruals, expected_dates)


def add_or_update_loan_charges(product_name):
loan_product = frappe.get_doc("Loan Product", product_name)
Expand Down Expand Up @@ -2368,3 +2435,22 @@ def create_loan_write_off(loan, posting_date, write_off_amount=None):
loan_write_off.submit()

return loan_write_off


def set_loan_accrual_frequency(loan_accrual_frequency):
frappe.db.set_value(
"Company",
"_Test Company",
"loan_accrual_frequency",
loan_accrual_frequency,
)


def get_loan_interest_accrual(loan, from_date, to_date):
loan_interest_accruals = frappe.db.get_all(
"Loan Interest Accrual",
{"loan": loan, "docstatus": 1, "posting_date": ("between", [from_date, to_date])},
pluck="posting_date",
order_by="posting_date",
)
return loan_interest_accruals
Loading
Loading