fix: Validate loan repayment amount in Loan Application (#18169)
* fix: validate loan repayment amount
* fix: test for loan application
diff --git a/erpnext/hr/doctype/loan_application/loan_application.py b/erpnext/hr/doctype/loan_application/loan_application.py
index 5dbcf15..67be9f2 100644
--- a/erpnext/hr/doctype/loan_application/loan_application.py
+++ b/erpnext/hr/doctype/loan_application/loan_application.py
@@ -29,9 +29,12 @@
if self.repayment_method == "Repay Fixed Amount per Period":
monthly_interest_rate = flt(self.rate_of_interest) / (12 *100)
if monthly_interest_rate:
- self.repayment_periods = math.ceil((math.log(self.repayment_amount) -
- math.log(self.repayment_amount - (self.loan_amount*monthly_interest_rate))) /
- (math.log(1 + monthly_interest_rate)))
+ min_repayment_amount = self.loan_amount*monthly_interest_rate
+ if self.repayment_amount - min_repayment_amount < 0:
+ frappe.throw(_("Repayment Amount must be greater than " \
+ + str(flt(min_repayment_amount, 2))))
+ self.repayment_periods = math.ceil(math.log(self.repayment_amount) -
+ math.log(self.repayment_amount - min_repayment_amount) /(math.log(1 + monthly_interest_rate)))
else:
self.repayment_periods = self.loan_amount / self.repayment_amount
diff --git a/erpnext/hr/doctype/loan_application/test_loan_application.py b/erpnext/hr/doctype/loan_application/test_loan_application.py
index 7644dd0..b08b522 100644
--- a/erpnext/hr/doctype/loan_application/test_loan_application.py
+++ b/erpnext/hr/doctype/loan_application/test_loan_application.py
@@ -31,21 +31,22 @@
"rate_of_interest": 9.2,
"loan_amount": 250000,
"repayment_method": "Repay Over Number of Periods",
- "repayment_periods": 24
+ "repayment_periods": 18
})
loan_application.insert()
-
+
def test_loan_totals(self):
loan_application = frappe.get_doc("Loan Application", {"applicant":self.applicant})
- self.assertEquals(loan_application.repayment_amount, 11445)
- self.assertEquals(loan_application.total_payable_interest, 24657)
- self.assertEquals(loan_application.total_payable_amount, 274657)
- loan_application.repayment_method = "Repay Fixed Amount per Period"
- loan_application.repayment_amount = 15000
+ self.assertEqual(loan_application.total_payable_interest, 18599)
+ self.assertEqual(loan_application.total_payable_amount, 268599)
+ self.assertEqual(loan_application.repayment_amount, 14923)
+
+ loan_application.repayment_periods = 24
loan_application.save()
+ loan_application.reload()
- self.assertEqual(loan_application.repayment_periods, 18)
- self.assertEqual(loan_application.total_payable_interest, 18506)
- self.assertEqual(loan_application.total_payable_amount, 268506)
\ No newline at end of file
+ self.assertEqual(loan_application.total_payable_interest, 24657)
+ self.assertEqual(loan_application.total_payable_amount, 274657)
+ self.assertEqual(loan_application.repayment_amount, 11445)