diff --git a/crm/fcrm/doctype/crm_deal/api.py b/crm/fcrm/doctype/crm_deal/api.py index 5e1e562e..75a89de1 100644 --- a/crm/fcrm/doctype/crm_deal/api.py +++ b/crm/fcrm/doctype/crm_deal/api.py @@ -27,4 +27,5 @@ def get_deal(name): ) deal["doctype_fields"] = get_doctype_fields("CRM Deal") + deal["doctype"] = "CRM Deal" return deal diff --git a/crm/fcrm/doctype/crm_deal/crm_deal.py b/crm/fcrm/doctype/crm_deal/crm_deal.py index c978eee6..076c62ee 100644 --- a/crm/fcrm/doctype/crm_deal/crm_deal.py +++ b/crm/fcrm/doctype/crm_deal/crm_deal.py @@ -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. diff --git a/crm/fcrm/doctype/crm_lead/api.py b/crm/fcrm/doctype/crm_lead/api.py index 26c58cff..0cb80556 100644 --- a/crm/fcrm/doctype/crm_lead/api.py +++ b/crm/fcrm/doctype/crm_lead/api.py @@ -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 \ No newline at end of file diff --git a/crm/fcrm/doctype/crm_lead/crm_lead.py b/crm/fcrm/doctype/crm_lead/crm_lead.py index 0f07908d..779e0085 100644 --- a/crm/fcrm/doctype/crm_lead/crm_lead.py +++ b/crm/fcrm/doctype/crm_lead/crm_lead.py @@ -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()