Pass buying or selling as filter parameters

Wherever get_exchange_rate is called, pass filter buying or selling on the basis of doctype.
diff --git a/erpnext/accounts/doctype/sales_invoice/pos.py b/erpnext/accounts/doctype/sales_invoice/pos.py
index be6078a..4789464 100644
--- a/erpnext/accounts/doctype/sales_invoice/pos.py
+++ b/erpnext/accounts/doctype/sales_invoice/pos.py
@@ -97,7 +97,7 @@
 	doc.conversion_rate = 1.0
 
 	if doc.currency != company_data.default_currency:
-		doc.conversion_rate = get_exchange_rate(doc.currency, company_data.default_currency, doc.posting_date)
+		doc.conversion_rate = get_exchange_rate(doc.currency, company_data.default_currency, doc.posting_date, args="for_selling")
 
 	doc.selling_price_list = pos_profile.get('selling_price_list') or \
 		frappe.db.get_value('Selling Settings', None, 'selling_price_list')
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index 050a143..0c8e485 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -148,8 +148,13 @@
 
 		if self.meta.get_field("currency"):
 			# price list part
-			fieldname = "selling_price_list" if buying_or_selling.lower() == "selling" \
-				else "buying_price_list"
+			if buying_or_selling.lower() == "selling":
+				fieldname = "selling_price_list"
+				args = "for_selling"
+			else:
+				fieldname = "buying_price_list"
+				args = "for_buying"
+
 			if self.meta.get_field(fieldname) and self.get(fieldname):
 				self.price_list_currency = frappe.db.get_value("Price List",
 					self.get(fieldname), "currency")
@@ -159,7 +164,7 @@
 
 				elif not self.plc_conversion_rate:
 					self.plc_conversion_rate = get_exchange_rate(self.price_list_currency,
-						self.company_currency, transaction_date)
+						self.company_currency, transaction_date, args)
 
 			# currency
 			if not self.currency:
@@ -169,7 +174,7 @@
 				self.conversion_rate = 1.0
 			elif not self.conversion_rate:
 				self.conversion_rate = get_exchange_rate(self.currency,
-					self.company_currency, transaction_date)
+					self.company_currency, transaction_date, args)
 
 	def set_missing_item_details(self, for_validate=False):
 		"""set missing item values"""
diff --git a/erpnext/crm/doctype/opportunity/opportunity.py b/erpnext/crm/doctype/opportunity/opportunity.py
index fc886ac..5c203d9 100644
--- a/erpnext/crm/doctype/opportunity/opportunity.py
+++ b/erpnext/crm/doctype/opportunity/opportunity.py
@@ -227,7 +227,7 @@
 			exchange_rate = 1
 		else:
 			exchange_rate = get_exchange_rate(quotation.currency, company_currency,
-				quotation.transaction_date)
+				quotation.transaction_date, args="for_selling")
 
 		quotation.conversion_rate = exchange_rate
 
diff --git a/erpnext/demo/user/purchase.py b/erpnext/demo/user/purchase.py
index d9fc1f3..327f617 100644
--- a/erpnext/demo/user/purchase.py
+++ b/erpnext/demo/user/purchase.py
@@ -56,7 +56,7 @@
 	if company_currency == party_account_currency:
 		exchange_rate = 1
 	else:
-		exchange_rate = get_exchange_rate(party_account_currency, company_currency)
+		exchange_rate = get_exchange_rate(party_account_currency, company_currency, args="for_buying")
 
 	# make supplier quotations
 	if random.random() < 0.2:
diff --git a/erpnext/demo/user/sales.py b/erpnext/demo/user/sales.py
index 2fd2565..02e1d42 100644
--- a/erpnext/demo/user/sales.py
+++ b/erpnext/demo/user/sales.py
@@ -89,7 +89,7 @@
 		if company_currency == party_account_currency:
 			exchange_rate = 1
 		else:
-			exchange_rate = get_exchange_rate(party_account_currency, company_currency)
+			exchange_rate = get_exchange_rate(party_account_currency, company_currency, args="for_selling")
 
 		qtn = frappe.get_doc({
 			"creation": frappe.flags.current_date,
diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py
index 73c8ca0..1fbc806 100644
--- a/erpnext/manufacturing/doctype/bom/bom.py
+++ b/erpnext/manufacturing/doctype/bom/bom.py
@@ -303,7 +303,7 @@
 		if self.currency == self.company_currency():
 			self.conversion_rate = 1
 		elif self.conversion_rate == 1 or flt(self.conversion_rate) <= 0:
-			self.conversion_rate = get_exchange_rate(self.currency, self.company_currency())
+			self.conversion_rate = get_exchange_rate(self.currency, self.company_currency(), args="for_buying")
 
 	def validate_materials(self):
 		""" Validate raw material entries """
diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js
index 7c1a42a..ab7cee6 100644
--- a/erpnext/public/js/controllers/transaction.js
+++ b/erpnext/public/js/controllers/transaction.js
@@ -667,9 +667,14 @@
 	},
 
 	get_exchange_rate: function(transaction_date, from_currency, to_currency, callback) {
-		if (this.frm.doctype == "Purchase Order") {
-			var args = "for_buying";
+		var args;
+		if (["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"].includes(this.frm.doctype)) {
+			args = "for_selling";
 		}
+		else if (["Purchase Order", "Purchase Receipt", "Purchase Invoice"].includes(this.frm.doctype)) {
+			args = "for_buying";
+		}
+
 		if (!transaction_date || !from_currency || !to_currency) return;
 		return frappe.call({
 			method: "erpnext.setup.utils.get_exchange_rate",
diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py
index b36cd39..31ee534 100644
--- a/erpnext/stock/get_item_details.py
+++ b/erpnext/stock/get_item_details.py
@@ -628,6 +628,11 @@
 	if not args.price_list:
 		return {}
 
+	if args.doctype in ['Quotation', 'Sales Order', 'Delivery Note', 'Sales Invoice']:
+		args.update({"exchange_rate": "for_selling"})
+	elif args.doctype in ['Purchase Order', 'Purchase Receipt', 'Purchase Invoice']:
+		args.update({"exchange_rate": "for_buying"})
+
 	price_list_currency = get_price_list_currency(args.price_list)
 	price_list_uom_dependant = get_price_list_uom_dependant(args.price_list)
 	plc_conversion_rate = args.plc_conversion_rate
@@ -637,7 +642,7 @@
 		and price_list_currency != args.price_list_currency):
 			# cksgb 19/09/2016: added args.transaction_date as posting_date argument for get_exchange_rate
 			plc_conversion_rate = get_exchange_rate(price_list_currency, company_currency,
-				args.transaction_date) or plc_conversion_rate
+				args.transaction_date, args.exchange_rate) or plc_conversion_rate
 
 	return frappe._dict({
 		"price_list_currency": price_list_currency,