2020-10-14 14:01:37 +00:00
|
|
|
import frappe
|
2020-10-14 14:18:57 +00:00
|
|
|
from frappe import _
|
2021-09-02 11:14:59 +00:00
|
|
|
from frappe.contacts.doctype.address.address import (
|
|
|
|
Address,
|
|
|
|
get_address_display,
|
|
|
|
get_address_templates,
|
|
|
|
)
|
|
|
|
|
2020-10-14 14:01:37 +00:00
|
|
|
|
2020-10-16 09:58:12 +00:00
|
|
|
class ERPNextAddress(Address):
|
2020-10-14 14:01:37 +00:00
|
|
|
def validate(self):
|
|
|
|
self.validate_reference()
|
2021-10-21 10:07:59 +00:00
|
|
|
self.update_compnay_address()
|
2020-10-16 09:58:12 +00:00
|
|
|
super(ERPNextAddress, self).validate()
|
2020-10-14 14:01:37 +00:00
|
|
|
|
|
|
|
def link_address(self):
|
|
|
|
"""Link address based on owner"""
|
2020-10-16 10:25:25 +00:00
|
|
|
if self.is_your_company_address:
|
2020-10-16 09:58:12 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
return super(ERPNextAddress, self).link_address()
|
2020-10-14 14:01:37 +00:00
|
|
|
|
2021-10-21 10:07:59 +00:00
|
|
|
def update_compnay_address(self):
|
2022-03-28 13:22:46 +00:00
|
|
|
for link in self.get("links"):
|
|
|
|
if link.link_doctype == "Company":
|
2021-10-21 10:07:59 +00:00
|
|
|
self.is_your_company_address = 1
|
|
|
|
|
2020-10-14 14:18:57 +00:00
|
|
|
def validate_reference(self):
|
2020-10-16 09:58:12 +00:00
|
|
|
if self.is_your_company_address and not [
|
|
|
|
row for row in self.links if row.link_doctype == "Company"
|
|
|
|
]:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(
|
|
|
|
_("Address needs to be linked to a Company. Please add a row for Company in the Links table."),
|
|
|
|
title=_("Company Not Linked"),
|
|
|
|
)
|
2020-10-16 09:58:12 +00:00
|
|
|
|
2021-08-26 06:27:28 +00:00
|
|
|
def on_update(self):
|
|
|
|
"""
|
|
|
|
After Address is updated, update the related 'Primary Address' on Customer.
|
|
|
|
"""
|
|
|
|
address_display = get_address_display(self.as_dict())
|
2022-03-28 13:22:46 +00:00
|
|
|
filters = {"customer_primary_address": self.name}
|
2021-08-26 06:27:28 +00:00
|
|
|
customers = frappe.db.get_all("Customer", filters=filters, as_list=True)
|
|
|
|
for customer_name in customers:
|
|
|
|
frappe.db.set_value("Customer", customer_name[0], "primary_address", address_display)
|
2021-09-01 11:15:57 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-10-14 14:39:05 +00:00
|
|
|
@frappe.whitelist()
|
2022-03-28 13:22:46 +00:00
|
|
|
def get_shipping_address(company, address=None):
|
2020-10-14 14:39:05 +00:00
|
|
|
filters = [
|
|
|
|
["Dynamic Link", "link_doctype", "=", "Company"],
|
|
|
|
["Dynamic Link", "link_name", "=", company],
|
2022-03-28 13:22:46 +00:00
|
|
|
["Address", "is_your_company_address", "=", 1],
|
2020-10-14 14:39:05 +00:00
|
|
|
]
|
|
|
|
fields = ["*"]
|
2022-03-28 13:22:46 +00:00
|
|
|
if address and frappe.db.get_value("Dynamic Link", {"parent": address, "link_name": company}):
|
2020-10-14 14:39:05 +00:00
|
|
|
filters.append(["Address", "name", "=", address])
|
2021-06-23 04:24:12 +00:00
|
|
|
if not address:
|
|
|
|
filters.append(["Address", "is_shipping_address", "=", 1])
|
2020-10-14 14:39:05 +00:00
|
|
|
|
|
|
|
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)
|