fix: auto add assignee if lead_owner or deal_owner is set

This commit is contained in:
Shariq Ansari 2023-12-27 16:33:06 +05:30
parent cb9a11cbda
commit bca741c8bc
4 changed files with 44 additions and 0 deletions

View File

@ -27,4 +27,5 @@ def get_deal(name):
)
deal["doctype_fields"] = get_doctype_fields("CRM Deal")
deal["doctype"] = "CRM Deal"
return deal

View File

@ -1,8 +1,10 @@
# Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt
import json
import frappe
from frappe import _
from frappe.desk.form.assign_to import add as assign
from frappe.model.document import Document
from crm.fcrm.doctype.crm_service_level_agreement.utils import get_sla
@ -15,6 +17,12 @@ class CRMDeal(Document):
def validate(self):
self.set_primary_contact()
self.set_primary_email_mobile_no()
if self.deal_owner and not self.is_new():
self.assign_agent(self.deal_owner)
def after_insert(self):
if self.deal_owner:
self.assign_agent(self.deal_owner)
def before_save(self):
self.apply_sla()
@ -53,6 +61,19 @@ class CRMDeal(Document):
self.email = ""
self.mobile_no = ""
def assign_agent(self, agent):
if not agent:
return
if self._assign:
assignees = json.loads(self._assign)
for assignee in assignees:
if agent == assignee:
# the agent is already set as an assignee
return
assign({"assign_to": [agent], "doctype": "CRM Deal", "name": self.name})
def set_sla(self):
"""
Find an SLA to apply to the deal.

View File

@ -15,4 +15,5 @@ def get_lead(name):
lead = lead.pop()
lead["doctype_fields"] = get_doctype_fields("CRM Lead")
lead["doctype"] = "CRM Lead"
return lead

View File

@ -1,8 +1,10 @@
# Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt
import json
import frappe
from frappe import _
from frappe.desk.form.assign_to import add as assign
from frappe.model.document import Document
from frappe.utils import has_gravatar, validate_email_address
@ -18,6 +20,12 @@ class CRMLead(Document):
self.set_lead_name()
self.set_title()
self.validate_email()
if self.lead_owner and not self.is_new():
self.assign_agent(self.lead_owner)
def after_insert(self):
if self.lead_owner:
self.assign_agent(self.lead_owner)
def before_save(self):
self.apply_sla()
@ -54,6 +62,19 @@ class CRMLead(Document):
if self.is_new() or not self.image:
self.image = has_gravatar(self.email)
def assign_agent(self, agent):
if not agent:
return
if self.get("_assign"):
assignees = json.loads(self._assign)
for assignee in assignees:
if agent == assignee:
# the agent is already set as an assignee
return
assign({"assign_to": [agent], "doctype": "CRM Lead", "name": self.name})
def create_contact(self, throw=True):
if not self.lead_name:
self.set_full_name()