fix: Add test for loan top up
diff --git a/erpnext/loan_management/doctype/loan/test_loan.py b/erpnext/loan_management/doctype/loan/test_loan.py
index c385411..1634697 100644
--- a/erpnext/loan_management/doctype/loan/test_loan.py
+++ b/erpnext/loan_management/doctype/loan/test_loan.py
@@ -320,7 +320,7 @@
 
 		amounts = amounts = calculate_amounts(loan.name, add_days(last_date, 6))
 		self.assertTrue(amounts['pending_principal_amount'] < 0)
-		self.assertTrue(amounts['payable_principal_amount'] < 0)
+		self.assertEquals(amounts['payable_principal_amount'], 0.0)
 		self.assertEqual(amounts['interest_amount'], 0)
 
 	def test_disbursal_check_with_shortfall(self):
@@ -706,7 +706,7 @@
 			"haircut": 50.00,
 		}).insert(ignore_permissions=True)
 
-def create_loan_security_pledge(applicant, pledges, loan_application):
+def create_loan_security_pledge(applicant, pledges, loan_application=None, loan=None):
 
 	lsp = frappe.new_doc("Loan Security Pledge")
 	lsp.applicant_type = 'Customer'
@@ -714,11 +714,13 @@
 	lsp.company = "_Test Company"
 	lsp.loan_application = loan_application
 
+	if loan:
+		lsp.loan = loan
+
 	for pledge in pledges:
 		lsp.append('securities', {
 			"loan_security": pledge['loan_security'],
-			"qty": pledge['qty'],
-			"haircut": pledge['haircut']
+			"qty": pledge['qty']
 		})
 
 	lsp.save()
diff --git a/erpnext/loan_management/doctype/loan_disbursement/test_loan_disbursement.py b/erpnext/loan_management/doctype/loan_disbursement/test_loan_disbursement.py
index 3ade5c5..aaaeea8 100644
--- a/erpnext/loan_management/doctype/loan_disbursement/test_loan_disbursement.py
+++ b/erpnext/loan_management/doctype/loan_disbursement/test_loan_disbursement.py
@@ -8,9 +8,10 @@
 from erpnext.loan_management.doctype.loan.test_loan import (create_loan_type, create_loan_security_pledge, create_repayment_entry, create_loan_application,
 	make_loan_disbursement_entry, create_loan_accounts, create_loan_security_type, create_loan_security, create_demand_loan, create_loan_security_price)
 from erpnext.loan_management.doctype.process_loan_interest_accrual.process_loan_interest_accrual import process_loan_interest_accrual_for_demand_loans
-from erpnext.loan_management.doctype.loan_interest_accrual.loan_interest_accrual import days_in_year
+from erpnext.loan_management.doctype.loan_interest_accrual.loan_interest_accrual import days_in_year, get_per_day_interest
 from erpnext.selling.doctype.customer.test_customer import get_customer_dict
 from erpnext.loan_management.doctype.loan_application.loan_application import create_pledge
+from erpnext.loan_management.doctype.loan_repayment.loan_repayment import calculate_amounts
 
 class TestLoanDisbursement(unittest.TestCase):
 
@@ -68,3 +69,44 @@
 		# After repayment loan disbursement entry should go through
 		make_loan_disbursement_entry(loan.name, 500000, disbursement_date=add_days(last_date, 16))
 
+	def test_loan_topup_with_additional_pledge(self):
+		pledge = [{
+			"loan_security": "Test Security 1",
+			"qty": 4000.00
+		}]
+
+		loan_application = create_loan_application('_Test Company', self.applicant, 'Demand Loan', pledge)
+		create_pledge(loan_application)
+
+		loan = create_demand_loan(self.applicant, "Demand Loan", loan_application, posting_date='2019-10-01')
+		loan.submit()
+
+		self.assertEquals(loan.loan_amount, 1000000)
+
+		first_date = '2019-10-01'
+		last_date = '2019-10-30'
+
+		# Disbursed 10,00,000 amount
+		make_loan_disbursement_entry(loan.name, loan.loan_amount, disbursement_date=first_date)
+		process_loan_interest_accrual_for_demand_loans(posting_date = last_date)
+		amounts = calculate_amounts(loan.name, add_days(last_date, 1))
+
+		previous_interest = amounts['interest_amount']
+
+		pledge1 = [{
+			"loan_security": "Test Security 1",
+			"qty": 2000.00
+		}]
+
+		create_loan_security_pledge(self.applicant, pledge1, loan=loan.name)
+
+		# Topup 500000
+		make_loan_disbursement_entry(loan.name, 500000, disbursement_date=add_days(last_date, 1))
+		process_loan_interest_accrual_for_demand_loans(posting_date = add_days(last_date, 15))
+		amounts = calculate_amounts(loan.name, add_days(last_date, 15))
+
+		per_day_interest = get_per_day_interest(1500000, 13.5, '2019-10-30')
+		interest = per_day_interest * 15
+
+		self.assertEquals(amounts['pending_principal_amount'], 1500000)
+		self.assertEquals(amounts['interest_amount'], flt(interest + previous_interest, 2))
diff --git a/erpnext/loan_management/doctype/loan_interest_accrual/test_loan_interest_accrual.py b/erpnext/loan_management/doctype/loan_interest_accrual/test_loan_interest_accrual.py
index 5495d1c..46a6440 100644
--- a/erpnext/loan_management/doctype/loan_interest_accrual/test_loan_interest_accrual.py
+++ b/erpnext/loan_management/doctype/loan_interest_accrual/test_loan_interest_accrual.py
@@ -5,7 +5,7 @@
 import frappe
 import unittest
 from frappe.utils import (nowdate, add_days, get_datetime, get_first_day, get_last_day, date_diff, flt, add_to_date)
-from erpnext.loan_management.doctype.loan.test_loan import (create_loan_type, create_loan_security_pledge, create_loan_security_price,
+from erpnext.loan_management.doctype.loan.test_loan import (create_loan_type, create_loan_security_price,
 	make_loan_disbursement_entry, create_loan_accounts, create_loan_security_type, create_loan_security, create_demand_loan, create_loan_application)
 from erpnext.loan_management.doctype.process_loan_interest_accrual.process_loan_interest_accrual import process_loan_interest_accrual_for_demand_loans
 from erpnext.loan_management.doctype.loan_interest_accrual.loan_interest_accrual import days_in_year
diff --git a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py
index de5ba8f..6b3fba4 100644
--- a/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py
+++ b/erpnext/loan_management/doctype/loan_repayment/loan_repayment.py
@@ -357,8 +357,8 @@
 		pending_days = date_diff(posting_date, against_loan_doc.disbursement_date) + 1
 
 	if pending_days > 0:
-		payable_principal_amount = flt(pending_principal_amount, precision)
-		per_day_interest = get_per_day_interest(payable_principal_amount, loan_type_details.rate_of_interest, posting_date)
+		principal_amount = flt(pending_principal_amount, precision)
+		per_day_interest = get_per_day_interest(principal_amount, loan_type_details.rate_of_interest, posting_date)
 		unaccrued_interest += (pending_days * flt(per_day_interest, precision))
 
 	amounts["pending_principal_amount"] = flt(pending_principal_amount, precision)
@@ -389,9 +389,11 @@
 
 	amounts = get_amounts(amounts, against_loan, posting_date)
 
+	# update values for closure
 	if payment_type == 'Loan Closure':
-		amounts['payable_amount'] += amounts['unaccrued_interest']
+		amounts['payable_principal_amount'] = amounts['pending_principal_amount']
 		amounts['interest_amount'] += amounts['unaccrued_interest']
+		amounts['payable_amount'] = amounts['payable_principal_amount'] + amounts['interest_amount']
 
 	return amounts