From 6eb779392edaca95e5d90e610246164524e17b36 Mon Sep 17 00:00:00 2001 From: Anupam Date: Fri, 19 Nov 2021 10:13:05 +0530 Subject: [PATCH] feat: carry forward communication and comments throughout the sales cycle --- .../crm/doctype/opportunity/opportunity.py | 5 +++++ erpnext/crm/doctype/prospect/prospect.py | 7 ++++++ erpnext/crm/utils.py | 22 +++++++++++++++++++ .../selling/doctype/quotation/quotation.py | 10 +++++++++ 4 files changed, 44 insertions(+) diff --git a/erpnext/crm/doctype/opportunity/opportunity.py b/erpnext/crm/doctype/opportunity/opportunity.py index 0bef80a749..d565ad2f2c 100644 --- a/erpnext/crm/doctype/opportunity/opportunity.py +++ b/erpnext/crm/doctype/opportunity/opportunity.py @@ -12,6 +12,7 @@ from frappe.utils import cint, cstr, flt, get_fullname from erpnext.setup.utils import get_exchange_rate from erpnext.utilities.transaction_base import TransactionBase +from erpnext.crm.utils import copy_comments, add_link_in_communication class Opportunity(TransactionBase): @@ -19,6 +20,10 @@ class Opportunity(TransactionBase): if self.opportunity_from == "Lead": frappe.get_doc("Lead", self.party_name).set_status(update=True) + if self.opportunity_from in ["Lead", "Prospect"]: + copy_comments(self.opportunity_from, self.party_name, self) + add_link_in_communication(self.opportunity_from, self.party_name, self) + def validate(self): self._prev = frappe._dict({ "contact_date": frappe.db.get_value("Opportunity", self.name, "contact_date") if \ diff --git a/erpnext/crm/doctype/prospect/prospect.py b/erpnext/crm/doctype/prospect/prospect.py index 367aa3d312..736b3dfdd5 100644 --- a/erpnext/crm/doctype/prospect/prospect.py +++ b/erpnext/crm/doctype/prospect/prospect.py @@ -6,6 +6,8 @@ from frappe.contacts.address_and_contact import load_address_and_contact from frappe.model.document import Document from frappe.model.mapper import get_mapped_doc +from erpnext.crm.utils import copy_comments, add_link_in_communication + class Prospect(Document): def onload(self): @@ -20,6 +22,11 @@ class Prospect(Document): def on_trash(self): self.unlink_dynamic_links() + def after_insert(self): + for row in self.get('prospect_lead'): + copy_comments("Lead", row.lead, self) + add_link_in_communication("Lead", row.lead, self) + def update_lead_details(self): for row in self.get('prospect_lead'): lead = frappe.get_value('Lead', row.lead, ['lead_name', 'status', 'email_id', 'mobile_no'], as_dict=True) diff --git a/erpnext/crm/utils.py b/erpnext/crm/utils.py index 95b19ec21e..531d6c1594 100644 --- a/erpnext/crm/utils.py +++ b/erpnext/crm/utils.py @@ -21,3 +21,25 @@ def update_lead_phone_numbers(contact, method): lead = frappe.get_doc("Lead", contact_lead) lead.db_set("phone", phone) lead.db_set("mobile_no", mobile_no) + +def copy_comments(doctype, docname, doc): + comments = frappe.db.get_values("Comment", filters={"reference_doctype": doctype, "reference_name": docname}, fieldname="*") + for comment in comments: + comment = frappe.get_doc(comment.update({"doctype":"Comment"})) + comment.name = None + comment.reference_doctype = doc.doctype + comment.reference_name = doc.name + comment.insert() + +def add_link_in_communication(doctype, docname, doc): + communications = frappe.get_all("Communication", filters={"reference_doctype": doctype, "reference_name": docname}, pluck='name') + communication_links = frappe.get_all('Communication Link', + { + "link_doctype": doctype, + "link_name": docname, + "parent": ("not in", communications) + }, pluck="parent") + + for communication in communications + communication_links: + communication_doc = frappe.get_doc("Communication", communication) + communication_doc.add_link(doc.doctype, doc.name, autosave=True) diff --git a/erpnext/selling/doctype/quotation/quotation.py b/erpnext/selling/doctype/quotation/quotation.py index c4752aebb5..adea9ec406 100644 --- a/erpnext/selling/doctype/quotation/quotation.py +++ b/erpnext/selling/doctype/quotation/quotation.py @@ -8,6 +8,7 @@ from frappe.model.mapper import get_mapped_doc from frappe.utils import flt, getdate, nowdate from erpnext.controllers.selling_controller import SellingController +from erpnext.crm.utils import copy_comments, add_link_in_communication form_grid_templates = { "items": "templates/form_grid/item_grid.html" @@ -34,6 +35,15 @@ class Quotation(SellingController): from erpnext.stock.doctype.packed_item.packed_item import make_packing_list make_packing_list(self) + def after_insert(self): + if self.opportunity: + copy_comments("Opportunity", self.opportunity, self) + add_link_in_communication("Opportunity", self.opportunity, self) + + elif self.quotation_to == "Lead" and self.party_name: + copy_comments("Lead", self.party_name, self) + add_link_in_communication("Lead", self.party_name, self) + def validate_valid_till(self): if self.valid_till and getdate(self.valid_till) < getdate(self.transaction_date): frappe.throw(_("Valid till date cannot be before transaction date"))