Merge pull request #37041 from vorasmit/fix-advances

fix: multiple fixes for booking advances in seperate account
diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py
index 2c2efc0..8a894e2 100644
--- a/erpnext/accounts/doctype/payment_entry/payment_entry.py
+++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py
@@ -98,7 +98,6 @@
 		if self.difference_amount:
 			frappe.throw(_("Difference Amount must be zero"))
 		self.make_gl_entries()
-		self.make_advance_gl_entries()
 		self.update_outstanding_amounts()
 		self.update_advance_paid()
 		self.update_payment_schedule()
@@ -152,7 +151,6 @@
 		)
 		super(PaymentEntry, self).on_cancel()
 		self.make_gl_entries(cancel=1)
-		self.make_advance_gl_entries(cancel=1)
 		self.update_outstanding_amounts()
 		self.update_advance_paid()
 		self.delink_advance_entry_references()
@@ -1060,6 +1058,8 @@
 		else:
 			self.make_exchange_gain_loss_journal()
 
+		self.make_advance_gl_entries(cancel=cancel)
+
 	def add_party_gl_entries(self, gl_entries):
 		if self.party_account:
 			if self.payment_type == "Receive":
@@ -1128,7 +1128,7 @@
 		if self.book_advance_payments_in_separate_party_account:
 			gl_entries = []
 			for d in self.get("references"):
-				if d.reference_doctype in ("Sales Invoice", "Purchase Invoice"):
+				if d.reference_doctype in ("Sales Invoice", "Purchase Invoice", "Journal Entry"):
 					if not (against_voucher_type and against_voucher) or (
 						d.reference_doctype == against_voucher_type and d.reference_name == against_voucher
 					):
@@ -1164,6 +1164,13 @@
 			"voucher_detail_no": invoice.name,
 		}
 
+		posting_date = frappe.db.get_value(
+			invoice.reference_doctype, invoice.reference_name, "posting_date"
+		)
+
+		if getdate(posting_date) < getdate(self.posting_date):
+			posting_date = self.posting_date
+
 		dr_or_cr = "credit" if invoice.reference_doctype == "Sales Invoice" else "debit"
 		args_dict["account"] = invoice.account
 		args_dict[dr_or_cr] = invoice.allocated_amount
@@ -1172,6 +1179,7 @@
 			{
 				"against_voucher_type": invoice.reference_doctype,
 				"against_voucher": invoice.reference_name,
+				"posting_date": posting_date,
 			}
 		)
 		gle = self.get_gl_dict(
diff --git a/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.js b/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.js
index 7b7ce7a..d9f00be 100644
--- a/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.js
+++ b/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.js
@@ -24,7 +24,8 @@
 				filters: {
 					"company": this.frm.doc.company,
 					"is_group": 0,
-					"account_type": frappe.boot.party_account_types[this.frm.doc.party_type]
+					"account_type": frappe.boot.party_account_types[this.frm.doc.party_type],
+					"root_type": this.frm.doc.party_type == 'Customer' ? "Asset" : "Liability"
 				}
 			};
 		});
diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py
index bbfe3d2..6a80f20 100644
--- a/erpnext/accounts/utils.py
+++ b/erpnext/accounts/utils.py
@@ -491,14 +491,13 @@
 		gl_map = doc.build_gl_map()
 		create_payment_ledger_entry(gl_map, update_outstanding="No", cancel=0, adv_adj=1)
 
-		if voucher_type == "Payment Entry":
-			doc.make_advance_gl_entries()
-
 		# Only update outstanding for newly linked vouchers
 		for entry in entries:
 			update_voucher_outstanding(
 				entry.against_voucher_type, entry.against_voucher, entry.account, entry.party_type, entry.party
 			)
+			if voucher_type == "Payment Entry":
+				doc.make_advance_gl_entries(entry.against_voucher_type, entry.against_voucher)
 
 		frappe.flags.ignore_party_validation = False
 
diff --git a/erpnext/buying/doctype/supplier/supplier.js b/erpnext/buying/doctype/supplier/supplier.js
index 372ca56..08dc44c 100644
--- a/erpnext/buying/doctype/supplier/supplier.js
+++ b/erpnext/buying/doctype/supplier/supplier.js
@@ -12,6 +12,7 @@
 			return {
 				filters: {
 					'account_type': 'Payable',
+					'root_type': 'Liability',
 					'company': d.company,
 					"is_group": 0
 				}
diff --git a/erpnext/selling/doctype/customer/customer.js b/erpnext/selling/doctype/customer/customer.js
index 60f0941..e274a52 100644
--- a/erpnext/selling/doctype/customer/customer.js
+++ b/erpnext/selling/doctype/customer/customer.js
@@ -23,6 +23,7 @@
 			let d  = locals[cdt][cdn];
 			let filters = {
 				'account_type': 'Receivable',
+				'root_type': 'Asset',
 				'company': d.company,
 				"is_group": 0
 			};
diff --git a/erpnext/setup/doctype/company/company.js b/erpnext/setup/doctype/company/company.js
index fa207ec..4973dab 100644
--- a/erpnext/setup/doctype/company/company.js
+++ b/erpnext/setup/doctype/company/company.js
@@ -200,8 +200,8 @@
 	$.each([
 		["default_bank_account", {"account_type": "Bank"}],
 		["default_cash_account", {"account_type": "Cash"}],
-		["default_receivable_account", {"account_type": "Receivable"}],
-		["default_payable_account", {"account_type": "Payable"}],
+		["default_receivable_account", { "root_type": "Asset", "account_type": "Receivable" }],
+		["default_payable_account", { "root_type": "Liability", "account_type": "Payable" }],
 		["default_expense_account", {"root_type": "Expense"}],
 		["default_income_account", {"root_type": "Income"}],
 		["round_off_account", {"root_type": "Expense"}],
diff --git a/erpnext/setup/doctype/customer_group/customer_group.js b/erpnext/setup/doctype/customer_group/customer_group.js
index 49a90f9..3c81b02 100644
--- a/erpnext/setup/doctype/customer_group/customer_group.js
+++ b/erpnext/setup/doctype/customer_group/customer_group.js
@@ -30,6 +30,7 @@
 		frm.set_query('account', 'accounts', function (doc, cdt, cdn) {
 			return {
 				filters: {
+					'root_type': 'Asset',
 					"account_type": 'Receivable',
 					"company": locals[cdt][cdn].company,
 					"is_group": 0
diff --git a/erpnext/setup/doctype/supplier_group/supplier_group.js b/erpnext/setup/doctype/supplier_group/supplier_group.js
index b2acfd7..3362929 100644
--- a/erpnext/setup/doctype/supplier_group/supplier_group.js
+++ b/erpnext/setup/doctype/supplier_group/supplier_group.js
@@ -30,6 +30,7 @@
 		frm.set_query('account', 'accounts', function (doc, cdt, cdn) {
 			return {
 				filters: {
+					'root_type': 'Liability',
 					'account_type': 'Payable',
 					'company': locals[cdt][cdn].company,
 					"is_group": 0