From 1e2b27594c8c175758324f44321c6a83a3235a1e Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Mon, 26 Feb 2024 16:28:01 +0530 Subject: [PATCH] fix: share deal when deal_owner is changed --- crm/fcrm/doctype/crm_deal/crm_deal.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/crm/fcrm/doctype/crm_deal/crm_deal.py b/crm/fcrm/doctype/crm_deal/crm_deal.py index 44034e92..be40f3d4 100644 --- a/crm/fcrm/doctype/crm_deal/crm_deal.py +++ b/crm/fcrm/doctype/crm_deal/crm_deal.py @@ -20,6 +20,7 @@ class CRMDeal(Document): self.set_primary_email_mobile_no() self.update_organization() if self.deal_owner and not self.is_new(): + self.share_with_agent(self.deal_owner) self.assign_agent(self.deal_owner) if self.has_value_changed("status"): add_status_change_log(self) @@ -95,6 +96,27 @@ class CRMDeal(Document): assign({"assign_to": [agent], "doctype": "CRM Deal", "name": self.name}) + def share_with_agent(self, agent): + if not agent: + return + + docshares = frappe.get_all( + "DocShare", + filters={"share_name": self.name, "share_doctype": self.doctype}, + fields=["name", "user"], + ) + + shared_with = [d.user for d in docshares] + [agent] + + for user in shared_with: + if user == agent and not frappe.db.exists("DocShare", {"user": agent, "share_name": self.name, "share_doctype": self.doctype}): + frappe.share.add_docshare( + self.doctype, self.name, agent, write=1, flags={"ignore_share_permission": True} + ) + elif user != agent: + frappe.share.remove(self.doctype, self.name, user) + + def set_sla(self): """ Find an SLA to apply to the deal.