[mapper] added sales order > material request, added postprocess function
diff --git a/selling/doctype/lead/lead.js b/selling/doctype/lead/lead.js
index 1b51505..b3c200d 100644
--- a/selling/doctype/lead/lead.js
+++ b/selling/doctype/lead/lead.js
@@ -105,29 +105,9 @@
 	})
 }
 
-// Create New Opportunity
-// ===============================================================
 cur_frm.cscript['Create Opportunity'] = function(){
-	var doc = cur_frm.doc;
-	$c('runserverobj',args={ 'method':'check_status', 'docs':wn.model.compress(make_doclist(doc.doctype, doc.name))},
-		function(r,rt){
-			if(r.message == 'Converted'){
-				msgprint("This lead is now converted to customer. Please create enquiry on behalf of customer");
-			}
-			else{
-				n = wn.model.make_new_doc_and_get_name("Opportunity");
-				$c('dt_map', args={
-					'docs':wn.model.compress([locals["Opportunity"][n]]),
-					'from_doctype':'Lead',
-					'to_doctype':'Opportunity',
-					'from_docname':doc.name,
-					'from_to_list':"[['Lead', 'Opportunity']]"
-				}
-				, function(r,rt) {
-						loaddoc("Opportunity", n);
-					}
-				);
-			}
-		}
-	);
+	wn.model.open_mapped_doc({
+		method: "selling.doctype.lead.lead.make_opportunity",
+		source_name: cur_frm.doc.name
+	})
 }
\ No newline at end of file
diff --git a/selling/doctype/lead/lead.py b/selling/doctype/lead/lead.py
index 3a3d8e5..4dde84a 100644
--- a/selling/doctype/lead/lead.py
+++ b/selling/doctype/lead/lead.py
@@ -100,10 +100,7 @@
 @webnotes.whitelist()
 def make_customer(source_name, target_doclist=None):
 	from webnotes.model.mapper import get_mapped_doclist
-	
-	if target_doclist:
-		target_doclist = json.loads(target_doclist)
-	
+		
 	doclist = get_mapped_doclist("Lead", source_name, 
 		{"Lead": {
 			"doctype": "Customer",
@@ -115,4 +112,20 @@
 			}
 		}}, target_doclist)
 		
+	return [d.fields for d in doclist]
+	
+@webnotes.whitelist()
+def make_opportunity(source_name, target_doclist=None):
+	from webnotes.model.mapper import get_mapped_doclist
+		
+	doclist = get_mapped_doclist("Lead", source_name, 
+		{"Lead": {
+			"doctype": "Opportunity",
+			"field_map": {
+				"campaign_name": "campaign",
+				"doctype": "enquiry_from",
+				"name": "lead",
+			}
+		}}, target_doclist)
+		
 	return [d.fields for d in doclist]
\ No newline at end of file
diff --git a/selling/doctype/sales_order/sales_order.js b/selling/doctype/sales_order/sales_order.js
index c4d50cc..6ed5f1c 100644
--- a/selling/doctype/sales_order/sales_order.js
+++ b/selling/doctype/sales_order/sales_order.js
@@ -195,21 +195,10 @@
 }
 
 cur_frm.cscript['Make Material Request'] = function() {
-	var doc = cur_frm.doc;
-	if (doc.docstatus == 1) { 
-	n = wn.model.make_new_doc_and_get_name("Material Request");
-	$c('dt_map', args={
-					'docs':wn.model.compress([locals["Material Request"][n]]),
-					'from_doctype':'Sales Order',
-					'to_doctype':'Material Request',
-					'from_docname':doc.name,
-		'from_to_list':"[['Sales Order', 'Material Request'], ['Sales Order Item', 'Material Request Item']]"
-	}
-	, function(r,rt) {
-		loaddoc("Material Request", n);
-		}
-		);
-	}
+	wn.model.open_mapped_doc({
+		method: "selling.doctype.sales_order.sales_order.make_material_request",
+		source_name: cur_frm.doc.name
+	})
 }
 
 
diff --git a/selling/doctype/sales_order/sales_order.py b/selling/doctype/sales_order/sales_order.py
index ec365a2..0c9237a 100644
--- a/selling/doctype/sales_order/sales_order.py
+++ b/selling/doctype/sales_order/sales_order.py
@@ -378,4 +378,31 @@
 		"currency": webnotes.conn.get_default("currency"),
 		"currency_symbols": json.dumps(dict(webnotes.conn.sql("""select name, symbol
 			from tabCurrency where ifnull(enabled,0)=1""")))
-	}
\ No newline at end of file
+	}
+	
+@webnotes.whitelist()
+def make_material_request(source_name, target_doclist=None):
+	from webnotes.model.mapper import get_mapped_doclist
+	
+	def postprocess(source, doclist):
+		doclist[0].material_request_type = "Purchase"
+	
+	doclist = get_mapped_doclist("Sales Order", source_name, {
+		"Sales Order": {
+			"doctype": "Material Request", 
+			"validation": {
+				"docstatus": ["=", 1]
+			}
+		}, 
+		"Sales Order Item": {
+			"doctype": "Material Request Item", 
+			"field_map": {
+				"parent": "sales_order_no", 
+				"reserved_warehouse": "warehouse", 
+				"stock_uom": "uom"
+			}
+		}
+	}, target_doclist, postprocess)
+	
+	return [d.fields for d in doclist]
+	
\ No newline at end of file
diff --git a/selling/doctype/sales_order/test_sales_order.py b/selling/doctype/sales_order/test_sales_order.py
index 40a10b4..7678f37 100644
--- a/selling/doctype/sales_order/test_sales_order.py
+++ b/selling/doctype/sales_order/test_sales_order.py
@@ -3,6 +3,19 @@
 import unittest
 
 class TestSalesOrder(unittest.TestCase):
+	def test_make_material_request(self):
+		from selling.doctype.sales_order.sales_order import make_material_request
+		
+		self.assertRaises(webnotes.ValidationError, make_material_request, 
+			"_T-Sales Order-00001")
+
+		sales_order = webnotes.bean("Sales Order", "_T-Sales Order-00001")
+		sales_order.submit()
+		mr = make_material_request("_T-Sales Order-00001")
+		
+		self.assertEquals(mr[0]["material_request_type"], "Purchase")
+		self.assertEquals(len(mr), len(sales_order.doclist))
+				
 	def create_so(self, so_doclist = None):
 		if not so_doclist:
 			so_doclist =test_records[0]