From 6e9e7b4c7a73541419b4653128fe4ee0314087f3 Mon Sep 17 00:00:00 2001 From: Abhishek Balam Date: Wed, 14 Oct 2020 19:31:37 +0530 Subject: [PATCH] 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