Merge pull request #18757 from deepeshgarg007/accounting_dimension_fix

fix: Default dimensions in child doctypes
diff --git a/erpnext/accounts/doctype/accounting_dimension/accounting_dimension.js b/erpnext/accounts/doctype/accounting_dimension/accounting_dimension.js
index 88b11dd..5cea0d1 100644
--- a/erpnext/accounts/doctype/accounting_dimension/accounting_dimension.js
+++ b/erpnext/accounts/doctype/accounting_dimension/accounting_dimension.js
@@ -40,16 +40,9 @@
 	},
 
 	document_type: function(frm) {
-
 		frm.set_value('label', frm.doc.document_type);
 		frm.set_value('fieldname', frappe.model.scrub(frm.doc.document_type));
 
-		if (frm.is_new()){
-			let row = frappe.model.add_child(frm.doc, "Accounting Dimension Detail", "dimension_defaults");
-			row.reference_document = frm.doc.document_type;
-			frm.refresh_fields("dimension_defaults");
-		}
-
 		frappe.db.get_value('Accounting Dimension', {'document_type': frm.doc.document_type}, 'document_type', (r) => {
 			if (r && r.document_type) {
 				frm.set_df_property('document_type', 'description', "Document type is already set as dimension");
diff --git a/erpnext/accounts/doctype/accounting_dimension_detail/accounting_dimension_detail.json b/erpnext/accounts/doctype/accounting_dimension_detail/accounting_dimension_detail.json
index 1ccef6c..e9e1f43 100644
--- a/erpnext/accounts/doctype/accounting_dimension_detail/accounting_dimension_detail.json
+++ b/erpnext/accounts/doctype/accounting_dimension_detail/accounting_dimension_detail.json
@@ -17,8 +17,7 @@
    "fieldtype": "Link",
    "in_list_view": 1,
    "label": "Company",
-   "options": "Company",
-   "reqd": 1
+   "options": "Company"
   },
   {
    "fieldname": "reference_document",
@@ -34,8 +33,7 @@
    "fieldtype": "Dynamic Link",
    "in_list_view": 1,
    "label": "Default Dimension",
-   "options": "reference_document",
-   "reqd": 1
+   "options": "reference_document"
   },
   {
    "columns": 3,
@@ -55,7 +53,7 @@
   }
  ],
  "istable": 1,
- "modified": "2019-07-17 23:34:33.026883",
+ "modified": "2019-08-15 11:59:09.389891",
  "modified_by": "Administrator",
  "module": "Accounts",
  "name": "Accounting Dimension Detail",
diff --git a/erpnext/public/js/utils/dimension_tree_filter.js b/erpnext/public/js/utils/dimension_tree_filter.js
index f1c9209..e6c18a1 100644
--- a/erpnext/public/js/utils/dimension_tree_filter.js
+++ b/erpnext/public/js/utils/dimension_tree_filter.js
@@ -1,12 +1,13 @@
 frappe.provide('frappe.ui.form');
 
 erpnext.doctypes_with_dimensions = ["GL Entry", "Sales Invoice", "Purchase Invoice", "Payment Entry", "Asset",
-	"Expense Claim", "Stock Entry", "Budget", "Payroll Entry", "Delivery Note", "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", "Sales Taxes and Charges", "Purchase Taxes and Charges", "Shipping Rule",
-	"Landed Cost Item", "Asset Value Adjustment", "Loyalty Program", "Fee Schedule", "Fee Structure", "Stock Reconciliation",
-	"Travel Request", "Fees", "POS Profile", "Opening Invoice Creation Tool", "Opening Invoice Creation Tool Item", "Subscription",
-	"Subscription Plan"];
+	"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", "Landed Cost Item", "Asset"];
+
+erpnext.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"];
 
 frappe.call({
 	method: "erpnext.accounts.doctype.accounting_dimension.accounting_dimension.get_dimension_filters",
@@ -26,21 +27,40 @@
 							"is_group": 0
 						});
 					}
-					if (frm.is_new() && frappe.meta.has_field(doctype, 'company') && frm.doc.company) {
-						frm.set_value(dimension['fieldname'], erpnext.default_dimensions[frm.doc.company][dimension['document_type']]);
+
+					if (Object.keys(erpnext.default_dimensions).length > 0) {
+						if (frappe.meta.has_field(doctype, dimension['fieldname'])) {
+							if (frm.is_new() && frappe.meta.has_field(doctype, 'company') && frm.doc.company) {
+								frm.set_value(dimension['fieldname'], erpnext.default_dimensions[frm.doc.company][dimension['document_type']]);
+							}
+						}
+
+						if (frm.doc.items && frm.doc.items.length) {
+							frm.doc.items[0][dimension['fieldname']] = erpnext.default_dimensions[frm.doc.company][dimension['document_type']];
+						}
+
+						if (frm.doc.accounts && frm.doc.accounts.length) {
+							frm.doc.accounts[0][dimension['fieldname']] = erpnext.default_dimensions[frm.doc.company][dimension['document_type']];
+						}
 					}
 				});
 			});
 		},
 
 		company: function(frm) {
-			if(frm.doc.company) {
+			if(frm.doc.company && (Object.keys(erpnext.default_dimensions).length > 0)) {
 				erpnext.dimension_filters.forEach((dimension) => {
-					frm.set_value(dimension['fieldname'], erpnext.default_dimensions[frm.doc.company][dimension['document_type']]);
+					if (frappe.meta.has_field(doctype, dimension['fieldname'])) {
+						frm.set_value(dimension['fieldname'], erpnext.default_dimensions[frm.doc.company][dimension['document_type']]);
+					}
 				});
 			}
 		},
+	});
+});
 
+erpnext.child_docs.forEach((doctype) => {
+	frappe.ui.form.on(doctype, {
 		items_add: function(frm, cdt, cdn) {
 			erpnext.dimension_filters.forEach((dimension) => {
 				var row = frappe.get_doc(cdt, cdn);