fix: show assigned & unassigned notification for Lead, Deal and Task

This commit is contained in:
Shariq Ansari 2024-09-23 21:14:39 +05:30
parent d479315e18
commit a9318d71c0
4 changed files with 102 additions and 34 deletions

View File

@ -1,4 +1,6 @@
import frappe import frappe
from frappe import _
from crm.fcrm.doctype.crm_notification.crm_notification import notify_user
def after_insert(doc, method): def after_insert(doc, method):
if doc.reference_type in ["CRM Lead", "CRM Deal"] and doc.reference_name and doc.allocated_to: if doc.reference_type in ["CRM Lead", "CRM Deal"] and doc.reference_name and doc.allocated_to:
@ -6,3 +8,96 @@ def after_insert(doc, method):
lead_owner = frappe.db.get_value(doc.reference_type, doc.reference_name, fieldname) lead_owner = frappe.db.get_value(doc.reference_type, doc.reference_name, fieldname)
if not lead_owner: if not lead_owner:
frappe.db.set_value(doc.reference_type, doc.reference_name, fieldname, doc.allocated_to) frappe.db.set_value(doc.reference_type, doc.reference_name, fieldname, doc.allocated_to)
if doc.reference_type in ["CRM Lead", "CRM Deal", "CRM Task"] and doc.reference_name and doc.allocated_to:
notify_assigned_user(doc)
def on_update(doc, method):
if doc.has_value_changed("status") and doc.status == "Cancelled" and doc.reference_type in ["CRM Lead", "CRM Deal", "CRM Task"] and doc.reference_name and doc.allocated_to:
notify_assigned_user(doc, is_cancelled=True)
def notify_assigned_user(doc, is_cancelled=False):
_doc = frappe.get_doc(doc.reference_type, doc.reference_name)
owner = frappe.get_cached_value("User", frappe.session.user, "full_name")
notification_text = get_notification_text(owner, doc, _doc, is_cancelled)
message = _("Your assignment on {0} {1} has been removed by {2}").format(
doc.reference_type,
doc.reference_name,
owner
) if is_cancelled else _("{0} assigned a {1} {2} to you").format(
owner,
doc.reference_type,
doc.reference_name
)
redirect_to_doctype, redirect_to_name = get_redirect_to_doc(doc)
notify_user({
"owner": frappe.session.user,
"assigned_to": doc.allocated_to,
"notification_type": "Assignment",
"message": message,
"notification_text": notification_text,
"reference_doctype": doc.reference_type,
"reference_docname": doc.reference_name,
"redirect_to_doctype": redirect_to_doctype,
"redirect_to_docname": redirect_to_name,
})
def get_notification_text(owner, doc, reference_doc, is_cancelled=False):
name = doc.reference_name
doctype = doc.reference_type
if doctype.startswith("CRM "):
doctype = doctype[4:].lower()
if doctype in ["CRM Lead", "CRM Deal"]:
name = reference_doc.lead_name or name if doctype == "CRM Lead" else reference_doc.organization or reference_doc.lead_name or name
if is_cancelled:
return f"""
<div class="mb-2 leading-5 text-gray-600">
<span>{ _('Your assignment on {0} {1} has been removed by {2}').format(
doctype,
f'<span class="font-medium text-gray-900">{ name }</span>',
f'<span class="font-medium text-gray-900">{ owner }</span>'
) }</span>
</div>
"""
return f"""
<div class="mb-2 leading-5 text-gray-600">
<span class="font-medium text-gray-900">{ owner }</span>
<span>{ _('assigned a {0} {1} to you').format(
doctype,
f'<span class="font-medium text-gray-900">{ name }</span>'
) }</span>
</div>
"""
if doc.reference_type == "CRM Task":
if is_cancelled:
return f"""
<div class="mb-2 leading-5 text-gray-600">
<span>{ _('Your assignment on task {0} has been removed by {1}').format(
f'<span class="font-medium text-gray-900">{ reference_doc.title }</span>',
f'<span class="font-medium text-gray-900">{ owner }</span>'
) }</span>
</div>
"""
return f"""
<div class="mb-2 leading-5 text-gray-600">
<span class="font-medium text-gray-900">{ owner }</span>
<span>{ _('assigned a new task {0} to you').format(
f'<span class="font-medium text-gray-900">{ reference_doc.title }</span>'
) }</span>
</div>
"""
def get_redirect_to_doc(doc):
if doc.reference_type == "CRM Task":
reference_doc = frappe.get_doc(doc.reference_type, doc.reference_name)
return reference_doc.reference_doctype, reference_doc.reference_docname
return doc.reference_type, doc.reference_name

View File

@ -34,7 +34,7 @@
"fieldtype": "Select", "fieldtype": "Select",
"in_list_view": 1, "in_list_view": 1,
"label": "Type", "label": "Type",
"options": "Mention\nTask\nWhatsApp", "options": "Mention\nTask\nAssignment\nWhatsApp",
"reqd": 1 "reqd": 1
}, },
{ {
@ -116,7 +116,7 @@
], ],
"index_web_pages_for_search": 1, "index_web_pages_for_search": 1,
"links": [], "links": [],
"modified": "2024-09-23 17:22:06.973897", "modified": "2024-09-23 19:34:08.635305",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "FCRM", "module": "FCRM",
"name": "CRM Notification", "name": "CRM Notification",

View File

@ -4,7 +4,7 @@
import frappe import frappe
from frappe import _ from frappe import _
from frappe.model.document import Document from frappe.model.document import Document
from frappe.desk.form.assign_to import add as assign from frappe.desk.form.assign_to import add as assign, remove as unassign
from crm.fcrm.doctype.crm_notification.crm_notification import notify_user from crm.fcrm.doctype.crm_notification.crm_notification import notify_user
@ -31,34 +31,6 @@ class CRMTask(Document):
"name": self.name, "name": self.name,
"description": self.title or self.description, "description": self.title or self.description,
}) })
self.notify_assigned_user()
def notify_assigned_user(self):
"""
Notify the assigned user about the task assignment
"""
owner = frappe.get_cached_value("User", self.owner, "full_name")
notification_text = f"""
<div class="mb-2 leading-5 text-gray-600">
<span class="font-medium text-gray-900">{ owner }</span>
<span>{ _('assigned a new task {0} to you').format(
f'<span class="font-medium text-gray-900">{ self.title }</span>'
) }</span>
</div>
"""
notify_user({
"owner": self.owner,
"assigned_to": self.assigned_to,
"notification_type": "Task",
"message": self.description,
"notification_text": notification_text,
"doctype": self.doctype,
"name": self.name,
"reference_doctype": self.reference_doctype,
"reference_docname": self.reference_docname,
})
@staticmethod @staticmethod

View File

@ -144,6 +144,7 @@ doc_events = {
}, },
"ToDo": { "ToDo": {
"after_insert": ["crm.api.todo.after_insert"], "after_insert": ["crm.api.todo.after_insert"],
"on_update": ["crm.api.todo.on_update"],
}, },
"Comment": { "Comment": {
"on_update": ["crm.api.comment.on_update"], "on_update": ["crm.api.comment.on_update"],
@ -152,8 +153,8 @@ doc_events = {
"validate": ["crm.api.whatsapp.validate"], "validate": ["crm.api.whatsapp.validate"],
"on_update": ["crm.api.whatsapp.on_update"], "on_update": ["crm.api.whatsapp.on_update"],
}, },
"CRM Deal": { "CRM Deal": {
"on_update": ["crm.fcrm.doctype.erpnext_crm_settings.erpnext_crm_settings.create_customer_in_erpnext"], "on_update": ["crm.fcrm.doctype.erpnext_crm_settings.erpnext_crm_settings.create_customer_in_erpnext"],
}, },
} }