fix: Cancel repayment accrual interest entry on payment cancellation
diff --git a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py
index 12d81d3..b973cd6 100644
--- a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py
+++ b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py
@@ -24,8 +24,10 @@
 		self.validate_amount()
 		self.allocate_amounts(amounts)
 
-	def on_submit(self):
+	def before_submit(self):
 		self.book_unaccrued_interest()
+
+	def on_submit(self):
 		self.update_paid_amount()
 		self.make_gl_entries()
 
@@ -99,7 +101,8 @@
 				self.append('repayment_details', {
 					'loan_interest_accrual': lia.name,
 					'paid_interest_amount': flt(self.total_interest_paid - self.interest_payable, precision),
-					'paid_principal_amount': 0.0
+					'paid_principal_amount': 0.0,
+					'accrual_type': 'Repayment'
 				})
 
 	def update_paid_amount(self):
@@ -123,6 +126,8 @@
 	def mark_as_unpaid(self):
 		loan = frappe.get_doc("Loan", self.against_loan)
 
+		no_of_repayments = len(self.repayment_details)
+
 		for payment in self.repayment_details:
 			frappe.db.sql(""" UPDATE `tabLoan Interest Accrual`
 				SET paid_principal_amount = `paid_principal_amount` - %s,
@@ -130,6 +135,12 @@
 				WHERE name = %s""",
 				(payment.paid_principal_amount, payment.paid_interest_amount, payment.loan_interest_accrual))
 
+			# Cancel repayment interest accrual
+			# checking idx as a preventive measure, repayment accrual will always be the last entry
+			if payment.accrual_type == 'Repayment' and payment.idx == no_of_repayments:
+				lia_doc = frappe.get_doc('Loan Interest Accrual', payment.loan_interest_accrual)
+				lia_doc.cancel()
+
 		frappe.db.sql(""" UPDATE `tabLoan` SET total_amount_paid = %s, total_principal_paid = %s
 			WHERE name = %s """, (loan.total_amount_paid - self.amount_paid,
 			loan.total_principal_paid - self.principal_amount_paid, self.against_loan))
diff --git a/erpnext/loan_management/doctype/loan_repayment_detail/loan_repayment_detail.json b/erpnext/loan_management/doctype/loan_repayment_detail/loan_repayment_detail.json
index cff1dbb..4b9b191 100644
--- a/erpnext/loan_management/doctype/loan_repayment_detail/loan_repayment_detail.json
+++ b/erpnext/loan_management/doctype/loan_repayment_detail/loan_repayment_detail.json
@@ -7,7 +7,8 @@
  "field_order": [
   "loan_interest_accrual",
   "paid_principal_amount",
-  "paid_interest_amount"
+  "paid_interest_amount",
+  "accrual_type"
  ],
  "fields": [
   {
@@ -27,11 +28,20 @@
    "fieldtype": "Currency",
    "label": "Paid Interest Amount",
    "options": "Company:company:default_currency"
+  },
+  {
+   "fetch_from": "loan_interest_accrual.accrual_type",
+   "fetch_if_empty": 1,
+   "fieldname": "accrual_type",
+   "fieldtype": "Select",
+   "label": "Accrual Type",
+   "options": "Regular\nRepayment\nDisbursement"
   }
  ],
+ "index_web_pages_for_search": 1,
  "istable": 1,
  "links": [],
- "modified": "2020-04-15 21:50:03.837019",
+ "modified": "2020-10-23 08:09:18.267030",
  "modified_by": "Administrator",
  "module": "Loan Management",
  "name": "Loan Repayment Detail",