[lead] [next contact] [fix] create event
diff --git a/selling/doctype/lead/lead.py b/selling/doctype/lead/lead.py
index 571cdfd..d80f843 100644
--- a/selling/doctype/lead/lead.py
+++ b/selling/doctype/lead/lead.py
@@ -17,8 +17,7 @@
 from __future__ import unicode_literals
 import webnotes
 from webnotes import _
-from webnotes.utils import cstr, validate_email_add
-from webnotes.model.doc import Document, addchild
+from webnotes.utils import cstr, validate_email_add, cint
 from webnotes import session, msgprint
 
 sql = webnotes.conn.sql
@@ -54,11 +53,17 @@
 			if not validate_email_add(self.doc.email_id):
 				msgprint('Please enter valid email id.')
 				raise Exception
+		
+		self._prev = webnotes._dict({
+			"contact_date": webnotes.conn.get_value("Lead", self.doc.name, "contact_date") if \
+				(not cint(self.doc.fields.get("__islocal"))) else None,
+			"contact_by": webnotes.conn.get_value("Lead", self.doc.name, "contact_by") if \
+				(not cint(self.doc.fields.get("__islocal"))) else None,
+		})
 				
 	
 	def on_update(self):
-		if self.doc.contact_date:
-			self.add_calendar_event()
+		self.add_calendar_event()
 			
 		self.check_email_id_is_unique()
 
@@ -73,25 +78,33 @@
 					", ".join(items), raise_exception=True)
 		
 	def add_calendar_event(self):
-		# delete any earlier event by this lead
-		sql("delete from tabEvent where ref_type='Lead' and ref_name=%s", self.doc.name)
-	
-		# create new event
-		ev = Document('Event')
-		ev.owner = self.doc.lead_owner
-		ev.description = ('Contact ' + cstr(self.doc.lead_name)) + \
-			(self.doc.contact_by and ('. By : ' + cstr(self.doc.contact_by)) or '') + \
-			(self.doc.remark and ('.To Discuss : ' + cstr(self.doc.remark)) or '')
-		ev.event_date = self.doc.contact_date
-		ev.event_hour = '10:00'
-		ev.event_type = 'Private'
-		ev.ref_type = 'Lead'
-		ev.ref_name = self.doc.name
-		ev.save(1)
-		
-		event_user = addchild(ev, 'event_individuals', 'Event User')
-		event_user.person = self.doc.contact_by
-		event_user.save()
+		if self.doc.contact_by != cstr(self._prev.contact_by) or \
+				self.doc.contact_date != cstr(self._prev.contact_date):
+			# delete any earlier event by this lead
+			for name in webnotes.conn.sql_list("""select name from `tabEvent` 
+				where ref_type="Lead" and ref_name=%s""", self.doc.name):
+					webnotes.delete_doc("Event", name)
+			
+			if self.doc.contact_date:	
+				webnotes.bean([
+					{
+						"doctype": "Event",
+						"owner": self.doc.lead_owner or self.doc.owner,
+						"subject": ('Contact ' + cstr(self.doc.lead_name)),
+						"description": ('Contact ' + cstr(self.doc.lead_name)) + \
+							(self.doc.contact_by and ('. By : ' + cstr(self.doc.contact_by)) or '') + \
+							(self.doc.remark and ('.To Discuss : ' + cstr(self.doc.remark)) or ''),
+						"starts_on": self.doc.contact_date + " 10:00:00",
+						"event_type": "Private",
+						"ref_type": "Lead",
+						"ref_name": self.doc.name
+					},
+					{
+						"doctype": "Event User",
+						"parentfield": "event_individuals",
+						"person": self.doc.contact_by
+					}
+				]).insert()
 
 	def get_sender(self, comm):
 		return webnotes.conn.get_value('Sales Email Settings',None,'email_id')