Merge branch 'develop' into address_customisation
diff --git a/erpnext/accounts/custom/address.json b/erpnext/accounts/custom/address.json
index 08f972d..5c921da 100644
--- a/erpnext/accounts/custom/address.json
+++ b/erpnext/accounts/custom/address.json
@@ -1,58 +1,126 @@
 {
  "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": "2018-12-28 22:29:21.828090", 
-   "default": null, 
-   "depends_on": null, 
-   "description": null, 
-   "docstatus": 0, 
-   "dt": "Address", 
-   "fetch_from": null, 
-   "fieldname": "tax_category", 
-   "fieldtype": "Link", 
-   "hidden": 0, 
-   "idx": 14, 
-   "ignore_user_permissions": 0, 
-   "ignore_xss_filter": 0, 
-   "in_global_search": 0, 
-   "in_list_view": 0, 
-   "in_standard_filter": 0, 
-   "insert_after": "fax", 
-   "label": "Tax Category", 
-   "modified": "2018-12-28 22:29:21.828090", 
-   "modified_by": "Administrator", 
-   "name": "Address-tax_category", 
-   "no_copy": 0, 
-   "options": "Tax Category", 
-   "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, 
+   "_assign": null,
+   "_comments": null,
+   "_liked_by": null,
+   "_user_tags": null,
+   "allow_in_quick_entry": 0,
+   "allow_on_submit": 0,
+   "bold": 0,
+   "collapsible": 0,
+   "collapsible_depends_on": null,
+   "columns": 0,
+   "creation": "2018-12-28 22:29:21.828090",
+   "default": null,
+   "depends_on": null,
+   "description": null,
+   "docstatus": 0,
+   "dt": "Address",
+   "fetch_from": null,
+   "fetch_if_empty": 0,
+   "fieldname": "tax_category",
+   "fieldtype": "Link",
+   "hidden": 0,
+   "hide_border": 0,
+   "hide_days": 0,
+   "hide_seconds": 0,
+   "idx": 15,
+   "ignore_user_permissions": 0,
+   "ignore_xss_filter": 0,
+   "in_global_search": 0,
+   "in_list_view": 0,
+   "in_preview": 0,
+   "in_standard_filter": 0,
+   "insert_after": "fax",
+   "label": "Tax Category",
+   "length": 0,
+   "mandatory_depends_on": null,
+   "modified": "2018-12-28 22:29:21.828090",
+   "modified_by": "Administrator",
+   "name": "Address-tax_category",
+   "no_copy": 0,
+   "options": "Tax Category",
+   "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,
+   "read_only_depends_on": null,
+   "report_hide": 0,
+   "reqd": 0,
+   "search_index": 0,
+   "translatable": 0,
+   "unique": 0,
+   "width": null
+  },
+  {
+   "_assign": null,
+   "_comments": null,
+   "_liked_by": null,
+   "_user_tags": null,
+   "allow_in_quick_entry": 0,
+   "allow_on_submit": 0,
+   "bold": 0,
+   "collapsible": 0,
+   "collapsible_depends_on": null,
+   "columns": 0,
+   "creation": "2020-10-14 17:41:40.878179",
+   "default": "0",
+   "depends_on": null,
+   "description": null,
+   "docstatus": 0,
+   "dt": "Address",
+   "fetch_from": null,
+   "fetch_if_empty": 0,
+   "fieldname": "is_your_company_address",
+   "fieldtype": "Check",
+   "hidden": 0,
+   "hide_border": 0,
+   "hide_days": 0,
+   "hide_seconds": 0,
+   "idx": 20,
+   "ignore_user_permissions": 0,
+   "ignore_xss_filter": 0,
+   "in_global_search": 0,
+   "in_list_view": 0,
+   "in_preview": 0,
+   "in_standard_filter": 0,
+   "insert_after": "linked_with",
+   "label": "Is Your Company Address",
+   "length": 0,
+   "mandatory_depends_on": null,
+   "modified": "2020-10-14 17:41:40.878179",
+   "modified_by": "Administrator",
+   "name": "Address-is_your_company_address",
+   "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,
+   "read_only_depends_on": null,
+   "report_hide": 0,
+   "reqd": 0,
+   "search_index": 0,
+   "translatable": 0,
+   "unique": 0,
    "width": null
   }
- ], 
- "custom_perms": [], 
- "doctype": "Address", 
- "property_setters": [], 
+ ],
+ "custom_perms": [],
+ "doctype": "Address",
+ "property_setters": [],
  "sync_on_migrate": 1
 }
\ No newline at end of file
diff --git a/erpnext/accounts/custom/address.py b/erpnext/accounts/custom/address.py
new file mode 100644
index 0000000..433f9b2
--- /dev/null
+++ b/erpnext/accounts/custom/address.py
@@ -0,0 +1,31 @@
+import frappe
+from frappe.contacts.doctype.address.address import Address
+from frappe.contacts.address_and_contact import set_link_title
+from frappe.core.doctype.dynamic_link.dynamic_link import deduplicate_dynamic_links
+
+class CustomAddress(Address):
+	def validate(self):
+		self.link_address()
+		self.validate_reference()
+		super(CustomAddress, self).validate_preferred_address()
+		set_link_title(self)
+		deduplicate_dynamic_links(self)
+
+	def validate_reference(self):
+		if self.is_your_company_address:
+			if not [row for row in self.links if row.link_doctype == "Company"]:
+				frappe.throw(_("Address needs to be linked to a Company. Please add a row for Company in the Links table below."),
+					title =_("Company not Linked"))
+	
+	def link_address(self):
+		"""Link address based on owner"""
+		if not self.links and not self.is_your_company_address:
+			contact_name = frappe.db.get_value("Contact", {"email_id": self.owner})
+			if contact_name:
+				contact = frappe.get_cached_doc('Contact', contact_name)
+				print('here', str(contact))
+				for link in contact.links:
+					self.append('links', dict(link_doctype=link.link_doctype, link_name=link.link_name))
+				return True
+
+		return False
\ No newline at end of file
diff --git a/erpnext/hooks.py b/erpnext/hooks.py
index cf1d69f..fb93df8 100644
--- a/erpnext/hooks.py
+++ b/erpnext/hooks.py
@@ -21,11 +21,16 @@
 web_include_css = "assets/css/erpnext-web.css"
 
 doctype_js = {
+	"Address": "public/js/address.js",
 	"Communication": "public/js/communication.js",
 	"Event": "public/js/event.js",
 	"Newsletter": "public/js/newsletter.js"
 }
 
+override_doctype_class = {
+    'Address': 'erpnext.accounts.custom.address.CustomAddress'
+}
+
 welcome_email = "erpnext.setup.utils.welcome_email"
 
 # setup wizard
diff --git a/erpnext/public/js/address.js b/erpnext/public/js/address.js
new file mode 100644
index 0000000..1176075
--- /dev/null
+++ b/erpnext/public/js/address.js
@@ -0,0 +1,26 @@
+// Copyright (c) 2016, Frappe Technologies and contributors
+// For license information, please see license.txt
+
+frappe.ui.form.on("Address", {
+	is_your_company_address: function(frm) {
+		if(frm.doc.is_your_company_address){
+			frm.add_child('links', {
+				link_doctype: 'Company',
+				link_name: frappe.defaults.get_user_default('Company')
+			});
+			frm.fields_dict.links.grid.get_field('link_doctype').get_query = function() {
+				return {
+					filters: {
+						name: 'Company'
+					}
+				};
+			};
+			frm.refresh_field('links');
+		}
+		else{
+			frm.fields_dict.links.grid.get_field('link_doctype').get_query = null;
+			frm.clear_table('links');
+		}
+		frm.refresh_field('links');
+	}
+});