[minor] don't create lead if customer contact is already created against contact_email (#10976)

* [minor] don't create lead if customer contact is already created against contact_email

* [tests] added tests cases for opportunity to check if lead is required or not
diff --git a/erpnext/crm/doctype/opportunity/opportunity.py b/erpnext/crm/doctype/opportunity/opportunity.py
index 4251cae..970fd57 100644
--- a/erpnext/crm/doctype/opportunity/opportunity.py
+++ b/erpnext/crm/doctype/opportunity/opportunity.py
@@ -42,10 +42,28 @@
 		if not self.with_items:
 			self.items = []
 
-
 	def make_new_lead_if_required(self):
 		"""Set lead against new opportunity"""
 		if not (self.lead or self.customer) and self.contact_email:
+			# check if customer is already created agains the self.contact_email
+			customer = frappe.db.sql("""select
+				distinct `tabDynamic Link`.link_name as customer
+				from 
+					`tabContact`,
+					`tabDynamic Link`
+				where `tabContact`.email_id='{0}'
+				and 
+					`tabContact`.name=`tabDynamic Link`.parent
+				and
+					ifnull(`tabDynamic Link`.link_name, '')<>'' 
+				and 
+					`tabDynamic Link`.link_doctype='Customer' 
+			""".format(self.contact_email), as_dict=True)
+			if customer and customer[0].customer:
+				self.customer = customer[0].customer
+				self.enquiry_from = "Customer"
+				return
+
 			lead_name = frappe.db.get_value("Lead", {"email_id": self.contact_email})
 			if not lead_name:
 				sender_name = get_fullname(self.contact_email)
diff --git a/erpnext/crm/doctype/opportunity/test_opportunity.py b/erpnext/crm/doctype/opportunity/test_opportunity.py
index 4cd20ea..61b583c 100644
--- a/erpnext/crm/doctype/opportunity/test_opportunity.py
+++ b/erpnext/crm/doctype/opportunity/test_opportunity.py
@@ -4,6 +4,7 @@
 
 import frappe
 from frappe.utils import today
+from erpnext.crm.doctype.lead.lead import make_customer
 from erpnext.crm.doctype.opportunity.opportunity import make_quotation
 import unittest
 
@@ -25,12 +26,45 @@
 		doc = frappe.get_doc('Opportunity', doc.name)
 		self.assertEquals(doc.status, "Quotation")
 
+	def test_make_new_lead_if_required(self):
+		args = {
+			"doctype": "Opportunity",
+			"contact_email":"new.opportunity@example.com",
+			"enquiry_type": "Sales",
+			"with_items": 0,
+			"transaction_date": today()
+		}
+		# new lead should be created against the new.opportunity@example.com
+		opp_doc = frappe.get_doc(args).insert(ignore_permissions=True)
+
+		self.assertTrue(opp_doc.lead)
+		self.assertEquals(opp_doc.enquiry_from, "Lead")
+		self.assertEquals(frappe.db.get_value("Lead", opp_doc.lead, "email_id"),
+			'new.opportunity@example.com')
+
+		# create new customer and create new contact against 'new.opportunity@example.com'
+		customer = make_customer(opp_doc.lead).insert(ignore_permissions=True)
+		frappe.get_doc({
+			"doctype": "Contact",
+			"email_id": "new.opportunity@example.com",
+			"first_name": "_Test Opportunity Customer",
+			"links": [{
+				"link_doctype": "Customer",
+				"link_name": customer.name
+			}]
+		}).insert(ignore_permissions=True)
+
+		opp_doc = frappe.get_doc(args).insert(ignore_permissions=True)
+		self.assertTrue(opp_doc.customer)
+		self.assertEquals(opp_doc.enquiry_from, "Customer")
+		self.assertEquals(opp_doc.customer, customer.name)
+
 def make_opportunity(**args):
 	args = frappe._dict(args)
 
 	opp_doc = frappe.get_doc({
 		"doctype": "Opportunity",
-		"enquiry_from": "Customer" or args.enquiry_from,
+		"enquiry_from": args.enquiry_from or "Customer",
 		"enquiry_type": "Sales",
 		"with_items": args.with_items or 0,
 		"transaction_date": today()