Merge branch 'develop' into tax_fetch_quote
diff --git a/erpnext/accounts/doctype/tax_rule/test_tax_rule.py b/erpnext/accounts/doctype/tax_rule/test_tax_rule.py
index bbbcc7f..632e30d 100644
--- a/erpnext/accounts/doctype/tax_rule/test_tax_rule.py
+++ b/erpnext/accounts/doctype/tax_rule/test_tax_rule.py
@@ -6,6 +6,8 @@
 import frappe
 import unittest
 from erpnext.accounts.doctype.tax_rule.tax_rule import IncorrectCustomerGroup, IncorrectSupplierType, ConflictingTaxRule, get_tax_template
+from erpnext.crm.doctype.opportunity.test_opportunity import make_opportunity
+from erpnext.crm.doctype.opportunity.opportunity import make_quotation
 
 test_records = frappe.get_test_records('Tax Rule')
 
@@ -144,6 +146,23 @@
 		self.assertEqual(get_tax_template("2015-01-01", {"customer":"_Test Customer", "billing_city": "Test City 1"}),
 			"_Test Sales Taxes and Charges Template 1 - _TC")
 
+	def test_taxes_fetch_via_tax_rule(self):
+		make_tax_rule(customer= "_Test Customer", billing_city = "_Test City",
+			sales_tax_template = "_Test Sales Taxes and Charges Template - _TC", save=1)
+
+		# create opportunity for customer
+		opportunity = make_opportunity(with_items=1)
+
+		# make quotation from opportunity
+		quotation = make_quotation(opportunity.name)
+		quotation.save()
+
+		self.assertEqual(quotation.taxes_and_charges, "_Test Sales Taxes and Charges Template - _TC")
+
+		# Check if accounts heads and rate fetched are also fetched from tax template or not
+		self.assertTrue(len(quotation.taxes) > 0)
+
+
 
 def make_tax_rule(**args):
 	args = frappe._dict(args)
diff --git a/erpnext/controllers/selling_controller.py b/erpnext/controllers/selling_controller.py
index 7f7aae3..0ae1759 100644
--- a/erpnext/controllers/selling_controller.py
+++ b/erpnext/controllers/selling_controller.py
@@ -10,6 +10,7 @@
 from erpnext.stock.get_item_details import get_conversion_factor
 from erpnext.stock.doctype.item.item import set_item_default
 from frappe.contacts.doctype.address.address import get_address_display
+from erpnext.controllers.accounts_controller import get_taxes_and_charges
 
 from erpnext.controllers.stock_controller import StockController
 
@@ -53,10 +54,10 @@
 		super(SellingController, self).set_missing_values(for_validate)
 
 		# set contact and address details for customer, if they are not mentioned
-		self.set_missing_lead_customer_details()
+		self.set_missing_lead_customer_details(for_validate=for_validate)
 		self.set_price_list_and_item_details(for_validate=for_validate)
 
-	def set_missing_lead_customer_details(self):
+	def set_missing_lead_customer_details(self, for_validate=False):
 		customer, lead = None, None
 		if getattr(self, "customer", None):
 			customer = self.customer
@@ -94,6 +95,11 @@
 				posting_date=self.get('transaction_date') or self.get('posting_date'),
 				company=self.company))
 
+		if self.get('taxes_and_charges') and not self.get('taxes') and not for_validate:
+			taxes = get_taxes_and_charges('Sales Taxes and Charges Template', self.taxes_and_charges)
+			for tax in taxes:
+				self.append('taxes', tax)
+
 	def set_price_list_and_item_details(self, for_validate=False):
 		self.set_price_list_currency("Selling")
 		self.set_missing_item_details(for_validate=for_validate)
diff --git a/erpnext/selling/doctype/quotation/test_quotation.py b/erpnext/selling/doctype/quotation/test_quotation.py
index b4c3d79..f0143f3 100644
--- a/erpnext/selling/doctype/quotation/test_quotation.py
+++ b/erpnext/selling/doctype/quotation/test_quotation.py
@@ -108,6 +108,10 @@
 		sales_order.transaction_date = nowdate()
 		sales_order.insert()
 
+		# Remove any unknown taxes if applied
+		sales_order.set('taxes', [])
+		sales_order.save()
+
 		self.assertEqual(sales_order.payment_schedule[0].payment_amount, 8906.00)
 		self.assertEqual(sales_order.payment_schedule[0].due_date, getdate(quotation.transaction_date))
 		self.assertEqual(sales_order.payment_schedule[1].payment_amount, 8906.00)
diff --git a/erpnext/selling/doctype/sales_order/test_sales_order.py b/erpnext/selling/doctype/sales_order/test_sales_order.py
index 735b071..5aef096 100644
--- a/erpnext/selling/doctype/sales_order/test_sales_order.py
+++ b/erpnext/selling/doctype/sales_order/test_sales_order.py
@@ -89,6 +89,8 @@
 		self.assertEqual(len(si.get("items")), 1)
 
 		si.insert()
+		si.set('taxes', [])
+		si.save()
 
 		self.assertEqual(si.payment_schedule[0].payment_amount, 500.0)
 		self.assertEqual(si.payment_schedule[0].due_date, so.transaction_date)