Merge branch 'develop' into billing-contact
diff --git a/erpnext/accounts/party.py b/erpnext/accounts/party.py
index 8567740..98b6d79 100644
--- a/erpnext/accounts/party.py
+++ b/erpnext/accounts/party.py
@@ -46,7 +46,7 @@
 	currency = party.default_currency if party.get("default_currency") else get_company_currency(company)
 
 	party_address, shipping_address = set_address_details(party_details, party, party_type, doctype, company, party_address, company_address, shipping_address)
-	set_contact_details(party_details, party, party_type)
+	set_contact_details(party_details, party, party_type, doctype)
 	set_other_values(party_details, party, party_type)
 	set_price_list(party_details, party, party_type, price_list, pos_profile)
 
@@ -115,8 +115,11 @@
 def get_regional_address_details(party_details, doctype, company):
 	pass
 
-def set_contact_details(party_details, party, party_type):
-	party_details.contact_person = get_default_contact(party_type, party.name)
+def set_contact_details(party_details, party, party_type, doctype=None):
+	if doctype == 'Sales Invoice':
+		party_details.contact_person = get_default_billing_contact(doctype, party.name)
+	else:
+		party_details.contact_person = get_default_contact(party_type, party.name)
 
 	if not party_details.contact_person:
 		party_details.update({
@@ -613,3 +616,26 @@
 
 	if data:
 		return frappe._dict(data)
+
+def get_default_billing_contact(doctype, name):
+	""" 
+		Returns default contact for the given doctype and name.
+		Can be ordered by `contact_type` to either is_primary_contact or is_billing_contact.
+	"""
+	out = frappe.db.sql("""
+			SELECT dl.parent, c.is_primary_contact, c.is_billing_contact
+			FROM `tabDynamic Link` dl
+			INNER JOIN tabContact c ON c.name = dl.parent
+			WHERE 
+				dl.link_doctype=%s AND
+				dl.link_name=%s AND
+				dl.parenttype = "Contact"
+			ORDER BY is_billing_contact DESC, is_primary_contact DESC
+		""", (doctype, name))
+	if out:
+		try:
+			return out[0][0]
+		except:
+			return None
+	else:
+		return None
\ No newline at end of file
diff --git a/erpnext/erpnext_integrations/custom/contact.json b/erpnext/erpnext_integrations/custom/contact.json
new file mode 100644
index 0000000..98a4bbc
--- /dev/null
+++ b/erpnext/erpnext_integrations/custom/contact.json
@@ -0,0 +1,60 @@
+{
+ "custom_fields": [
+  {
+   "_assign": null,
+   "_comments": null,
+   "_liked_by": null,
+   "_user_tags": null,
+   "allow_on_submit": 0,
+   "bold": 0,
+   "collapsible": 0,
+   "collapsible_depends_on": null,
+   "columns": 0,
+   "creation": "2019-12-02 11:00:03.432994",
+   "default": null,
+   "depends_on": null,
+   "description": null,
+   "docstatus": 0,
+   "dt": "Contact",
+   "fetch_from": null,
+   "fetch_if_empty": 0,
+   "fieldname": "is_billing_contact",
+   "fieldtype": "Check",
+   "hidden": 0,
+   "idx": 27,
+   "ignore_user_permissions": 0,
+   "ignore_xss_filter": 0,
+   "in_global_search": 0,
+   "in_list_view": 0,
+   "in_standard_filter": 0,
+   "insert_after": "is_primary_contact",
+   "label": "Is Billing Contact",
+   "length": 0,
+   "modified": "2019-12-02 11:00:03.432994",
+   "modified_by": "Administrator",
+   "name": "Contact-is_billing_contact",
+   "no_copy": 0,
+   "options": null,
+   "owner": "Administrator",
+   "parent": null,
+   "parentfield": null,
+   "parenttype": null,
+   "permlevel": 0,
+   "precision": "",
+   "print_hide": 0,
+   "print_hide_if_no_value": 0,
+   "print_width": null,
+   "read_only": 0,
+   "report_hide": 0,
+   "reqd": 0,
+   "search_index": 0,
+   "translatable": 0,
+   "unique": 0,
+   "width": null
+  }
+ ],
+ "custom_perms": [],
+ "doctype": "Contact",
+ "property_setters": [],
+ "sync_on_migrate": 1
+}
\ No newline at end of file