Merge branch 'shf_rename'

Conflicts:
	erpnext/patches/patch_list.py
diff --git a/erpnext/accounts/doctype/account/account.py b/erpnext/accounts/doctype/account/account.py
index 1d739e2..a9bbcb1 100644
--- a/erpnext/accounts/doctype/account/account.py
+++ b/erpnext/accounts/doctype/account/account.py
@@ -136,7 +136,7 @@
 	# check if child exists
 	# ==================================================================
 	def check_if_child_exists(self):
-		return sql("select name from `tabAccount` where parent_account = %s and docstatus != 2", self.doc.name, debug=0)
+		return sql("select name from `tabAccount` where parent_account = %s and docstatus != 2", self.doc.name)
 	
 	# Update balance
 	# ==================================================================
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index 7f87d3b..c4ed73c 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -98,7 +98,13 @@
 	# Get Account Head to which amount needs to be Debited based on Customer
 	# ----------------------------------------------------------------------
 	def get_customer_account(self):
-		acc_head = webnotes.conn.sql("select name from `tabAccount` where (name = %s or (master_name = %s and master_type = 'customer')) and docstatus != 2", (cstr(self.doc.customer) + " - " + self.get_company_abbr(),self.doc.customer))
+		if not self.doc.company:
+			msgprint("Please select company first and re-select the customer after doing so", raise_exception=1)
+			
+		acc_head = webnotes.conn.sql("""select name from `tabAccount` 
+			where (name = %s or (master_name = %s and master_type = 'customer')) and docstatus != 2""", 
+			(cstr(self.doc.customer) + " - " + self.get_company_abbr(),self.doc.customer))
+			
 		if acc_head and acc_head[0][0]:
 			return acc_head[0][0]
 		else:
diff --git a/erpnext/accounts/search_criteria/accounts_payable/accounts_payable.py b/erpnext/accounts/search_criteria/accounts_payable/accounts_payable.py
index 2ace21c..4ecba4a 100644
--- a/erpnext/accounts/search_criteria/accounts_payable/accounts_payable.py
+++ b/erpnext/accounts/search_criteria/accounts_payable/accounts_payable.py
@@ -73,7 +73,7 @@
 if filter_values.has_key('aging_based_on') and filter_values['aging_based_on']:
 	aging_based_on = filter_values['aging_based_on'].split(NEWLINE)[-1]
 
-if	len(res) > 600 and from_export == 0:
+if	len(res) > 2000 and from_export == 0:
 	msgprint("This is a very large report and cannot be shown in the browser as it is likely to make your browser very slow.Please select Account or click on 'Export' to open in excel")
 	raise Exception
 
diff --git a/erpnext/accounts/search_criteria/accounts_receivable/accounts_receivable.py b/erpnext/accounts/search_criteria/accounts_receivable/accounts_receivable.py
index 6cd71fe..48899dc 100644
--- a/erpnext/accounts/search_criteria/accounts_receivable/accounts_receivable.py
+++ b/erpnext/accounts/search_criteria/accounts_receivable/accounts_receivable.py
@@ -68,7 +68,7 @@
 # ageing based on
 aging_based_on = filter_values.get('aging_based_on') and filter_values['aging_based_on'].split(NEWLINE)[-1] or 'Aging Date'
 
-if  len(res) > 600 and from_export == 0:
+if  len(res) > 2000 and from_export == 0:
   msgprint("This is a very large report and cannot be shown in the browser as it is likely to make your browser very slow.Please select Account or click on 'Export' to open in excel")
   raise Exception
 
diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.js b/erpnext/buying/doctype/purchase_order/purchase_order.js
index 559779e..c3c3a38 100644
--- a/erpnext/buying/doctype/purchase_order/purchase_order.js
+++ b/erpnext/buying/doctype/purchase_order/purchase_order.js
@@ -130,16 +130,6 @@
 	return 'SELECT DISTINCT `tabPurchase Request`.`name` FROM `tabPurchase Request` WHERE `tabPurchase Request`.company = "' + doc.company + '" and `tabPurchase Request`.`docstatus` = 1 and `tabPurchase Request`.`status` != "Stopped" and ifnull(`tabPurchase Request`.`per_ordered`,0) < 100 and `tabPurchase Request`.%(key)s LIKE "%s" ORDER BY `tabPurchase Request`.`name` DESC LIMIT 50';
 }
 
-/*
-//*********** get approved supplier quotation ********************
-cur_frm.fields_dict['supplier_qtn'].get_query = function(doc) {
-	var cond='';
-	if(doc.supplier) cond = 'ifnull(`tabSupplier Quotation`.supplier, "") = "'+doc.supplier+'" and';
-	
-	return repl('SELECT DISTINCT `tabSupplier Quotation`.`name` FROM `tabSupplier Quotation` WHERE `tabSupplier Quotation`.company = "%(company)s" and`tabSupplier Quotation`.`docstatus` = 1 and `tabSupplier Quotation`.`approval_status` = "Approved" and %(cond)s `tabSupplier Quotation`.%(key)s LIKE "%s" ORDER BY `tabSupplier Quotation`.`name` DESC LIMIT 50', {company:doc.company,cond:cond});
-}
-*/
-
 
 //========================= Get Last Purhase Rate =====================================
 cur_frm.cscript.get_last_purchase_rate = function(doc, cdt, cdn){
diff --git a/erpnext/hr/doctype/attendance_control_panel/attendance_control_panel.py b/erpnext/hr/doctype/attendance_control_panel/attendance_control_panel.py
index 0e17494..06a9a76 100644
--- a/erpnext/hr/doctype/attendance_control_panel/attendance_control_panel.py
+++ b/erpnext/hr/doctype/attendance_control_panel/attendance_control_panel.py
@@ -8,11 +8,11 @@
 # 
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
 # GNU General Public License for more details.
-# 
+# 	
 # You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+# along with this program.	If not, see <http://www.gnu.org/licenses/>.
 
 # Please edit this list and import only required elements
 import webnotes
@@ -34,89 +34,85 @@
 
 
 class DocType:
-  def __init__(self,d,dt):
-    self.doc, self.doclist = d,dt
-    
-  #==========================================================================
-  def get_att_list(self):
-    
-    lst = [['Attendance','','','Please fill columns which are Mandatory.',' Please do not modify the structure','',''],['','','','','','',''],['[Mandatory]','','[Mandatory]','[Mandatory]','[Mandatory]','[Mandatory]','[Mandatory]'],['Employee','Employee Name','Attendance Date','Status','Fiscal Year','Company','Naming Series']]
-    
-    dt = self.date_diff_list()          # get date list inbetween from date and to date
-    
-    att_dt = self.get_att_data()        # get default attendance data like fiscal yr, company, naming series
-      
-    fy, comp, sr = att_dt['fy'], att_dt['comp'], att_dt['sr']
-   
-    res = sql("select name, employee_name from `tabEmployee` where status = 'Active' and docstatus !=2") 
-   
-    for d in dt:
-      for r in res:       
-        lst.append([r[0],r[1],d,'',fy,comp,sr])
+	def __init__(self,d,dt):
+		self.doc, self.doclist = d,dt
+		
+	#==========================================================================
+	def get_att_list(self):
+		lst = [['Attendance','','','Please fill columns which are Mandatory.',' Please do not modify the structure','',''],['','','','','','',''],['[Mandatory]','','[Mandatory]','[Mandatory]','[Mandatory]','[Mandatory]','[Mandatory]'],['Employee','Employee Name','Attendance Date','Status','Fiscal Year','Company','Naming Series']]
+		
+		dt = self.date_diff_list()					# get date list inbetween from date and to date		
+		att_dt = self.get_att_data()				# get default attendance data like fiscal yr, company, naming series
+			
+		fy, comp, sr = att_dt['fy'], att_dt['comp'], att_dt['sr']	 
+		res = sql("select name, employee_name from `tabEmployee` where status = 'Active' and docstatus !=2") 
+	 
+		for d in dt:
+			for r in res:			 
+				lst.append([r[0],r[1],d,'',fy,comp,sr])
 
-    return lst
-  
-  #------------------------------------------------------------------------------
-  # get date list inbetween from date and to date
-  def date_diff_list(self):
-    import datetime
-    #get from date 
-    att_fr_date = self.doc.att_fr_date and self.doc.att_fr_date or ''
-    
-    #get to date
-    att_to_date = self.doc.att_to_date and self.doc.att_to_date or ''
+		return lst
+	
+	#------------------------------------------------------------------------------
+	# get date list inbetween from date and to date
+	def date_diff_list(self):
+		import datetime
+		#get from date 
+		att_fr_date = self.doc.att_fr_date and self.doc.att_fr_date or ''
+		
+		#get to date
+		att_to_date = self.doc.att_to_date and self.doc.att_to_date or ''
 
-    if att_to_date:
-      r = (getdate(self.doc.att_to_date)+datetime.timedelta(days=1)-getdate(self.doc.att_fr_date)).days
-    else:
-      r = 1
-    dateList = [getdate(self.doc.att_fr_date)+datetime.timedelta(days=i) for i in range(0,r)]
-    dt=([str(date) for date in dateList])
-    
-    return dt
+		if att_to_date:
+			r = (getdate(self.doc.att_to_date)+datetime.timedelta(days=1)-getdate(self.doc.att_fr_date)).days
+		else:
+			r = 1
+		dateList = [getdate(self.doc.att_fr_date)+datetime.timedelta(days=i) for i in range(0,r)]
+		dt=([str(date) for date in dateList])
+		
+		return dt
 
-  #------------------------------------------------------------------------------
-  def get_att_data(self):
-    
-    fy = get_defaults()['fiscal_year']    #get default fiscal year 
+	#------------------------------------------------------------------------------
+	def get_att_data(self):
+		fy = get_defaults()['fiscal_year']		#get default fiscal year 
+		comp = get_defaults()['company']		#get default company
+		
+		#get naming series of attendance
+		import webnotes.model.doctype
+		docfield = webnotes.model.doctype.get('Attendance')
+		series = [d.options for d in docfield if d.doctype == 'DocField' and d.fieldname == 'naming_series']
+		if not series:
+			msgprint("Please create naming series for Attendance.\nGo to Setup--> Numbering Series.")
+			raise Exception
+		else:
+			sr = series[0][0] or ''
+		
+		return {'fy':fy,'comp':comp,'sr':sr}
 
-    comp = get_defaults()['company']    #get default company
-    
-    #get naming series of attendance
-    #sr = sql("select series_options from `tabNaming Series Options` where doc_type='Attendance'")
-    sr = sql("select options from `tabDocField` where parent = 'Attendance' and fieldname = 'naming_series'")
-    if not sr:
-      msgprint("Please create naming series for Attendance.\nGo to Setup--> Manage Series.")
-      raise Exception
-    else:
-      sr = sr and sr[0][0]
-    
-    return {'fy':fy,'comp':comp,'sr':sr}
+	#=================================================================================	
+	def import_att_data(self):
+		filename = self.doc.file_list.split(',')
 
-  #=================================================================================  
-  def import_att_data(self):
-    filename = self.doc.file_list.split(',')
-
-    if not filename:
-      msgprint("Please attach a .CSV File.")
-      raise Exception
-    
-    if filename[0].find('.csv') < 0:
-      raise Exception
-    
-    if not filename and filename[0] and file[1]:
-      msgprint("Please Attach File. ")
-      raise Exception
-      
-    from webnotes.utils import file_manager
-    fn, content = file_manager.get_file(filename[1])
+		if not filename:
+			msgprint("Please attach a .CSV File.")
+			raise Exception
+		
+		if filename[0].find('.csv') < 0:
+			raise Exception
+		
+		if not filename and filename[0] and file[1]:
+			msgprint("Please Attach File. ")
+			raise Exception
+			
+		from webnotes.utils import file_manager
+		fn, content = file_manager.get_file(filename[1])
  
 	# NOTE: Don't know why this condition exists
-    if not isinstance(content, basestring) and hasattr(content, 'tostring'):
-      content = content.tostring()
+		if not isinstance(content, basestring) and hasattr(content, 'tostring'):
+			content = content.tostring()
 
-    import webnotes.model.import_docs
-    im = webnotes.model.import_docs.CSVImport()
-    out = im.import_csv(content,self.doc.import_date_format, cint(self.doc.overwrite))
-    return out
+		import webnotes.model.import_docs
+		im = webnotes.model.import_docs.CSVImport()
+		out = im.import_csv(content,self.doc.import_date_format, cint(self.doc.overwrite))
+		return out
 
diff --git a/erpnext/patches/may_2012/clear_session_cache.py b/erpnext/patches/may_2012/clear_session_cache.py
new file mode 100644
index 0000000..fee33dc
--- /dev/null
+++ b/erpnext/patches/may_2012/clear_session_cache.py
@@ -0,0 +1,3 @@
+def execute():
+	import webnotes
+	webnotes.conn.sql("delete from __SessionCache")
\ No newline at end of file
diff --git a/erpnext/patches/patch_list.py b/erpnext/patches/patch_list.py
index 60db1ec..4ae8feb 100644
--- a/erpnext/patches/patch_list.py
+++ b/erpnext/patches/patch_list.py
@@ -377,5 +377,10 @@
 		'patch_file': 'cs_server_readonly',
 		'description': 'Make server custom script readonly for system manager'
 	},
+	{
+		'patch_module': 'patches.may_2012',
+		'patch_file': 'clear_session_cache',
+		'description': 'clears session cache as shifting to json format'
+	},
 
 ]
diff --git a/erpnext/selling/doctype/lead/lead.py b/erpnext/selling/doctype/lead/lead.py
index 7336967..ba15624 100644
--- a/erpnext/selling/doctype/lead/lead.py
+++ b/erpnext/selling/doctype/lead/lead.py
@@ -8,11 +8,11 @@
 # 
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
 # GNU General Public License for more details.
 # 
 # You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+# along with this program.	If not, see <http://www.gnu.org/licenses/>.
 
 # Please edit this list and import only required elements
 import webnotes
@@ -34,195 +34,169 @@
 
 
 class DocType:
-  def __init__(self, doc, doclist):
-    self.doc = doc
-    self.doclist = doclist
-  
-  # Autoname
-  # ---------
-  def autoname(self):
-    self.doc.name = make_autoname(self.doc.naming_series+'.#####')
-  
-  #check status of lead
-  #------------------------
-  def check_status(self):
-    chk = sql("select status from `tabLead` where name=%s", self.doc.name)
-    chk = chk and chk[0][0] or ''
-    return cstr(chk)
+	def __init__(self, doc, doclist):
+		self.doc = doc
+		self.doclist = doclist
+	
+	# Autoname
+	# ---------
+	def autoname(self):
+		self.doc.name = make_autoname(self.doc.naming_series+'.#####')
+	
+	#check status of lead
+	#------------------------
+	def check_status(self):
+		chk = sql("select status from `tabLead` where name=%s", self.doc.name)
+		chk = chk and chk[0][0] or ''
+		return cstr(chk)
 
-  # Gets states belonging cto country selected
-  # =====================================================================
-  #def check_state(self):
-  #  return "\n" + "\n".join([i[0] for i in sql("select state_name from `tabState` where `tabState`.country='%s' " % self.doc.country)])
-  
-  # Get item detail (will be removed later)
-  #=======================================
-  def get_item_detail(self,item_code):
-    it=sql("select item_name,brand,item_group,description,stock_uom from `tabItem` where name='%s'"%item_code)
-    if it:
-      ret = {
-      'item_name'  : it and it[0][0] or '',
-      'brand'      : it and it[0][1] or '',
-      'item_group' : it and it[0][2] or '',
-      'description': it and it[0][3] or '',
-      'uom' : it and it[0][4] or ''
-      }
-      return ret
-  
-  def validate(self):
-    import string
-    # Get Address
-    # ======================================================================
-    #if (self.doc.address_line1) or (self.doc.address_line2) or (self.doc.city) or (self.doc.state) or (self.doc.country) or (self.doc.pincode):
-    #  address =["address_line1", "address_line2", "city", "state", "country", "pincode"]
-    #  comp_address=''
-    #  for d in address:
-    #    if self.doc.fields[d]:
-    #      comp_address += self.doc.fields[d] + "\n"
-    #  if self.doc.website:
-    #    comp_address += "Website : "+ self.doc.website
-    #  self.doc.address = comp_address
-    
-    if self.doc.status == 'Lead Lost' and not self.doc.order_lost_reason:
-      msgprint("Please Enter Quotation Lost Reason")
-      raise Exception  
-    
-    if self.doc.source == 'Campaign' and not self.doc.campaign_name and session['user'] != 'Guest':
-      msgprint("Please specify campaign name")
-      raise Exception
-    
-    if self.doc.email_id:
-      if not validate_email_add(self.doc.email_id):
-        msgprint('Please enter valid email id.')
-        raise Exception
-    
-    if not self.doc.naming_series:
-      if session['user'] == 'Guest':
-        so = sql("select options from `tabDocField` where parent = 'Lead' and fieldname = 'naming_series'")
-        #so = sql("select series_options from `tabNaming Series Options` where doc_type='Lead'")
-        if so:
-          sr = so[0][0].split("\n")
-          set(self.doc, 'naming_series', sr[0])
-      else:
-        msgprint("Please specify naming series")
-        raise Exception  
-  
-  def on_update(self):
-    # Add to calendar
-    # ========================================================================
-    if self.doc.contact_by:
-      self.add_calendar_event()
-    
-    if session['user'] == 'Guest':
-      if self.doc.email_id:
-        self.send_email_notification()
-    
-    if not self.doc.naming_series:
-      if session['user'] == 'Guest':
-        #so = sql("select series_options from `tabNaming Series Options` where doc_type='Lead'")
-        so = sql("select options from `tabDocField` where parent = 'Lead' and fieldname = 'naming_series'")
-        if so:
-          sr = so[0][0].split("\n")
-          set(self.doc, 'naming_series', sr[0])
-      else:
-        msgprint("Please specify naming series")
-        raise Exception
-  
-  def send_email_notification(self):
-    if not validate_email_add(self.doc.email_id.strip(' ')):
-      msgprint('error:%s is not a valid email id' % self.doc.email_id.strip(' '))
-      raise Exception
-    else:
-      subject = 'Thank you for interest in erpnext'
-       
-      sendmail([self.doc.email_id.strip(' ')], sender = sender_email[0][0], subject = subject , parts = [['text/html', self.get_notification_msg()]])
-      #sendmail(cc_list, sender = sender_email[0][0], subject = subject , parts = [['text/html', message]],attach=attach_list)
-      msgprint("Mail Sent")
-  
-  def get_notification_msg(self):
-    t = """
-<html>
-<body>
-Dear %s,<br><br>
 
-Thank you for contacting us.<br><br>
+	# Get item detail (will be removed later)
+	#=======================================
+	def get_item_detail(self,item_code):
+		it=sql("select item_name,brand,item_group,description,stock_uom from `tabItem` where name='%s'"%item_code)
+		if it:
+			ret = {
+			'item_name'	: it and it[0][0] or '',
+			'brand'			: it and it[0][1] or '',
+			'item_group' : it and it[0][2] or '',
+			'description': it and it[0][3] or '',
+			'uom' : it and it[0][4] or ''
+			}
+			return ret
+	
+	def validate(self):
+		import string		
+		if self.doc.status == 'Lead Lost' and not self.doc.order_lost_reason:
+			msgprint("Please Enter Quotation Lost Reason")
+			raise Exception	
+		
+		if self.doc.source == 'Campaign' and not self.doc.campaign_name and session['user'] != 'Guest':
+			msgprint("Please specify campaign name")
+			raise Exception
+		
+		if self.doc.email_id:
+			if not validate_email_add(self.doc.email_id):
+				msgprint('Please enter valid email id.')
+				raise Exception
+		
+	
+	def on_update(self):
+		# Add to calendar
+		# ========================================================================
+		if self.doc.contact_by:
+			self.add_calendar_event()
+		
+		if session['user'] == 'Guest':
+			if self.doc.email_id:
+				self.send_email_notification()
+		
+		if not self.doc.naming_series:
+			if session['user'] == 'Guest':
+				import webnotes.model.doctype
+				docfield = webnotes.model.doctype.get('Lead')
+				series = [d.options for d in docfield if d.doctype == 'DocField' and d.fieldname == 'naming_series']
+				if series:
+					sr = series[0].split("\n")
+					set(self.doc, 'naming_series', sr[0])
+			else:
+				msgprint("Please specify naming series")
+				raise Exception
+	
+	def send_email_notification(self):
+		if not validate_email_add(self.doc.email_id.strip(' ')):
+			msgprint('error:%s is not a valid email id' % self.doc.email_id.strip(' '))
+			raise Exception
+		else:
+			subject = 'Thank you for interest in erpnext'
+			 
+			sendmail([self.doc.email_id.strip(' ')], sender = sender_email[0][0], subject = subject , parts = [['text/html', self.get_notification_msg()]])
+			msgprint("Mail Sent")
+	
+	def get_notification_msg(self):
+		t = """
+			<html>
+				<body>
+					Dear %s,<br><br>
 
-You have left following message for us,<br>
-%s
-<br><br>
+					Thank you for contacting us.<br><br>
 
-You will receive reply on this shortly.<br><br>
+					You have left following message for us,<br>
+					%s
+					<br><br>
 
-Cheers!
-</body>
-</html>
-""" % (self.doc.lead_name, self.doc.remark)
+					You will receive reply on this shortly.<br><br>
 
-    return t
+					Cheers!
+				</body>
+			</html>
+		""" % (self.doc.lead_name, self.doc.remark)
 
-  # Add to Calendar
-  # ===========================================================================
-  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)
-  
-    in_calendar_of = self.doc.lead_owner
-    
-    # get profile (id) if exists for contact_by
-    email_id = webnotes.conn.get_value('Sales Person', self.doc.contact_by, 'email_id')
-    if webnotes.conn.exists('Profile', email_id):
-      in_calendar_of = email_id
-    
-    ev = Document('Event')
-    ev.owner = in_calendar_of
-    ev.description = 'Contact ' + cstr(self.doc.lead_name) + '.By : ' + cstr(self.doc.contact_by) + '.To Discuss : ' + cstr(self.doc.remark)
-    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)
+		return t
+
+	# Add to Calendar
+	# ===========================================================================
+	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)
+	
+		in_calendar_of = self.doc.lead_owner
+		
+		# get profile (id) if exists for contact_by
+		email_id = webnotes.conn.get_value('Sales Person', self.doc.contact_by, 'email_id')
+		if webnotes.conn.exists('Profile', email_id):
+			in_calendar_of = email_id
+		
+		ev = Document('Event')
+		ev.owner = in_calendar_of
+		ev.description = 'Contact ' + cstr(self.doc.lead_name) + '.By : ' + cstr(self.doc.contact_by) + '.To Discuss : ' + cstr(self.doc.remark)
+		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)
 
 
 #-----------------Email-------------------------------------------- 
-  def send_emails(self, email=[], subject='', message=''):
-    if email:
-      sendmail(email, sender = webnotes.user.name, subject = subject , parts = [['text/html', message]])
-      msgprint("Mail Sent")
-      self.add_in_follow_up(message,'Email')
+	def send_emails(self, email=[], subject='', message=''):
+		if email:
+			sendmail(email, sender = webnotes.user.name, subject = subject , parts = [['text/html', message]])
+			msgprint("Mail Sent")
+			self.add_in_follow_up(message,'Email')
 
-#-------------------------Checking Sent Mails Details----------------------------------------------        
-  def send_mail(self):
-    if not self.doc.subject or not self.doc.message:
-      msgprint("Please enter subject & message in their respective fields.")
-    elif not self.doc.email_id:
-      msgprint("Recipient not specified. Please add email id of lead in 'Email id' field provided in 'Contact Info' section.")
-      raise Exception
-    else :
-     self.send_emails([self.doc.email_id.strip(' ')], subject = self.doc.subject ,message = self.doc.message)
+#-------------------------Checking Sent Mails Details----------------------------------------------				
+	def send_mail(self):
+		if not self.doc.subject or not self.doc.message:
+			msgprint("Please enter subject & message in their respective fields.")
+		elif not self.doc.email_id:
+			msgprint("Recipient not specified. Please add email id of lead in 'Email id' field provided in 'Contact Info' section.")
+			raise Exception
+		else :
+		 self.send_emails([self.doc.email_id.strip(' ')], subject = self.doc.subject ,message = self.doc.message)
 
 #---------------------- Add details in follow up table----------------
-  def add_in_follow_up(self,message,type):
-    import datetime
-    child = addchild( self.doc, 'follow_up', 'Communication Log', 1, self.doclist)
-    child.date = datetime.datetime.now().date().strftime('%Y-%m-%d')
-    child.notes = message
-    child.follow_up_type = type
-    child.save()
+	def add_in_follow_up(self,message,type):
+		import datetime
+		child = addchild( self.doc, 'follow_up', 'Communication Log', 1, self.doclist)
+		child.date = datetime.datetime.now().date().strftime('%Y-%m-%d')
+		child.notes = message
+		child.follow_up_type = type
+		child.save()
 
 #-------------------SMS----------------------------------------------
-  def send_sms(self):
-    if not self.doc.sms_message or not self.doc.mobile_no:
-      msgprint("Please enter mobile number in Basic Info Section and message in SMS Section ")
-      raise Exception
-    else:
-      receiver_list = []
-      if self.doc.mobile_no:
-        receiver_list.append(self.doc.mobile_no)
-      for d in getlist(self.doclist,'lead_sms_detail'):
-        if d.other_mobile_no:
-          receiver_list.append(d.other_mobile_no)
-    
-    if receiver_list:
-      msgprint(get_obj('SMS Control', 'SMS Control').send_sms(receiver_list, self.doc.sms_message))
-      self.add_in_follow_up(self.doc.sms_message,'SMS')
+	def send_sms(self):
+		if not self.doc.sms_message or not self.doc.mobile_no:
+			msgprint("Please enter mobile number in Basic Info Section and message in SMS Section ")
+			raise Exception
+		else:
+			receiver_list = []
+			if self.doc.mobile_no:
+				receiver_list.append(self.doc.mobile_no)
+			for d in getlist(self.doclist,'lead_sms_detail'):
+				if d.other_mobile_no:
+					receiver_list.append(d.other_mobile_no)
+		
+		if receiver_list:
+			msgprint(get_obj('SMS Control', 'SMS Control').send_sms(receiver_list, self.doc.sms_message))
+			self.add_in_follow_up(self.doc.sms_message,'SMS')
diff --git a/erpnext/selling/doctype/sales_common/sales_common.py b/erpnext/selling/doctype/sales_common/sales_common.py
index 7de85b6..41f5699 100644
--- a/erpnext/selling/doctype/sales_common/sales_common.py
+++ b/erpnext/selling/doctype/sales_common/sales_common.py
@@ -432,6 +432,7 @@
 	# ------------------ 
 	def make_packing_list(self, obj, fname):
 		self.packing_list_idx = 0
+		parent_items = []
 		for d in getlist(obj.doclist, fname):
 			warehouse = fname == "sales_order_details" and d.reserved_warehouse or d.warehouse
 			if self.has_sales_bom(d.item_code):
@@ -439,6 +440,16 @@
 					self.update_packing_list_item(obj, i['item_code'], flt(i['qty'])*flt(d.qty), warehouse, d)
 			else:
 				self.update_packing_list_item(obj, d.item_code, d.qty, warehouse, d)
+			if d.item_code not in parent_items:
+				parent_items.append(d.item_code)
+				
+		self.cleanup_packing_list(obj, parent_items)
+		
+	def cleanup_packing_list(self, obj, parent_items):
+		"""Remove all those parent items which are no longer present in main item table"""
+		for d in getlist(obj.doclist, 'packing_details'):
+			if d.parent_item not in parent_items:
+				d.parent = ''
 
 
 	# Get total in words
diff --git a/erpnext/setup/doctype/customer_group/customer_group.py b/erpnext/setup/doctype/customer_group/customer_group.py
index 91b1b93..375fd7d 100644
--- a/erpnext/setup/doctype/customer_group/customer_group.py
+++ b/erpnext/setup/doctype/customer_group/customer_group.py
@@ -60,6 +60,15 @@
 
 	def on_trash(self):
 		cust = sql("select name from `tabCustomer` where ifnull(customer_group, '') = %s", self.doc.name)
+		cust = [d[0] for d in cust]
+		
 		if cust:
 			msgprint("""Customer Group: %s can not be trashed/deleted because it is used in customer: %s. 
-				To trash/delete this, remove/change customer group in customer master""" % (self.doc.name, cust[0][0] or ''), raise_exception=1)
+				To trash/delete this, remove/change customer group in customer master""" % (self.doc.name, cust or ''), raise_exception=1)
+
+		if sql("select name from `tabCustomer Group` where parent_customer_group = %s and docstatus != 2", self.doc.name):
+			msgprint("Child customer group exists for this customer group. You can not trash/cancel/delete this customer group.", raise_exception=1)
+
+		# rebuild tree
+		webnotes.conn.set(self.doc,'old_parent', '')
+		self.update_nsm_model()
diff --git a/erpnext/setup/doctype/item_group/item_group.py b/erpnext/setup/doctype/item_group/item_group.py
index 512e916..4a5d5d0 100644
--- a/erpnext/setup/doctype/item_group/item_group.py
+++ b/erpnext/setup/doctype/item_group/item_group.py
@@ -63,7 +63,17 @@
 			raise Exception
 	
 	def on_trash(self):
-		ig = sql("select name from `tabItem` where ifnull(item_group, '') = %s", self.doc.name)
+		item = sql("select name from `tabItem` where ifnull(item_group, '') = %s", self.doc.name)
+		item = [d[0] for d in item]
+		
 		if ig:
 			msgprint("""Item Group: %s can not be trashed/deleted because it is used in item: %s. 
-				To trash/delete this, remove/change item group in item master""" % (self.doc.name, ig[0][0] or ''), raise_exception=1)
+				To trash/delete this, remove/change item group in item master""" % (self.doc.name, item or ''), raise_exception=1)
+				
+		if sql("select name from `tabItem Group` where parent_item_group = %s and docstatus != 2", self.doc.name):
+			msgprint("Child item group exists for this item group. You can not trash/cancel/delete this item group.", raise_exception=1)
+		
+		
+		# rebuild tree
+		set(self.doc,'old_parent', '')
+		self.update_nsm_model()
diff --git a/erpnext/setup/doctype/sales_person/sales_person.py b/erpnext/setup/doctype/sales_person/sales_person.py
index 651cfb4..d8c6e43 100644
--- a/erpnext/setup/doctype/sales_person/sales_person.py
+++ b/erpnext/setup/doctype/sales_person/sales_person.py
@@ -21,7 +21,7 @@
 from webnotes.model.doclist import getlist
 from webnotes.model.code import get_obj
 from webnotes import session, form, is_testing, msgprint, errprint
-from webnotes.utils import flt
+from webnotes.utils import flt, cstr
 
 sql = webnotes.conn.sql
 convert_to_lists = webnotes.conn.convert_to_lists
@@ -77,4 +77,19 @@
 		d.sales_person_id = self.doc.name
 		d.is_sales_person = 1
 		
-		d.save(new = (not d.name))		
+		d.save(new = (not d.name))
+		
+
+	def on_trash(self):
+		st = sql("select parent, parenttype from `tabSales Team` where ifnull(sales_person, '') = %s and docstatus != 2", self.doc.name)
+		st = [(d[1] + ' : ' + d[0]) for d in st]
+		if st:
+			msgprint("""Sales Person: %s can not be trashed/deleted because it is used in  %s. 
+				To trash/delete this, remove/change sales person in %s""" % (self.doc.name, st or '', st or ''), raise_exception=1)
+
+		if sql("select name from `tabSales Person` where parent_sales_person = %s and docstatus != 2", self.doc.name):
+			msgprint("Child sales person exists for this sales person. You can not trash/cancel this sales person.", raise_exception=1)
+
+		# rebuild tree
+		webnotes.conn.set(self.doc,'old_parent', '')
+		self.update_nsm_model()
diff --git a/erpnext/setup/doctype/territory/territory.py b/erpnext/setup/doctype/territory/territory.py
index c4d9285..5f71cf6 100644
--- a/erpnext/setup/doctype/territory/territory.py
+++ b/erpnext/setup/doctype/territory/territory.py
@@ -78,7 +78,17 @@
 
 
 	def on_trash(self):
-		terr = sql("select name from `tabCustomer` where ifnull(territory, '') = %s", self.doc.name)
-		if terr:
-			msgprint("""Territory: %s can not be trashed/deleted because it is used in territory: %s. 
-				To trash/delete this, remove/change territory in customer master""" % (self.doc.name, terr[0][0] or ''), raise_exception=1)
+		cust = sql("select name from `tabCustomer` where ifnull(territory, '') = %s", self.doc.name)
+		cust = [d[0] for d in cust]		
+		
+		if cust:
+			msgprint("""Territory: %s can not be trashed/deleted because it is used in customer: %s. 
+				To trash/delete this, remove/change territory in customer master""" % (self.doc.name, cust or ''), raise_exception=1)
+				
+				
+		if sql("select name from `tabTerritory` where parent_territory = %s and docstatus != 2", self.doc.name):
+			msgprint("Child territory exists for this territory. You can not trash/cancel/delete this territory.", raise_exception=1)
+
+		# rebuild tree
+		set(self.doc,'old_parent', '')
+		self.update_nsm_model()
diff --git a/erpnext/support/doctype/customer_issue/customer_issue.js b/erpnext/support/doctype/customer_issue/customer_issue.js
index 699c97f..0749424 100644
--- a/erpnext/support/doctype/customer_issue/customer_issue.js
+++ b/erpnext/support/doctype/customer_issue/customer_issue.js
@@ -8,85 +8,85 @@
 // 
 // This program is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
 // GNU General Public License for more details.
 // 
 // You should have received a copy of the GNU General Public License
-// along with this program.  If not, see <http://www.gnu.org/licenses/>.
+// along with this program.	If not, see <http://www.gnu.org/licenses/>.
 
 cur_frm.cscript.onload = function(doc,cdt,cdn){
-  if(!doc.status) set_multiple(dt,dn,{status:'Open'});  
-  if(doc.__islocal){    
-    hide_field(['customer_address','contact_person','customer_name','address_display','contact_display','contact_mobile','contact_email','territory','customer_group']);
-  }   
+	if(!doc.status) set_multiple(dt,dn,{status:'Open'});	
+	if(doc.__islocal){		
+		hide_field(['customer_address','contact_person']);
+	}	 
 }
 
 cur_frm.cscript.refresh = function(doc,ct,cdn){
-  if(!doc.docstatus) hide_field('make_maintenance_visit');
-  else if(doc.docstatus && (doc.status == 'Open' || doc.status == 'Work In Progress')) unhide_field('make_maintenance_visit');
+	if(doc.docstatus == 1 && (doc.status == 'Open' || doc.status == 'Work In Progress')) 
+		cur_frm.add_custom_button('Make Maintenance Visit', cur_frm.cscript['Make Maintenance Visit']);
 }
 
 
 //customer
 cur_frm.cscript.customer = function(doc,dt,dn) {
-  var callback = function(r,rt) {
-      var doc = locals[cur_frm.doctype][cur_frm.docname];
-      cur_frm.refresh();
-  }   
+	var callback = function(r,rt) {
+			var doc = locals[cur_frm.doctype][cur_frm.docname];
+			cur_frm.refresh();
+	}
 
-  if(doc.customer) $c_obj(make_doclist(doc.doctype, doc.name), 'get_default_customer_address', '', callback);
-  if(doc.customer) unhide_field(['customer_address','contact_person','customer_name','address_display','contact_display','contact_mobile','contact_email','territory','customer_group']);
+	if(doc.customer) $c_obj(make_doclist(doc.doctype, doc.name), 'get_default_customer_address', '', callback);
+	if(doc.customer) unhide_field(['customer_address','contact_person']);
 }
 
-cur_frm.cscript.customer_address = cur_frm.cscript.contact_person = function(doc,dt,dn) {    
-  if(doc.customer) get_server_fields('get_customer_address', JSON.stringify({customer: doc.customer, address: doc.customer_address, contact: doc.contact_person}),'', doc, dt, dn, 1);
+cur_frm.cscript.customer_address = cur_frm.cscript.contact_person = function(doc,dt,dn) {		
+	if(doc.customer) 
+		get_server_fields('get_customer_address', 
+			JSON.stringify({customer: doc.customer, address: doc.customer_address, contact: doc.contact_person}),'', doc, dt, dn, 1);
 }
 
 cur_frm.fields_dict.customer_address.on_new = function(dn) {
-  locals['Address'][dn].customer = locals[cur_frm.doctype][cur_frm.docname].customer;
-  locals['Address'][dn].customer_name = locals[cur_frm.doctype][cur_frm.docname].customer_name;
+	locals['Address'][dn].customer = locals[cur_frm.doctype][cur_frm.docname].customer;
+	locals['Address'][dn].customer_name = locals[cur_frm.doctype][cur_frm.docname].customer_name;
 }
 
 cur_frm.fields_dict.contact_person.on_new = function(dn) {
-  locals['Contact'][dn].customer = locals[cur_frm.doctype][cur_frm.docname].customer;
-  locals['Contact'][dn].customer_name = locals[cur_frm.doctype][cur_frm.docname].customer_name;
+	locals['Contact'][dn].customer = locals[cur_frm.doctype][cur_frm.docname].customer;
+	locals['Contact'][dn].customer_name = locals[cur_frm.doctype][cur_frm.docname].customer_name;
 }
 
 cur_frm.fields_dict['customer_address'].get_query = function(doc, cdt, cdn) {
-  return 'SELECT name,address_line1,city FROM tabAddress WHERE customer = "'+ doc.customer +'" AND docstatus != 2 AND name LIKE "%s" ORDER BY name ASC LIMIT 50';
+	return 'SELECT name,address_line1,city FROM tabAddress WHERE customer = "'+ doc.customer +
+		'" AND docstatus != 2 AND name LIKE "%s" ORDER BY name ASC LIMIT 50';
 }
 
 cur_frm.fields_dict['contact_person'].get_query = function(doc, cdt, cdn) {
-  return 'SELECT name,CONCAT(first_name," ",ifnull(last_name,"")) As FullName,department,designation FROM tabContact WHERE customer = "'+ doc.customer +'" AND docstatus != 2 AND name LIKE "%s" ORDER BY name ASC LIMIT 50';
+	return 'SELECT name,CONCAT(first_name," ",ifnull(last_name,"")) As FullName,department,designation \
+		FROM tabContact WHERE customer = "'	+ doc.customer +
+		'" AND docstatus != 2 AND name LIKE "%s" ORDER BY name ASC LIMIT 50';
 }
 
-
-
-
-cur_frm.cscript.make_maintenance_visit = function(doc, cdt, cdn) {
-  if (doc.docstatus == 1) { 
-    $c_obj(make_doclist(doc.doctype, doc.name),'check_maintenance_visit','',
-      function(r,rt){
-        if(r.message == 'No'){
-          n = createLocal("Maintenance Visit");
-          $c('dt_map', args={
-                  'docs':compress_doclist([locals["Maintenance Visit"][n]]),
-                  'from_doctype':'Customer Issue',
-                  'to_doctype':'Maintenance Visit',
-                  'from_docname':doc.name,
-            'from_to_list':"[['Customer Issue', 'Maintenance Visit'], ['Customer Issue', 'Maintenance Visit Purpose']]"
-          }
-          , function(r,rt) {
-            loaddoc("Maintenance Visit", n);
-          }
-          );
-        }
-        else{
-          msgprint("You have already completed maintenance against this Customer Issue");
-        }
-      }
-    );
-  }
+cur_frm.cscript['Make Maintenance Visit'] = function() {
+	var doc = cur_frm.doc;
+	if (doc.docstatus == 1) { 
+		$c_obj(make_doclist(doc.doctype, doc.name),'check_maintenance_visit','',
+			function(r,rt){
+				if(r.message == 'No'){
+					n = createLocal("Maintenance Visit");
+					$c('dt_map', args={
+						'docs':compress_doclist([locals["Maintenance Visit"][n]]),
+						'from_doctype':'Customer Issue',
+						'to_doctype':'Maintenance Visit',
+						'from_docname':doc.name,
+						'from_to_list':"[['Customer Issue', 'Maintenance Visit'], ['Customer Issue', 'Maintenance Visit Purpose']]"
+					}, function(r,rt) {
+						loaddoc("Maintenance Visit", n);
+					});
+				} else{
+					msgprint("You have already completed maintenance against this Customer Issue");
+				}
+			}
+		);
+	}
 }
 
 // ----------
@@ -94,9 +94,13 @@
 // ----------
 
 cur_frm.fields_dict['serial_no'].get_query = function(doc, cdt, cdn) {
-  var cond = '';
-  if(doc.item_code) cond = ' AND `tabSerial No`.item_code = "'+ doc.item_code +'"';
-  return 'SELECT `tabSerial No`.name, `tabSerial No`.description FROM `tabSerial No` WHERE `tabSerial No`.docstatus != 2 AND `tabSerial No`.name LIKE "%s" '+cond+' ORDER BY `tabSerial No`.name ASC LIMIT 50';
+	var cond = '';
+	if(doc.item_code) cond = ' AND `tabSerial No`.item_code = "'+ doc.item_code +'"';
+	if(doc.customer) cond += ' AND `tabSerial No`.customer = "' + doc.customer + '"';
+	return 'SELECT `tabSerial No`.name, `tabSerial No`.description \
+		FROM `tabSerial No` \
+		WHERE `tabSerial No`.docstatus != 2 AND `tabSerial No`.status = "Delivered" \
+		AND `tabSerial No`.name LIKE "%s" ' + cond + ' ORDER BY `tabSerial No`.name ASC LIMIT 50';
 }
 
 cur_frm.add_fetch('serial_no', 'item_code', 'item_code');
@@ -105,20 +109,27 @@
 cur_frm.add_fetch('serial_no', 'maintenance_status', 'warranty_amc_status');
 cur_frm.add_fetch('serial_no', 'warranty_expiry_date', 'warranty_expiry_date');
 cur_frm.add_fetch('serial_no', 'amc_expiry_date', 'amc_expiry_date');
-cur_frm.add_fetch('serial_no', 'customer', 'customer');
-cur_frm.add_fetch('serial_no', 'customer_name', 'customer_name');
-cur_frm.add_fetch('serial_no', 'delivery_address', 'customer_address');
+if (cstr(doc.customer) == '') {
+	cur_frm.add_fetch('serial_no', 'customer', 'customer');
+	cur_frm.add_fetch('serial_no', 'customer_name', 'customer_name');
+	cur_frm.add_fetch('serial_no', 'delivery_address', 'customer_address');
+}
 
 // ----------
 // item code
 // ----------
 cur_frm.fields_dict['item_code'].get_query = function(doc, cdt, cdn) {
-  if(doc.serial_no){
-    return 'SELECT `tabSerial No`.item_code, `tabSerial No`.description FROM `tabSerial No` WHERE `tabSerial No`.docstatus != 2 AND `tabSerial No`.name = "'+ doc.serial_no +'" AND `tabSerial No`.item_code LIKE "%s" ORDER BY `tabSerial No`.item_code ASC LIMIT 50';
-  }
-  else{
-    return 'SELECT `tabItem`.name, `tabItem`.item_name, `tabItem`.description FROM `tabItem` WHERE `tabItem`.docstatus != 2 AND `tabItem`.name LIKE "%s" ORDER BY `tabItem`.name ASC LIMIT 50';
-  }
+	if(doc.serial_no) {
+		return 'SELECT `tabSerial No`.item_code, `tabSerial No`.description \
+			FROM `tabSerial No` \
+			WHERE `tabSerial No`.docstatus != 2 AND `tabSerial No`.name = "' + doc.serial_no +
+			'" AND `tabSerial No`.item_code LIKE "%s" ORDER BY `tabSerial No`.item_code ASC LIMIT 50';
+	}
+	else{
+		return 'SELECT `tabItem`.name, `tabItem`.item_name, `tabItem`.description \
+			FROM `tabItem` \
+			WHERE `tabItem`.docstatus != 2 AND `tabItem`.name LIKE "%s" ORDER BY `tabItem`.name ASC LIMIT 50';
+	}
 }
 
 cur_frm.add_fetch('item_code', 'item_name', 'item_name');
@@ -128,5 +139,8 @@
 //get query select Territory
 //=======================================================================================================================
 cur_frm.fields_dict['territory'].get_query = function(doc,cdt,cdn) {
-  return 'SELECT `tabTerritory`.`name`,`tabTerritory`.`parent_territory` FROM `tabTerritory` WHERE `tabTerritory`.`is_group` = "No" AND `tabTerritory`.`docstatus`!= 2 AND `tabTerritory`.%(key)s LIKE "%s"  ORDER BY  `tabTerritory`.`name` ASC LIMIT 50';
+	return 'SELECT `tabTerritory`.`name`,`tabTerritory`.`parent_territory` \
+		FROM `tabTerritory` \
+		WHERE `tabTerritory`.`is_group` = "No" AND `tabTerritory`.`docstatus`!= 2 \
+		AND `tabTerritory`.%(key)s LIKE "%s"	ORDER BY	`tabTerritory`.`name` ASC LIMIT 50';
 }
diff --git a/erpnext/support/doctype/customer_issue/customer_issue.py b/erpnext/support/doctype/customer_issue/customer_issue.py
index ecf95bd..0561ce0 100644
--- a/erpnext/support/doctype/customer_issue/customer_issue.py
+++ b/erpnext/support/doctype/customer_issue/customer_issue.py
@@ -8,11 +8,11 @@
 # 
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
 # GNU General Public License for more details.
 # 
 # You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+# along with this program.	If not, see <http://www.gnu.org/licenses/>.
 
 # Please edit this list and import only required elements
 
@@ -36,49 +36,42 @@
 from utilities.transaction_base import TransactionBase
 
 class DocType(TransactionBase):
-  def __init__(self, doc, doclist=[]):
-    self.doc = doc
-    self.doclist = doclist
-    self.prefix = is_testing and 'test' or 'tab'
-    
-  def autoname(self):
-    #self.doc.name = make_autoname('CI/' + self.doc.fiscal_year + '/.######')
-    self.doc.name = make_autoname(self.doc.naming_series + '.######')
-    
+	def __init__(self, doc, doclist=[]):
+		self.doc = doc
+		self.doclist = doclist
+			
+	def autoname(self):
+		self.doc.name = make_autoname(self.doc.naming_series + '.######')
+		
 #check if maintenance schedule already generated
 #============================================
-  def check_maintenance_visit(self):
-    nm = sql("select t1.name from `tabMaintenance Visit` t1, `tabMaintenance Visit Purpose` t2 where t2.parent=t1.name and t2.prevdoc_docname=%s and t1.docstatus=1 and t1.completion_status='Fully Completed'", self.doc.name)
-    nm = nm and nm[0][0] or ''
-    
-    if not nm:
-      return 'No'
-  
-  def on_submit(self):
-    if session['user'] != 'Guest':
-      if not self.doc.allocated_to:
-        msgprint("Please select service person name whom you want to assign this issue")
-        raise Exception
-  
-  def validate(self):
-    if session['user'] != 'Guest' and not self.doc.customer:
-      msgprint("Please select Customer from whom issue is raised")
-      raise Exception
-    #if not self.doc.email_id and not self.doc.contact_no:
-    #  msgprint("Please specify contact no. and/or email_id")
-    #  raise Exception
-    #elif self.doc.email_id and not validate_email_add(self.doc.email_id.strip(' ')):
-    #  msgprint('error:%s is not a valid email id' % self.doc.email_id)
-    #  raise Exception
-  
-  def on_cancel(self):
-    lst = sql("select t1.name from `tabMaintenance Visit` t1, `tabMaintenance Visit Purpose` t2 where t2.parent = t1.name and t2.prevdoc_docname = '%s' and  t1.docstatus!=2"%(self.doc.name))
-    if lst:
-      lst1 = ','.join([x[0] for x in lst])
-      msgprint("Maintenance Visit No. "+lst1+" already created against this customer issue. So can not be Cancelled")
-      raise Exception
-    else:
-      set(self.doc, 'status', 'Cancelled')
+	def check_maintenance_visit(self):
+		nm = sql("select t1.name from `tabMaintenance Visit` t1, `tabMaintenance Visit Purpose` t2 where t2.parent=t1.name and t2.prevdoc_docname=%s and t1.docstatus=1 and t1.completion_status='Fully Completed'", self.doc.name)
+		nm = nm and nm[0][0] or ''
+		
+		if not nm:
+			return 'No'
+	
+	def on_submit(self):
+		if session['user'] != 'Guest':
+			if not self.doc.allocated_to:
+				msgprint("Please select service person name whom you want to assign this issue")
+				raise Exception
+	
+	def validate(self):
+		if session['user'] != 'Guest' and not self.doc.customer:
+			msgprint("Please select Customer from whom issue is raised")
+			raise Exception
 
-  def on_update(self):
-    pass
+	
+	def on_cancel(self):
+		lst = sql("select t1.name from `tabMaintenance Visit` t1, `tabMaintenance Visit Purpose` t2 where t2.parent = t1.name and t2.prevdoc_docname = '%s' and	t1.docstatus!=2"%(self.doc.name))
+		if lst:
+			lst1 = ','.join([x[0] for x in lst])
+			msgprint("Maintenance Visit No. "+lst1+" already created against this customer issue. So can not be Cancelled")
+			raise Exception
+		else:
+			set(self.doc, 'status', 'Cancelled')
+
+	def on_update(self):
+		pass
diff --git a/erpnext/support/doctype/customer_issue/customer_issue.txt b/erpnext/support/doctype/customer_issue/customer_issue.txt
index 2c27735..a96e91e 100644
--- a/erpnext/support/doctype/customer_issue/customer_issue.txt
+++ b/erpnext/support/doctype/customer_issue/customer_issue.txt
@@ -3,9 +3,9 @@
 
 	# These values are common in all dictionaries
 	{
-		'creation': '2012-03-27 14:36:40',
+		'creation': '2012-04-13 11:56:39',
 		'docstatus': 0,
-		'modified': '2012-03-27 14:45:47',
+		'modified': '2012-05-18 17:04:36',
 		'modified_by': u'Administrator',
 		'owner': u'harshada@webnotestech.com'
 	},
@@ -25,7 +25,7 @@
 		'show_in_menu': 0,
 		'subject': u'%(complaint)s By %(complaint_raised_by)s on %(issue_date)s',
 		'tag_fields': u'status',
-		'version': 97
+		'version': 1
 	},
 
 	# These values are common for all DocField
@@ -55,6 +55,15 @@
 
 	# DocPerm
 	{
+		'create': 1,
+		'doctype': u'DocPerm',
+		'permlevel': 0,
+		'role': u'Guest',
+		'write': 1
+	},
+
+	# DocPerm
+	{
 		'amend': 1,
 		'cancel': 1,
 		'create': 1,
@@ -110,21 +119,12 @@
 		'role': u'Maintenance User'
 	},
 
-	# DocPerm
-	{
-		'create': 1,
-		'doctype': u'DocPerm',
-		'permlevel': 0,
-		'role': u'Guest',
-		'write': 1
-	},
-
 	# DocField
 	{
 		'doctype': u'DocField',
-		'fieldname': u'customer_issue',
+		'fieldname': u'basic_info',
 		'fieldtype': u'Section Break',
-		'label': u'Customer Issue',
+		'label': u'Basic Info',
 		'oldfieldtype': u'Section Break',
 		'permlevel': 0
 	},
@@ -147,24 +147,6 @@
 
 	# DocField
 	{
-		'colour': u'White:FFF',
-		'default': u'Open',
-		'doctype': u'DocField',
-		'fieldname': u'status',
-		'fieldtype': u'Select',
-		'in_filter': 1,
-		'label': u'Status',
-		'no_copy': 1,
-		'oldfieldname': u'status',
-		'oldfieldtype': u'Select',
-		'options': u'\nOpen\nClosed\nWork In Progress\nCancelled',
-		'permlevel': 0,
-		'reqd': 1,
-		'search_index': 1
-	},
-
-	# DocField
-	{
 		'default': u'Today',
 		'doctype': u'DocField',
 		'fieldname': u'complaint_date',
@@ -181,39 +163,6 @@
 	# DocField
 	{
 		'doctype': u'DocField',
-		'fieldname': u'complaint_raised_by',
-		'fieldtype': u'Data',
-		'label': u'Raised By',
-		'oldfieldname': u'complaint_raised_by',
-		'oldfieldtype': u'Data',
-		'permlevel': 0
-	},
-
-	# DocField
-	{
-		'doctype': u'DocField',
-		'fieldname': u'from_company',
-		'fieldtype': u'Data',
-		'label': u'From Company',
-		'oldfieldname': u'from_company',
-		'oldfieldtype': u'Data',
-		'permlevel': 0
-	},
-
-	# DocField
-	{
-		'doctype': u'DocField',
-		'fieldname': u'service_address',
-		'fieldtype': u'Small Text',
-		'label': u'Service Address',
-		'oldfieldname': u'service_address',
-		'oldfieldtype': u'Small Text',
-		'permlevel': 0
-	},
-
-	# DocField
-	{
-		'doctype': u'DocField',
 		'fieldname': u'column_break0',
 		'fieldtype': u'Column Break',
 		'oldfieldtype': u'Column Break',
@@ -264,144 +213,6 @@
 	# DocField
 	{
 		'doctype': u'DocField',
-		'fieldname': u'customer_name',
-		'fieldtype': u'Data',
-		'label': u'Name',
-		'permlevel': 1
-	},
-
-	# DocField
-	{
-		'doctype': u'DocField',
-		'fieldname': u'address_display',
-		'fieldtype': u'Small Text',
-		'label': u'Address',
-		'permlevel': 1
-	},
-
-	# DocField
-	{
-		'doctype': u'DocField',
-		'fieldname': u'contact_display',
-		'fieldtype': u'Small Text',
-		'label': u'Contact',
-		'permlevel': 1
-	},
-
-	# DocField
-	{
-		'doctype': u'DocField',
-		'fieldname': u'contact_mobile',
-		'fieldtype': u'Data',
-		'label': u'Mobile No',
-		'permlevel': 1
-	},
-
-	# DocField
-	{
-		'doctype': u'DocField',
-		'fieldname': u'contact_email',
-		'fieldtype': u'Data',
-		'label': u'Contact Email',
-		'permlevel': 1
-	},
-
-	# DocField
-	{
-		'colour': u'White:FFF',
-		'doctype': u'DocField',
-		'fieldname': u'territory',
-		'fieldtype': u'Link',
-		'in_filter': 1,
-		'label': u'Territory',
-		'oldfieldname': u'territory',
-		'oldfieldtype': u'Link',
-		'options': u'Territory',
-		'permlevel': 0,
-		'print_hide': 1,
-		'reqd': 0,
-		'search_index': 1
-	},
-
-	# DocField
-	{
-		'doctype': u'DocField',
-		'fieldname': u'customer_group',
-		'fieldtype': u'Link',
-		'label': u'Customer Group',
-		'options': u'Customer Group',
-		'permlevel': 0,
-		'print_hide': 1,
-		'reqd': 0
-	},
-
-	# DocField
-	{
-		'colour': u'White:FFF',
-		'doctype': u'DocField',
-		'fieldname': u'company',
-		'fieldtype': u'Link',
-		'in_filter': 1,
-		'label': u'Company',
-		'oldfieldname': u'company',
-		'oldfieldtype': u'Link',
-		'options': u'Company',
-		'permlevel': 0,
-		'print_hide': 1,
-		'reqd': 1,
-		'search_index': 1,
-		'trigger': u'Client'
-	},
-
-	# DocField
-	{
-		'doctype': u'DocField',
-		'fieldname': u'fiscal_year',
-		'fieldtype': u'Select',
-		'in_filter': 1,
-		'label': u'Fiscal Year',
-		'oldfieldname': u'fiscal_year',
-		'oldfieldtype': u'Select',
-		'options': u'link:Fiscal Year',
-		'permlevel': 0,
-		'print_hide': 1,
-		'reqd': 1,
-		'search_index': 1
-	},
-
-	# DocField
-	{
-		'doctype': u'DocField',
-		'fieldname': u'amended_from',
-		'fieldtype': u'Data',
-		'hidden': 1,
-		'label': u'Amended From',
-		'no_copy': 1,
-		'oldfieldname': u'amended_from',
-		'oldfieldtype': u'Data',
-		'permlevel': 0,
-		'print_hide': 1,
-		'width': u'150px'
-	},
-
-	# DocField
-	{
-		'doctype': u'DocField',
-		'fieldname': u'amendment_date',
-		'fieldtype': u'Date',
-		'hidden': 1,
-		'label': u'Amendment Date',
-		'no_copy': 1,
-		'oldfieldname': u'amendment_date',
-		'oldfieldtype': u'Date',
-		'permlevel': 0,
-		'print_hide': 1,
-		'width': u'100px'
-	},
-
-	# DocField
-	{
-		'doctype': u'DocField',
 		'fieldname': u'issue_details',
 		'fieldtype': u'Section Break',
 		'label': u'Issue Details',
@@ -440,6 +251,29 @@
 
 	# DocField
 	{
+		'doctype': u'DocField',
+		'fieldname': u'complaint',
+		'fieldtype': u'Small Text',
+		'label': u'Issue',
+		'no_copy': 1,
+		'oldfieldname': u'complaint',
+		'oldfieldtype': u'Small Text',
+		'permlevel': 0,
+		'reqd': 1
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
+		'fieldname': u'column_break1',
+		'fieldtype': u'Column Break',
+		'oldfieldtype': u'Column Break',
+		'permlevel': 0,
+		'width': u'50%'
+	},
+
+	# DocField
+	{
 		'colour': u'White:FFF',
 		'depends_on': u'eval:doc.item_code',
 		'doctype': u'DocField',
@@ -467,29 +301,6 @@
 
 	# DocField
 	{
-		'doctype': u'DocField',
-		'fieldname': u'complaint',
-		'fieldtype': u'Small Text',
-		'label': u'Issue',
-		'no_copy': 1,
-		'oldfieldname': u'complaint',
-		'oldfieldtype': u'Small Text',
-		'permlevel': 0,
-		'reqd': 1
-	},
-
-	# DocField
-	{
-		'doctype': u'DocField',
-		'fieldname': u'column_break1',
-		'fieldtype': u'Column Break',
-		'oldfieldtype': u'Column Break',
-		'permlevel': 0,
-		'width': u'50%'
-	},
-
-	# DocField
-	{
 		'colour': u'White:FFF',
 		'doctype': u'DocField',
 		'fieldname': u'warranty_amc_status',
@@ -522,6 +333,17 @@
 	# DocField
 	{
 		'doctype': u'DocField',
+		'fieldname': u'resolution_section',
+		'fieldtype': u'Section Break',
+		'label': u'Resolution',
+		'oldfieldtype': u'Section Break',
+		'options': u'Simple',
+		'permlevel': 0
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
 		'fieldname': u'allocated_on',
 		'fieldtype': u'Date',
 		'in_filter': 1,
@@ -549,29 +371,6 @@
 	# DocField
 	{
 		'doctype': u'DocField',
-		'fieldname': u'resolution_detail',
-		'fieldtype': u'Section Break',
-		'label': u'Resolution Detail',
-		'oldfieldtype': u'Section Break',
-		'options': u'Simple',
-		'permlevel': 0
-	},
-
-	# DocField
-	{
-		'doctype': u'DocField',
-		'fieldname': u'resolution_details',
-		'fieldtype': u'Text',
-		'label': u'Resolution Details',
-		'no_copy': 1,
-		'oldfieldname': u'resolution_details',
-		'oldfieldtype': u'Text',
-		'permlevel': 0
-	},
-
-	# DocField
-	{
-		'doctype': u'DocField',
 		'fieldname': u'column_break2',
 		'fieldtype': u'Column Break',
 		'oldfieldtype': u'Column Break',
@@ -610,27 +409,256 @@
 
 	# DocField
 	{
-		'allow_on_submit': 0,
-		'colour': u'White:FFF',
-		'depends_on': u'eval:!doc.__islocal',
 		'doctype': u'DocField',
-		'fieldname': u'next_steps',
-		'fieldtype': u'Section Break',
-		'label': u'Next Steps',
-		'oldfieldtype': u'Section Break',
+		'fieldname': u'resolution_details',
+		'fieldtype': u'Text',
+		'label': u'Resolution Details',
+		'no_copy': 1,
+		'oldfieldname': u'resolution_details',
+		'oldfieldtype': u'Text',
 		'permlevel': 0
 	},
 
 	# DocField
 	{
-		'allow_on_submit': 1,
+		'doctype': u'DocField',
+		'fieldname': u'contact_info',
+		'fieldtype': u'Section Break',
+		'label': u'Contact Info',
+		'permlevel': 0
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
+		'fieldname': u'col_break3',
+		'fieldtype': u'Column Break',
+		'permlevel': 0,
+		'width': u'50%'
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
+		'fieldname': u'customer_name',
+		'fieldtype': u'Data',
+		'label': u'Customer Name',
+		'permlevel': 1
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
+		'fieldname': u'customer_group',
+		'fieldtype': u'Link',
+		'label': u'Customer Group',
+		'options': u'Customer Group',
+		'permlevel': 0,
+		'print_hide': 1,
+		'reqd': 0
+	},
+
+	# DocField
+	{
 		'colour': u'White:FFF',
 		'doctype': u'DocField',
-		'fieldname': u'make_maintenance_visit',
-		'fieldtype': u'Button',
-		'label': u'Make Maintenance Visit',
-		'oldfieldtype': u'Button',
+		'fieldname': u'territory',
+		'fieldtype': u'Link',
+		'in_filter': 1,
+		'label': u'Territory',
+		'oldfieldname': u'territory',
+		'oldfieldtype': u'Link',
+		'options': u'Territory',
 		'permlevel': 0,
+		'print_hide': 1,
+		'reqd': 0,
+		'search_index': 1
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
+		'fieldname': u'address_display',
+		'fieldtype': u'Small Text',
+		'label': u'Address',
+		'permlevel': 1
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
+		'fieldname': u'col_break4',
+		'fieldtype': u'Column Break',
+		'permlevel': 0,
+		'width': u'50%'
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
+		'fieldname': u'contact_display',
+		'fieldtype': u'Small Text',
+		'label': u'Contact',
+		'permlevel': 1
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
+		'fieldname': u'contact_mobile',
+		'fieldtype': u'Data',
+		'label': u'Mobile No',
+		'permlevel': 1
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
+		'fieldname': u'contact_email',
+		'fieldtype': u'Data',
+		'label': u'Contact Email',
+		'permlevel': 1
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
+		'fieldname': u'service_address',
+		'fieldtype': u'Small Text',
+		'label': u'Service Address',
+		'oldfieldname': u'service_address',
+		'oldfieldtype': u'Small Text',
+		'permlevel': 0
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
+		'fieldname': u'more_info',
+		'fieldtype': u'Section Break',
+		'label': u'More Info',
+		'permlevel': 0
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
+		'fieldname': u'col_break5',
+		'fieldtype': u'Column Break',
+		'permlevel': 0,
+		'width': u'50%'
+	},
+
+	# DocField
+	{
+		'colour': u'White:FFF',
+		'doctype': u'DocField',
+		'fieldname': u'company',
+		'fieldtype': u'Link',
+		'in_filter': 1,
+		'label': u'Company',
+		'oldfieldname': u'company',
+		'oldfieldtype': u'Link',
+		'options': u'Company',
+		'permlevel': 0,
+		'print_hide': 1,
+		'reqd': 1,
+		'search_index': 1,
 		'trigger': u'Client'
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
+		'fieldname': u'fiscal_year',
+		'fieldtype': u'Select',
+		'in_filter': 1,
+		'label': u'Fiscal Year',
+		'oldfieldname': u'fiscal_year',
+		'oldfieldtype': u'Select',
+		'options': u'link:Fiscal Year',
+		'permlevel': 0,
+		'print_hide': 1,
+		'reqd': 1,
+		'search_index': 1
+	},
+
+	# DocField
+	{
+		'colour': u'White:FFF',
+		'default': u'Open',
+		'doctype': u'DocField',
+		'fieldname': u'status',
+		'fieldtype': u'Select',
+		'in_filter': 1,
+		'label': u'Status',
+		'no_copy': 1,
+		'oldfieldname': u'status',
+		'oldfieldtype': u'Select',
+		'options': u'\nOpen\nClosed\nWork In Progress\nCancelled',
+		'permlevel': 0,
+		'reqd': 1,
+		'search_index': 1
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
+		'fieldname': u'col_break6',
+		'fieldtype': u'Column Break',
+		'permlevel': 0,
+		'width': u'50%'
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
+		'fieldname': u'complaint_raised_by',
+		'fieldtype': u'Data',
+		'label': u'Raised By',
+		'oldfieldname': u'complaint_raised_by',
+		'oldfieldtype': u'Data',
+		'permlevel': 0
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
+		'fieldname': u'from_company',
+		'fieldtype': u'Data',
+		'label': u'From Company',
+		'oldfieldname': u'from_company',
+		'oldfieldtype': u'Data',
+		'permlevel': 0
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
+		'fieldname': u'amended_from',
+		'fieldtype': u'Data',
+		'hidden': 1,
+		'label': u'Amended From',
+		'no_copy': 1,
+		'oldfieldname': u'amended_from',
+		'oldfieldtype': u'Data',
+		'permlevel': 0,
+		'print_hide': 1,
+		'width': u'150px'
+	},
+
+	# DocField
+	{
+		'doctype': u'DocField',
+		'fieldname': u'amendment_date',
+		'fieldtype': u'Date',
+		'hidden': 1,
+		'label': u'Amendment Date',
+		'no_copy': 1,
+		'oldfieldname': u'amendment_date',
+		'oldfieldtype': u'Date',
+		'permlevel': 0,
+		'print_hide': 1,
+		'width': u'100px'
 	}
 ]
\ No newline at end of file