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:
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:
self.set_full_name()
self.set_lead_name()
existing_contact = self.contact_exists(throw)
existing_contact = existing_contact or self.contact_exists(throw)
if existing_contact:
self.update_lead_contact(existing_contact)
return existing_contact
contact = frappe.new_doc("Contact")
@ -151,12 +152,15 @@ class CRMLead(Document):
return contact.name
def create_organization(self):
if not self.organization:
def create_organization(self, existing_organization=None):
if not self.organization and not existing_organization:
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:
self.db_set("organization", existing_organization)
return existing_organization
organization = frappe.new_doc("CRM Organization")
@ -172,6 +176,20 @@ class CRMLead(Document):
organization.insert(ignore_permissions=True)
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):
email_exist = frappe.db.exists("Contact Email", {"email_id": self.email})
phone_exist = frappe.db.exists("Contact Phone", {"phone": self.phone})
@ -383,7 +401,7 @@ class CRMLead(Document):
@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(
"CRM Lead", "write", lead
):
@ -395,7 +413,7 @@ def convert_to_deal(lead, doc=None, deal=None):
lead.db_set("converted", 1)
if lead.sla and frappe.db.exists("CRM Communication Status", "Replied"):
lead.db_set("communication_status", "Replied")
contact = lead.create_contact(False)
organization = lead.create_organization()
contact = lead.create_contact(existing_contact, False)
organization = lead.create_organization(existing_organization)
_deal = lead.create_deal(contact, organization, deal)
return _deal

View File

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