[mapper] support
diff --git a/selling/doctype/sales_order/sales_order.js b/selling/doctype/sales_order/sales_order.js
index 8500a51..4c12580 100644
--- a/selling/doctype/sales_order/sales_order.js
+++ b/selling/doctype/sales_order/sales_order.js
@@ -48,7 +48,7 @@
 				if(flt(doc.per_delivered, 2) < 100 && (doc.order_type !='Sales')) {
 					cur_frm.add_custom_button('Make Maint. Visit', this.make_maintenance_visit);
 					cur_frm.add_custom_button('Make Maint. Schedule', 
-						this.make_maintainance_schedule);
+						this.make_maintenance_schedule);
 				}
 
 				// indent
@@ -98,13 +98,26 @@
 
 	make_sales_invoice: function() {
 		wn.model.open_mapped_doc({
-			method: "selling.doctype.quotation.quotation.make_quotation",
+			method: "selling.doctype.sales_order.sales_order.make_sales_invoice",
+			source_name: cur_frm.doc.name
+		})
+	},
+	
+	make_maintenance_schedule: function() {
+		wn.model.open_mapped_doc({
+			method: "selling.doctype.sales_order.sales_order.make_maintenance_schedule",
+			source_name: cur_frm.doc.name
+		})
+	}, 
+	
+	make_maintenance_visit: function() {
+		wn.model.open_mapped_doc({
+			method: "selling.doctype.sales_order.sales_order.make_maintenance_visit",
 			source_name: cur_frm.doc.name
 		})
 	},
 	
 	pull_quotation_details: function() {
-		
 		wn.model.map_current_doc({
 			method: "selling.doctype.quotation.quotation.make_sales_order",
 			source_name: cur_frm.doc.quotation_no,
@@ -155,64 +168,6 @@
 			ORDER BY `tabQuotation`.`name` DESC LIMIT 50', {cond:cond});
 }
 
-//----------- make maintenance schedule----------
-cur_frm.cscript['Make Maintenance Schedule'] = function() {
-	var doc = cur_frm.doc;
-
-	if (doc.docstatus == 1) { 
-		$c_obj(make_doclist(doc.doctype, doc.name),'check_maintenance_schedule','',
-			function(r,rt){
-				if(r.message == 'No'){
-					n = wn.model.make_new_doc_and_get_name("Maintenance Schedule");
-					$c('dt_map', args={
-									'docs':wn.model.compress([locals["Maintenance Schedule"][n]]),
-									'from_doctype':'Sales Order',
-									'to_doctype':'Maintenance Schedule',
-									'from_docname':doc.name,
-						'from_to_list':"[['Sales Order', 'Maintenance Schedule'], ['Sales Order Item', 'Maintenance Schedule Item']]"
-					}
-					, function(r,rt) {
-						loaddoc("Maintenance Schedule", n);
-					}
-					);
-				}
-				else{
-					msgprint("You have already created Maintenance Schedule against this Sales Order");
-				}
-			}
-		);
-	}
-}
-
-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 = wn.model.make_new_doc_and_get_name("Maintenance Visit");
-					$c('dt_map', args={
-									'docs':wn.model.compress([locals["Maintenance Visit"][n]]),
-									'from_doctype':'Sales Order',
-									'to_doctype':'Maintenance Visit',
-									'from_docname':doc.name,
-						'from_to_list':"[['Sales Order', 'Maintenance Visit'], ['Sales Order Item', 'Maintenance Visit Purpose']]"
-					}
-					, function(r,rt) {
-						loaddoc("Maintenance Visit", n);
-					}
-					);
-				}
-				else{
-					msgprint("You have already completed maintenance against this Sales Order");
-				}
-			}
-		);
-	}
-}
-
-
 cur_frm.cscript['Stop Sales Order'] = function() {
 	var doc = cur_frm.doc;
 
diff --git a/selling/doctype/sales_order/sales_order.py b/selling/doctype/sales_order/sales_order.py
index 6bb85f8..af860fc 100644
--- a/selling/doctype/sales_order/sales_order.py
+++ b/selling/doctype/sales_order/sales_order.py
@@ -23,6 +23,7 @@
 from webnotes.model.bean import getlist
 from webnotes.model.code import get_obj
 from webnotes import msgprint
+from webnotes.model.mapper import get_mapped_doclist
 
 sql = webnotes.conn.sql
 	
@@ -55,20 +56,6 @@
 	def get_rate(self,arg):
 		return get_obj('Sales Common').get_rate(arg)
 
-	def check_maintenance_schedule(self):
-		nm = sql("select t1.name from `tabMaintenance Schedule` t1, `tabMaintenance Schedule Item` t2 where t2.parent=t1.name and t2.prevdoc_docname=%s and t1.docstatus=1", self.doc.name)
-		nm = nm and nm[0][0] or ''
-		
-		if not nm:
-			return 'No'
-
-	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 validate_fiscal_year(self):
 		get_obj('Sales Common').validate_fiscal_year(self.doc.fiscal_year,self.doc.transaction_date,'Sales Order Date')
 	
@@ -365,9 +352,7 @@
 	}
 	
 @webnotes.whitelist()
-def make_material_request(source_name, target_doclist=None):
-	from webnotes.model.mapper import get_mapped_doclist
-	
+def make_material_request(source_name, target_doclist=None):	
 	def postprocess(source, doclist):
 		doclist[0].material_request_type = "Purchase"
 	
@@ -391,9 +376,7 @@
 	return [d.fields for d in doclist]
 
 @webnotes.whitelist()
-def make_delivery_note(source_name, target_doclist=None):
-	from webnotes.model.mapper import get_mapped_doclist
-	
+def make_delivery_note(source_name, target_doclist=None):	
 	def update_item(obj, target, source_parent):
 		target.amount = (flt(obj.qty) - flt(obj.delivered_qty)) * flt(obj.basic_rate)
 		target.export_amount = (flt(obj.qty) - flt(obj.delivered_qty)) * flt(obj.export_rate)
@@ -433,9 +416,7 @@
 	return [d.fields for d in doclist]
 
 @webnotes.whitelist()
-def make_sales_invoice(source_name, target_doclist=None):
-	from webnotes.model.mapper import get_mapped_doclist
-	
+def make_sales_invoice(source_name, target_doclist=None):	
 	def update_item(obj, target, source_parent):
 		target.export_amount = flt(obj.amount) - flt(obj.billed_amt)
 		target.amount = target.export_amount / flt(source_parent.conversion_rate)
@@ -470,4 +451,60 @@
 		}
 	}, target_doclist, update_accounts)
 	
-	return [d.fields for d in doclist]
\ No newline at end of file
+	return [d.fields for d in doclist]
+	
+@webnotes.whitelist()
+def make_maintenance_schedule(source_name, target_doclist=None):
+	maint_schedule = webnotes.conn.sql("""select t1.name 
+		from `tabMaintenance Schedule` t1, `tabMaintenance Schedule Item` t2 
+		where t2.parent=t1.name and t2.prevdoc_docname=%s and t1.docstatus=1""", source_name)
+		
+	if not maint_schedule:
+		doclist = get_mapped_doclist("Sales Order", source_name, {
+			"Sales Order": {
+				"doctype": "Maintenance Schedule", 
+				"field_map": {
+					"name": "sales_order_no"
+				}, 
+				"validation": {
+					"docstatus": ["=", 1]
+				}
+			}, 
+			"Sales Order Item": {
+				"doctype": "Maintenance Schedule Item", 
+				"field_map": {
+					"parent": "prevdoc_docname"
+				}
+			}
+		}, target_doclist)
+	
+		return [d.fields for d in doclist]
+	
+@webnotes.whitelist()
+def make_maintenance_visit(source_name, target_doclist=None):
+	visit = webnotes.conn.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'""", source_name)
+		
+	if not visit:
+		doclist = get_mapped_doclist("Sales Order", source_name, {
+			"Sales Order": {
+				"doctype": "Maintenance Visit", 
+				"field_map": {
+					"name": "sales_order_no"
+				},
+				"validation": {
+					"docstatus": ["=", 1]
+				}
+			}, 
+			"Sales Order Item": {
+				"doctype": "Maintenance Visit Purpose", 
+				"field_map": {
+					"parent": "prevdoc_docname", 
+					"parenttype": "prevdoc_doctype"
+				}
+			}
+		}, target_doclist)
+	
+		return [d.fields for d in doclist]
\ No newline at end of file
diff --git a/support/doctype/customer_issue/customer_issue.js b/support/doctype/customer_issue/customer_issue.js
index 3a602b8..558ef32 100644
--- a/support/doctype/customer_issue/customer_issue.js
+++ b/support/doctype/customer_issue/customer_issue.js
@@ -17,6 +17,13 @@
 wn.provide("erpnext.support");
 // TODO commonify this code
 erpnext.support.CustomerIssue = wn.ui.form.Controller.extend({
+	refresh: function() {
+		if(cur_frm.doc.docstatus==1 && (cur_frm.doc.status=='Open' || 
+			cur_frm.doc.status == 'Work In Progress')) {
+				cur_frm.add_custom_button('Make Maintenance Visit', this.make_maintenance_visit)
+			}
+	}, 
+	
 	customer: function() {
 		var me = this;
 		if(this.frm.doc.customer) {
@@ -31,6 +38,13 @@
 			// TODO shift this to depends_on
 			unhide_field(['customer_address', 'contact_person']);
 		}
+	}, 
+	
+	make_maintenance_visit: function() {
+		wn.model.open_mapped_doc({
+			method: "support.doctype.customer_issue.customer_issue.make_maintenance_visit",
+			source_name: cur_frm.doc.name
+		})
 	}
 });
 
@@ -44,12 +58,6 @@
 	} 
 }
 
-cur_frm.cscript.refresh = function(doc,ct,cdn){
-	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']);
-}
-
 cur_frm.cscript.customer_address = cur_frm.cscript.contact_person = function(doc,dt,dn) {		
 	if(doc.customer) 
 		get_server_fields('get_customer_address', 
@@ -67,34 +75,6 @@
 		'" AND docstatus != 2 AND name LIKE "%s" ORDER BY name ASC LIMIT 50';
 }
 
-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 = wn.model.make_new_doc_and_get_name("Maintenance Visit");
-					$c('dt_map', args={
-						'docs':wn.model.compress([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");
-				}
-			}
-		);
-	}
-}
-
-// ----------
-// serial no
-// ----------
-
 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 +'"';
@@ -115,9 +95,6 @@
 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 \
@@ -135,9 +112,6 @@
 cur_frm.add_fetch('item_code', 'item_name', 'item_name');
 cur_frm.add_fetch('item_code', 'description', 'description');
 
-
-//get query select Territory
-//=======================================================================================================================
 cur_frm.fields_dict['territory'].get_query = function(doc,cdt,cdn) {
 	return 'SELECT `tabTerritory`.`name`,`tabTerritory`.`parent_territory` \
 		FROM `tabTerritory` \
diff --git a/support/doctype/customer_issue/customer_issue.py b/support/doctype/customer_issue/customer_issue.py
index 3a4ffc1..60887f9 100644
--- a/support/doctype/customer_issue/customer_issue.py
+++ b/support/doctype/customer_issue/customer_issue.py
@@ -17,9 +17,6 @@
 
 from __future__ import unicode_literals
 import webnotes
-
-from webnotes.model import db_exists
-from webnotes.model.bean import copy_doclist
 from webnotes import session, msgprint
 from webnotes.utils import today
 
@@ -32,13 +29,6 @@
 	def __init__(self, doc, doclist=[]):
 		self.doc = doc
 		self.doclist = doclist
-		
-	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 validate(self):
 		if session['user'] != 'Guest' and not self.doc.customer:
@@ -61,3 +51,26 @@
 
 	def on_update(self):
 		pass
+
+@webnotes.whitelist()
+def make_maintenance_visit(source_name, target_doclist=None):
+	from webnotes.model.mapper import get_mapped_doclist
+	
+	visit = webnotes.conn.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'""", source_name)
+		
+	if not visit:
+		doclist = get_mapped_doclist("Customer Issue", source_name, {
+			"Customer Issue": {
+				"doctype": "Maintenance Visit", 
+				"field_map": {
+					"complaint": "description", 
+					"doctype": "prevdoc_doctype", 
+					"name": "prevdoc_docname"
+				}
+			}
+		}, target_doclist)
+	
+		return [d.fields for d in doclist]
\ No newline at end of file
diff --git a/support/doctype/maintenance_schedule/maintenance_schedule.js b/support/doctype/maintenance_schedule/maintenance_schedule.js
index 6b3ce10..ec75289 100644
--- a/support/doctype/maintenance_schedule/maintenance_schedule.js
+++ b/support/doctype/maintenance_schedule/maintenance_schedule.js
@@ -34,6 +34,15 @@
 				'territory', 'customer_group']);
 			
 		}
+	}, 
+	
+	get_items: function() {
+		wn.model.map_current_doc({
+			method: "selling.doctype.sales_order.sales_order.make_maintenance_schedule",
+			source_name: cur_frm.doc.quotation_no,
+		});
+		unhide_field(['customer_address', 'contact_person', 'customer_name', 'address_display', 
+			'contact_display', 'contact_mobile', 'contact_email', 'territory', 'customer_group']);
 	}
 });
 
@@ -65,16 +74,6 @@
   return 'SELECT tabItem.name,tabItem.item_name,tabItem.description FROM tabItem WHERE tabItem.is_service_item="Yes" AND tabItem.docstatus != 2 AND tabItem.%(key)s LIKE "%s" LIMIT 50';
 }
 
-// Get Items based on SO Selected
-cur_frm.cscript.get_items = function(doc, dt, dn) {
-  var callback = function(r,rt) { 
-	  unhide_field(['customer_address','contact_person','customer_name','address_display','contact_display','contact_mobile','contact_email','territory','customer_group']);
-	  cur_frm.refresh();
-  }
-  get_server_fields('pull_sales_order_detail','','',doc, dt, dn,1,callback);
-}
-
-
 cur_frm.cscript.item_code = function(doc, cdt, cdn) {
   var fname = cur_frm.cscript.fname;
   var d = locals[cdt][cdn];
@@ -83,17 +82,6 @@
   }
 }
 
-/*
-cur_frm.fields_dict['contact_person'].get_query = function(doc, cdt, cdn) {
-  return 'SELECT `tabContact`.contact_name FROM `tabContact` WHERE `tabContact`.is_customer = 1 AND `tabContact`.customer = "'+ doc.customer+'" AND `tabContact`.contact_name LIKE "%s" ORDER BY `tabContact`.contact_name ASC LIMIT 50';
-}
-
-
-cur_frm.cscript.customer = function(doc, cdt, cdn) {
-  get_server_fields('get_customer_details','','',doc, cdt, cdn, 1);
-}
-*/
-
 cur_frm.fields_dict['sales_order_no'].get_query = function(doc) {
   doc = locals[this.doctype][this.docname];
   var cond = '';
@@ -129,8 +117,6 @@
   }  
 }
 
-//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';
 }
diff --git a/support/doctype/maintenance_schedule/maintenance_schedule.py b/support/doctype/maintenance_schedule/maintenance_schedule.py
index baed6a9..dad63eb 100644
--- a/support/doctype/maintenance_schedule/maintenance_schedule.py
+++ b/support/doctype/maintenance_schedule/maintenance_schedule.py
@@ -33,15 +33,6 @@
 		self.doc = doc
 		self.doclist = doclist
 	
-	# pull sales order details
-	#--------------------------
-	def pull_sales_order_detail(self):
-		self.doclist = self.doc.clear_table(self.doclist, 'item_maintenance_detail')
-		self.doclist = self.doc.clear_table(self.doclist, 'maintenance_schedule_detail')
-		self.doclist = get_obj('DocType Mapper', 'Sales Order-Maintenance Schedule').dt_map('Sales Order', 'Maintenance Schedule', self.doc.sales_order_no, self.doc, self.doclist, "[['Sales Order', 'Maintenance Schedule'],['Sales Order Item', 'Maintenance Schedule Item']]")
-	
-	#pull item details 
-	#-------------------
 	def get_item_details(self, item_code):
 		item = sql("select item_name, description from `tabItem` where name = '%s'" %(item_code), as_dict=1)
 		ret = {
@@ -50,8 +41,6 @@
 		}
 		return ret
 		
-	# generate maintenance schedule
-	#-------------------------------------
 	def generate_schedule(self):
 		self.doclist = self.doc.clear_table(self.doclist, 'maintenance_schedule_detail')
 		count = 0
@@ -75,8 +64,6 @@
 				
 		self.on_update()
 
-
-
 	def on_submit(self):
 		if not getlist(self.doclist, 'maintenance_schedule_detail'):
 			msgprint("Please click on 'Generate Schedule' to get schedule")
@@ -149,10 +136,6 @@
 			msgprint("Weekly periodicity can be set for period of atleast 1 week or more")
 			raise Exception
 	
-
-
-	#get count on the basis of periodicity selected
-	#----------------------------------------------------
 	def get_no_of_visits(self, arg):
 		arg1 = eval(arg)		
 		self.validate_period(arg)
@@ -331,3 +314,28 @@
 		
 	def on_trash(self):
 		delete_events(self.doc.doctype, self.doc.name)
+
+@webnotes.whitelist()
+def make_maintenance_visit(source_name, target_doclist=None):
+	from webnotes.model.mapper import get_mapped_doclist
+	
+	doclist = get_mapped_doclist("Maintenance Schedule", source_name, {
+		"Maintenance Schedule": {
+			"doctype": "Maintenance Visit", 
+			"field_map": {
+				"name": "maintenance_schedule"
+			},
+			"validation": {
+				"docstatus": ["=", 1]
+			}
+		}, 
+		"Maintenance Schedule Item": {
+			"doctype": "Maintenance Visit Purpose", 
+			"field_map": {
+				"parent": "prevdoc_docname", 
+				"parenttype": "prevdoc_doctype"
+			}
+		}
+	}, target_doclist)
+
+	return [d.fields for d in doclist]
\ No newline at end of file
diff --git a/support/doctype/maintenance_schedule/maintenance_schedule.txt b/support/doctype/maintenance_schedule/maintenance_schedule.txt
index 63148dc..fc95ec6 100644
--- a/support/doctype/maintenance_schedule/maintenance_schedule.txt
+++ b/support/doctype/maintenance_schedule/maintenance_schedule.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-01-10 16:34:30", 
   "docstatus": 0, 
-  "modified": "2013-07-05 14:45:42", 
+  "modified": "2013-07-07 12:18:51", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -24,19 +24,13 @@
   "permlevel": 0
  }, 
  {
-  "amend": 1, 
-  "cancel": 1, 
-  "create": 1, 
   "doctype": "DocPerm", 
   "name": "__common__", 
   "parent": "Maintenance Schedule", 
   "parentfield": "permissions", 
   "parenttype": "DocType", 
-  "permlevel": 0, 
   "read": 1, 
-  "report": 1, 
-  "submit": 1, 
-  "write": 1
+  "report": 1
  }, 
  {
   "doctype": "DocType", 
@@ -227,7 +221,6 @@
   "fieldtype": "Button", 
   "label": "Get Items", 
   "oldfieldtype": "Button", 
-  "options": "pull_sales_order_detail", 
   "report_hide": 1
  }, 
  {
@@ -265,11 +258,41 @@
   "read_only": 1
  }, 
  {
+  "amend": 1, 
+  "cancel": 1, 
+  "create": 1, 
   "doctype": "DocPerm", 
-  "role": "System Manager"
+  "permlevel": 0, 
+  "role": "System Manager", 
+  "submit": 1, 
+  "write": 1
  }, 
  {
+  "amend": 0, 
+  "cancel": 0, 
+  "create": 0, 
   "doctype": "DocPerm", 
-  "role": "Maintenance Manager"
+  "permlevel": 1, 
+  "role": "System Manager", 
+  "submit": 0
+ }, 
+ {
+  "amend": 1, 
+  "cancel": 1, 
+  "create": 1, 
+  "doctype": "DocPerm", 
+  "permlevel": 0, 
+  "role": "Maintenance Manager", 
+  "submit": 1, 
+  "write": 1
+ }, 
+ {
+  "amend": 0, 
+  "cancel": 0, 
+  "create": 0, 
+  "doctype": "DocPerm", 
+  "permlevel": 1, 
+  "role": "Maintenance Manager", 
+  "submit": 0
  }
 ]
\ No newline at end of file
diff --git a/support/doctype/maintenance_visit/maintenance_visit.js b/support/doctype/maintenance_visit/maintenance_visit.js
index 3b8de75..6c282c0 100644
--- a/support/doctype/maintenance_visit/maintenance_visit.js
+++ b/support/doctype/maintenance_visit/maintenance_visit.js
@@ -31,6 +31,26 @@
 			// TODO shift this to depends_on
 			hide_contact_info(this.frm.doc);			
 		}
+	}, 
+	
+	get_items: function() {
+		if(cur_frm.doc.sales_order_no) {
+			wn.model.map_current_doc({
+				method: "selling.doctype.sales_order.sales_order.make_maintenance_visit",
+				source_name: cur_frm.doc.quotation_no,
+			});
+		} else if (cur_frm.doc.customer_issue_no) {
+			wn.model.map_current_doc({
+				method: "support.doctype.customer_issue.customer_issue.make_maintenance_visit",
+				source_name: cur_frm.doc.quotation_no,
+			});
+		} else if (cur_frm.doc.maintenance_schedule) {
+			wn.model.map_current_doc({
+				method: "support.doctype.maintenance_schedule.maintenance_schedule\
+					.make_maintenance_visit",
+				source_name: cur_frm.doc.quotation_no,
+			});
+		}	
 	}
 });
 
diff --git a/support/doctype/maintenance_visit/maintenance_visit.py b/support/doctype/maintenance_visit/maintenance_visit.py
index cf1ddeb..d87d6e9 100644
--- a/support/doctype/maintenance_visit/maintenance_visit.py
+++ b/support/doctype/maintenance_visit/maintenance_visit.py
@@ -18,8 +18,7 @@
 import webnotes
 
 from webnotes.utils import cstr
-from webnotes.model import db_exists
-from webnotes.model.bean import getlist, copy_doclist
+from webnotes.model.bean import getlist
 from webnotes.model.code import get_obj
 from webnotes import msgprint
 
@@ -33,48 +32,20 @@
 		self.doc = doc
 		self.doclist = doclist
 	
-	#get item details
-	#------------------
 	def get_item_details(self, item_code):
 		item = sql("select item_name,description from `tabItem` where name = '%s'" %(item_code), as_dict=1)
 		ret = {
 			'item_name' : item and item[0]['item_name'] or '',
 			'description' : item and item[0]['description'] or ''
 		}
-		return ret
-	
-	# fetch details of resp Sales order or customer issue
-	#-----------------------------------------------------------
-	def fetch_items(self):
-		if self.doc.sales_order_no and self.doc.customer_issue_no :
-			msgprint("You can not fetch details of both, Sales Order and Customer Issue, in same Maintenance Visit")
-			raise Exception
-		
-		self.doclist = self.doc.clear_table(self.doclist, 'maintenance_visit_details')
-		
-		if self.doc.sales_order_no:
-			self.doclist = get_obj('DocType Mapper', 'Sales Order-Maintenance Visit').dt_map('Sales Order', \
-				'Maintenance Visit', self.doc.sales_order_no, self.doc, self.doclist, "[['Sales Order', 'Maintenance Visit'],\
-				['Sales Order Item', 'Maintenance Visit Purpose']]")
-		elif self.doc.customer_issue_no:			
-			self.doclist = get_obj('DocType Mapper', 'Customer Issue-Maintenance Visit').dt_map('Customer Issue', \
-				'Maintenance Visit', self.doc.customer_issue_no, self.doc, self.doclist, "[['Customer Issue', 'Maintenance Visit'],\
-				['Customer Issue', 'Maintenance Visit Purpose']]")			
-		elif self.doc.maintenance_schedule:
-			self.doclist = get_obj('DocType Mapper', 'Maintenance Schedule-Maintenance Visit').dt_map('Maintenance Schedule',\
-			 	'Maintenance Visit', self.doc.maintenance_schedule, self.doc, self.doclist, "[['Maintenance Schedule', \
-				'Maintenance Visit'], ['Maintenance Schedule Item', 'Maintenance Visit Purpose']]")			
-	
-	#validate reference value using doctype mapper
-	#-----------------------------------------------------
+		return ret		
+
 	def validate_reference_value(self, check_for):
 		if check_for == 'Sales Order':
 			get_obj('DocType Mapper', 'Sales Order-Maintenance Visit', with_children = 1).validate_reference_value(self, self.doc.name)
 		elif check_for == 'Customer Issue':
 			get_obj('DocType Mapper', 'Customer Issue-Maintenance Visit', with_children = 1).validate_reference_value(self, self.doc.name)
 	
-	#check if serial no exist in system
-	#--------------------------------------
 	def validate_serial_no(self):
 		for d in getlist(self.doclist, 'maintenance_visit_details'):
 			if d.serial_no and not sql("select name from `tabSerial No` where name = '%s' and docstatus != 2" % d.serial_no):
@@ -155,4 +126,4 @@
 		webnotes.conn.set(self.doc, 'status', 'Cancelled')
 
 	def on_update(self):
-		pass
+		pass
\ No newline at end of file
diff --git a/support/doctype/maintenance_visit/maintenance_visit.txt b/support/doctype/maintenance_visit/maintenance_visit.txt
index 5f9bb1b..c57787e 100644
--- a/support/doctype/maintenance_visit/maintenance_visit.txt
+++ b/support/doctype/maintenance_visit/maintenance_visit.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-01-10 16:34:31", 
   "docstatus": 0, 
-  "modified": "2013-07-05 14:45:54", 
+  "modified": "2013-07-07 12:08:12", 
   "modified_by": "Administrator", 
   "owner": "ashwini@webnotestech.com"
  }, 
@@ -24,19 +24,13 @@
   "permlevel": 0
  }, 
  {
-  "amend": 1, 
-  "cancel": 1, 
-  "create": 1, 
   "doctype": "DocPerm", 
   "name": "__common__", 
   "parent": "Maintenance Visit", 
   "parentfield": "permissions", 
   "parenttype": "DocType", 
-  "permlevel": 0, 
   "read": 1, 
-  "report": 1, 
-  "submit": 1, 
-  "write": 1
+  "report": 1
  }, 
  {
   "doctype": "DocType", 
@@ -213,7 +207,6 @@
   "fieldtype": "Button", 
   "label": "Get Items", 
   "oldfieldtype": "Button", 
-  "options": "fetch_items", 
   "print_hide": 1, 
   "report_hide": 1
  }, 
@@ -348,15 +341,60 @@
   "print_hide": 1
  }, 
  {
+  "amend": 1, 
+  "cancel": 1, 
+  "create": 1, 
   "doctype": "DocPerm", 
-  "role": "System Manager"
+  "permlevel": 0, 
+  "role": "System Manager", 
+  "submit": 1, 
+  "write": 1
  }, 
  {
+  "amend": 0, 
+  "cancel": 0, 
+  "create": 0, 
   "doctype": "DocPerm", 
-  "role": "Maintenance Manager"
+  "permlevel": 1, 
+  "role": "System Manager", 
+  "submit": 0
  }, 
  {
+  "amend": 1, 
+  "cancel": 1, 
+  "create": 1, 
   "doctype": "DocPerm", 
-  "role": "Maintenance User"
+  "permlevel": 0, 
+  "role": "Maintenance Manager", 
+  "submit": 1, 
+  "write": 1
+ }, 
+ {
+  "amend": 0, 
+  "cancel": 0, 
+  "create": 0, 
+  "doctype": "DocPerm", 
+  "permlevel": 1, 
+  "role": "Maintenance Manager", 
+  "submit": 0
+ }, 
+ {
+  "amend": 1, 
+  "cancel": 1, 
+  "create": 1, 
+  "doctype": "DocPerm", 
+  "permlevel": 0, 
+  "role": "Maintenance User", 
+  "submit": 1, 
+  "write": 1
+ }, 
+ {
+  "amend": 0, 
+  "cancel": 0, 
+  "create": 0, 
+  "doctype": "DocPerm", 
+  "permlevel": 1, 
+  "role": "Maintenance User", 
+  "submit": 0
  }
 ]
\ No newline at end of file