From d01b3e35208e769ba273cc2abfcc905932dd1959 Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Thu, 9 Nov 2023 14:36:20 +0530 Subject: [PATCH] fix: create contact after lead is converted --- crm/fcrm/doctype/crm_lead/crm_lead.py | 58 +++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/crm/fcrm/doctype/crm_lead/crm_lead.py b/crm/fcrm/doctype/crm_lead/crm_lead.py index 82a822d7..5704544c 100644 --- a/crm/fcrm/doctype/crm_lead/crm_lead.py +++ b/crm/fcrm/doctype/crm_lead/crm_lead.py @@ -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 [