Error in the Address fetched in Sales Order.(#11129) (#11145)

* add new function - `get_party_shipping_address`

* `swap `get_default_address` with `get_party_shipping_address`

* test cases

* properly sets order by direction

* move `get_party_shipping_address` to party.py

* fix test module import
diff --git a/erpnext/accounts/party.py b/erpnext/accounts/party.py
index bcec0a2..ce049f5 100644
--- a/erpnext/accounts/party.py
+++ b/erpnext/accounts/party.py
@@ -76,7 +76,7 @@
 
 	# shipping address
 	if party_type in ["Customer", "Lead"]:
-		out.shipping_address_name = get_default_address(party_type, party.name, 'is_shipping_address')
+		out.shipping_address_name = get_party_shipping_address(party_type, party.name)
 		out.shipping_address = get_address_display(out["shipping_address_name"])
 		if doctype:
 			out.update(get_fetch_values(doctype, 'shipping_address_name', out.shipping_address_name))
@@ -418,3 +418,32 @@
 		info["total_unpaid"] = -1 * info["total_unpaid"]
 
 	return info
+
+
+def get_party_shipping_address(doctype, name):
+	"""
+	Returns an Address name (best guess) for the given doctype and name for which `address_type == 'Shipping'` is true.
+	and/or `is_shipping_address = 1`.
+
+	It returns an empty string if there is no matching record.
+
+	:param doctype: Party Doctype
+	:param name: Party name
+	:return: String
+	"""
+	out = frappe.db.sql(
+		'SELECT dl.parent '
+		'from `tabDynamic Link` dl join `tabAddress` ta on dl.parent=ta.name '
+		'where '
+		'dl.link_doctype=%s '
+		'and dl.link_name=%s '
+		'and dl.parenttype="Address" '
+		'and '
+		'(ta.address_type="Shipping" or ta.is_shipping_address=1) '
+		'order by ta.is_shipping_address desc, ta.address_type desc limit 1',
+		(doctype, name)
+	)
+	if out:
+		return out[0][0]
+	else:
+		return ''
diff --git a/erpnext/accounts/test/__init__.py b/erpnext/accounts/test/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/accounts/test/__init__.py
diff --git a/erpnext/accounts/test/test_utils.py b/erpnext/accounts/test/test_utils.py
new file mode 100644
index 0000000..0fca470
--- /dev/null
+++ b/erpnext/accounts/test/test_utils.py
@@ -0,0 +1,84 @@
+import unittest
+from erpnext.accounts.party import get_party_shipping_address
+from frappe.test_runner import make_test_objects
+
+
+class TestUtils(unittest.TestCase):
+	@classmethod
+	def setUpClass(cls):
+		super(TestUtils, cls).setUpClass()
+		make_test_objects('Address', ADDRESS_RECORDS)
+
+	def test_get_party_shipping_address(self):
+		address = get_party_shipping_address('Customer', '_Test Customer 1')
+		self.assertEqual(address, '_Test Billing Address 2 Title-Billing')
+
+	def test_get_party_shipping_address2(self):
+		address = get_party_shipping_address('Customer', '_Test Customer 2')
+		self.assertEqual(address, '_Test Shipping Address 2 Title-Shipping')
+
+
+ADDRESS_RECORDS = [
+	{
+		"doctype": "Address",
+		"address_type": "Billing",
+		"address_line1": "Address line 1",
+		"address_title": "_Test Billing Address Title",
+		"city": "Lagos",
+		"country": "Nigeria",
+		"links": [
+			{
+				"link_doctype": "Customer",
+				"link_name": "_Test Customer 2",
+				"doctype": "Dynamic Link"
+			}
+		]
+	},
+	{
+		"doctype": "Address",
+		"address_type": "Shipping",
+		"address_line1": "Address line 2",
+		"address_title": "_Test Shipping Address 1 Title",
+		"city": "Lagos",
+		"country": "Nigeria",
+		"links": [
+			{
+				"link_doctype": "Customer",
+				"link_name": "_Test Customer 2",
+				"doctype": "Dynamic Link"
+			}
+		]
+	},
+	{
+		"doctype": "Address",
+		"address_type": "Shipping",
+		"address_line1": "Address line 3",
+		"address_title": "_Test Shipping Address 2 Title",
+		"city": "Lagos",
+		"country": "Nigeria",
+		"is_shipping_address": "1",
+		"links": [
+			{
+				"link_doctype": "Customer",
+				"link_name": "_Test Customer 2",
+				"doctype": "Dynamic Link"
+			}
+		]
+	},
+	{
+		"doctype": "Address",
+		"address_type": "Billing",
+		"address_line1": "Address line 4",
+		"address_title": "_Test Billing Address 2 Title",
+		"city": "Lagos",
+		"country": "Nigeria",
+		"is_shipping_address": "1",
+		"links": [
+			{
+				"link_doctype": "Customer",
+				"link_name": "_Test Customer 1",
+				"doctype": "Dynamic Link"
+			}
+		]
+	}
+]