fix: Invalid reference doctypes for accounting dimensions (#19027)

* fix: Invalid reference doctypes for accounting dimensions

* fix: Add server side validation for accounting doctypes

* fix: set fieldname and label before insert
diff --git a/erpnext/accounts/doctype/accounting_dimension/accounting_dimension.js b/erpnext/accounts/doctype/accounting_dimension/accounting_dimension.js
index 88b11dd..a36f421 100644
--- a/erpnext/accounts/doctype/accounting_dimension/accounting_dimension.js
+++ b/erpnext/accounts/doctype/accounting_dimension/accounting_dimension.js
@@ -5,9 +5,13 @@
 
 	refresh: function(frm) {
 		frm.set_query('document_type', () => {
+			let invalid_doctypes = frappe.model.core_doctypes_list;
+			invalid_doctypes.push('Accounting Dimension', 'Project',
+				'Cost Center', 'Accounting Dimension Detail');
+
 			return {
 				filters: {
-					name: ['not in', ['Accounting Dimension', 'Project', 'Cost Center', 'Accounting Dimension Detail']]
+					name: ['not in', invalid_doctypes]
 				}
 			};
 		});
diff --git a/erpnext/accounts/doctype/accounting_dimension/accounting_dimension.py b/erpnext/accounts/doctype/accounting_dimension/accounting_dimension.py
index 1f418de..59d7557 100644
--- a/erpnext/accounts/doctype/accounting_dimension/accounting_dimension.py
+++ b/erpnext/accounts/doctype/accounting_dimension/accounting_dimension.py
@@ -11,10 +11,20 @@
 from frappe import scrub
 from frappe.utils import cstr
 from frappe.utils.background_jobs import enqueue
+from frappe.model import core_doctypes_list
 
 class AccountingDimension(Document):
 	def before_insert(self):
 		self.set_fieldname_and_label()
+
+	def validate(self):
+		if self.document_type in core_doctypes_list + ('Accounting Dimension', 'Project',
+				'Cost Center', 'Accounting Dimension Detail') :
+
+			msg = _("Not allowed to create accounting dimension for {0}").format(self.document_type)
+			frappe.throw(msg)
+
+	def after_insert(self):
 		if frappe.flags.in_test:
 			make_dimension_in_accounting_doctypes(doc=self)
 		else: