Abhishek Balam | 6e9e7b4 | 2020-10-14 19:31:37 +0530 | [diff] [blame] | 1 | import frappe |
Abhishek Balam | 2f528c1 | 2020-10-14 19:48:57 +0530 | [diff] [blame] | 2 | from frappe import _ |
Abhishek Balam | 6e9e7b4 | 2020-10-14 19:31:37 +0530 | [diff] [blame] | 3 | from frappe.contacts.doctype.address.address import Address |
Abhishek Balam | 89419c9 | 2020-10-14 20:09:05 +0530 | [diff] [blame] | 4 | from frappe.contacts.doctype.address.address import get_address_templates |
Abhishek Balam | 6e9e7b4 | 2020-10-14 19:31:37 +0530 | [diff] [blame] | 5 | |
Faris Ansari | 22bcad9 | 2020-10-16 15:28:12 +0530 | [diff] [blame] | 6 | class ERPNextAddress(Address): |
Abhishek Balam | 6e9e7b4 | 2020-10-14 19:31:37 +0530 | [diff] [blame] | 7 | def validate(self): |
Abhishek Balam | 6e9e7b4 | 2020-10-14 19:31:37 +0530 | [diff] [blame] | 8 | self.validate_reference() |
Faris Ansari | 22bcad9 | 2020-10-16 15:28:12 +0530 | [diff] [blame] | 9 | super(ERPNextAddress, self).validate() |
Abhishek Balam | 6e9e7b4 | 2020-10-14 19:31:37 +0530 | [diff] [blame] | 10 | |
Abhishek Balam | 6e9e7b4 | 2020-10-14 19:31:37 +0530 | [diff] [blame] | 11 | def link_address(self): |
| 12 | """Link address based on owner""" |
Faris Ansari | be0fcbe | 2020-10-16 15:55:25 +0530 | [diff] [blame] | 13 | if self.is_your_company_address: |
Faris Ansari | 22bcad9 | 2020-10-16 15:28:12 +0530 | [diff] [blame] | 14 | return |
| 15 | |
| 16 | return super(ERPNextAddress, self).link_address() |
Abhishek Balam | 6e9e7b4 | 2020-10-14 19:31:37 +0530 | [diff] [blame] | 17 | |
Abhishek Balam | 2f528c1 | 2020-10-14 19:48:57 +0530 | [diff] [blame] | 18 | def validate_reference(self): |
Faris Ansari | 22bcad9 | 2020-10-16 15:28:12 +0530 | [diff] [blame] | 19 | if self.is_your_company_address and not [ |
| 20 | row for row in self.links if row.link_doctype == "Company" |
| 21 | ]: |
| 22 | frappe.throw(_("Address needs to be linked to a Company. Please add a row for Company in the Links table."), |
| 23 | title=_("Company Not Linked")) |
| 24 | |
Abhishek Balam | 89419c9 | 2020-10-14 20:09:05 +0530 | [diff] [blame] | 25 | @frappe.whitelist() |
| 26 | def get_shipping_address(company, address = None): |
| 27 | filters = [ |
| 28 | ["Dynamic Link", "link_doctype", "=", "Company"], |
| 29 | ["Dynamic Link", "link_name", "=", company], |
| 30 | ["Address", "is_your_company_address", "=", 1] |
| 31 | ] |
| 32 | fields = ["*"] |
| 33 | if address and frappe.db.get_value('Dynamic Link', |
| 34 | {'parent': address, 'link_name': company}): |
| 35 | filters.append(["Address", "name", "=", address]) |
| 36 | |
| 37 | address = frappe.get_all("Address", filters=filters, fields=fields) or {} |
| 38 | |
| 39 | if address: |
| 40 | address_as_dict = address[0] |
| 41 | name, address_template = get_address_templates(address_as_dict) |
| 42 | return address_as_dict.get("name"), frappe.render_template(address_template, address_as_dict) |