Get default tax template only if tax template not selected or template is for other company (#12492)

diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index e176472..81d2786 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -54,6 +54,7 @@
 
 		if self.meta.get_field("taxes_and_charges"):
 			self.validate_enabled_taxes_and_charges()
+			self.validate_tax_account_company()
 
 		self.validate_party()
 		self.validate_currency()
@@ -255,6 +256,14 @@
 		if frappe.db.get_value(taxes_and_charges_doctype, self.taxes_and_charges, "disabled"):
 			frappe.throw(_("{0} '{1}' is disabled").format(taxes_and_charges_doctype, self.taxes_and_charges))
 
+	def validate_tax_account_company(self):
+		for d in self.get("taxes"):
+			if d.account_head:
+				tax_account_company = frappe.db.get_value("Account", d.account_head, "company")
+				if tax_account_company != self.company:
+					frappe.throw(_("Row #{0}: Account {1} does not belong to company {2}")
+						.format(d.idx, d.account_head, self.company))
+
 	def get_gl_dict(self, args, account_currency=None):
 		"""this method populates the common properties of a gl entry record"""
 
@@ -722,11 +731,15 @@
 	return frappe.db.get_value("Account", account_head, ["tax_rate", "account_name"], as_dict=True)
 
 @frappe.whitelist()
-def get_default_taxes_and_charges(master_doctype, company=None):
+def get_default_taxes_and_charges(master_doctype, tax_template=None, company=None):
 	if not company: return {}
 
-	default_tax = frappe.db.get_value(master_doctype,
-		{"is_default": 1, "company": company})
+	if tax_template and company:
+		tax_template_company = frappe.db.get_value(master_doctype, tax_template, "company")
+		if tax_template_company == company:
+			return
+
+	default_tax = frappe.db.get_value(master_doctype, {"is_default": 1, "company": company})
 
 	return {
 		'taxes_and_charges': default_tax,
diff --git a/erpnext/crm/doctype/opportunity/opportunity.py b/erpnext/crm/doctype/opportunity/opportunity.py
index 9b5d6a6..75f9dea 100644
--- a/erpnext/crm/doctype/opportunity/opportunity.py
+++ b/erpnext/crm/doctype/opportunity/opportunity.py
@@ -232,7 +232,7 @@
 		quotation.conversion_rate = exchange_rate
 
 		# get default taxes
-		taxes = get_default_taxes_and_charges("Sales Taxes and Charges Template", quotation.company)
+		taxes = get_default_taxes_and_charges("Sales Taxes and Charges Template", company=quotation.company)
 		if taxes.get('taxes'):
 			quotation.update(taxes)
 
diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js
index b7ad866..e198c7e 100644
--- a/erpnext/public/js/controllers/transaction.js
+++ b/erpnext/public/js/controllers/transaction.js
@@ -244,10 +244,11 @@
 				method: "erpnext.controllers.accounts_controller.get_default_taxes_and_charges",
 				args: {
 					"master_doctype": taxes_and_charges_field.options,
+					"tax_template": me.frm.doc.taxes_and_charges,
 					"company": me.frm.doc.company
 				},
 				callback: function(r) {
-					if(!r.exc) {
+					if(!r.exc && r.message) {
 						frappe.run_serially([
 							() => {
 								// directly set in doc, so as not to call triggers