Merge branch 'develop' into supplier-primary-address
diff --git a/erpnext/buying/doctype/supplier/supplier.js b/erpnext/buying/doctype/supplier/supplier.js
index 1766c2c..7ee9196 100644
--- a/erpnext/buying/doctype/supplier/supplier.js
+++ b/erpnext/buying/doctype/supplier/supplier.js
@@ -24,7 +24,26 @@
 				}
 			}
 		});
+
+		frm.set_query("supplier_primary_contact", function(doc) {
+			return {
+				query: "erpnext.buying.doctype.supplier.supplier.get_supplier_primary_contact",
+				filters: {
+					"supplier": doc.name
+				}
+			};
+		});
+
+		frm.set_query("supplier_primary_address", function(doc) {
+			return {
+				filters: {
+					"link_doctype": "Supplier",
+					"link_name": doc.name
+				}
+			};
+		});
 	},
+
 	refresh: function (frm) {
 		frappe.dynamic_link = { doc: frm.doc, fieldname: 'name', doctype: 'Supplier' }
 
@@ -78,6 +97,30 @@
 		});
 	},
 
+	supplier_primary_address: function(frm) {
+		if (frm.doc.supplier_primary_address) {
+			frappe.call({
+				method: 'frappe.contacts.doctype.address.address.get_address_display',
+				args: {
+					"address_dict": frm.doc.supplier_primary_address
+				},
+				callback: function(r) {
+					frm.set_value("primary_address", r.message);
+				}
+			});
+		}
+		if (!frm.doc.supplier_primary_address) {
+			frm.set_value("primary_address", "");
+		}
+	},
+
+	supplier_primary_contact: function(frm) {
+		if (!frm.doc.supplier_primary_contact) {
+			frm.set_value("mobile_no", "");
+			frm.set_value("email_id", "");
+		}
+	},
+
 	is_internal_supplier: function(frm) {
 		if (frm.doc.is_internal_supplier == 1) {
 			frm.toggle_reqd("represents_company", true);
diff --git a/erpnext/buying/doctype/supplier/supplier.json b/erpnext/buying/doctype/supplier/supplier.json
index 38b8dfd..c7a5db5 100644
--- a/erpnext/buying/doctype/supplier/supplier.json
+++ b/erpnext/buying/doctype/supplier/supplier.json
@@ -49,6 +49,13 @@
   "address_html",
   "column_break1",
   "contact_html",
+  "primary_address_and_contact_detail_section",
+  "supplier_primary_contact",
+  "mobile_no",
+  "email_id",
+  "column_break_44",
+  "supplier_primary_address",
+  "primary_address",
   "default_payable_accounts",
   "accounts",
   "default_tax_withholding_config",
@@ -378,6 +385,47 @@
    "fieldname": "allow_purchase_invoice_creation_without_purchase_receipt",
    "fieldtype": "Check",
    "label": "Allow Purchase Invoice Creation Without Purchase Receipt"
+  },
+  {
+   "fieldname": "primary_address_and_contact_detail_section",
+   "fieldtype": "Section Break",
+   "label": "Primary Address and Contact Detail"
+  },
+  {
+   "description": "Reselect, if the chosen contact is edited after save",
+   "fieldname": "supplier_primary_contact",
+   "fieldtype": "Link",
+   "label": "Supplier Primary Contact",
+   "options": "Contact"
+  },
+  {
+   "fetch_from": "supplier_primary_contact.mobile_no",
+   "fieldname": "mobile_no",
+   "fieldtype": "Read Only",
+   "label": "Mobile No"
+  },
+  {
+   "fetch_from": "supplier_primary_contact.email_id",
+   "fieldname": "email_id",
+   "fieldtype": "Read Only",
+   "label": "Email Id"
+  },
+  {
+   "fieldname": "column_break_44",
+   "fieldtype": "Column Break"
+  },
+  {
+   "fieldname": "primary_address",
+   "fieldtype": "Text",
+   "label": "Primary Address",
+   "read_only": 1
+  },
+  {
+   "description": "Reselect, if the chosen address is edited after save",
+   "fieldname": "supplier_primary_address",
+   "fieldtype": "Link",
+   "label": "Supplier Primary Address",
+   "options": "Address"
   }
  ],
  "icon": "fa fa-user",
@@ -390,7 +438,7 @@
    "link_fieldname": "supplier"
   }
  ],
- "modified": "2021-05-18 15:10:11.087191",
+ "modified": "2021-08-27 18:02:44.314077",
  "modified_by": "Administrator",
  "module": "Buying",
  "name": "Supplier",
diff --git a/erpnext/buying/doctype/supplier/supplier.py b/erpnext/buying/doctype/supplier/supplier.py
index fd16b23..c9750ca 100644
--- a/erpnext/buying/doctype/supplier/supplier.py
+++ b/erpnext/buying/doctype/supplier/supplier.py
@@ -42,7 +42,12 @@
 		if not self.naming_series:
 			self.naming_series = ''
 
+		self.create_primary_contact()
+		self.create_primary_address()
+
 	def validate(self):
+		self.flags.is_new_doc = self.is_new()
+
 		# validation for Naming Series mandatory field...
 		if frappe.defaults.get_global_default('supp_master_name') == 'Naming Series':
 			if not self.naming_series:
@@ -76,7 +81,39 @@
 			frappe.throw(_("Internal Supplier for company {0} already exists").format(
 				frappe.bold(self.represents_company)))
 
+	def create_primary_contact(self):
+		from erpnext.selling.doctype.customer.customer import make_contact
+
+		if not self.supplier_primary_contact:
+			if self.mobile_no or self.email_id:
+				contact = make_contact(self)
+				self.db_set('supplier_primary_contact', contact.name)
+				self.db_set('mobile_no', self.mobile_no)
+				self.db_set('email_id', self.email_id)
+
+	def create_primary_address(self):
+		from erpnext.selling.doctype.customer.customer import make_address
+		from frappe.contacts.doctype.address.address import get_address_display
+
+		if self.flags.is_new_doc and self.get('address_line1'):
+			address = make_address(self)
+			address_display = get_address_display(address.name)
+
+			self.db_set("supplier_primary_address", address.name)
+			self.db_set("primary_address", address_display)
+
 	def on_trash(self):
+		if self.supplier_primary_contact:
+			frappe.db.sql("""
+				UPDATE `tabSupplier`
+				SET
+					supplier_primary_contact=null,
+					supplier_primary_address=null,
+					mobile_no=null,
+					email_id=null,
+					primary_address=null
+				WHERE name=%(name)s""", {"name": self.name})
+
 		delete_contact_and_address('Supplier', self.name)
 
 	def after_rename(self, olddn, newdn, merge=False):
@@ -104,3 +141,21 @@
 							doc.name, args.get('supplier_email_' + str(i)))
 				except frappe.NameError:
 					pass
+
+@frappe.whitelist()
+@frappe.validate_and_sanitize_search_inputs
+def get_supplier_primary_contact(doctype, txt, searchfield, start, page_len, filters):
+	supplier = filters.get("supplier")
+	return frappe.db.sql("""
+		SELECT
+			`tabContact`.name from `tabContact`,
+			`tabDynamic Link`
+		WHERE
+			`tabContact`.name = `tabDynamic Link`.parent
+			and `tabDynamic Link`.link_name = %(supplier)s
+			and `tabDynamic Link`.link_doctype = 'Supplier'
+			and `tabContact`.name like %(txt)s
+		""", {
+			'supplier': supplier,
+			'txt': '%%%s%%' % txt
+		})
diff --git a/erpnext/public/build.json b/erpnext/public/build.json
index 3c60e3e..6b70dab 100644
--- a/erpnext/public/build.json
+++ b/erpnext/public/build.json
@@ -38,6 +38,7 @@
 		"public/js/templates/item_quick_entry.html",
 		"public/js/utils/item_quick_entry.js",
 		"public/js/utils/customer_quick_entry.js",
+		"public/js/utils/supplier_quick_entry.js",
 		"public/js/education/student_button.html",
 		"public/js/education/assessment_result_tool.html",
 		"public/js/hub/hub_factory.js",
diff --git a/erpnext/public/js/erpnext.bundle.js b/erpnext/public/js/erpnext.bundle.js
index 9f7f29a..febdb24 100644
--- a/erpnext/public/js/erpnext.bundle.js
+++ b/erpnext/public/js/erpnext.bundle.js
@@ -15,6 +15,7 @@
 import "./templates/item_quick_entry.html";
 import "./utils/item_quick_entry";
 import "./utils/customer_quick_entry";
+import "./utils/supplier_quick_entry";
 import "./education/student_button.html";
 import "./education/assessment_result_tool.html";
 import "./hub/hub_factory";
diff --git a/erpnext/public/js/utils/supplier_quick_entry.js b/erpnext/public/js/utils/supplier_quick_entry.js
new file mode 100644
index 0000000..8d591a9
--- /dev/null
+++ b/erpnext/public/js/utils/supplier_quick_entry.js
@@ -0,0 +1,77 @@
+frappe.provide('frappe.ui.form');
+
+frappe.ui.form.SupplierQuickEntryForm = class SupplierQuickEntryForm extends frappe.ui.form.QuickEntryForm {
+	constructor(doctype, after_insert, init_callback, doc, force) {
+		super(doctype, after_insert, init_callback, doc, force);
+		this.skip_redirect_on_error = true;
+	}
+
+	render_dialog() {
+		this.mandatory = this.mandatory.concat(this.get_variant_fields());
+		super.render_dialog();
+	}
+
+	get_variant_fields() {
+		var variant_fields = [
+			{
+				fieldtype: "Section Break",
+				label: __("Primary Contact Details"),
+				collapsible: 1
+			},
+			{
+				label: __("Email Id"),
+				fieldname: "email_id",
+				fieldtype: "Data"
+			},
+			{
+				fieldtype: "Column Break"
+			},
+			{
+				label: __("Mobile Number"),
+				fieldname: "mobile_no",
+				fieldtype: "Data"
+			},
+			{
+				fieldtype: "Section Break",
+				label: __("Primary Address Details"),
+				collapsible: 1
+			},
+			{
+				label: __("Address Line 1"),
+				fieldname: "address_line1",
+				fieldtype: "Data"
+			},
+			{
+				label: __("Address Line 2"),
+				fieldname: "address_line2",
+				fieldtype: "Data"
+			},
+			{
+				label: __("ZIP Code"),
+				fieldname: "pincode",
+				fieldtype: "Data"
+			},
+			{
+				fieldtype: "Column Break"
+			},
+			{
+				label: __("City"),
+				fieldname: "city",
+				fieldtype: "Data"
+			},
+			{
+				label: __("State"),
+				fieldname: "state",
+				fieldtype: "Data"
+			},
+			{
+				label: __("Country"),
+				fieldname: "country",
+				fieldtype: "Link",
+				options: "Country"
+			}
+		];
+
+		return variant_fields;
+	}
+};
diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py
index abf146c..1164f40 100644
--- a/erpnext/selling/doctype/customer/customer.py
+++ b/erpnext/selling/doctype/customer/customer.py
@@ -150,8 +150,14 @@
 				self.db_set('email_id', self.email_id)
 
 	def create_primary_address(self):
+		from frappe.contacts.doctype.address.address import get_address_display
+
 		if self.flags.is_new_doc and self.get('address_line1'):
-			make_address(self)
+			address = make_address(self)
+			address_display = get_address_display(address.name)
+
+			self.db_set("customer_primary_address", address.name)
+			self.db_set("primary_address", address_display)
 
 	def update_lead_status(self):
 		'''If Customer created from Lead, update lead status to "Converted"
@@ -246,9 +252,15 @@
 
 	def on_trash(self):
 		if self.customer_primary_contact:
-			frappe.db.sql("""update `tabCustomer`
-				set customer_primary_contact=null, mobile_no=null, email_id=null
-				where name=%s""", self.name)
+			frappe.db.sql("""
+				UPDATE `tabCustomer`
+				SET
+					customer_primary_contact=null,
+					customer_primary_address=null,
+					mobile_no=null,
+					email_id=null,
+					primary_address=null
+				WHERE name=%(name)s""", {"name": self.name})
 
 		delete_contact_and_address('Customer', self.name)
 		if self.lead_name: