Skip to content

Commit

Permalink
fix: create and link address while creating prospect & customer
Browse files Browse the repository at this point in the history
  • Loading branch information
shariquerik committed Sep 16, 2024
1 parent 705d3de commit 74908d1
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions erpnext/crm/frappe_crm_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def create_prospect_against_crm_deal():
pass

create_contacts(json.loads(doc.contacts), prospect.company_name, "Prospect", prospect_name)
create_address("Prospect", prospect_name, doc.address)
frappe.response["message"] = prospect_name


Expand All @@ -86,17 +87,49 @@ def create_contacts(contacts, organization=None, link_doctype=None, link_docname
if c.get("mobile_no"):
contact.append("phone_nos", {"phone": c.get("mobile_no"), "is_primary_mobile_no": 1})

link_contact_to_prospect(contact, link_doctype, link_docname)
link_doc(contact, link_doctype, link_docname)

contact.save(ignore_permissions=True)


def link_contact_to_prospect(contact, link_doctype, link_docname):
def create_address(doctype, docname, address):
if not address:
return
try:
_address = frappe.db.exists("Address", address.get("name"))
if not _address:
new_address_doc = frappe.new_doc("Address")
for field in [
"address_title",
"address_type",
"address_line1",
"address_line2",
"city",
"state",
"pincode",
"country",
]:
if address.get(field):
new_address_doc.set(field, address.get(field))

new_address_doc.append("links", {"link_doctype": doctype, "link_name": docname})
new_address_doc.insert(ignore_mandatory=True)
return new_address_doc.name
else:
address = frappe.get_doc("Address", _address)
link_doc(address, doctype, docname)
address.save(ignore_permissions=True)
return address.name
except Exception:
frappe.log_error(frappe.get_traceback(), f"Error while creating address for {docname}")


def link_doc(doc, link_doctype, link_docname):
already_linked = any(
[(link.link_doctype == link_doctype and link.link_name == link_docname) for link in contact.links]
[(link.link_doctype == link_doctype and link.link_name == link_docname) for link in doc.links]
)
if not already_linked:
contact.append(
doc.append(
"links", {"link_doctype": link_doctype, "link_name": link_docname, "link_title": link_docname}
)

Expand Down Expand Up @@ -130,6 +163,8 @@ def create_customer(customer_data=None):

contacts = json.loads(customer_data.get("contacts"))
create_contacts(contacts, customer_name, "Customer", customer_name)
create_address("Customer", customer_name, customer_data.get("address"))
return customer_name
except Exception:
frappe.log_error(frappe.get_traceback(), "Error while creating customer against Frappe CRM Deal")
pass

0 comments on commit 74908d1

Please sign in to comment.