Merge branch 'develop' into crm-carry-forward-communication-comments
diff --git a/erpnext/crm/doctype/opportunity/opportunity.py b/erpnext/crm/doctype/opportunity/opportunity.py
index 0bef80a..3a04df0 100644
--- a/erpnext/crm/doctype/opportunity/opportunity.py
+++ b/erpnext/crm/doctype/opportunity/opportunity.py
@@ -10,6 +10,7 @@
 from frappe.model.mapper import get_mapped_doc
 from frappe.utils import cint, cstr, flt, get_fullname
 
+from erpnext.crm.utils import add_link_in_communication, copy_comments
 from erpnext.setup.utils import get_exchange_rate
 from erpnext.utilities.transaction_base import TransactionBase
 
@@ -19,6 +20,10 @@
 		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 367aa3d..c255310 100644
--- a/erpnext/crm/doctype/prospect/prospect.py
+++ b/erpnext/crm/doctype/prospect/prospect.py
@@ -6,6 +6,8 @@
 from frappe.model.document import Document
 from frappe.model.mapper import get_mapped_doc
 
+from erpnext.crm.utils import add_link_in_communication, copy_comments
+
 
 class Prospect(Document):
 	def onload(self):
@@ -20,6 +22,11 @@
 	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 95b19ec..8ae991f 100644
--- a/erpnext/crm/utils.py
+++ b/erpnext/crm/utils.py
@@ -21,3 +21,25 @@
 			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 c4752ae..2bb9b55 100644
--- a/erpnext/selling/doctype/quotation/quotation.py
+++ b/erpnext/selling/doctype/quotation/quotation.py
@@ -8,6 +8,7 @@
 from frappe.utils import flt, getdate, nowdate
 
 from erpnext.controllers.selling_controller import SellingController
+from erpnext.crm.utils import add_link_in_communication, copy_comments
 
 form_grid_templates = {
 	"items": "templates/form_grid/item_grid.html"
@@ -34,6 +35,15 @@
 		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"))