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 _ |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 3 | from frappe.contacts.doctype.address.address import ( |
| 4 | Address, |
| 5 | get_address_display, |
| 6 | get_address_templates, |
| 7 | ) |
| 8 | |
Abhishek Balam | 6e9e7b4 | 2020-10-14 19:31:37 +0530 | [diff] [blame] | 9 | |
Faris Ansari | 22bcad9 | 2020-10-16 15:28:12 +0530 | [diff] [blame] | 10 | class ERPNextAddress(Address): |
Abhishek Balam | 6e9e7b4 | 2020-10-14 19:31:37 +0530 | [diff] [blame] | 11 | def validate(self): |
Abhishek Balam | 6e9e7b4 | 2020-10-14 19:31:37 +0530 | [diff] [blame] | 12 | self.validate_reference() |
Deepesh Garg | c6d0681 | 2021-10-21 15:37:59 +0530 | [diff] [blame] | 13 | self.update_compnay_address() |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 14 | super().validate() |
Abhishek Balam | 6e9e7b4 | 2020-10-14 19:31:37 +0530 | [diff] [blame] | 15 | |
Abhishek Balam | 6e9e7b4 | 2020-10-14 19:31:37 +0530 | [diff] [blame] | 16 | def link_address(self): |
| 17 | """Link address based on owner""" |
Faris Ansari | be0fcbe | 2020-10-16 15:55:25 +0530 | [diff] [blame] | 18 | if self.is_your_company_address: |
Faris Ansari | 22bcad9 | 2020-10-16 15:28:12 +0530 | [diff] [blame] | 19 | return |
| 20 | |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 21 | return super().link_address() |
Abhishek Balam | 6e9e7b4 | 2020-10-14 19:31:37 +0530 | [diff] [blame] | 22 | |
Deepesh Garg | c6d0681 | 2021-10-21 15:37:59 +0530 | [diff] [blame] | 23 | def update_compnay_address(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 24 | for link in self.get("links"): |
| 25 | if link.link_doctype == "Company": |
Deepesh Garg | c6d0681 | 2021-10-21 15:37:59 +0530 | [diff] [blame] | 26 | self.is_your_company_address = 1 |
| 27 | |
Abhishek Balam | 2f528c1 | 2020-10-14 19:48:57 +0530 | [diff] [blame] | 28 | def validate_reference(self): |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 29 | if self.is_your_company_address and not [row for row in self.links if row.link_doctype == "Company"]: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 30 | frappe.throw( |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 31 | _( |
| 32 | "Address needs to be linked to a Company. Please add a row for Company in the Links table." |
| 33 | ), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 34 | title=_("Company Not Linked"), |
| 35 | ) |
Faris Ansari | 22bcad9 | 2020-10-16 15:28:12 +0530 | [diff] [blame] | 36 | |
Brian Pond | 1a91977 | 2021-08-25 23:27:28 -0700 | [diff] [blame] | 37 | def on_update(self): |
| 38 | """ |
| 39 | After Address is updated, update the related 'Primary Address' on Customer. |
| 40 | """ |
| 41 | address_display = get_address_display(self.as_dict()) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 42 | filters = {"customer_primary_address": self.name} |
Brian Pond | 1a91977 | 2021-08-25 23:27:28 -0700 | [diff] [blame] | 43 | customers = frappe.db.get_all("Customer", filters=filters, as_list=True) |
| 44 | for customer_name in customers: |
| 45 | frappe.db.set_value("Customer", customer_name[0], "primary_address", address_display) |
Ankush Menat | b147b85 | 2021-09-01 16:45:57 +0530 | [diff] [blame] | 46 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 47 | |
Abhishek Balam | 89419c9 | 2020-10-14 20:09:05 +0530 | [diff] [blame] | 48 | @frappe.whitelist() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 49 | def get_shipping_address(company, address=None): |
Abhishek Balam | 89419c9 | 2020-10-14 20:09:05 +0530 | [diff] [blame] | 50 | filters = [ |
| 51 | ["Dynamic Link", "link_doctype", "=", "Company"], |
| 52 | ["Dynamic Link", "link_name", "=", company], |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 53 | ["Address", "is_your_company_address", "=", 1], |
Abhishek Balam | 89419c9 | 2020-10-14 20:09:05 +0530 | [diff] [blame] | 54 | ] |
| 55 | fields = ["*"] |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 56 | if address and frappe.db.get_value("Dynamic Link", {"parent": address, "link_name": company}): |
Abhishek Balam | 89419c9 | 2020-10-14 20:09:05 +0530 | [diff] [blame] | 57 | filters.append(["Address", "name", "=", address]) |
Noah Jacob | b4b6288 | 2021-06-23 09:54:12 +0530 | [diff] [blame] | 58 | if not address: |
| 59 | filters.append(["Address", "is_shipping_address", "=", 1]) |
Abhishek Balam | 89419c9 | 2020-10-14 20:09:05 +0530 | [diff] [blame] | 60 | |
| 61 | address = frappe.get_all("Address", filters=filters, fields=fields) or {} |
| 62 | |
| 63 | if address: |
| 64 | address_as_dict = address[0] |
| 65 | name, address_template = get_address_templates(address_as_dict) |
| 66 | return address_as_dict.get("name"), frappe.render_template(address_template, address_as_dict) |