[fixes] set lead, contact and company on support ticket
diff --git a/support/doctype/support_ticket/get_support_mails.py b/support/doctype/support_ticket/get_support_mails.py
index d1fe0dc..82ae9a3 100644
--- a/support/doctype/support_ticket/get_support_mails.py
+++ b/support/doctype/support_ticket/get_support_mails.py
@@ -50,7 +50,7 @@
 				"subject": mail.mail["Subject"],
 				"raised_by": mail.from_email,
 				"content_type": mail.content_type,
-				"status": "Open"
+				"status": "Open",
 			}])
 			ticket.insert()
 			new_ticket = True
@@ -58,7 +58,7 @@
 		mail.save_attachments_in_doc(ticket.doc)
 				
 		make(content=mail.content, sender=mail.from_email, subject = ticket.doc.subject,
-			doctype="Support Ticket", name=ticket.doc.name, 
+			doctype="Support Ticket", name=ticket.doc.name,
 			lead = ticket.doc.lead, contact=ticket.doc.contact, date=mail.date)
 			
 		if new_ticket and cint(self.email_settings.send_autoreply) and \
diff --git a/support/doctype/support_ticket/support_ticket.py b/support/doctype/support_ticket/support_ticket.py
index ca46a8e..5f01516 100644
--- a/support/doctype/support_ticket/support_ticket.py
+++ b/support/doctype/support_ticket/support_ticket.py
@@ -18,7 +18,6 @@
 import webnotes
 
 from utilities.transaction_base import TransactionBase
-from home import update_feed
 from webnotes.utils import now
 
 class DocType(TransactionBase):
@@ -44,6 +43,7 @@
 	
 	def validate(self):
 		self.update_status()
+		self.set_lead_contact(self.doc.raised_by)
 		
 		if self.doc.status == "Closed":
 			from webnotes.widgets.form.assign_to import clear
@@ -51,10 +51,24 @@
 		
 	def on_communication_sent(self, comm):
 		webnotes.conn.set(self.doc, 'status', 'Waiting for Customer')
-		if comm.lead and not self.doc.lead:
-			webnotes.conn.set(self.doc, 'lead', comm.lead)
-		if comm.contact and not self.doc.contact:
-			webnotes.conn.set(self.doc, 'contact', comm.contact)
+		
+		
+	def set_lead_contact(self, email_id):
+		import email.utils
+		email_id = email.utils.parseaddr(email_id)
+		if email_id:
+			if not self.doc.lead:
+				self.doc.lead = webnotes.conn.get_value("Lead", {"email_id": email_id})
+			if not self.doc.contact:
+				self.doc.contact = webnotes.conn.get_value("Contact", {"email_id": email_id})
+				
+			if not self.doc.company:
+				if self.doc.lead:
+					company = webnotes.conn.get_value("Lead", self.doc.lead, "company")
+				elif self.doc.contact:
+					company = webnotes.conn.get_value("Contact", self.doc.contact, "company")
+				
+				self.doc.company = company or webnotes.conn.get_default("company")
 			
 	def on_trash(self):
 		webnotes.conn.sql("""update `tabCommunication` set support_ticket=NULL 
diff --git a/support/doctype/support_ticket/support_ticket.txt b/support/doctype/support_ticket/support_ticket.txt
index 769bb9d..50e547e 100644
--- a/support/doctype/support_ticket/support_ticket.txt
+++ b/support/doctype/support_ticket/support_ticket.txt
@@ -1,8 +1,8 @@
 [
  {
-  "creation": "2013-01-31 22:22:27", 
+  "creation": "2013-02-01 10:36:25", 
   "docstatus": 0, 
-  "modified": "2013-01-31 22:17:24", 
+  "modified": "2013-05-21 16:27:46", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -23,13 +23,18 @@
   "permlevel": 0
  }, 
  {
+  "amend": 0, 
+  "create": 1, 
   "doctype": "DocPerm", 
   "name": "__common__", 
   "parent": "Support Ticket", 
   "parentfield": "permissions", 
   "parenttype": "DocType", 
+  "permlevel": 0, 
   "read": 1, 
-  "submit": 0
+  "report": 1, 
+  "submit": 0, 
+  "write": 1
  }, 
  {
   "doctype": "DocType", 
@@ -192,6 +197,15 @@
   "read_only": 1
  }, 
  {
+  "doctype": "DocField", 
+  "fieldname": "company", 
+  "fieldtype": "Link", 
+  "label": "Company", 
+  "options": "Company", 
+  "print_hide": 1, 
+  "reqd": 0
+ }, 
+ {
   "depends_on": "eval:!doc.__islocal", 
   "doctype": "DocField", 
   "fieldname": "column_break1", 
@@ -237,51 +251,18 @@
   "label": "Content Type"
  }, 
  {
-  "amend": 0, 
   "cancel": 0, 
-  "create": 1, 
   "doctype": "DocPerm", 
-  "permlevel": 0, 
-  "report": 1, 
-  "role": "Guest", 
-  "write": 1
+  "role": "Guest"
  }, 
  {
-  "create": 1, 
+  "cancel": 0, 
   "doctype": "DocPerm", 
-  "match": "customer", 
-  "permlevel": 0, 
-  "report": 1, 
-  "role": "Customer", 
-  "write": 1
+  "role": "Customer"
  }, 
  {
   "cancel": 1, 
-  "create": 1, 
   "doctype": "DocPerm", 
-  "permlevel": 0, 
-  "report": 1, 
-  "role": "Support Team", 
-  "write": 1
- }, 
- {
-  "amend": 0, 
-  "cancel": 0, 
-  "create": 0, 
-  "doctype": "DocPerm", 
-  "match": "", 
-  "permlevel": 1, 
-  "report": 1, 
-  "role": "Support Team", 
-  "write": 1
- }, 
- {
-  "amend": 0, 
-  "cancel": 0, 
-  "create": 0, 
-  "doctype": "DocPerm", 
-  "match": "", 
-  "permlevel": 2, 
   "role": "Support Team"
  }
 ]
\ No newline at end of file