fix: Move filter setup to doctype controllers
diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js
index ec6b3dc..3f29378 100644
--- a/erpnext/public/js/controllers/transaction.js
+++ b/erpnext/public/js/controllers/transaction.js
@@ -1,6 +1,8 @@
 // Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
 // License: GNU General Public License v3. See license.txt
 
+frappe.provide('erpnext.accounts.dimensions');
+
 erpnext.TransactionController = erpnext.taxes_and_totals.extend({
 	setup: function() {
 		this._super();
@@ -106,6 +108,8 @@
 				if(!item.warehouse && frm.doc.set_warehouse) {
 					item.warehouse = frm.doc.set_warehouse;
 				}
+
+				erpnext.accounts.dimensions.copy_dimension_from_first_row(frm, cdt, cdn, 'items');
 			}
 		});
 
diff --git a/erpnext/public/js/utils/dimension_tree_filter.js b/erpnext/public/js/utils/dimension_tree_filter.js
index 7a42fb5..c79736d 100644
--- a/erpnext/public/js/utils/dimension_tree_filter.js
+++ b/erpnext/public/js/utils/dimension_tree_filter.js
@@ -1,60 +1,79 @@
-frappe.provide('frappe.ui.form');
-let default_dimensions = {};
+frappe.provide('erpnext.accounts');
 
-let doctypes_with_dimensions = ["GL Entry", "Sales Invoice", "Purchase Invoice", "Payment Entry", "Asset",
-	"Expense Claim", "Stock Entry", "Budget", "Payroll Entry", "Delivery Note", "Shipping Rule", "Loyalty Program",
-	"Fee Schedule", "Fee Structure", "Stock Reconciliation", "Travel Request", "Fees", "POS Profile", "Opening Invoice Creation Tool",
-	"Subscription", "Purchase Order", "Journal Entry", "Material Request", "Purchase Receipt", "Asset", "Asset Value Adjustment"];
-
-let child_docs = ["Sales Invoice Item", "Purchase Invoice Item", "Purchase Order Item", "Journal Entry Account",
-	"Material Request Item", "Delivery Note Item", "Purchase Receipt Item", "Stock Entry Detail", "Payment Entry Deduction",
-	"Landed Cost Item", "Asset Value Adjustment", "Opening Invoice Creation Tool Item", "Subscription Plan",
-	"Sales Taxes and Charges", "Purchase Taxes and Charges"];
-
-frappe.call({
-	method: "erpnext.accounts.doctype.accounting_dimension.accounting_dimension.get_dimension_filters",
-	args: {
-		'with_costcenter_and_project': true
+erpnext.accounts.dimensions = {
+	setup_dimension_filters(frm, doctype) {
+		this.accounting_dimensions = [];
+		this.default_dimensions = {};
+		this.fetch_custom_dimensions(frm, doctype);
 	},
-	callback: function(r) {
-		erpnext.dimension_filters = r.message[0];
-		default_dimensions = r.message[1];
-	}
-});
 
-doctypes_with_dimensions.forEach((doctype) => {
-	frappe.ui.form.on(doctype, {
-		onload: function(frm) {
-			erpnext.dimension_filters.forEach((dimension) => {
-				frappe.model.with_doctype(dimension['document_type'], () => {
-					let parent_fields = [];
-					frappe.meta.get_docfields(doctype).forEach((df) => {
-						if (df.fieldtype === 'Link' && df.options === 'Account') {
-							parent_fields.push(df.fieldname);
-						} else if (df.fieldtype === 'Table') {
-							setup_child_filters(frm, df.options, df.fieldname, dimension['fieldname']);
-						}
+	fetch_custom_dimensions(frm, doctype) {
+		let me = this;
+		frappe.call({
+			method: "erpnext.accounts.doctype.accounting_dimension.accounting_dimension.get_dimensions",
+			args: {
+				'with_cost_center_and_project': true
+			},
+			callback: function(r) {
+				me.accounting_dimensions = r.message[0];
+				me.default_dimensions = r.message[1];
+				me.setup_filters(frm, doctype);
+			}
+		});
+	},
 
-						setup_account_filters(frm, dimension['fieldname'], parent_fields);
-					});
+	setup_filters(frm, doctype) {
+		this.accounting_dimensions.forEach((dimension) => {
+			frappe.model.with_doctype(dimension['document_type'], () => {
+				let parent_fields = [];
+				frappe.meta.get_docfields(doctype).forEach((df) => {
+					if (df.fieldtype === 'Link' && df.options === 'Account') {
+						parent_fields.push(df.fieldname);
+					} else if (df.fieldtype === 'Table') {
+						this.setup_child_filters(frm, df.options, df.fieldname, dimension['fieldname']);
+					}
+
+					if (frappe.meta.has_field(doctype, dimension['fieldname'])) {
+						this.setup_account_filters(frm, dimension['fieldname'], parent_fields);
+					}
 				});
 			});
-		},
+		});
+	},
 
-		company: function(frm) {
-			if(frm.doc.company && (Object.keys(default_dimensions || {}).length > 0)
-				&& default_dimensions[frm.doc.company]) {
-				frm.trigger('update_dimension');
-			}
-		},
+	setup_child_filters(frm, doctype, parentfield, dimension) {
+		let fields = [];
 
-		update_dimension: function(frm) {
-			erpnext.dimension_filters.forEach((dimension) => {
+		if (frappe.meta.has_field(doctype, dimension)) {
+			frappe.model.with_doctype(doctype, () => {
+				frappe.meta.get_docfields(doctype).forEach((df) => {
+					if (df.fieldtype === 'Link' && df.options === 'Account') {
+						fields.push(df.fieldname);
+					}
+				});
+
+				frm.set_query(dimension, parentfield, function(doc, cdt, cdn) {
+					let row = locals[cdt][cdn];
+					return erpnext.queries.get_filtered_dimensions(row, fields, dimension, doc.company);
+				});
+			});
+		}
+	},
+
+	setup_account_filters(frm, dimension, fields) {
+		frm.set_query(dimension, function(doc) {
+			return erpnext.queries.get_filtered_dimensions(doc, fields, dimension, doc.company);
+		});
+	},
+
+	update_dimension(frm, doctype) {
+		if (this.accounting_dimensions) {
+			this.accounting_dimensions.forEach((dimension) => {
 				if(frm.is_new()) {
-					if(frm.doc.company && Object.keys(default_dimensions || {}).length > 0
-						&& default_dimensions[frm.doc.company]) {
+					if(frm.doc.company && Object.keys(this.default_dimensions || {}).length > 0
+						&& this.default_dimensions[frm.doc.company]) {
 
-						let default_dimension = default_dimensions[frm.doc.company][dimension['fieldname']];
+						let default_dimension = this.default_dimensions[frm.doc.company][dimension['fieldname']];
 
 						if(default_dimension) {
 							if (frappe.meta.has_field(doctype, dimension['fieldname'])) {
@@ -69,47 +88,14 @@
 				}
 			});
 		}
-	});
-});
+	},
 
-child_docs.forEach((doctype) => {
-	frappe.ui.form.on(doctype, {
-		items_add: function(frm, cdt, cdn) {
-			copy_dimension(frm, cdt, cdn, "items");
-		},
-
-		accounts_add: function(frm, cdt, cdn) {
-			copy_dimension(frm, cdt, cdn, "accounts");
+	copy_dimension_from_first_row(frm, cdt, cdn, fieldname) {
+		if (frappe.meta.has_field(frm.doctype, fieldname)) {
+			this.accounting_dimensions.forEach((dimension) => {
+				let row = frappe.get_doc(cdt, cdn);
+				frm.script_manager.copy_from_first_row(fieldname, row, [dimension['fieldname']]);
+			});
 		}
-	});
-});
-
-let copy_dimension = function(frm, cdt, cdn, fieldname) {
-	erpnext.dimension_filters.forEach((dimension) => {
-		let row = frappe.get_doc(cdt, cdn);
-		frm.script_manager.copy_from_first_row(fieldname, row, [dimension['fieldname']]);
-	});
-};
-
-let setup_child_filters = function(frm, doctype, parentfield, dimension) {
-	let fields = [];
-
-	frappe.model.with_doctype(doctype, () => {
-		frappe.meta.get_docfields(doctype).forEach((df) => {
-			if (df.fieldtype === 'Link' && df.options === 'Account') {
-				fields.push(df.fieldname);
-			}
-		});
-
-		frm.set_query(dimension, parentfield, function(doc, cdt, cdn) {
-			let row = locals[cdt][cdn];
-			return erpnext.queries.get_filtered_dimensions(row, fields, dimension, doc.company);
-		});
-	});
-};
-
-let setup_account_filters = function(frm, dimension, fields) {
-	frm.set_query(dimension, function(doc) {
-		return erpnext.queries.get_filtered_dimensions(doc, fields, dimension, doc.company);
-	});
-};
\ No newline at end of file
+	}
+}
\ No newline at end of file