fix: create contact after lead is converted

This commit is contained in:
Shariq Ansari 2023-11-09 14:36:20 +05:30
parent 4000dfab09
commit d01b3e3520

View File

@ -15,6 +15,9 @@ class CRMLead(Document):
self.set_title()
self.validate_email()
if self.has_value_changed('converted') and self.converted:
self.create_contact()
def set_full_name(self):
if self.first_name:
self.lead_name = " ".join(
@ -47,6 +50,61 @@ class CRMLead(Document):
if self.is_new() or not self.image:
self.image = has_gravatar(self.email)
def create_contact(self):
if not self.lead_name:
self.set_full_name()
self.set_lead_name()
if self.contact_exists():
return
contact = frappe.new_doc("Contact")
contact.update(
{
"first_name": self.first_name or self.lead_name,
"last_name": self.last_name,
"salutation": self.salutation,
"gender": self.gender,
"designation": self.job_title,
"company_name": self.organization,
"image": self.image or "",
}
)
if self.email:
contact.append("email_ids", {"email_id": self.email, "is_primary": 1})
if self.phone:
contact.append("phone_nos", {"phone": self.phone, "is_primary_phone": 1})
if self.mobile_no:
contact.append("phone_nos", {"phone": self.mobile_no, "is_primary_mobile_no": 1})
contact.insert(ignore_permissions=True)
contact.reload() # load changes by hooks on contact
return contact
def contact_exists(self):
email_exist = frappe.db.exists("Contact Email", {"email_id": self.email})
phone_exist = frappe.db.exists("Contact Phone", {"phone": self.phone})
mobile_exist = frappe.db.exists("Contact Phone", {"phone": self.mobile_no})
if email_exist or phone_exist or mobile_exist:
text = "Email" if email_exist else "Phone" if phone_exist else "Mobile No"
data = self.email if email_exist else self.phone if phone_exist else self.mobile_no
value = "{0}: {1}".format(text, data)
frappe.throw(
_("Contact already exists with {0}").format(value),
title=_("Contact Already Exists"),
)
return True
return False
@staticmethod
def sort_options():
return [