From 346ca568b71079ccd47da28536cebc539bf60968 Mon Sep 17 00:00:00 2001 From: Abhishek Balam Date: Wed, 14 Oct 2020 18:11:04 +0530 Subject: [PATCH 1/8] fix: set company link in address when is_your_company_address is set --- erpnext/accounts/custom/address.json | 170 +++++++++++++++++++-------- erpnext/hooks.py | 1 + erpnext/public/js/address.js | 26 ++++ 3 files changed, 146 insertions(+), 51 deletions(-) create mode 100644 erpnext/public/js/address.js diff --git a/erpnext/accounts/custom/address.json b/erpnext/accounts/custom/address.json index 08f972d13b..5c921da9b7 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/hooks.py b/erpnext/hooks.py index cf1d69f914..e47e460586 100644 --- a/erpnext/hooks.py +++ b/erpnext/hooks.py @@ -21,6 +21,7 @@ web_include_js = "assets/js/erpnext-web.min.js" 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" diff --git a/erpnext/public/js/address.js b/erpnext/public/js/address.js new file mode 100644 index 0000000000..1563ec6f20 --- /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'); + } +}); From 38b26c8f3e7c8a53661a5a3b8ff02a9cbd83fdfa Mon Sep 17 00:00:00 2001 From: Abhishek Balam Date: Wed, 14 Oct 2020 18:24:51 +0530 Subject: [PATCH 2/8] fix: added semicolons --- erpnext/public/js/address.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/erpnext/public/js/address.js b/erpnext/public/js/address.js index 1563ec6f20..1176075b39 100644 --- a/erpnext/public/js/address.js +++ b/erpnext/public/js/address.js @@ -13,8 +13,8 @@ frappe.ui.form.on("Address", { filters: { name: 'Company' } - } - } + }; + }; frm.refresh_field('links'); } else{ From 6e9e7b4c7a73541419b4653128fe4ee0314087f3 Mon Sep 17 00:00:00 2001 From: Abhishek Balam Date: Wed, 14 Oct 2020 19:31:37 +0530 Subject: [PATCH 3/8] fix: serverside validation overrides --- erpnext/accounts/custom/address.py | 31 ++++++++++++++++++++++++++++++ erpnext/hooks.py | 4 ++++ 2 files changed, 35 insertions(+) create mode 100644 erpnext/accounts/custom/address.py diff --git a/erpnext/accounts/custom/address.py b/erpnext/accounts/custom/address.py new file mode 100644 index 0000000000..433f9b2f42 --- /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 e47e460586..fb93df8b03 100644 --- a/erpnext/hooks.py +++ b/erpnext/hooks.py @@ -27,6 +27,10 @@ doctype_js = { "Newsletter": "public/js/newsletter.js" } +override_doctype_class = { + 'Address': 'erpnext.accounts.custom.address.CustomAddress' +} + welcome_email = "erpnext.setup.utils.welcome_email" # setup wizard From 2f528c1c73e336cf316b8c6e3e44d9fd2f630411 Mon Sep 17 00:00:00 2001 From: Abhishek Balam Date: Wed, 14 Oct 2020 19:48:57 +0530 Subject: [PATCH 4/8] fix: gettext import and changed function order in address.py --- erpnext/accounts/custom/address.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/erpnext/accounts/custom/address.py b/erpnext/accounts/custom/address.py index 433f9b2f42..ac2cb0586d 100644 --- a/erpnext/accounts/custom/address.py +++ b/erpnext/accounts/custom/address.py @@ -1,4 +1,5 @@ import frappe +from frappe import _ 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 @@ -11,12 +12,6 @@ class CustomAddress(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: @@ -27,5 +22,12 @@ class CustomAddress(Address): for link in contact.links: self.append('links', dict(link_doctype=link.link_doctype, link_name=link.link_name)) return True + return False - return False \ No newline at end of file + def validate_reference(self): + if self.is_your_company_address: + print('here') + 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")) + \ No newline at end of file From 89419c9a3086c72c1183ca53c612fc0ed9564680 Mon Sep 17 00:00:00 2001 From: Abhishek Balam Date: Wed, 14 Oct 2020 20:09:05 +0530 Subject: [PATCH 5/8] fix: move get_shipping_address() from frappe to erpnext and fix references --- erpnext/accounts/custom/address.py | 21 ++++++++++++++++++++- erpnext/public/js/utils/party.js | 2 +- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/custom/address.py b/erpnext/accounts/custom/address.py index ac2cb0586d..2a98df2e26 100644 --- a/erpnext/accounts/custom/address.py +++ b/erpnext/accounts/custom/address.py @@ -3,6 +3,7 @@ from frappe import _ 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 +from frappe.contacts.doctype.address.address import get_address_templates class CustomAddress(Address): def validate(self): @@ -30,4 +31,22 @@ class CustomAddress(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")) - \ No newline at end of file + +@frappe.whitelist() +def get_shipping_address(company, address = None): + filters = [ + ["Dynamic Link", "link_doctype", "=", "Company"], + ["Dynamic Link", "link_name", "=", company], + ["Address", "is_your_company_address", "=", 1] + ] + fields = ["*"] + if address and frappe.db.get_value('Dynamic Link', + {'parent': address, 'link_name': company}): + filters.append(["Address", "name", "=", address]) + + address = frappe.get_all("Address", filters=filters, fields=fields) or {} + + if address: + address_as_dict = address[0] + name, address_template = get_address_templates(address_as_dict) + return address_as_dict.get("name"), frappe.render_template(address_template, address_as_dict) diff --git a/erpnext/public/js/utils/party.js b/erpnext/public/js/utils/party.js index 44e75aee36..770704e595 100644 --- a/erpnext/public/js/utils/party.js +++ b/erpnext/public/js/utils/party.js @@ -277,7 +277,7 @@ erpnext.utils.validate_mandatory = function(frm, label, value, trigger_on) { erpnext.utils.get_shipping_address = function(frm, callback){ if (frm.doc.company) { frappe.call({ - method: "frappe.contacts.doctype.address.address.get_shipping_address", + method: "erpnext.accounts.custom.address.get_shipping_address", args: { company: frm.doc.company, address: frm.doc.shipping_address From 349fcfd6a520321b4675b11bc128180e641da8d1 Mon Sep 17 00:00:00 2001 From: Abhishek Balam Date: Thu, 15 Oct 2020 12:21:43 +0530 Subject: [PATCH 6/8] fix: set default filters on unchecking box, code cleanup --- erpnext/public/js/address.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/erpnext/public/js/address.js b/erpnext/public/js/address.js index 1176075b39..57f7163bbb 100644 --- a/erpnext/public/js/address.js +++ b/erpnext/public/js/address.js @@ -3,24 +3,23 @@ frappe.ui.form.on("Address", { is_your_company_address: function(frm) { - if(frm.doc.is_your_company_address){ + frm.clear_table('links'); + 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() { + frm.set_query('link_doctype', 'links', () => { 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'); + else { + frm.trigger('refresh'); } - frm.refresh_field('links'); } }); From 22bcad9dc75d57381a21dbbf3317c906b0a666fb Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Fri, 16 Oct 2020 15:28:12 +0530 Subject: [PATCH 7/8] fix: Cleanup code --- erpnext/accounts/custom/address.py | 34 +++++++++++------------------- erpnext/hooks.py | 4 ++-- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/erpnext/accounts/custom/address.py b/erpnext/accounts/custom/address.py index 2a98df2e26..4894c64a1f 100644 --- a/erpnext/accounts/custom/address.py +++ b/erpnext/accounts/custom/address.py @@ -1,37 +1,27 @@ import frappe from frappe import _ 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 from frappe.contacts.doctype.address.address import get_address_templates -class CustomAddress(Address): +class ERPNextAddress(Address): def validate(self): - self.link_address() self.validate_reference() - super(CustomAddress, self).validate_preferred_address() - set_link_title(self) - deduplicate_dynamic_links(self) + super(ERPNextAddress, self).validate() 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 + if not self.is_your_company_address: + return + + return super(ERPNextAddress, self).link_address() def validate_reference(self): - if self.is_your_company_address: - print('here') - 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")) - + if self.is_your_company_address and 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."), + title=_("Company Not Linked")) + @frappe.whitelist() def get_shipping_address(company, address = None): filters = [ diff --git a/erpnext/hooks.py b/erpnext/hooks.py index fb93df8b03..9bd31050c4 100644 --- a/erpnext/hooks.py +++ b/erpnext/hooks.py @@ -28,7 +28,7 @@ doctype_js = { } override_doctype_class = { - 'Address': 'erpnext.accounts.custom.address.CustomAddress' + 'Address': 'erpnext.accounts.custom.address.ERPNextAddress' } welcome_email = "erpnext.setup.utils.welcome_email" @@ -566,4 +566,4 @@ global_search_doctypes = { {'doctype': 'Hotel Room Package', 'index': 3}, {'doctype': 'Hotel Room Type', 'index': 4} ] -} \ No newline at end of file +} From be0fcbea68bd24981b8c77a4b01eacaf63835d0a Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Fri, 16 Oct 2020 15:55:25 +0530 Subject: [PATCH 8/8] fix: Dont add Contact link if is company address --- erpnext/accounts/custom/address.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/custom/address.py b/erpnext/accounts/custom/address.py index 4894c64a1f..5e764037a7 100644 --- a/erpnext/accounts/custom/address.py +++ b/erpnext/accounts/custom/address.py @@ -10,7 +10,7 @@ class ERPNextAddress(Address): def link_address(self): """Link address based on owner""" - if not self.is_your_company_address: + if self.is_your_company_address: return return super(ERPNextAddress, self).link_address()