fix: attach contact if already exist on lead convertion

This commit is contained in:
Shariq Ansari 2023-11-27 13:10:45 +05:30
parent 329ad89239
commit 78409c441a

View File

@ -47,13 +47,14 @@ class CRMLead(Document):
if self.is_new() or not self.image: if self.is_new() or not self.image:
self.image = has_gravatar(self.email) self.image = has_gravatar(self.email)
def create_contact(self): def create_contact(self, throw=True):
if not self.lead_name: if not self.lead_name:
self.set_full_name() self.set_full_name()
self.set_lead_name() self.set_lead_name()
if self.contact_exists(): existing_contact = self.contact_exists(throw)
return if existing_contact:
return existing_contact
contact = frappe.new_doc("Contact") contact = frappe.new_doc("Contact")
contact.update( contact.update(
@ -82,23 +83,28 @@ class CRMLead(Document):
return contact.name return contact.name
def contact_exists(self): def contact_exists(self, throw=True):
email_exist = frappe.db.exists("Contact Email", {"email_id": self.email}) email_exist = frappe.db.exists("Contact Email", {"email_id": self.email})
phone_exist = frappe.db.exists("Contact Phone", {"phone": self.phone}) phone_exist = frappe.db.exists("Contact Phone", {"phone": self.phone})
mobile_exist = frappe.db.exists("Contact Phone", {"phone": self.mobile_no}) mobile_exist = frappe.db.exists("Contact Phone", {"phone": self.mobile_no})
if email_exist or phone_exist or mobile_exist: doctype = "Contact Email" if email_exist else "Contact Phone"
name = email_exist or phone_exist or mobile_exist
if name:
text = "Email" if email_exist else "Phone" if phone_exist else "Mobile No" 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 data = self.email if email_exist else self.phone if phone_exist else self.mobile_no
value = "{0}: {1}".format(text, data) value = "{0}: {1}".format(text, data)
frappe.throw( contact = frappe.db.get_value(doctype, name, "parent")
_("Contact already exists with {0}").format(value),
title=_("Contact Already Exists"), if throw:
) frappe.throw(
return True _("Contact already exists with {0}").format(value),
title=_("Contact Already Exists"),
)
return contact
return False return False
@ -138,7 +144,7 @@ def convert_to_deal(lead):
lead = frappe.get_cached_doc("CRM Lead", lead) lead = frappe.get_cached_doc("CRM Lead", lead)
lead.status = "Qualified" lead.status = "Qualified"
lead.converted = 1 lead.converted = 1
contact = lead.create_contact() contact = lead.create_contact(False)
deal = lead.create_deal(contact) deal = lead.create_deal(contact)
lead.save() lead.save()
return deal return deal