fix: send existing contact/org data while converting to deal

This commit is contained in:
Shariq Ansari 2025-03-15 15:55:47 +05:30
parent f9043511a8
commit e84351cb57
2 changed files with 51 additions and 57 deletions

View File

@ -115,13 +115,14 @@ class CRMLead(Document):
elif user != agent: elif user != agent:
frappe.share.remove(self.doctype, self.name, user) frappe.share.remove(self.doctype, self.name, user)
def create_contact(self, throw=True): def create_contact(self, existing_contact=None, 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()
existing_contact = self.contact_exists(throw) existing_contact = existing_contact or self.contact_exists(throw)
if existing_contact: if existing_contact:
self.update_lead_contact(existing_contact)
return existing_contact return existing_contact
contact = frappe.new_doc("Contact") contact = frappe.new_doc("Contact")
@ -151,12 +152,15 @@ class CRMLead(Document):
return contact.name return contact.name
def create_organization(self): def create_organization(self, existing_organization=None):
if not self.organization: if not self.organization and not existing_organization:
return return
existing_organization = frappe.db.exists("CRM Organization", {"organization_name": self.organization}) existing_organization = existing_organization or frappe.db.exists(
"CRM Organization", {"organization_name": self.organization}
)
if existing_organization: if existing_organization:
self.db_set("organization", existing_organization)
return existing_organization return existing_organization
organization = frappe.new_doc("CRM Organization") organization = frappe.new_doc("CRM Organization")
@ -172,6 +176,20 @@ class CRMLead(Document):
organization.insert(ignore_permissions=True) organization.insert(ignore_permissions=True)
return organization.name return organization.name
def update_lead_contact(self, contact):
contact = frappe.get_cached_doc("Contact", contact)
frappe.db.set_value(
"CRM Lead",
self.name,
{
"salutation": contact.salutation,
"first_name": contact.first_name,
"last_name": contact.last_name,
"email": contact.email_id,
"mobile_no": contact.mobile_no,
},
)
def contact_exists(self, throw=True): 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})
@ -383,7 +401,7 @@ class CRMLead(Document):
@frappe.whitelist() @frappe.whitelist()
def convert_to_deal(lead, doc=None, deal=None): def convert_to_deal(lead, doc=None, deal=None, existing_contact=None, existing_organization=None):
if not (doc and doc.flags.get("ignore_permissions")) and not frappe.has_permission( if not (doc and doc.flags.get("ignore_permissions")) and not frappe.has_permission(
"CRM Lead", "write", lead "CRM Lead", "write", lead
): ):
@ -395,7 +413,7 @@ def convert_to_deal(lead, doc=None, deal=None):
lead.db_set("converted", 1) lead.db_set("converted", 1)
if lead.sla and frappe.db.exists("CRM Communication Status", "Replied"): if lead.sla and frappe.db.exists("CRM Communication Status", "Replied"):
lead.db_set("communication_status", "Replied") lead.db_set("communication_status", "Replied")
contact = lead.create_contact(False) contact = lead.create_contact(existing_contact, False)
organization = lead.create_organization() organization = lead.create_organization(existing_organization)
_deal = lead.create_deal(contact, organization, deal) _deal = lead.create_deal(contact, organization, deal)
return _deal return _deal

View File

@ -341,7 +341,6 @@ import { getView } from '@/utils/view'
import { getSettings } from '@/stores/settings' import { getSettings } from '@/stores/settings'
import { usersStore } from '@/stores/users' import { usersStore } from '@/stores/users'
import { globalStore } from '@/stores/global' import { globalStore } from '@/stores/global'
import { contactsStore } from '@/stores/contacts'
import { statusesStore } from '@/stores/statuses' import { statusesStore } from '@/stores/statuses'
import { getMeta } from '@/stores/meta' import { getMeta } from '@/stores/meta'
import { import {
@ -369,7 +368,6 @@ import { useActiveTabManager } from '@/composables/useActiveTabManager'
const { brand } = getSettings() const { brand } = getSettings()
const { isManager } = usersStore() const { isManager } = usersStore()
const { $dialog, $socket, makeCall } = globalStore() const { $dialog, $socket, makeCall } = globalStore()
const { getContactByName, contacts } = contactsStore()
const { statusOptions, getLeadStatus, getDealStatus } = statusesStore() const { statusOptions, getLeadStatus, getDealStatus } = statusesStore()
const { doctypeMeta } = getMeta('CRM Lead') const { doctypeMeta } = getMeta('CRM Lead')
const route = useRoute() const route = useRoute()
@ -599,9 +597,7 @@ const existingOrganizationChecked = ref(false)
const existingContact = ref('') const existingContact = ref('')
const existingOrganization = ref('') const existingOrganization = ref('')
async function convertToDeal(updated) { async function convertToDeal() {
let valueUpdated = false
if (existingContactChecked.value && !existingContact.value) { if (existingContactChecked.value && !existingContact.value) {
createToast({ createToast({
title: __('Error'), title: __('Error'),
@ -622,55 +618,35 @@ async function convertToDeal(updated) {
return return
} }
if (existingContactChecked.value && existingContact.value) { if (!existingContactChecked.value && existingContact.value) {
lead.data.salutation = getContactByName(existingContact.value).salutation existingContact.value = ''
lead.data.first_name = getContactByName(existingContact.value).first_name
lead.data.last_name = getContactByName(existingContact.value).last_name
lead.data.email_id = getContactByName(existingContact.value).email_id
lead.data.mobile_no = getContactByName(existingContact.value).mobile_no
existingContactChecked.value = false
valueUpdated = true
} }
if (existingOrganizationChecked.value && existingOrganization.value) { if (!existingOrganizationChecked.value && existingOrganization.value) {
lead.data.organization = existingOrganization.value existingOrganization.value = ''
existingOrganizationChecked.value = false
valueUpdated = true
} }
if (valueUpdated) { let _deal = await call('crm.fcrm.doctype.crm_lead.crm_lead.convert_to_deal', {
updateLead( lead: lead.data.name,
{ deal,
salutation: lead.data.salutation, existing_contact: existingContact.value,
first_name: lead.data.first_name, existing_organization: existingOrganization.value,
last_name: lead.data.last_name, }).catch((err) => {
email_id: lead.data.email_id, createToast({
mobile_no: lead.data.mobile_no, title: __('Error converting to deal'),
organization: lead.data.organization, text: __(err.messages?.[0]),
}, icon: 'x',
'', iconClasses: 'text-ink-red-4',
() => convertToDeal(true),
)
showConvertToDealModal.value = false
} else {
let _deal = await call(
'crm.fcrm.doctype.crm_lead.crm_lead.convert_to_deal',
{ lead: lead.data.name, deal },
).catch((err) => {
createToast({
title: __('Error converting to deal'),
text: __(err.messages?.[0]),
icon: 'x',
iconClasses: 'text-ink-red-4',
})
}) })
if (_deal) { })
capture('convert_lead_to_deal') if (_deal) {
if (updated) { showConvertToDealModal.value = false
await contacts.reload() existingContactChecked.value = false
} existingOrganizationChecked.value = false
router.push({ name: 'Deal', params: { dealId: _deal } }) existingContact.value = ''
} existingOrganization.value = ''
capture('convert_lead_to_deal')
router.push({ name: 'Deal', params: { dealId: _deal } })
} }
} }