test: Dunning and PE against partially due invoice

- Check if the right payment portion is picked
- Check if the SI and Dunning are updated on submission and cancellation of PE
diff --git a/erpnext/accounts/doctype/dunning/dunning.js b/erpnext/accounts/doctype/dunning/dunning.js
index 7c4e952..1ac909e 100644
--- a/erpnext/accounts/doctype/dunning/dunning.js
+++ b/erpnext/accounts/doctype/dunning/dunning.js
@@ -1,4 +1,4 @@
-// Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and contributors
+// Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and contributors
 // For license information, please see license.txt
 
 frappe.ui.form.on("Dunning", {
diff --git a/erpnext/accounts/doctype/dunning/dunning.py b/erpnext/accounts/doctype/dunning/dunning.py
index c8cfbca..9d0d36b 100644
--- a/erpnext/accounts/doctype/dunning/dunning.py
+++ b/erpnext/accounts/doctype/dunning/dunning.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and contributors
+# Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and contributors
 # For license information, please see license.txt
 """
 # Accounting
diff --git a/erpnext/accounts/doctype/dunning/test_dunning.py b/erpnext/accounts/doctype/dunning/test_dunning.py
index be8c533..b29ace2 100644
--- a/erpnext/accounts/doctype/dunning/test_dunning.py
+++ b/erpnext/accounts/doctype/dunning/test_dunning.py
@@ -1,9 +1,7 @@
-# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and Contributors
+# Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and Contributors
 # See license.txt
-
-import unittest
-
 import frappe
+from frappe.tests.utils import FrappeTestCase
 from frappe.utils import add_days, nowdate, today
 
 from erpnext import get_default_cost_center
@@ -21,9 +19,10 @@
 test_dependencies = ["Company", "Cost Center"]
 
 
-class TestDunning(unittest.TestCase):
+class TestDunning(FrappeTestCase):
 	@classmethod
 	def setUpClass(cls):
+		super().setUpClass()
 		create_dunning_type("First Notice", fee=0.0, interest=0.0, is_default=1)
 		create_dunning_type("Second Notice", fee=10.0, interest=10.0, is_default=0)
 		unlink_payment_on_cancel_of_invoice()
@@ -31,8 +30,9 @@
 	@classmethod
 	def tearDownClass(cls):
 		unlink_payment_on_cancel_of_invoice(0)
+		super().tearDownClass()
 
-	def test_first_dunning(self):
+	def test_dunning_without_fees(self):
 		dunning = create_dunning(overdue_days=20)
 
 		self.assertEqual(round(dunning.total_outstanding, 2), 100.00)
@@ -41,7 +41,7 @@
 		self.assertEqual(round(dunning.dunning_amount, 2), 0.00)
 		self.assertEqual(round(dunning.grand_total, 2), 100.00)
 
-	def test_second_dunning(self):
+	def test_dunning_with_fees_and_interest(self):
 		dunning = create_dunning(overdue_days=15, dunning_type_name="Second Notice - _TC")
 
 		self.assertEqual(round(dunning.total_outstanding, 2), 100.00)
@@ -50,7 +50,7 @@
 		self.assertEqual(round(dunning.dunning_amount, 2), 10.41)
 		self.assertEqual(round(dunning.grand_total, 2), 110.41)
 
-	def test_payment_entry(self):
+	def test_dunning_with_payment_entry(self):
 		dunning = create_dunning(overdue_days=15, dunning_type_name="Second Notice - _TC")
 		dunning.submit()
 		pe = get_payment_entry("Dunning", dunning.name)
@@ -68,6 +68,44 @@
 		dunning.reload()
 		self.assertEqual(dunning.status, "Resolved")
 
+	def test_dunning_and_payment_against_partially_due_invoice(self):
+		"""
+		Create SI with first installment overdue. Check impact of Dunning and Payment Entry.
+		"""
+		create_payment_terms_template_for_dunning()
+		sales_invoice = create_sales_invoice_against_cost_center(
+			posting_date=add_days(today(), -1 * 6),
+			qty=1,
+			rate=100,
+			do_not_submit=True,
+		)
+		sales_invoice.payment_terms_template = "_Test 50-50 for Dunning"
+		sales_invoice.submit()
+		dunning = create_dunning_from_sales_invoice(sales_invoice.name)
+
+		self.assertEqual(len(dunning.overdue_payments), 1)
+		self.assertEqual(dunning.overdue_payments[0].payment_term, "_Test Payment Term 1 for Dunning")
+
+		dunning.submit()
+		pe = get_payment_entry("Dunning", dunning.name)
+		pe.reference_no, pe.reference_date = "2", nowdate()
+		pe.insert()
+		pe.submit()
+		sales_invoice.load_from_db()
+		dunning.load_from_db()
+
+		self.assertEqual(sales_invoice.status, "Partly Paid")
+		self.assertEqual(sales_invoice.payment_schedule[0].outstanding, 0)
+		self.assertEqual(dunning.status, "Resolved")
+
+		# Test impact on cancellation of PE
+		pe.cancel()
+		sales_invoice.reload()
+		dunning.reload()
+
+		self.assertEqual(sales_invoice.status, "Overdue")
+		self.assertEqual(dunning.status, "Unresolved")
+
 
 def create_dunning(overdue_days, dunning_type_name=None):
 	posting_date = add_days(today(), -1 * overdue_days)
@@ -125,3 +163,35 @@
 			pluck="name",
 		)[0]
 	)
+
+
+def create_payment_terms_template_for_dunning():
+	from erpnext.accounts.doctype.payment_entry.test_payment_entry import create_payment_term
+
+	create_payment_term("_Test Payment Term 1 for Dunning")
+	create_payment_term("_Test Payment Term 2 for Dunning")
+
+	if not frappe.db.exists("Payment Terms Template", "_Test 50-50 for Dunning"):
+		frappe.get_doc(
+			{
+				"doctype": "Payment Terms Template",
+				"template_name": "_Test 50-50 for Dunning",
+				"allocate_payment_based_on_payment_terms": 1,
+				"terms": [
+					{
+						"doctype": "Payment Terms Template Detail",
+						"payment_term": "_Test Payment Term 1 for Dunning",
+						"invoice_portion": 50.00,
+						"credit_days_based_on": "Day(s) after invoice date",
+						"credit_days": 5,
+					},
+					{
+						"doctype": "Payment Terms Template Detail",
+						"payment_term": "_Test Payment Term 2 for Dunning",
+						"invoice_portion": 50.00,
+						"credit_days_based_on": "Day(s) after invoice date",
+						"credit_days": 10,
+					},
+				],
+			}
+		).insert()