test: update ple on payment unlink for SI's and SO's
diff --git a/erpnext/accounts/doctype/payment_ledger_entry/test_payment_ledger_entry.py b/erpnext/accounts/doctype/payment_ledger_entry/test_payment_ledger_entry.py
index a71b19e..fc6dbba 100644
--- a/erpnext/accounts/doctype/payment_ledger_entry/test_payment_ledger_entry.py
+++ b/erpnext/accounts/doctype/payment_ledger_entry/test_payment_ledger_entry.py
@@ -3,12 +3,13 @@
 
 import frappe
 from frappe import qb
-from frappe.tests.utils import FrappeTestCase
+from frappe.tests.utils import FrappeTestCase, change_settings
 from frappe.utils import nowdate
 
 from erpnext.accounts.doctype.payment_entry.payment_entry import get_payment_entry
 from erpnext.accounts.doctype.payment_entry.test_payment_entry import create_payment_entry
 from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_sales_invoice
+from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order
 from erpnext.stock.doctype.item.test_item import create_item
 
 
@@ -127,6 +128,25 @@
 		payment.posting_date = posting_date
 		return payment
 
+	def create_sales_order(
+		self, qty=1, rate=100, posting_date=nowdate(), do_not_save=False, do_not_submit=False
+	):
+		so = make_sales_order(
+			company=self.company,
+			transaction_date=posting_date,
+			customer=self.customer,
+			item_code=self.item,
+			cost_center=self.cost_center,
+			warehouse=self.warehouse,
+			debit_to=self.debit_to,
+			currency="INR",
+			qty=qty,
+			rate=100,
+			do_not_save=do_not_save,
+			do_not_submit=do_not_submit,
+		)
+		return so
+
 	def clear_old_entries(self):
 		doctype_list = [
 			"GL Entry",
@@ -406,3 +426,89 @@
 		]
 		self.assertEqual(pl_entries_for_crnote[0], expected_values[0])
 		self.assertEqual(pl_entries_for_crnote[1], expected_values[1])
+
+	@change_settings(
+		"Accounts Settings",
+		{"unlink_payment_on_cancellation_of_invoice": 1, "delete_linked_ledger_entries": 1},
+	)
+	def test_multi_payment_unlink_on_invoice_cancellation(self):
+		transaction_date = nowdate()
+		amount = 100
+		si = self.create_sales_invoice(qty=1, rate=amount, posting_date=transaction_date)
+
+		for amt in [40, 40, 20]:
+			# payment 1
+			pe = get_payment_entry(si.doctype, si.name)
+			pe.paid_amount = amt
+			pe.get("references")[0].allocated_amount = amt
+			pe = pe.save().submit()
+
+		si.reload()
+		si.cancel()
+
+		entries = frappe.db.get_list(
+			"Payment Ledger Entry",
+			filters={"against_voucher_type": si.doctype, "against_voucher_no": si.name, "delinked": 0},
+		)
+		self.assertEqual(entries, [])
+
+		# with references removed, deletion should be possible
+		si.delete()
+		self.assertRaises(frappe.DoesNotExistError, frappe.get_doc, si.doctype, si.name)
+
+	@change_settings(
+		"Accounts Settings",
+		{"unlink_payment_on_cancellation_of_invoice": 1, "delete_linked_ledger_entries": 1},
+	)
+	def test_multi_je_unlink_on_invoice_cancellation(self):
+		transaction_date = nowdate()
+		amount = 100
+		si = self.create_sales_invoice(qty=1, rate=amount, posting_date=transaction_date)
+
+		# multiple JE's against invoice
+		for amt in [40, 40, 20]:
+			je1 = self.create_journal_entry(
+				self.income_account, self.debit_to, amt, posting_date=transaction_date
+			)
+			je1.get("accounts")[1].party_type = "Customer"
+			je1.get("accounts")[1].party = self.customer
+			je1.get("accounts")[1].reference_type = si.doctype
+			je1.get("accounts")[1].reference_name = si.name
+			je1 = je1.save().submit()
+
+		si.reload()
+		si.cancel()
+
+		entries = frappe.db.get_list(
+			"Payment Ledger Entry",
+			filters={"against_voucher_type": si.doctype, "against_voucher_no": si.name, "delinked": 0},
+		)
+		self.assertEqual(entries, [])
+
+		# with references removed, deletion should be possible
+		si.delete()
+		self.assertRaises(frappe.DoesNotExistError, frappe.get_doc, si.doctype, si.name)
+
+	@change_settings(
+		"Accounts Settings",
+		{"unlink_payment_on_cancellation_of_invoice": 1, "delete_linked_ledger_entries": 1},
+	)
+	def test_advance_payment_unlink_on_order_cancellation(self):
+		transaction_date = nowdate()
+		amount = 100
+		so = self.create_sales_order(qty=1, rate=amount, posting_date=transaction_date).save().submit()
+
+		pe = get_payment_entry(so.doctype, so.name).save().submit()
+
+		so.reload()
+		so.cancel()
+
+		entries = frappe.db.get_list(
+			"Payment Ledger Entry",
+			filters={"against_voucher_type": so.doctype, "against_voucher_no": so.name, "delinked": 0},
+		)
+		self.assertEqual(entries, [])
+
+		# with references removed, deletion should be possible
+		so.delete()
+		self.assertRaises(frappe.DoesNotExistError, frappe.get_doc, so.doctype, so.name)