From a9318d71c02b07a5adc8b8d485cb7ca852679e7d Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Mon, 23 Sep 2024 21:14:39 +0530 Subject: [PATCH] fix: show assigned & unassigned notification for Lead, Deal and Task --- crm/api/todo.py | 97 ++++++++++++++++++- .../crm_notification/crm_notification.json | 4 +- crm/fcrm/doctype/crm_task/crm_task.py | 30 +----- crm/hooks.py | 5 +- 4 files changed, 102 insertions(+), 34 deletions(-) diff --git a/crm/api/todo.py b/crm/api/todo.py index 7792e848..e4bb50b5 100644 --- a/crm/api/todo.py +++ b/crm/api/todo.py @@ -1,8 +1,103 @@ import frappe +from frappe import _ +from crm.fcrm.doctype.crm_notification.crm_notification import notify_user def after_insert(doc, method): if doc.reference_type in ["CRM Lead", "CRM Deal"] and doc.reference_name and doc.allocated_to: fieldname = "lead_owner" if doc.reference_type == "CRM Lead" else "deal_owner" lead_owner = frappe.db.get_value(doc.reference_type, doc.reference_name, fieldname) if not lead_owner: - frappe.db.set_value(doc.reference_type, doc.reference_name, fieldname, doc.allocated_to) \ No newline at end of file + 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""" +
+ { _('Your assignment on {0} {1} has been removed by {2}').format( + doctype, + f'{ name }', + f'{ owner }' + ) } +
+ """ + + return f""" +
+ { owner } + { _('assigned a {0} {1} to you').format( + doctype, + f'{ name }' + ) } +
+ """ + + if doc.reference_type == "CRM Task": + if is_cancelled: + return f""" +
+ { _('Your assignment on task {0} has been removed by {1}').format( + f'{ reference_doc.title }', + f'{ owner }' + ) } +
+ """ + return f""" +
+ { owner } + { _('assigned a new task {0} to you').format( + f'{ reference_doc.title }' + ) } +
+ """ + +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 diff --git a/crm/fcrm/doctype/crm_notification/crm_notification.json b/crm/fcrm/doctype/crm_notification/crm_notification.json index cc4302b3..f6bc0380 100644 --- a/crm/fcrm/doctype/crm_notification/crm_notification.json +++ b/crm/fcrm/doctype/crm_notification/crm_notification.json @@ -34,7 +34,7 @@ "fieldtype": "Select", "in_list_view": 1, "label": "Type", - "options": "Mention\nTask\nWhatsApp", + "options": "Mention\nTask\nAssignment\nWhatsApp", "reqd": 1 }, { @@ -116,7 +116,7 @@ ], "index_web_pages_for_search": 1, "links": [], - "modified": "2024-09-23 17:22:06.973897", + "modified": "2024-09-23 19:34:08.635305", "modified_by": "Administrator", "module": "FCRM", "name": "CRM Notification", diff --git a/crm/fcrm/doctype/crm_task/crm_task.py b/crm/fcrm/doctype/crm_task/crm_task.py index fed31c1c..06b50c4d 100644 --- a/crm/fcrm/doctype/crm_task/crm_task.py +++ b/crm/fcrm/doctype/crm_task/crm_task.py @@ -4,7 +4,7 @@ import frappe from frappe import _ 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 @@ -31,34 +31,6 @@ class CRMTask(Document): "name": self.name, "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""" -
- { owner } - { _('assigned a new task {0} to you').format( - f'{ self.title }' - ) } -
- """ - - 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 diff --git a/crm/hooks.py b/crm/hooks.py index fa7e606a..286eee4d 100644 --- a/crm/hooks.py +++ b/crm/hooks.py @@ -144,6 +144,7 @@ doc_events = { }, "ToDo": { "after_insert": ["crm.api.todo.after_insert"], + "on_update": ["crm.api.todo.on_update"], }, "Comment": { "on_update": ["crm.api.comment.on_update"], @@ -152,8 +153,8 @@ doc_events = { "validate": ["crm.api.whatsapp.validate"], "on_update": ["crm.api.whatsapp.on_update"], }, - "CRM Deal": { - "on_update": ["crm.fcrm.doctype.erpnext_crm_settings.erpnext_crm_settings.create_customer_in_erpnext"], + "CRM Deal": { + "on_update": ["crm.fcrm.doctype.erpnext_crm_settings.erpnext_crm_settings.create_customer_in_erpnext"], }, }